[MM-44954] Regenerate default avatar (#22871)

* Regenerate default profile picture if username has changed

- Only actions is profile picture has not been changed
- Adjusts ResetLastPictureUpdate store function to store
-curTime instead of 0
    - This is to support updating the default picture while still
    retaining the ability to discern a default image from a set one.
- Changes SetDefaultProfileImage to leverage UpdateDefaultProfileImage
- Test updates around updating user default profile pictures

* App interface updates

* Only display picture update date if non-negative

- Ensures we don't display negative timestamps (default images)
- Change ported for mono-repo changes

* Remove duplicate test assertion

---------

Co-authored-by: Nathan Geist <ngeist@spiria.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Nathan
2023-05-04 08:14:26 -06:00
коммит произвёл GitHub
родитель 349e5d4573
Коммит 670b0e4c9f
8 изменённых файлов: 105 добавлений и 18 удалений

Просмотреть файл

@@ -85,9 +85,11 @@ func TestCreateOAuthUser(t *testing.T) {
})
}
func TestSetDefaultProfileImage(t *testing.T) {
func TestUpdateDefaultProfileImage(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
startTime := model.GetMillis()
time.Sleep(time.Millisecond)
err := th.App.SetDefaultProfileImage(th.Context, &model.User{
Id: model.NewId(),
@@ -98,11 +100,11 @@ func TestSetDefaultProfileImage(t *testing.T) {
user := th.BasicUser
err = th.App.SetDefaultProfileImage(th.Context, user)
err = th.App.UpdateDefaultProfileImage(th.Context, user)
require.Nil(t, err)
user = getUserFromDB(th.App, user.Id, t)
assert.Equal(t, int64(0), user.LastPictureUpdate)
assert.Less(t, user.LastPictureUpdate, -startTime, "LastPictureUpdate should be set to -(current time in milliseconds)")
}
func TestAdjustProfileImage(t *testing.T) {
@@ -194,6 +196,33 @@ func TestUpdateUser(t *testing.T) {
require.NotNil(t, err)
require.Nil(t, u)
})
t.Run("fails if default profile picture is not updated when user has default profile picture and username is changed", func(t *testing.T) {
user.Username = "updatedUsername"
iLastPictureUpdate := user.LastPictureUpdate
require.Equal(t, iLastPictureUpdate, int64(0))
u, err := th.App.UpdateUser(th.Context, user, false)
require.Nil(t, err)
require.NotNil(t, u)
require.Less(t, u.LastPictureUpdate, iLastPictureUpdate)
})
t.Run("fails if profile picture is updated when user has custom profile picture and username is changed", func(t *testing.T) {
// Give the user a LastPictureUpdate to mimic having a custom profile picture
err := th.App.Srv().Store().User().UpdateLastPictureUpdate(user.Id)
require.NoError(t, err)
iUser, errGetUser := th.App.GetUser(user.Id)
require.Nil(t, errGetUser)
iUser.Username = "updatedUsername"
iLastPictureUpdate := iUser.LastPictureUpdate
require.Greater(t, iLastPictureUpdate, int64(0))
// Attempt the update, ensure the LastPictureUpdate has not changed
updatedUser, errUpdateUser := th.App.UpdateUser(th.Context, iUser, false)
require.Nil(t, errUpdateUser)
require.NotNil(t, updatedUser)
require.Equal(t, updatedUser.LastPictureUpdate, iLastPictureUpdate)
})
}
func TestUpdateUserMissingFields(t *testing.T) {