[bugfix] Fix duplicating fields on profile edit (#1788)

* [bugfix] Fix duplicating fields on profile edit

* test non-duplicate fields
This commit is contained in:
tobi 2023-05-15 12:52:40 +02:00 committed by GitHub
parent e1b7ab2603
commit 17b9a937b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 198 additions and 136 deletions

View file

@ -155,6 +155,7 @@ func (p *Processor) Update(ctx context.Context, account *gtsmodel.Account, form
} }
// Process the raw fields we stored earlier. // Process the raw fields we stored earlier.
account.Fields = make([]*gtsmodel.Field, 0, len(account.FieldsRaw))
for _, fieldRaw := range account.FieldsRaw { for _, fieldRaw := range account.FieldsRaw {
field := &gtsmodel.Field{} field := &gtsmodel.Field{}

View file

@ -24,208 +24,268 @@
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/ap" "github.com/superseriousbusiness/gotosocial/internal/ap"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
) )
type AccountUpdateTestSuite struct { type AccountUpdateTestSuite struct {
AccountStandardTestSuite AccountStandardTestSuite
} }
func (suite *AccountStandardTestSuite) checkClientAPIChan(accountID string) {
msg := <-suite.fromClientAPIChan
// Profile update.
suite.Equal(ap.ActivityUpdate, msg.APActivityType)
suite.Equal(ap.ObjectProfile, msg.APObjectType)
// Correct account updated.
if msg.OriginAccount == nil {
suite.FailNow("expected msg.OriginAccount not to be nil")
}
suite.Equal(accountID, msg.OriginAccount.ID)
}
func (suite *AccountUpdateTestSuite) TestAccountUpdateSimple() { func (suite *AccountUpdateTestSuite) TestAccountUpdateSimple() {
testAccount := suite.testAccounts["local_account_1"] testAccount := &gtsmodel.Account{}
*testAccount = *suite.testAccounts["local_account_1"]
locked := true var (
displayName := "new display name" ctx = context.Background()
note := "#hello here i am!" locked = true
displayName = "new display name"
note = "#hello here i am!"
noteExpected = `<p><a href="http://localhost:8080/tags/hello" class="mention hashtag" rel="tag nofollow noreferrer noopener" target="_blank">#<span>hello</span></a> here i am!</p>`
)
form := &apimodel.UpdateCredentialsRequest{ // Call update function.
apiAccount, errWithCode := suite.accountProcessor.Update(ctx, testAccount, &apimodel.UpdateCredentialsRequest{
DisplayName: &displayName, DisplayName: &displayName,
Locked: &locked, Locked: &locked,
Note: &note, Note: &note,
})
if errWithCode != nil {
suite.FailNow(errWithCode.Error())
} }
// should get no error from the update function, and an api model account returned // Returned profile should be updated.
apiAccount, errWithCode := suite.accountProcessor.Update(context.Background(), testAccount, form)
suite.NoError(errWithCode)
suite.NotNil(apiAccount)
// fields on the profile should be updated
suite.True(apiAccount.Locked) suite.True(apiAccount.Locked)
suite.Equal(displayName, apiAccount.DisplayName) suite.Equal(displayName, apiAccount.DisplayName)
suite.Equal(`<p><a href="http://localhost:8080/tags/hello" class="mention hashtag" rel="tag nofollow noreferrer noopener" target="_blank">#<span>hello</span></a> here i am!</p>`, apiAccount.Note) suite.Equal(noteExpected, apiAccount.Note)
// we should have an update in the client api channel // We should have an update in the client api channel.
msg := <-suite.fromClientAPIChan suite.checkClientAPIChan(testAccount.ID)
suite.Equal(ap.ActivityUpdate, msg.APActivityType)
suite.Equal(ap.ObjectProfile, msg.APObjectType)
suite.NotNil(msg.OriginAccount)
suite.Equal(testAccount.ID, msg.OriginAccount.ID)
suite.Nil(msg.TargetAccount)
// fields should be updated in the database as well // Check database model of account as well.
dbAccount, err := suite.db.GetAccountByID(context.Background(), testAccount.ID) dbAccount, err := suite.db.GetAccountByID(ctx, testAccount.ID)
suite.NoError(err) if err != nil {
suite.FailNow(err.Error())
}
suite.True(*dbAccount.Locked) suite.True(*dbAccount.Locked)
suite.Equal(displayName, dbAccount.DisplayName) suite.Equal(displayName, dbAccount.DisplayName)
suite.Equal(`<p><a href="http://localhost:8080/tags/hello" class="mention hashtag" rel="tag nofollow noreferrer noopener" target="_blank">#<span>hello</span></a> here i am!</p>`, dbAccount.Note) suite.Equal(noteExpected, dbAccount.Note)
} }
func (suite *AccountUpdateTestSuite) TestAccountUpdateWithMention() { func (suite *AccountUpdateTestSuite) TestAccountUpdateWithMention() {
testAccount := suite.testAccounts["local_account_1"] testAccount := &gtsmodel.Account{}
*testAccount = *suite.testAccounts["local_account_1"]
var ( var (
ctx = context.Background()
locked = true locked = true
displayName = "new display name" displayName = "new display name"
note = "#hello here i am!\n\ngo check out @1happyturtle, they have a cool account!" note = "#hello here i am!\n\ngo check out @1happyturtle, they have a cool account!"
noteExpected = "<p><a href=\"http://localhost:8080/tags/hello\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>hello</span></a> here i am!<br><br>go check out <span class=\"h-card\"><a href=\"http://localhost:8080/@1happyturtle\" class=\"u-url mention\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">@<span>1happyturtle</span></a></span>, they have a cool account!</p>" noteExpected = "<p><a href=\"http://localhost:8080/tags/hello\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>hello</span></a> here i am!<br><br>go check out <span class=\"h-card\"><a href=\"http://localhost:8080/@1happyturtle\" class=\"u-url mention\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">@<span>1happyturtle</span></a></span>, they have a cool account!</p>"
) )
form := &apimodel.UpdateCredentialsRequest{ // Call update function.
apiAccount, errWithCode := suite.accountProcessor.Update(ctx, testAccount, &apimodel.UpdateCredentialsRequest{
DisplayName: &displayName, DisplayName: &displayName,
Locked: &locked, Locked: &locked,
Note: &note, Note: &note,
})
if errWithCode != nil {
suite.FailNow(errWithCode.Error())
} }
// should get no error from the update function, and an api model account returned // Returned profile should be updated.
apiAccount, errWithCode := suite.accountProcessor.Update(context.Background(), testAccount, form)
suite.NoError(errWithCode)
suite.NotNil(apiAccount)
// fields on the profile should be updated
suite.True(apiAccount.Locked) suite.True(apiAccount.Locked)
suite.Equal(displayName, apiAccount.DisplayName) suite.Equal(displayName, apiAccount.DisplayName)
suite.Equal(noteExpected, apiAccount.Note) suite.Equal(noteExpected, apiAccount.Note)
// we should have an update in the client api channel // We should have an update in the client api channel.
msg := <-suite.fromClientAPIChan suite.checkClientAPIChan(testAccount.ID)
suite.Equal(ap.ActivityUpdate, msg.APActivityType)
suite.Equal(ap.ObjectProfile, msg.APObjectType)
suite.NotNil(msg.OriginAccount)
suite.Equal(testAccount.ID, msg.OriginAccount.ID)
suite.Nil(msg.TargetAccount)
// fields should be updated in the database as well // Check database model of account as well.
dbAccount, err := suite.db.GetAccountByID(context.Background(), testAccount.ID) dbAccount, err := suite.db.GetAccountByID(ctx, testAccount.ID)
suite.NoError(err) if err != nil {
suite.FailNow(err.Error())
}
suite.True(*dbAccount.Locked) suite.True(*dbAccount.Locked)
suite.Equal(displayName, dbAccount.DisplayName) suite.Equal(displayName, dbAccount.DisplayName)
suite.Equal(noteExpected, dbAccount.Note) suite.Equal(noteExpected, dbAccount.Note)
} }
func (suite *AccountUpdateTestSuite) TestAccountUpdateWithMarkdownNote() { func (suite *AccountUpdateTestSuite) TestAccountUpdateWithMarkdownNote() {
testAccount := suite.testAccounts["local_account_1"] testAccount := &gtsmodel.Account{}
*testAccount = *suite.testAccounts["local_account_1"]
note := "*hello* ~~here~~ i am!" var (
expectedNote := `<p><em>hello</em> <del>here</del> i am!</p>` ctx = context.Background()
note = "*hello* ~~here~~ i am!"
noteExpected = `<p><em>hello</em> <del>here</del> i am!</p>`
)
form := &apimodel.UpdateCredentialsRequest{ // Set status content type of account 1 to markdown for this test.
Note: &note, testAccount.StatusContentType = "text/markdown"
if err := suite.db.UpdateAccount(ctx, testAccount, "status_content_type"); err != nil {
suite.FailNow(err.Error())
} }
// set default post content type of account 1 to markdown // Call update function.
testAccount.StatusContentType = "text/markdown" apiAccount, errWithCode := suite.accountProcessor.Update(ctx, testAccount, &apimodel.UpdateCredentialsRequest{
Note: &note,
})
if errWithCode != nil {
suite.FailNow(errWithCode.Error())
}
// should get no error from the update function, and an api model account returned // Returned profile should be updated.
apiAccount, errWithCode := suite.accountProcessor.Update(context.Background(), testAccount, form) suite.Equal(noteExpected, apiAccount.Note)
// reset test account to avoid breaking other tests
testAccount.StatusContentType = "text/plain"
suite.NoError(errWithCode)
suite.NotNil(apiAccount)
// fields on the profile should be updated // We should have an update in the client api channel.
suite.Equal(expectedNote, apiAccount.Note) suite.checkClientAPIChan(testAccount.ID)
// we should have an update in the client api channel // Check database model of account as well.
msg := <-suite.fromClientAPIChan dbAccount, err := suite.db.GetAccountByID(ctx, testAccount.ID)
suite.Equal(ap.ActivityUpdate, msg.APActivityType) if err != nil {
suite.Equal(ap.ObjectProfile, msg.APObjectType) suite.FailNow(err.Error())
suite.NotNil(msg.OriginAccount) }
suite.Equal(testAccount.ID, msg.OriginAccount.ID)
suite.Nil(msg.TargetAccount)
// fields should be updated in the database as well
dbAccount, err := suite.db.GetAccountByID(context.Background(), testAccount.ID)
suite.NoError(err) suite.NoError(err)
suite.Equal(expectedNote, dbAccount.Note) suite.Equal(noteExpected, dbAccount.Note)
} }
func (suite *AccountUpdateTestSuite) TestAccountUpdateWithFields() { func (suite *AccountUpdateTestSuite) TestAccountUpdateWithFields() {
testAccount := suite.testAccounts["local_account_1"] testAccount := &gtsmodel.Account{}
*testAccount = *suite.testAccounts["local_account_1"]
updateFields := []apimodel.UpdateField{ var (
{ ctx = context.Background()
Name: func() *string { s := "favourite emoji"; return &s }(), updateFields = []apimodel.UpdateField{
Value: func() *string { s := ":rainbow:"; return &s }(), {
}, Name: func() *string { s := "favourite emoji"; return &s }(),
{ Value: func() *string { s := ":rainbow:"; return &s }(),
Name: func() *string { s := "my website"; return &s }(), },
Value: func() *string { s := "https://example.org"; return &s }(), {
}, Name: func() *string { s := "my website"; return &s }(),
} Value: func() *string { s := "https://example.org"; return &s }(),
},
}
fieldsExpectedRaw = []apimodel.Field{
{
Name: "favourite emoji",
Value: ":rainbow:",
VerifiedAt: (*string)(nil),
},
{
Name: "my website",
Value: "https://example.org",
VerifiedAt: (*string)(nil),
},
}
fieldsExpectedParsed = []apimodel.Field{
{
Name: "favourite emoji",
Value: ":rainbow:",
VerifiedAt: (*string)(nil),
},
{
Name: "my website",
Value: "<a href=\"https://example.org\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">https://example.org</a>",
VerifiedAt: (*string)(nil),
},
}
emojisExpected = []apimodel.Emoji{
{
Shortcode: "rainbow",
URL: "http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/original/01F8MH9H8E4VG3KDYJR9EGPXCQ.png",
StaticURL: "http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/static/01F8MH9H8E4VG3KDYJR9EGPXCQ.png",
VisibleInPicker: true,
Category: "reactions",
},
}
)
form := &apimodel.UpdateCredentialsRequest{ apiAccount, errWithCode := suite.accountProcessor.Update(ctx, testAccount, &apimodel.UpdateCredentialsRequest{
FieldsAttributes: &updateFields, FieldsAttributes: &updateFields,
})
if errWithCode != nil {
suite.FailNow(errWithCode.Error())
} }
// should get no error from the update function, and an api model account returned // Returned profile should be updated.
apiAccount, errWithCode := suite.accountProcessor.Update(context.Background(), testAccount, form) suite.EqualValues(fieldsExpectedRaw, apiAccount.Source.Fields)
suite.EqualValues(fieldsExpectedParsed, apiAccount.Fields)
suite.EqualValues(emojisExpected, apiAccount.Emojis)
// reset test account to avoid breaking other tests // We should have an update in the client api channel.
testAccount.StatusContentType = "text/plain" suite.checkClientAPIChan(testAccount.ID)
suite.NoError(errWithCode)
suite.NotNil(apiAccount)
suite.EqualValues([]apimodel.Field{
{
Name: "favourite emoji",
Value: ":rainbow:",
VerifiedAt: (*string)(nil),
},
{
Name: "my website",
Value: "<a href=\"https://example.org\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">https://example.org</a>",
VerifiedAt: (*string)(nil),
},
}, apiAccount.Fields)
suite.EqualValues([]apimodel.Field{
{
Name: "favourite emoji",
Value: ":rainbow:",
VerifiedAt: (*string)(nil),
},
{
Name: "my website",
Value: "https://example.org",
VerifiedAt: (*string)(nil),
},
}, apiAccount.Source.Fields)
suite.EqualValues([]apimodel.Emoji{
{
Shortcode: "rainbow",
URL: "http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/original/01F8MH9H8E4VG3KDYJR9EGPXCQ.png",
StaticURL: "http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/static/01F8MH9H8E4VG3KDYJR9EGPXCQ.png",
VisibleInPicker: true,
Category: "reactions",
},
}, apiAccount.Emojis)
// we should have an update in the client api channel // Check database model of account as well.
msg := <-suite.fromClientAPIChan dbAccount, err := suite.db.GetAccountByID(ctx, testAccount.ID)
suite.Equal(ap.ActivityUpdate, msg.APActivityType) if err != nil {
suite.Equal(ap.ObjectProfile, msg.APObjectType) suite.FailNow(err.Error())
suite.NotNil(msg.OriginAccount) }
suite.Equal(testAccount.ID, msg.OriginAccount.ID) suite.Equal(fieldsExpectedParsed[0].Name, dbAccount.Fields[0].Name)
suite.Nil(msg.TargetAccount) suite.Equal(fieldsExpectedParsed[0].Value, dbAccount.Fields[0].Value)
suite.Equal(fieldsExpectedParsed[1].Name, dbAccount.Fields[1].Name)
suite.Equal(fieldsExpectedParsed[1].Value, dbAccount.Fields[1].Value)
suite.Equal(fieldsExpectedRaw[0].Name, dbAccount.FieldsRaw[0].Name)
suite.Equal(fieldsExpectedRaw[0].Value, dbAccount.FieldsRaw[0].Value)
suite.Equal(fieldsExpectedRaw[1].Name, dbAccount.FieldsRaw[1].Name)
suite.Equal(fieldsExpectedRaw[1].Value, dbAccount.FieldsRaw[1].Value)
}
// fields should be updated in the database as well func (suite *AccountUpdateTestSuite) TestAccountUpdateNoteNotFields() {
dbAccount, err := suite.db.GetAccountByID(context.Background(), testAccount.ID) // local_account_2 already has some fields set.
suite.NoError(err) // We want to ensure that the fields don't change
suite.Equal("favourite emoji", dbAccount.Fields[0].Name) // even if the account note is updated.
suite.Equal(":rainbow:", dbAccount.Fields[0].Value) testAccount := &gtsmodel.Account{}
suite.Equal("my website", dbAccount.Fields[1].Name) *testAccount = *suite.testAccounts["local_account_2"]
suite.Equal("<a href=\"https://example.org\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">https://example.org</a>", dbAccount.Fields[1].Value)
suite.Equal("favourite emoji", dbAccount.FieldsRaw[0].Name) var (
suite.Equal(":rainbow:", dbAccount.FieldsRaw[0].Value) ctx = context.Background()
suite.Equal("my website", dbAccount.FieldsRaw[1].Name) fieldsRawBefore = len(testAccount.FieldsRaw)
suite.Equal("https://example.org", dbAccount.FieldsRaw[1].Value) fieldsBefore = len(testAccount.Fields)
note = "#hello here i am!"
noteExpected = `<p><a href="http://localhost:8080/tags/hello" class="mention hashtag" rel="tag nofollow noreferrer noopener" target="_blank">#<span>hello</span></a> here i am!</p>`
)
// Call update function.
apiAccount, errWithCode := suite.accountProcessor.Update(ctx, testAccount, &apimodel.UpdateCredentialsRequest{
Note: &note,
})
if errWithCode != nil {
suite.FailNow(errWithCode.Error())
}
// Returned profile should be updated.
suite.True(apiAccount.Locked)
suite.Equal(noteExpected, apiAccount.Note)
suite.Equal(fieldsRawBefore, len(apiAccount.Source.Fields))
suite.Equal(fieldsBefore, len(apiAccount.Fields))
// We should have an update in the client api channel.
suite.checkClientAPIChan(testAccount.ID)
// Check database model of account as well.
dbAccount, err := suite.db.GetAccountByID(ctx, testAccount.ID)
if err != nil {
suite.FailNow(err.Error())
}
suite.True(*dbAccount.Locked)
suite.Equal(noteExpected, dbAccount.Note)
suite.Equal(fieldsRawBefore, len(dbAccount.FieldsRaw))
suite.Equal(fieldsBefore, len(dbAccount.Fields))
} }
func TestAccountUpdateTestSuite(t *testing.T) { func TestAccountUpdateTestSuite(t *testing.T) {

View file

@ -526,6 +526,7 @@ func NewTestAccounts() map[string]*gtsmodel.Account {
SuspendedAt: time.Time{}, SuspendedAt: time.Time{},
HideCollections: FalseBool(), HideCollections: FalseBool(),
SuspensionOrigin: "", SuspensionOrigin: "",
EnableRSS: FalseBool(),
}, },
"remote_account_1": { "remote_account_1": {
ID: "01F8MH5ZK5VRH73AKHQM6Y9VNX", ID: "01F8MH5ZK5VRH73AKHQM6Y9VNX",