From 8408a5fdc00e95bc2d4253f75acb6c310a3a1c09 Mon Sep 17 00:00:00 2001 From: Jakub Date: Thu, 1 Dec 2022 15:52:42 +0100 Subject: [PATCH] GODT-2170: Improving test server behaviour. --- go.mod | 4 ++-- go.sum | 6 ++++++ internal/user/events.go | 11 ++++++----- internal/user/imap.go | 7 ++++--- tests/features/imap/message/create.feature | 1 + tests/features/imap/message/drafts.feature | 4 ++++ tests/imap_test.go | 5 ++++- tests/types_test.go | 2 +- tests/user_test.go | 12 ++++++++---- 9 files changed, 36 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 2ddcdb2b..f1e98ef4 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,9 @@ go 1.18 require ( github.com/0xAX/notificator v0.0.0-20220220101646-ee9b8921e557 github.com/Masterminds/semver/v3 v3.1.1 - github.com/ProtonMail/gluon v0.14.2-0.20221129150032-c663738a6cee + github.com/ProtonMail/gluon v0.14.2-0.20221201132248-f620ad183873 github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a - github.com/ProtonMail/go-proton-api v0.1.4 + github.com/ProtonMail/go-proton-api v0.1.4-0.20221201145430-b0a0286c470e 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 323bc27d..edc29813 100644 --- a/go.sum +++ b/go.sum @@ -30,6 +30,10 @@ github.com/ProtonMail/docker-credential-helpers v1.1.0 h1:+kvUIpwWcbtP3WFv5sSvkF github.com/ProtonMail/docker-credential-helpers v1.1.0/go.mod h1:mK0aBveCxhnQ756AmaTfXMZDeULvheYVhF/MWMErN5g= github.com/ProtonMail/gluon v0.14.2-0.20221129150032-c663738a6cee h1:rDGqVa4CepqpJF8TDjqnBITqD8OzrLzeg66ibVDCPSc= github.com/ProtonMail/gluon v0.14.2-0.20221129150032-c663738a6cee/go.mod h1:z2AxLIiBCT1K+0OBHyaDI7AEaO5qI6/BEC2TE42vs4Q= +github.com/ProtonMail/gluon v0.14.2-0.20221201094406-fc09f2d35933 h1:kj6JBn/ASSDKdLvZPU7fFPKC5vN+UgboBvfJzAaZkrY= +github.com/ProtonMail/gluon v0.14.2-0.20221201094406-fc09f2d35933/go.mod h1:z2AxLIiBCT1K+0OBHyaDI7AEaO5qI6/BEC2TE42vs4Q= +github.com/ProtonMail/gluon v0.14.2-0.20221201132248-f620ad183873 h1:9LKxCftz374K4cse/AdIaUgGLwQlrCPIMIA7llcOck4= +github.com/ProtonMail/gluon v0.14.2-0.20221201132248-f620ad183873/go.mod h1:z2AxLIiBCT1K+0OBHyaDI7AEaO5qI6/BEC2TE42vs4Q= github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a h1:D+aZah+k14Gn6kmL7eKxoo/4Dr/lK3ChBcwce2+SQP4= github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a/go.mod h1:oTGdE7/DlWIr23G0IKW3OXK9wZ5Hw1GGiaJFccTvZi4= github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo= @@ -45,6 +49,8 @@ github.com/ProtonMail/go-mime v0.0.0-20220429130430-2192574d760f h1:4IWzKjHzZxdr github.com/ProtonMail/go-mime v0.0.0-20220429130430-2192574d760f/go.mod h1:qRZgbeASl2a9OwmsV85aWwRqic0NHPh+9ewGAzb4cgM= github.com/ProtonMail/go-proton-api v0.1.3 h1:Y0kdBTbyxEBo4eVK1BD7V9GSTPKUiy/soYR7bg2i2TU= github.com/ProtonMail/go-proton-api v0.1.3/go.mod h1:jqvJ2HqLHqiPJoEb+BTIB1IF7wvr6p+8ZfA6PO2NRNk= +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.4 h1:tGZlYuXlvT7AxFUPry42AsukLYm0HazlRg1iZLvlAZE= github.com/ProtonMail/go-proton-api v0.1.4/go.mod h1:jqvJ2HqLHqiPJoEb+BTIB1IF7wvr6p+8ZfA6PO2NRNk= github.com/ProtonMail/go-rfc5322 v0.11.0 h1:o5Obrm4DpmQEffvgsVqG6S4BKwC1Wat+hYwjIp2YcCY= diff --git a/internal/user/events.go b/internal/user/events.go index 4339128e..41b47a1e 100644 --- a/internal/user/events.go +++ b/internal/user/events.go @@ -487,10 +487,6 @@ func (user *User) handleUpdateDraftEvent(ctx context.Context, event proton.Messa "subject": logging.Sensitive(event.Message.Subject), }).Info("Handling draft updated event") - for _, updateCh := range user.updateCh { - updateCh.Enqueue(imap.NewMessagesDeleted(imap.MessageID(event.ID))) - } - full, err := user.client.GetFullMessage(ctx, event.Message.ID) if err != nil { return fmt.Errorf("failed to get full draft: %w", err) @@ -502,7 +498,12 @@ func (user *User) handleUpdateDraftEvent(ctx context.Context, event proton.Messa return fmt.Errorf("failed to build RFC822 draft: %w", err) } - user.updateCh[full.AddressID].Enqueue(imap.NewMessagesCreated(buildRes.update)) + user.updateCh[full.AddressID].Enqueue(imap.NewMessageUpdated( + buildRes.update.Message, + buildRes.update.Literal, + buildRes.update.MailboxIDs, + buildRes.update.ParsedMessage, + )) return nil }) diff --git a/internal/user/imap.go b/internal/user/imap.go index 33668627..ec5f70ff 100644 --- a/internal/user/imap.go +++ b/internal/user/imap.go @@ -21,6 +21,7 @@ import ( "bytes" "context" "fmt" + "net/mail" "sync/atomic" "time" @@ -442,7 +443,7 @@ func (conn *imapConnector) importMessage( messageID := "" if slices.Contains(labelIDs, proton.DraftsLabel) { - msg, err := conn.createDraft(ctx, literal, addrKR) + msg, err := conn.createDraft(ctx, literal, addrKR, conn.apiAddrs[conn.addrID]) if err != nil { return fmt.Errorf("failed to create draft: %w", err) } @@ -516,7 +517,7 @@ func toIMAPMessage(message proton.MessageMetadata) imap.Message { } } -func (conn *imapConnector) createDraft(ctx context.Context, literal []byte, addrKR *crypto.KeyRing) (proton.Message, error) { //nolint:funlen +func (conn *imapConnector) createDraft(ctx context.Context, literal []byte, addrKR *crypto.KeyRing, sender proton.Address) (proton.Message, error) { //nolint:funlen // Create a new message parser from the reader. parser, err := parser.New(bytes.NewReader(literal)) if err != nil { @@ -549,7 +550,7 @@ func (conn *imapConnector) createDraft(ctx context.Context, literal []byte, addr Body: armBody, MIMEType: message.MIMEType, - Sender: message.Sender, + Sender: &mail.Address{Name: sender.DisplayName, Address: sender.Email}, ToList: message.ToList, CCList: message.CCList, BCCList: message.BCCList, diff --git a/tests/features/imap/message/create.feature b/tests/features/imap/message/create.feature index 54be10d7..fee5fb7e 100644 --- a/tests/features/imap/message/create.feature +++ b/tests/features/imap/message/create.feature @@ -27,6 +27,7 @@ Feature: IMAP create messages And IMAP client "1" eventually sees the following messages in "Drafts": | from | to | subject | body | | user@pm.me | john.doe@email.com | foo | bar | + # This fails now And IMAP client "1" eventually sees the following messages in "All Mail": | from | to | subject | body | | user@pm.me | john.doe@email.com | foo | bar | diff --git a/tests/features/imap/message/drafts.feature b/tests/features/imap/message/drafts.feature index 72c91ee4..1f2a8c36 100644 --- a/tests/features/imap/message/drafts.feature +++ b/tests/features/imap/message/drafts.feature @@ -11,6 +11,10 @@ Feature: IMAP Draft messages This is a dra """ + Then IMAP client "1" eventually sees the following messages in "Drafts": + | body | + | This is a dra | + And IMAP client "1" sees 1 messages in "Drafts" Scenario: Draft edited locally When IMAP client "1" marks message 1 as deleted diff --git a/tests/imap_test.go b/tests/imap_test.go index 0b92f8ed..efeaf1bd 100644 --- a/tests/imap_test.go +++ b/tests/imap_test.go @@ -33,6 +33,7 @@ import ( "github.com/emersion/go-imap" id "github.com/emersion/go-imap-id" "github.com/emersion/go-imap/client" + "github.com/sirupsen/logrus" "golang.org/x/exp/slices" ) @@ -280,7 +281,9 @@ func (s *scenario) imapClientSeesTheFollowingMessagesInMailbox(clientID, mailbox func (s *scenario) imapClientEventuallySeesTheFollowingMessagesInMailbox(clientID, mailbox string, table *godog.Table) error { return eventually(func() error { - return s.imapClientSeesTheFollowingMessagesInMailbox(clientID, mailbox, table) + err := s.imapClientSeesTheFollowingMessagesInMailbox(clientID, mailbox, table) + logrus.WithError(err).Trace("Matching eventually") + return err }) } diff --git a/tests/types_test.go b/tests/types_test.go index 6901d5d2..151199b0 100644 --- a/tests/types_test.go +++ b/tests/types_test.go @@ -144,7 +144,7 @@ func matchMessages(have, want []Message) error { }) if !IsSub(ToAny(have), ToAny(want)) { - return fmt.Errorf("missing messages: have %+v, want %+v", have, want) + return fmt.Errorf("missing messages: have %#v, want %#v", have, want) } return nil diff --git a/tests/user_test.go b/tests/user_test.go index 08cc3f76..1985f8f8 100644 --- a/tests/user_test.go +++ b/tests/user_test.go @@ -238,7 +238,7 @@ func (s *scenario) addressDraftChanged(draftIndex int, address, username string, draftID := s.t.getDraftID(username, draftIndex) - encBody := []byte{} + encBody := "" if wantMessages[0].Body != "" { ctx, cancel := context.WithCancel(context.Background()) @@ -247,8 +247,12 @@ func (s *scenario) addressDraftChanged(draftIndex int, address, username string, 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 { - var err error - encBody, err = proton.EncryptRFC822(addrKR, wantMessages[0].Build()) + msg, err := addrKR.Encrypt(crypto.NewPlainMessage([]byte(wantMessages[0].Body)), addrKR) + if err != nil { + return err + } + + encBody, err = msg.GetArmored() return err }) }); err != nil { @@ -258,7 +262,7 @@ func (s *scenario) addressDraftChanged(draftIndex int, address, username string, changes := proton.DraftTemplate{ Subject: wantMessages[0].Subject, - Body: string(encBody), + Body: encBody, } if wantMessages[0].To != "" { changes.ToList = []*mail.Address{{Address: wantMessages[0].To}}