[MM-54024] Handle JSON nulls when unmarshalling in api4 (#24656)

* handle JSON nulls when unmarshalling in api4

* catch more cases of unhandled JSON null

* fix lint issues

* remove unnecessary checks

* add bot tests for null values

* fix bot_test lint

* add channel null tests

* add group null tests

* add notify_admin null tests

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Delaney Sylvans
2023-10-05 14:55:59 +01:00
коммит произвёл GitHub
родитель 5b25519890
Коммит d61364a24a
14 изменённых файлов: 130 добавлений и 29 удалений

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

@@ -133,6 +133,23 @@ func TestCreateBot(t *testing.T) {
CheckErrorID(t, err, "api.context.permissions.app_error")
})
t.Run("create bot with null value", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
th.AddPermissionToRole(model.PermissionCreateBot.Id, model.TeamUserRoleId)
th.App.UpdateUserRoles(th.Context, th.BasicUser.Id, model.TeamUserRoleId, false)
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.EnableBotAccountCreation = true
})
var bot *model.Bot
_, resp, err := th.Client.CreateBot(context.Background(), bot)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
})
}
func TestPatchBot(t *testing.T) {
@@ -474,6 +491,34 @@ func TestPatchBot(t *testing.T) {
require.Equal(t, th.BasicUser.Id, patchedBot.OwnerId)
})
t.Run("patch with null bot", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
th.AddPermissionToRole(model.PermissionCreateBot.Id, model.TeamUserRoleId)
th.AddPermissionToRole(model.PermissionManageBots.Id, model.TeamUserRoleId)
th.App.UpdateUserRoles(th.Context, th.BasicUser.Id, model.TeamUserRoleId, false)
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.EnableBotAccountCreation = true
})
createdBot, resp, err := th.Client.CreateBot(context.Background(), &model.Bot{
Username: GenerateTestUsername(),
DisplayName: "a bot",
Description: "bot",
})
require.NoError(t, err)
CheckCreatedStatus(t, resp)
defer th.App.PermanentDeleteBot(createdBot.UserId)
var botPatch *model.BotPatch
_, resp1, err1 := th.Client.PatchBot(context.Background(), createdBot.UserId, botPatch)
require.Error(t, err1)
CheckBadRequestStatus(t, resp1)
})
}
func TestGetBot(t *testing.T) {