test(GODT-2746): polish the test code.

This commit is contained in:
Jakub
2023-08-31 11:44:39 +02:00
parent 4c76e35a2d
commit 635b81314a
2 changed files with 58 additions and 34 deletions

View File

@ -28,6 +28,7 @@ import (
"time" "time"
"github.com/Masterminds/semver/v3" "github.com/Masterminds/semver/v3"
"github.com/ProtonMail/proton-bridge/v3/internal/bridge"
"github.com/ProtonMail/proton-bridge/v3/internal/events" "github.com/ProtonMail/proton-bridge/v3/internal/events"
"github.com/ProtonMail/proton-bridge/v3/internal/vault" "github.com/ProtonMail/proton-bridge/v3/internal/vault"
"github.com/cucumber/godog" "github.com/cucumber/godog"
@ -153,56 +154,79 @@ func (s *scenario) theUserSetSMTPModeToSSL() error {
return s.t.bridge.SetSMTPSSL(context.Background(), true) return s.t.bridge.SetSMTPSSL(context.Background(), true)
} }
type testBugReport struct {
OSType string `json:"OS"`
OSVersion string `json:"OSVersion"`
Title string `json:"Title"`
Description string `json:"Description"`
Username string `json:"Username"`
Email string `json:"Email"`
Client string `json:"Client"`
Attachment bool `json:"Attachment"`
bridge *bridge.Bridge
}
func newTestBugReport(bridge *bridge.Bridge) *testBugReport {
return &testBugReport{
OSType: "osType",
OSVersion: "osVersion",
Title: "title",
Description: "description",
Username: "username",
Email: "email",
Client: "client",
Attachment: false,
bridge: bridge,
}
}
func (r *testBugReport) report() error {
return r.bridge.ReportBug(context.Background(), r.OSType, r.OSVersion, r.Title, r.Description, r.Username, r.Email, r.Client, r.Attachment)
}
func (s *scenario) theUserReportsABug() error { func (s *scenario) theUserReportsABug() error {
return s.t.bridge.ReportBug(context.Background(), "osType", "osVersion", "title", "description", "username", "email", "client", false) return newTestBugReport(s.t.bridge).report()
} }
func (s *scenario) theUserReportsABugWithSingleHeaderChange(key, value string) error { func (s *scenario) theUserReportsABugWithSingleHeaderChange(key, value string) error {
switch keyValue := key; keyValue { bugReport := newTestBugReport(s.t.bridge)
switch key {
case "osType": case "osType":
return s.t.bridge.ReportBug(context.Background(), value, "osVersion", "title", "description", "username", "email", "client", false) bugReport.OSType = value
case "osVersion": case "osVersion":
return s.t.bridge.ReportBug(context.Background(), "osType", value, "title", "description", "username", "email", "client", false) bugReport.OSVersion = value
case "Title": case "Title":
return s.t.bridge.ReportBug(context.Background(), "osType", "osVersion", value, "description", "username", "email", "client", false) bugReport.Title = value
case "Description": case "Description":
return s.t.bridge.ReportBug(context.Background(), "osType", "osVersion", "title", value, "username", "email", "client", false) bugReport.Description = value
case "Username": case "Username":
return s.t.bridge.ReportBug(context.Background(), "osType", "osVersion", "title", "description", value, "email", "client", false) bugReport.Username = value
case "Email": case "Email":
return s.t.bridge.ReportBug(context.Background(), "osType", "osVersion", "title", "description", "username", value, "client", false) bugReport.Email = value
case "Client": case "Client":
return s.t.bridge.ReportBug(context.Background(), "osType", "osVersion", "title", "description", "username", "email", value, false) bugReport.Client = value
case "Attachment": case "Attachment":
att, _ := strconv.ParseBool(value) att, err := strconv.ParseBool(value)
return s.t.bridge.ReportBug(context.Background(), "osType", "osVersion", "title", "description", "username", "email", "client", att) if err != nil {
return fmt.Errorf("failed to parse bug report attachment preferences: %w", err)
}
bugReport.Attachment = att
default: default:
return fmt.Errorf("Wrong header (\"%s\") is being checked", key) return fmt.Errorf("Wrong header (\"%s\") is being checked", key)
} }
return bugReport.report()
} }
func (s *scenario) theUserReportsABugWithDetails(value *godog.DocString) error { func (s *scenario) theUserReportsABugWithDetails(value *godog.DocString) error {
type BugReportTest struct { bugReport := newTestBugReport(s.t.bridge)
OSType string `json:"OS"` if err := json.Unmarshal([]byte(value.Content), &bugReport); err != nil {
OSVersion string `json:"OSVersion"` return fmt.Errorf("cannot parse bug report details: %w", err)
Title string `json:"Title"`
Description string `json:"Description"`
Username string `json:"Username"`
Email string `json:"Email"`
Client string `json:"Client"`
Attachment bool `json:"Attachment"`
} }
bugReport := BugReportTest{OSType: "osType", OSVersion: "osVersion", Title: "title", Description: "description", Username: "username", Email: "email", Client: "client", Attachment: false} return bugReport.report()
jsonString := value
err := json.Unmarshal([]byte(jsonString.Content), &bugReport)
if err != nil {
fmt.Println(err)
}
return s.t.bridge.ReportBug(context.Background(), bugReport.OSType, bugReport.OSVersion, bugReport.Title, bugReport.Description, bugReport.Username, bugReport.Email, bugReport.Client, bugReport.Attachment)
} }
func (s *scenario) bridgeSendsAConnectionUpEvent() error { func (s *scenario) bridgeSendsAConnectionUpEvent() error {

View File

@ -83,8 +83,8 @@ func (s *scenario) theHeaderInTheRequestToHasSetTo(method, path, key, value stri
return err return err
} }
if haveKey := call.RequestHeader.Get(key); haveKey != value { if haveValue := call.RequestHeader.Get(key); haveValue != value {
return fmt.Errorf("have header %q, want %q", haveKey, value) return fmt.Errorf("header field %q have %q, want %q", key, haveValue, value)
} }
return nil return nil
@ -124,8 +124,8 @@ func (s *scenario) theHeaderInTheMultipartRequestToHasSetTo(method, path, key, v
return fmt.Errorf("failed to parse multipart form: %w", err) return fmt.Errorf("failed to parse multipart form: %w", err)
} }
if haveKey := req.FormValue(key); haveKey != value { if haveValue := req.FormValue(key); haveValue != value {
return fmt.Errorf("have header %q, want %q", haveKey, value) return fmt.Errorf("header field %q have %q, want %q", key, haveValue, value)
} }
return nil return nil