From e02eb78cec2d4baa1a1e8ba4f53eb71e5f70c00f Mon Sep 17 00:00:00 2001 From: ewin Date: Wed, 5 Mar 2025 14:18:31 -0500 Subject: [PATCH] Add test for handling of statuses with no stored content type --- internal/processing/status/edit_test.go | 65 +++++++++++++++++++++++++ testrig/testmodels.go | 26 ++++++++++ 2 files changed, 91 insertions(+) diff --git a/internal/processing/status/edit_test.go b/internal/processing/status/edit_test.go index f95914499..2d5ecb929 100644 --- a/internal/processing/status/edit_test.go +++ b/internal/processing/status/edit_test.go @@ -161,6 +161,71 @@ func (suite *StatusEditTestSuite) TestEditChangeContentType() { suite.Equal(status.UpdatedAt(), previousEdit.CreatedAt) } +func (suite *StatusEditTestSuite) TestEditOnStatusWithNoContentType() { + // Create cancellable context to use for test. + ctx, cncl := context.WithCancel(context.Background()) + defer cncl() + + // Get a local account to use as test requester. + requester := suite.testAccounts["local_account_1"] + requester, _ = suite.state.DB.GetAccountByID(ctx, requester.ID) + + // Get requester's existing status, which has no + // stored content type, to perform an edit on. + status := suite.testStatuses["local_account_1_status_10"] + status, _ = suite.state.DB.GetStatusByID(ctx, status.ID) + + // Prepare edit without setting a new content type. + form := &apimodel.StatusEditRequest{ + Status: "how will this text be parsed? it is a mystery", + SpoilerText: "shhhhh", + Sensitive: true, + Language: "fr", // hoh hoh hoh + MediaIDs: nil, + MediaAttributes: nil, + Poll: nil, + } + + // Pass the prepared form to the status processor to perform the edit. + apiStatus, errWithCode := suite.status.Edit(ctx, requester, status.ID, form) + suite.NotNil(apiStatus) + suite.NoError(errWithCode) + + // Check response against input form data. + suite.Equal(form.Status, apiStatus.Text) + suite.NotEqual(util.FormatISO8601(status.EditedAt), *apiStatus.EditedAt) + + // Fetched the latest version of edited status from the database. + latestStatus, err := suite.state.DB.GetStatusByID(ctx, status.ID) + suite.NoError(err) + + // Check latest status against input form data + suite.Equal(form.Status, latestStatus.Text) + suite.Equal(form.Sensitive, *latestStatus.Sensitive) + suite.Equal(form.Language, latestStatus.Language) + suite.Equal(len(status.EditIDs)+1, len(latestStatus.EditIDs)) + suite.NotEqual(status.UpdatedAt(), latestStatus.UpdatedAt()) + + // Check latest status against requester's default content type + // setting (the test accounts don't actually have settings on them, + // so instead we check that the global default content type is used) + suite.Equal(gtsmodel.StatusContentTypeDefault, latestStatus.ContentType) + + // Populate all historical edits for this status. + err = suite.state.DB.PopulateStatusEdits(ctx, latestStatus) + suite.NoError(err) + + // Check previous status edit matches original status content. + previousEdit := latestStatus.Edits[len(latestStatus.Edits)-1] + suite.Equal(status.Content, previousEdit.Content) + suite.Equal(status.Text, previousEdit.Text) + suite.Equal(status.ContentType, previousEdit.ContentType) + suite.Equal(status.ContentWarning, previousEdit.ContentWarning) + suite.Equal(*status.Sensitive, *previousEdit.Sensitive) + suite.Equal(status.Language, previousEdit.Language) + suite.Equal(status.UpdatedAt(), previousEdit.CreatedAt) +} + func (suite *StatusEditTestSuite) TestEditAddPoll() { // Create cancellable context to use for test. ctx, cncl := context.WithCancel(context.Background()) diff --git a/testrig/testmodels.go b/testrig/testmodels.go index 44eff82fa..a5e8b6fe8 100644 --- a/testrig/testmodels.go +++ b/testrig/testmodels.go @@ -1765,6 +1765,32 @@ func NewTestStatuses() map[string]*gtsmodel.Status { Federated: util.Ptr(true), ActivityStreamsType: ap.ObjectNote, }, + "local_account_1_status_10": { + ID: "01JNKTFJCF6494E5MRR8GN47NA", + URI: "http://localhost:8080/users/the_mighty_zork/statuses/01JNKTFJCF6494E5MRR8GN47NA", + URL: "http://localhost:8080/@the_mighty_zork/statuses/01JNKTFJCF6494E5MRR8GN47NA", + Content: "

I am an old post with no content type stored

", + Text: "I am an old post with no content type stored", + ContentType: 0, + ContentWarning: "edited status", + AttachmentIDs: nil, + CreatedAt: TimeMustParse("2025-03-05T13:53:24.239-05:00"), + EditedAt: TimeMustParse("2025-03-05T13:53:24.239-05:00"), + Local: util.Ptr(true), + AccountURI: "http://localhost:8080/users/the_mighty_zork", + AccountID: "01F8MH1H7YV1Z7D2C8K2730QBF", + InReplyToID: "", + InReplyToAccountID: "", + InReplyToURI: "", + BoostOfID: "", + ThreadID: "", + Visibility: gtsmodel.VisibilityPublic, + Sensitive: util.Ptr(false), + Language: "en", + CreatedWithApplicationID: "01F8MGY43H3N2C8EWPR2FPYEXG", + Federated: util.Ptr(true), + ActivityStreamsType: ap.ObjectNote, + }, "local_account_2_status_1": { ID: "01F8MHBQCBTDKN6X5VHGMMN4MA", URI: "http://localhost:8080/users/1happyturtle/statuses/01F8MHBQCBTDKN6X5VHGMMN4MA",