From 7bc608ce6c4c0852f157642ecb9793686c4375a3 Mon Sep 17 00:00:00 2001 From: James Houlahan Date: Fri, 2 Dec 2022 12:47:12 +0100 Subject: [PATCH] GODT-2170: Use client-side draft update in integration tests --- go.mod | 2 +- go.sum | 4 +- tests/api_test.go | 3 - tests/bdd_test.go | 2 +- tests/ctx_test.go | 22 ++---- tests/features/imap/message/drafts.feature | 2 +- tests/user_test.go | 82 ++++++++++++++-------- 7 files changed, 63 insertions(+), 54 deletions(-) diff --git a/go.mod b/go.mod index 8eef9471..3021f04a 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/Masterminds/semver/v3 v3.1.1 github.com/ProtonMail/gluon v0.14.2-0.20221202093012-ad1570c49c8c github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a - github.com/ProtonMail/go-proton-api v0.1.4-0.20221201145430-b0a0286c470e + github.com/ProtonMail/go-proton-api v0.1.5 github.com/ProtonMail/go-rfc5322 v0.11.0 github.com/ProtonMail/gopenpgp/v2 v2.4.10 github.com/PuerkitoBio/goquery v1.8.0 diff --git a/go.sum b/go.sum index 3e5049c5..1a784fd6 100644 --- a/go.sum +++ b/go.sum @@ -43,8 +43,8 @@ github.com/ProtonMail/go-message v0.0.0-20210611055058-fabeff2ec753/go.mod h1:NB github.com/ProtonMail/go-mime v0.0.0-20220302105931-303f85f7fe0f/go.mod h1:NYt+V3/4rEeDuaev/zw1zCq8uqVEuPHzDPo3OZrlGJ4= github.com/ProtonMail/go-mime v0.0.0-20220429130430-2192574d760f h1:4IWzKjHzZxdrW9k4zl/qCwenOVHDbVDADPPHFLjs0Oc= github.com/ProtonMail/go-mime v0.0.0-20220429130430-2192574d760f/go.mod h1:qRZgbeASl2a9OwmsV85aWwRqic0NHPh+9ewGAzb4cgM= -github.com/ProtonMail/go-proton-api v0.1.4-0.20221201145430-b0a0286c470e h1:PQIbcD4ZpHsZaLT2RcVuoWi7yl2f2x51KthTrerOpqI= -github.com/ProtonMail/go-proton-api v0.1.4-0.20221201145430-b0a0286c470e/go.mod h1:jqvJ2HqLHqiPJoEb+BTIB1IF7wvr6p+8ZfA6PO2NRNk= +github.com/ProtonMail/go-proton-api v0.1.5 h1:6RJO3jXP3opFfGqrXNvFTefdD4MlfxKjIebT8r5ROf8= +github.com/ProtonMail/go-proton-api v0.1.5/go.mod h1:jqvJ2HqLHqiPJoEb+BTIB1IF7wvr6p+8ZfA6PO2NRNk= github.com/ProtonMail/go-rfc5322 v0.11.0 h1:o5Obrm4DpmQEffvgsVqG6S4BKwC1Wat+hYwjIp2YcCY= github.com/ProtonMail/go-rfc5322 v0.11.0/go.mod h1:6oOKr0jXvpoE6pwTx/HukigQpX2J9WUf6h0auplrFTw= github.com/ProtonMail/go-srp v0.0.5 h1:xhUioxZgDbCnpo9JehyFhwwsn9JLWkUGfB0oiKXgiGg= diff --git a/tests/api_test.go b/tests/api_test.go index d32f7315..530cea4e 100644 --- a/tests/api_test.go +++ b/tests/api_test.go @@ -19,7 +19,6 @@ package tests import ( "github.com/Masterminds/semver/v3" - "github.com/ProtonMail/go-proton-api" "github.com/ProtonMail/go-proton-api/server" ) @@ -36,8 +35,6 @@ type API interface { RemoveAddress(userID, addrID string) error RemoveAddressKey(userID, addrID, keyID string) error - UpdateDraft(userID, draftID string, changes proton.DraftTemplate) error - Close() } diff --git a/tests/bdd_test.go b/tests/bdd_test.go index 8073172f..25ad2228 100644 --- a/tests/bdd_test.go +++ b/tests/bdd_test.go @@ -98,7 +98,7 @@ func TestFeatures(testingT *testing.T) { ctx.Step(`^the address "([^"]*)" of account "([^"]*)" has the following messages in "([^"]*)":$`, s.theAddressOfAccountHasTheFollowingMessagesInMailbox) ctx.Step(`^the address "([^"]*)" of account "([^"]*)" has (\d+) messages in "([^"]*)"$`, s.theAddressOfAccountHasMessagesInMailbox) ctx.Step(`^the address "([^"]*)" of account "([^"]*)" has no keys$`, s.theAddressOfAccountHasNoKeys) - ctx.Step(`^the following fields where changed in draft (\d+) for address "([^"]*)" of account "([^"]*)":$`, s.addressDraftChanged) + ctx.Step(`^the following fields were changed in draft (\d+) for address "([^"]*)" of account "([^"]*)":$`, s.theFollowingFieldsWereChangedInDraftForAddressOfAccount) // ==== BRIDGE ==== ctx.Step(`^bridge starts$`, s.bridgeStarts) diff --git a/tests/ctx_test.go b/tests/ctx_test.go index 9eb4ca0e..e489714c 100644 --- a/tests/ctx_test.go +++ b/tests/ctx_test.go @@ -230,36 +230,28 @@ func (t *testCtx) getMBoxID(userID string, name string) string { // getDraftID will return the API ID of draft message with draftIndex, where // draftIndex is similar to sequential ID i.e. 1 represents the first message // of draft folder sorted by API creation time. -func (t *testCtx) getDraftID(username string, draftIndex int) string { - if draftIndex < 1 { - panic(fmt.Sprintf("draft index suppose to be non-zero positive integer, but have %d", draftIndex)) - } - +func (t *testCtx) getDraftID(username string, draftIndex int) (string, error) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() var draftID string if err := t.withClient(ctx, username, func(ctx context.Context, client *proton.Client) error { - messages, err := client.GetMessageMetadata( - ctx, proton.MessageFilter{LabelID: proton.DraftsLabel}, - ) + messages, err := client.GetMessageMetadata(ctx, proton.MessageFilter{LabelID: proton.DraftsLabel}) if err != nil { - panic(err) - } - - if len(messages) < draftIndex { - panic("draft index too high") + return fmt.Errorf("failed to get message metadata: %w", err) + } else if len(messages) < draftIndex { + return fmt.Errorf("draft index %d is out of range", draftIndex) } draftID = messages[draftIndex-1].ID return nil }); err != nil { - panic(err) + return "", err } - return draftID + return draftID, nil } func (t *testCtx) getLastCall(method, pathExp string) (server.Call, error) { diff --git a/tests/features/imap/message/drafts.feature b/tests/features/imap/message/drafts.feature index 1f2a8c36..bdcfb46b 100644 --- a/tests/features/imap/message/drafts.feature +++ b/tests/features/imap/message/drafts.feature @@ -34,7 +34,7 @@ Feature: IMAP Draft messages And IMAP client "1" sees 1 messages in "Drafts" Scenario: Draft edited remotely - When the following fields where changed in draft 1 for address "user@pm.me" of account "user@pm.me": + When the following fields were changed in draft 1 for address "user@pm.me" of account "user@pm.me": | to | subject | body | | someone@proton.me | Basic Draft | This is a draft body, but longer | Then IMAP client "1" eventually sees the following messages in "Drafts": diff --git a/tests/user_test.go b/tests/user_test.go index 1985f8f8..f28c17fd 100644 --- a/tests/user_test.go +++ b/tests/user_test.go @@ -226,7 +226,7 @@ func (s *scenario) theAddressOfAccountHasNoKeys(address, username string) error // accountDraftChanged changes the draft attributes, where draftIndex is // similar to sequential ID i.e. 1 represents the first message of draft folder // sorted by API creation time. -func (s *scenario) addressDraftChanged(draftIndex int, address, username string, table *godog.Table) error { +func (s *scenario) theFollowingFieldsWereChangedInDraftForAddressOfAccount(draftIndex int, address, username string, table *godog.Table) error { wantMessages, err := unmarshalTable[Message](table) if err != nil { return err @@ -236,39 +236,59 @@ func (s *scenario) addressDraftChanged(draftIndex int, address, username string, return fmt.Errorf("expected to have one row in table but got %d instead", len(wantMessages)) } - draftID := s.t.getDraftID(username, draftIndex) - - encBody := "" - - if wantMessages[0].Body != "" { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - if err := s.t.withClient(ctx, username, func(ctx context.Context, c *proton.Client) error { - return s.t.withAddrKR(ctx, c, username, s.t.getUserAddrID(s.t.getUserID(username), address), - func(ctx context.Context, addrKR *crypto.KeyRing) error { - msg, err := addrKR.Encrypt(crypto.NewPlainMessage([]byte(wantMessages[0].Body)), addrKR) - if err != nil { - return err - } - - encBody, err = msg.GetArmored() - return err - }) - }); err != nil { - return err - } + draftID, err := s.t.getDraftID(username, draftIndex) + if err != nil { + return fmt.Errorf("failed to get draft ID: %w", err) } - changes := proton.DraftTemplate{ - Subject: wantMessages[0].Subject, - Body: encBody, - } - if wantMessages[0].To != "" { - changes.ToList = []*mail.Address{{Address: wantMessages[0].To}} - } + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() - return s.t.api.UpdateDraft(s.t.getUserID(username), draftID, changes) + return s.t.withClient(ctx, username, func(ctx context.Context, c *proton.Client) error { + return s.t.withAddrKR(ctx, c, username, s.t.getUserAddrID(s.t.getUserID(username), address), func(_ context.Context, addrKR *crypto.KeyRing) error { + var changes proton.DraftTemplate + + if wantMessages[0].From != "" { + return fmt.Errorf("changing from address is not supported") + } + + if wantMessages[0].To != "" { + changes.ToList = []*mail.Address{{Address: wantMessages[0].To}} + } + + if wantMessages[0].CC != "" { + changes.CCList = []*mail.Address{{Address: wantMessages[0].CC}} + } + + if wantMessages[0].BCC != "" { + changes.BCCList = []*mail.Address{{Address: wantMessages[0].BCC}} + } + + if wantMessages[0].Subject != "" { + changes.Subject = wantMessages[0].Subject + } + + if wantMessages[0].Body != "" { + enc, err := addrKR.Encrypt(crypto.NewPlainMessageFromString(wantMessages[0].Body), addrKR) + if err != nil { + return fmt.Errorf("failed to encrypt message body: %w", err) + } + + arm, err := enc.GetArmored() + if err != nil { + return fmt.Errorf("failed to get armored message: %w", err) + } + + changes.Body = arm + } + + if _, err := c.UpdateDraft(ctx, draftID, proton.UpdateDraftReq{Message: changes}); err != nil { + return fmt.Errorf("failed to update draft: %w", err) + } + + return nil + }) + }) } func (s *scenario) userLogsInWithUsernameAndPassword(username, password string) error {