MM 55199 Limit User Preferences (#25579)

* limit size and number of preferences in updatePreferences

* update unit tests

* fix another location and add tests

* update utility function to return an error

* update unit tests

* fix review comments

* revert package-lock.json

* more review mods

* update number of preferences allowed

* update unit tests for new limit

* update name of config setting

* Update server/public/model/utils.go

Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com>

* add check for error id

* use existing utility function, remove model.ObjectFromJSON()

* Update preference.go - remove blank line

---------

Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Scott Bishel
2024-02-01 15:33:24 -07:00
коммит произвёл GitHub
родитель a57be19da4
Коммит a38c1c54b4
3 изменённых файлов: 90 добавлений и 4 удалений

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

@@ -257,6 +257,42 @@ func TestUpdatePreferences(t *testing.T) {
CheckUnauthorizedStatus(t, resp)
}
func TestUpdatePreferencesOverload(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
client := th.Client
th.LoginBasic()
user1 := th.BasicUser
t.Run("No preferences", func(t *testing.T) {
preferences1 := model.Preferences{}
// should error if no preferences
resp, err := client.UpdatePreferences(context.Background(), user1.Id, preferences1)
require.Error(t, err)
CheckErrorID(t, err, "api.context.invalid_body_param.app_error")
CheckBadRequestStatus(t, resp)
})
t.Run("Too many preferences", func(t *testing.T) {
preferences1 := model.Preferences{}
category := model.NewId()
// should error if too many preferences
for i := 0; i <= 100; i++ {
preferences1 = append(preferences1, model.Preference{
UserId: user1.Id,
Category: category,
Name: model.NewId(),
Value: model.NewId(),
})
}
resp, err := client.UpdatePreferences(context.Background(), user1.Id, preferences1)
require.Error(t, err)
CheckErrorID(t, err, "api.context.invalid_body_param.app_error")
CheckBadRequestStatus(t, resp)
})
}
func TestUpdatePreferencesWebsocket(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
@@ -589,6 +625,42 @@ func TestDeletePreferences(t *testing.T) {
CheckUnauthorizedStatus(t, resp)
}
func TestDeletePreferencesOverload(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
client := th.Client
th.LoginBasic()
user1 := th.BasicUser
t.Run("No preferences", func(t *testing.T) {
preferences1 := model.Preferences{}
// should error if no preferences
resp, err := client.DeletePreferences(context.Background(), user1.Id, preferences1)
require.Error(t, err)
CheckErrorID(t, err, "api.context.invalid_body_param.app_error")
CheckBadRequestStatus(t, resp)
})
t.Run("Too many preferences", func(t *testing.T) {
category := model.NewId()
preferences1 := model.Preferences{}
// should error if too many preferences
for i := 0; i <= 100; i++ {
preferences1 = append(preferences1, model.Preference{
UserId: user1.Id,
Category: category,
Name: model.NewId(),
Value: model.NewId(),
})
}
resp, err := client.DeletePreferences(context.Background(), user1.Id, preferences1)
require.Error(t, err)
CheckErrorID(t, err, "api.context.invalid_body_param.app_error")
CheckBadRequestStatus(t, resp)
})
}
func TestDeletePreferencesWebsocket(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()