mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-10 04:36:43 +00:00
- Add function for checking header in form-data request Bug reports are sent with multipart/form-data, and the function parses this, then compares the needed field with the wanted Also, added the step definition. - Add functions for reporting a bug with changes Be able to report a bug by changing the value of a single field, or multiple fields through a JSON format - Add integration tests for reporting a problem
181 lines
4.6 KiB
Go
181 lines
4.6 KiB
Go
// Copyright (c) 2023 Proton AG
|
|
//
|
|
// This file is part of Proton Mail Bridge.
|
|
//
|
|
// Proton Mail Bridge is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Proton Mail Bridge is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
package tests
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/cucumber/godog"
|
|
)
|
|
|
|
func (s *scenario) itSucceeds() error {
|
|
if err := s.t.getLastError(); err != nil {
|
|
return fmt.Errorf("expected nil, got error %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *scenario) itFails() error {
|
|
if err := s.t.getLastError(); err == nil {
|
|
return fmt.Errorf("expected error, got nil")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *scenario) itFailsWithError(wantErr string) error {
|
|
err := s.t.getLastError()
|
|
if err == nil {
|
|
return fmt.Errorf("expected error, got nil")
|
|
}
|
|
|
|
if haveErr := err.Error(); !strings.Contains(haveErr, wantErr) {
|
|
return fmt.Errorf("expected error %q, got %q", wantErr, haveErr)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *scenario) internetIsTurnedOff() error {
|
|
s.t.netCtl.SetCanDial(false)
|
|
return nil
|
|
}
|
|
|
|
func (s *scenario) internetIsTurnedOn() error {
|
|
s.t.netCtl.SetCanDial(true)
|
|
return nil
|
|
}
|
|
|
|
func (s *scenario) theUserAgentIs(userAgent string) error {
|
|
return eventually(func() error {
|
|
if haveUserAgent := s.t.bridge.GetCurrentUserAgent(); haveUserAgent != userAgent {
|
|
return fmt.Errorf("have user agent %q, want %q", haveUserAgent, userAgent)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (s *scenario) theHeaderInTheRequestToHasSetTo(method, path, key, value string) error {
|
|
call, err := s.t.getLastCall(method, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if haveKey := call.RequestHeader.Get(key); haveKey != value {
|
|
return fmt.Errorf("have header %q, want %q", haveKey, value)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *scenario) theHeaderInTheMultipartRequestToHasSetTo(method, path, key, value string) error {
|
|
// We have to exclude HTTP-Overrides to avoid race condition with the creating and sending of the draft message.
|
|
call, err := s.t.getLastCallExcludingHTTPOverride(method, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if _, err := buf.WriteString(fmt.Sprintf("%s %s HTTP/1.1\r\n", call.Method, call.URL.Path)); err != nil {
|
|
return fmt.Errorf("failed to write request line: %w", err)
|
|
}
|
|
|
|
if err := call.RequestHeader.Write(buf); err != nil {
|
|
return fmt.Errorf("failed to write header: %w", err)
|
|
}
|
|
|
|
if _, err := buf.WriteString("\r\n"); err != nil {
|
|
return fmt.Errorf("failed to write header: %w", err)
|
|
}
|
|
|
|
if _, err := buf.Write(call.RequestBody); err != nil {
|
|
return fmt.Errorf("failed to write body: %w", err)
|
|
}
|
|
|
|
req, err := http.ReadRequest(bufio.NewReader(buf))
|
|
if err != nil {
|
|
return fmt.Errorf("failed to read request: %w", err)
|
|
}
|
|
|
|
if err := req.ParseMultipartForm(1 << 10); err != nil {
|
|
return fmt.Errorf("failed to parse multipart form: %w", err)
|
|
}
|
|
|
|
if haveKey := req.FormValue(key); haveKey != value {
|
|
return fmt.Errorf("have header %q, want %q", haveKey, value)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *scenario) theBodyInTheRequestToIs(method, path string, value *godog.DocString) error {
|
|
// We have to exclude HTTP-Overrides to avoid race condition with the creating and sending of the draft message.
|
|
call, err := s.t.getLastCallExcludingHTTPOverride(method, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var body, want map[string]any
|
|
|
|
if err := json.Unmarshal(call.RequestBody, &body); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := json.Unmarshal([]byte(value.Content), &want); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !IsSub(body, want) {
|
|
return fmt.Errorf("have body %v, want %v", body, want)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *scenario) theBodyInTheResponseToIs(method, path string, value *godog.DocString) error {
|
|
// We have to exclude HTTP-Overrides to avoid race condition with the creating and sending of the draft message.
|
|
call, err := s.t.getLastCallExcludingHTTPOverride(method, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var body, want map[string]any
|
|
|
|
if err := json.Unmarshal(call.ResponseBody, &body); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := json.Unmarshal([]byte(value.Content), &want); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !IsSub(body, want) {
|
|
return fmt.Errorf("have body %v, want %v", body, want)
|
|
}
|
|
|
|
return nil
|
|
}
|