From 2a771dda90e1ab573a9fb7e409471ae9e8c42509 Mon Sep 17 00:00:00 2001 From: Ashlie Martinez Date: Thu, 21 Dec 2023 16:54:46 -0700 Subject: [PATCH] Update test to check sanitized input Test deserializing and sending the sanitized version of the output from the original test. Also ensure that the returned body from the sanitized input is the same as the body from the original input. We compare only the body instead of the entire serialized content because the item's ID, mod time, etc will change between the versions. --- src/pkg/services/m365/api/mail_test.go | 87 ++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/src/pkg/services/m365/api/mail_test.go b/src/pkg/services/m365/api/mail_test.go index 9e6644f72..23d5058bb 100644 --- a/src/pkg/services/m365/api/mail_test.go +++ b/src/pkg/services/m365/api/mail_test.go @@ -15,6 +15,7 @@ import ( "github.com/stretchr/testify/suite" "github.com/alcionai/corso/src/internal/common/ptr" + "github.com/alcionai/corso/src/internal/common/sanitize" exchMock "github.com/alcionai/corso/src/internal/m365/service/exchange/mock" "github.com/alcionai/corso/src/internal/tester" "github.com/alcionai/corso/src/internal/tester/tconfig" @@ -567,6 +568,38 @@ func sendItemWithBodyAndGetSerialized( return serialized } +func sendSerializedItemAndGetSerialized( + t *testing.T, + ctx context.Context, //revive:disable-line:context-as-argument + msgs Mail, + userID string, + mailFolderID string, + serializedInput []byte, +) []byte { + msg, err := BytesToMessageable(serializedInput) + require.NoError(t, err, clues.ToCore(err)) + + item, err := msgs.PostItem(ctx, userID, mailFolderID, msg) + require.NoError(t, err, clues.ToCore(err)) + + fetched, _, err := msgs.GetItem( + ctx, + userID, + ptr.Val(item.GetId()), + false, + fault.New(true)) + require.NoError(t, err, clues.ToCore(err)) + + serialized, err := msgs.Serialize( + ctx, + fetched, + userID, + ptr.Val(item.GetId())) + require.NoError(t, err, clues.ToCore(err)) + + return serialized +} + func (suite *MailAPIIntgSuite) TestMail_WithSpecialCharacters() { t := suite.T() @@ -622,6 +655,33 @@ func (suite *MailAPIIntgSuite) TestMail_WithSpecialCharacters() { case bodyContent != string(matches[0][1]): t.Logf("text of 0x%x has been transformed to %s", i, matches[0][1]) } + + sanitized := sanitize.JSONBytes(serialized) + newSerialized := sendSerializedItemAndGetSerialized( + t, + ctx, + msgs, + userID, + ptr.Val(mailfolder.GetId()), + sanitized) + + newMatches := contentRegex.FindAllSubmatch(newSerialized, -1) + + switch { + case len(newMatches) == 0: + t.Logf("sanitized text of 0x%x wasn't found", i) + + case len(newMatches[0]) < 2: + t.Logf("sanitized text of 0x%x was removed", i) + + case bodyContent != string(newMatches[0][1]): + t.Logf( + "sanitized text of 0x%x has been transformed to %s", + i, + newMatches[0][1]) + } + + assert.Equal(t, matches[0][1], newMatches[0][1]) //}) } } @@ -680,6 +740,33 @@ func (suite *MailAPIIntgSuite) TestMail_WithSpecialCharacters() { case sequence != string(matches[0][1]): t.Logf("sequence %d has been transformed to %s", i, matches[0][1]) } + + sanitized := sanitize.JSONBytes(serialized) + newSerialized := sendSerializedItemAndGetSerialized( + t, + ctx, + msgs, + userID, + ptr.Val(mailfolder.GetId()), + sanitized) + + newMatches := contentRegex.FindAllSubmatch(newSerialized, -1) + + switch { + case len(newMatches) == 0: + t.Logf("sanitized sequence %d wasn't found", i) + + case len(newMatches[0]) < 2: + t.Logf("sanitized sequence %d was removed", i) + + case sequence != string(newMatches[0][1]): + t.Logf( + "sanitized sequence %d has been transformed to %s", + i, + newMatches[0][1]) + } + + assert.Equal(t, matches[0][1], newMatches[0][1]) //}) } }