MM-25083: local mode for patchBot, getBots, enableBot, disableBot and assignBot (#14652)
* local mode for patchBot * local mode for getBots * local mode for enableBot and disableBot * local mode for assignBot
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
deeba8f609
Коммит
f2253df8a1
@@ -299,6 +299,9 @@ func InitLocal(configservice configservice.ConfigService, globalOptionsFunc app.
|
|||||||
|
|
||||||
api.BaseRoutes.License = api.BaseRoutes.ApiRoot.PathPrefix("/license").Subrouter()
|
api.BaseRoutes.License = api.BaseRoutes.ApiRoot.PathPrefix("/license").Subrouter()
|
||||||
|
|
||||||
|
api.BaseRoutes.Bots = api.BaseRoutes.ApiRoot.PathPrefix("/bots").Subrouter()
|
||||||
|
api.BaseRoutes.Bot = api.BaseRoutes.ApiRoot.PathPrefix("/bots/{bot_user_id:[A-Za-z0-9]+}").Subrouter()
|
||||||
|
|
||||||
api.BaseRoutes.Groups = api.BaseRoutes.ApiRoot.PathPrefix("/groups").Subrouter()
|
api.BaseRoutes.Groups = api.BaseRoutes.ApiRoot.PathPrefix("/groups").Subrouter()
|
||||||
|
|
||||||
api.InitUserLocal()
|
api.InitUserLocal()
|
||||||
@@ -308,6 +311,7 @@ func InitLocal(configservice configservice.ConfigService, globalOptionsFunc app.
|
|||||||
api.InitPluginLocal()
|
api.InitPluginLocal()
|
||||||
api.InitCommandLocal()
|
api.InitCommandLocal()
|
||||||
api.InitLicenseLocal()
|
api.InitLicenseLocal()
|
||||||
|
api.InitBotLocal()
|
||||||
api.InitGroupLocal()
|
api.InitGroupLocal()
|
||||||
|
|
||||||
root.Handle("/api/v4/{anything:.*}", http.HandlerFunc(api.Handle404))
|
root.Handle("/api/v4/{anything:.*}", http.HandlerFunc(api.Handle404))
|
||||||
|
|||||||
13
api4/bot_local.go
Обычный файл
13
api4/bot_local.go
Обычный файл
@@ -0,0 +1,13 @@
|
|||||||
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See LICENSE.txt for license information.
|
||||||
|
|
||||||
|
package api4
|
||||||
|
|
||||||
|
func (api *API) InitBotLocal() {
|
||||||
|
api.BaseRoutes.Bot.Handle("", api.ApiLocal(patchBot)).Methods("PUT")
|
||||||
|
api.BaseRoutes.Bot.Handle("/disable", api.ApiLocal(disableBot)).Methods("POST")
|
||||||
|
api.BaseRoutes.Bot.Handle("/enable", api.ApiLocal(enableBot)).Methods("POST")
|
||||||
|
api.BaseRoutes.Bot.Handle("/assign/{user_id:[A-Za-z0-9]+}", api.ApiLocal(assignBot)).Methods("POST")
|
||||||
|
|
||||||
|
api.BaseRoutes.Bots.Handle("", api.ApiLocal(getBots)).Methods("GET")
|
||||||
|
}
|
||||||
297
api4/bot_test.go
297
api4/bot_test.go
@@ -141,8 +141,66 @@ func TestPatchBot(t *testing.T) {
|
|||||||
defer th.TearDown()
|
defer th.TearDown()
|
||||||
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
||||||
|
|
||||||
_, resp := th.SystemAdminClient.PatchBot(model.NewId(), &model.BotPatch{})
|
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
|
||||||
CheckNotFoundStatus(t, resp)
|
_, resp := client.PatchBot(model.NewId(), &model.BotPatch{})
|
||||||
|
CheckNotFoundStatus(t, resp)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("system admin and local client can patch any bot", func(t *testing.T) {
|
||||||
|
th := Setup(t).InitBasic()
|
||||||
|
defer th.TearDown()
|
||||||
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
||||||
|
|
||||||
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
||||||
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
||||||
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||||
|
*cfg.ServiceSettings.EnableBotAccountCreation = true
|
||||||
|
})
|
||||||
|
|
||||||
|
createdBot, resp := th.Client.CreateBot(&model.Bot{
|
||||||
|
Username: GenerateTestUsername(),
|
||||||
|
DisplayName: "a bot",
|
||||||
|
Description: "bot created by a user",
|
||||||
|
})
|
||||||
|
CheckCreatedStatus(t, resp)
|
||||||
|
defer th.App.PermanentDeleteBot(createdBot.UserId)
|
||||||
|
|
||||||
|
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
|
||||||
|
botPatch := &model.BotPatch{
|
||||||
|
Username: sToP(GenerateTestUsername()),
|
||||||
|
DisplayName: sToP("an updated bot"),
|
||||||
|
Description: sToP("updated bot"),
|
||||||
|
}
|
||||||
|
patchedBot, patchResp := client.PatchBot(createdBot.UserId, botPatch)
|
||||||
|
CheckOKStatus(t, patchResp)
|
||||||
|
require.Equal(t, *botPatch.Username, patchedBot.Username)
|
||||||
|
require.Equal(t, *botPatch.DisplayName, patchedBot.DisplayName)
|
||||||
|
require.Equal(t, *botPatch.Description, patchedBot.Description)
|
||||||
|
require.Equal(t, th.BasicUser.Id, patchedBot.OwnerId)
|
||||||
|
}, "bot created by user")
|
||||||
|
|
||||||
|
createdBotSystemAdmin, resp := th.SystemAdminClient.CreateBot(&model.Bot{
|
||||||
|
Username: GenerateTestUsername(),
|
||||||
|
DisplayName: "another bot",
|
||||||
|
Description: "bot created by system admin user",
|
||||||
|
})
|
||||||
|
CheckCreatedStatus(t, resp)
|
||||||
|
defer th.App.PermanentDeleteBot(createdBotSystemAdmin.UserId)
|
||||||
|
|
||||||
|
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
|
||||||
|
botPatch := &model.BotPatch{
|
||||||
|
Username: sToP(GenerateTestUsername()),
|
||||||
|
DisplayName: sToP("an updated bot"),
|
||||||
|
Description: sToP("updated bot"),
|
||||||
|
}
|
||||||
|
patchedBot, patchResp := client.PatchBot(createdBotSystemAdmin.UserId, botPatch)
|
||||||
|
CheckOKStatus(t, patchResp)
|
||||||
|
require.Equal(t, *botPatch.Username, patchedBot.Username)
|
||||||
|
require.Equal(t, *botPatch.DisplayName, patchedBot.DisplayName)
|
||||||
|
require.Equal(t, *botPatch.Description, patchedBot.Description)
|
||||||
|
require.Equal(t, th.SystemAdminUser.Id, patchedBot.OwnerId)
|
||||||
|
}, "bot created by system admin")
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("patch someone else's bot without permission", func(t *testing.T) {
|
t.Run("patch someone else's bot without permission", func(t *testing.T) {
|
||||||
@@ -617,12 +675,15 @@ func TestGetBots(t *testing.T) {
|
|||||||
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
||||||
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
||||||
|
|
||||||
bots, resp := th.Client.GetBots(0, 10, "")
|
expectedBotList := []*model.Bot{bot1, bot2, bot3, orphanedBot}
|
||||||
CheckOKStatus(t, resp)
|
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||||
require.Equal(t, []*model.Bot{bot1, bot2, bot3, orphanedBot}, bots)
|
bots, resp := client.GetBots(0, 10, "")
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
require.Equal(t, expectedBotList, bots)
|
||||||
|
})
|
||||||
|
|
||||||
botList := model.BotList(bots)
|
botList := model.BotList(expectedBotList)
|
||||||
bots, resp = th.Client.GetBots(0, 10, botList.Etag())
|
bots, resp := th.Client.GetBots(0, 10, botList.Etag())
|
||||||
CheckEtag(t, bots, resp)
|
CheckEtag(t, bots, resp)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -633,12 +694,15 @@ func TestGetBots(t *testing.T) {
|
|||||||
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
||||||
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
||||||
|
|
||||||
bots, resp := th.Client.GetBots(0, 1, "")
|
expectedBotList := []*model.Bot{bot1}
|
||||||
CheckOKStatus(t, resp)
|
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||||
require.Equal(t, []*model.Bot{bot1}, bots)
|
bots, resp := client.GetBots(0, 1, "")
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
require.Equal(t, expectedBotList, bots)
|
||||||
|
})
|
||||||
|
|
||||||
botList := model.BotList(bots)
|
botList := model.BotList(expectedBotList)
|
||||||
bots, resp = th.Client.GetBots(0, 1, botList.Etag())
|
bots, resp := th.Client.GetBots(0, 1, botList.Etag())
|
||||||
CheckEtag(t, bots, resp)
|
CheckEtag(t, bots, resp)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -649,12 +713,15 @@ func TestGetBots(t *testing.T) {
|
|||||||
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
||||||
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
||||||
|
|
||||||
bots, resp := th.Client.GetBots(1, 2, "")
|
expectedBotList := []*model.Bot{bot3, orphanedBot}
|
||||||
CheckOKStatus(t, resp)
|
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||||
require.Equal(t, []*model.Bot{bot3, orphanedBot}, bots)
|
bots, resp := client.GetBots(1, 2, "")
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
require.Equal(t, expectedBotList, bots)
|
||||||
|
})
|
||||||
|
|
||||||
botList := model.BotList(bots)
|
botList := model.BotList(expectedBotList)
|
||||||
bots, resp = th.Client.GetBots(1, 2, botList.Etag())
|
bots, resp := th.Client.GetBots(1, 2, botList.Etag())
|
||||||
CheckEtag(t, bots, resp)
|
CheckEtag(t, bots, resp)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -665,12 +732,15 @@ func TestGetBots(t *testing.T) {
|
|||||||
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
||||||
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
||||||
|
|
||||||
bots, resp := th.Client.GetBots(2, 2, "")
|
expectedBotList := []*model.Bot{}
|
||||||
CheckOKStatus(t, resp)
|
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||||
require.Equal(t, []*model.Bot{}, bots)
|
bots, resp := client.GetBots(2, 2, "")
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
require.Equal(t, expectedBotList, bots)
|
||||||
|
})
|
||||||
|
|
||||||
botList := model.BotList(bots)
|
botList := model.BotList(expectedBotList)
|
||||||
bots, resp = th.Client.GetBots(2, 2, botList.Etag())
|
bots, resp := th.Client.GetBots(2, 2, botList.Etag())
|
||||||
CheckEtag(t, bots, resp)
|
CheckEtag(t, bots, resp)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -681,12 +751,15 @@ func TestGetBots(t *testing.T) {
|
|||||||
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
||||||
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
||||||
|
|
||||||
bots, resp := th.Client.GetBotsIncludeDeleted(0, 10, "")
|
expectedBotList := []*model.Bot{bot1, deletedBot1, bot2, bot3, deletedBot2, orphanedBot}
|
||||||
CheckOKStatus(t, resp)
|
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||||
require.Equal(t, []*model.Bot{bot1, deletedBot1, bot2, bot3, deletedBot2, orphanedBot}, bots)
|
bots, resp := client.GetBotsIncludeDeleted(0, 10, "")
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
require.Equal(t, expectedBotList, bots)
|
||||||
|
})
|
||||||
|
|
||||||
botList := model.BotList(bots)
|
botList := model.BotList(expectedBotList)
|
||||||
bots, resp = th.Client.GetBotsIncludeDeleted(0, 10, botList.Etag())
|
bots, resp := th.Client.GetBotsIncludeDeleted(0, 10, botList.Etag())
|
||||||
CheckEtag(t, bots, resp)
|
CheckEtag(t, bots, resp)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -697,12 +770,15 @@ func TestGetBots(t *testing.T) {
|
|||||||
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
||||||
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
||||||
|
|
||||||
bots, resp := th.Client.GetBotsIncludeDeleted(0, 1, "")
|
expectedBotList := []*model.Bot{bot1}
|
||||||
CheckOKStatus(t, resp)
|
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||||
require.Equal(t, []*model.Bot{bot1}, bots)
|
bots, resp := client.GetBotsIncludeDeleted(0, 1, "")
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
require.Equal(t, expectedBotList, bots)
|
||||||
|
})
|
||||||
|
|
||||||
botList := model.BotList(bots)
|
botList := model.BotList(expectedBotList)
|
||||||
bots, resp = th.Client.GetBotsIncludeDeleted(0, 1, botList.Etag())
|
bots, resp := th.Client.GetBotsIncludeDeleted(0, 1, botList.Etag())
|
||||||
CheckEtag(t, bots, resp)
|
CheckEtag(t, bots, resp)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -713,12 +789,15 @@ func TestGetBots(t *testing.T) {
|
|||||||
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
||||||
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
||||||
|
|
||||||
bots, resp := th.Client.GetBotsIncludeDeleted(1, 2, "")
|
expectedBotList := []*model.Bot{bot2, bot3}
|
||||||
CheckOKStatus(t, resp)
|
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||||
require.Equal(t, []*model.Bot{bot2, bot3}, bots)
|
bots, resp := client.GetBotsIncludeDeleted(1, 2, "")
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
require.Equal(t, expectedBotList, bots)
|
||||||
|
})
|
||||||
|
|
||||||
botList := model.BotList(bots)
|
botList := model.BotList(expectedBotList)
|
||||||
bots, resp = th.Client.GetBotsIncludeDeleted(1, 2, botList.Etag())
|
bots, resp := th.Client.GetBotsIncludeDeleted(1, 2, botList.Etag())
|
||||||
CheckEtag(t, bots, resp)
|
CheckEtag(t, bots, resp)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -729,12 +808,15 @@ func TestGetBots(t *testing.T) {
|
|||||||
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
||||||
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
||||||
|
|
||||||
bots, resp := th.Client.GetBotsIncludeDeleted(2, 2, "")
|
expectedBotList := []*model.Bot{deletedBot2, orphanedBot}
|
||||||
CheckOKStatus(t, resp)
|
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||||
require.Equal(t, []*model.Bot{deletedBot2, orphanedBot}, bots)
|
bots, resp := client.GetBotsIncludeDeleted(2, 2, "")
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
require.Equal(t, expectedBotList, bots)
|
||||||
|
})
|
||||||
|
|
||||||
botList := model.BotList(bots)
|
botList := model.BotList(expectedBotList)
|
||||||
bots, resp = th.Client.GetBotsIncludeDeleted(2, 2, botList.Etag())
|
bots, resp := th.Client.GetBotsIncludeDeleted(2, 2, botList.Etag())
|
||||||
CheckEtag(t, bots, resp)
|
CheckEtag(t, bots, resp)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -745,12 +827,15 @@ func TestGetBots(t *testing.T) {
|
|||||||
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
||||||
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
||||||
|
|
||||||
bots, resp := th.Client.GetBotsOrphaned(0, 10, "")
|
expectedBotList := []*model.Bot{orphanedBot}
|
||||||
CheckOKStatus(t, resp)
|
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||||
require.Equal(t, []*model.Bot{orphanedBot}, bots)
|
bots, resp := client.GetBotsOrphaned(0, 10, "")
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
require.Equal(t, expectedBotList, bots)
|
||||||
|
})
|
||||||
|
|
||||||
botList := model.BotList(bots)
|
botList := model.BotList(expectedBotList)
|
||||||
bots, resp = th.Client.GetBotsOrphaned(0, 10, botList.Etag())
|
bots, resp := th.Client.GetBotsOrphaned(0, 10, botList.Etag())
|
||||||
CheckEtag(t, bots, resp)
|
CheckEtag(t, bots, resp)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -772,8 +857,10 @@ func TestDisableBot(t *testing.T) {
|
|||||||
th := Setup(t).InitBasic()
|
th := Setup(t).InitBasic()
|
||||||
defer th.TearDown()
|
defer th.TearDown()
|
||||||
|
|
||||||
_, resp := th.Client.DisableBot(model.NewId())
|
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||||
CheckNotFoundStatus(t, resp)
|
_, resp := client.DisableBot(model.NewId())
|
||||||
|
CheckNotFoundStatus(t, resp)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("disable bot without permission", func(t *testing.T) {
|
t.Run("disable bot without permission", func(t *testing.T) {
|
||||||
@@ -837,28 +924,30 @@ func TestDisableBot(t *testing.T) {
|
|||||||
*cfg.ServiceSettings.EnableBotAccountCreation = true
|
*cfg.ServiceSettings.EnableBotAccountCreation = true
|
||||||
})
|
})
|
||||||
|
|
||||||
bot, resp := th.Client.CreateBot(&model.Bot{
|
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||||
Username: GenerateTestUsername(),
|
bot, resp := th.Client.CreateBot(&model.Bot{
|
||||||
Description: "bot",
|
Username: GenerateTestUsername(),
|
||||||
|
Description: "bot",
|
||||||
|
})
|
||||||
|
CheckCreatedStatus(t, resp)
|
||||||
|
defer th.App.PermanentDeleteBot(bot.UserId)
|
||||||
|
|
||||||
|
disabledBot, resp := client.DisableBot(bot.UserId)
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
bot.UpdateAt = disabledBot.UpdateAt
|
||||||
|
bot.DeleteAt = disabledBot.DeleteAt
|
||||||
|
require.Equal(t, bot, disabledBot)
|
||||||
|
|
||||||
|
// Check bot disabled
|
||||||
|
disab, resp := th.SystemAdminClient.GetBotIncludeDeleted(bot.UserId, "")
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
require.NotZero(t, disab.DeleteAt)
|
||||||
|
|
||||||
|
// Disabling should be idempotent.
|
||||||
|
disabledBot2, resp := client.DisableBot(bot.UserId)
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
require.Equal(t, bot, disabledBot2)
|
||||||
})
|
})
|
||||||
CheckCreatedStatus(t, resp)
|
|
||||||
defer th.App.PermanentDeleteBot(bot.UserId)
|
|
||||||
|
|
||||||
enabledBot1, resp := th.Client.DisableBot(bot.UserId)
|
|
||||||
CheckOKStatus(t, resp)
|
|
||||||
bot.UpdateAt = enabledBot1.UpdateAt
|
|
||||||
bot.DeleteAt = enabledBot1.DeleteAt
|
|
||||||
require.Equal(t, bot, enabledBot1)
|
|
||||||
|
|
||||||
// Check bot disabled
|
|
||||||
disab, resp := th.SystemAdminClient.GetBotIncludeDeleted(bot.UserId, "")
|
|
||||||
CheckOKStatus(t, resp)
|
|
||||||
require.NotZero(t, disab.DeleteAt)
|
|
||||||
|
|
||||||
// Disabling should be idempotent.
|
|
||||||
enabledBot2, resp := th.Client.DisableBot(bot.UserId)
|
|
||||||
CheckOKStatus(t, resp)
|
|
||||||
require.Equal(t, bot, enabledBot2)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
func TestEnableBot(t *testing.T) {
|
func TestEnableBot(t *testing.T) {
|
||||||
@@ -866,8 +955,10 @@ func TestEnableBot(t *testing.T) {
|
|||||||
th := Setup(t).InitBasic()
|
th := Setup(t).InitBasic()
|
||||||
defer th.TearDown()
|
defer th.TearDown()
|
||||||
|
|
||||||
_, resp := th.Client.EnableBot(model.NewId())
|
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||||
CheckNotFoundStatus(t, resp)
|
_, resp := th.Client.EnableBot(model.NewId())
|
||||||
|
CheckNotFoundStatus(t, resp)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("enable bot without permission", func(t *testing.T) {
|
t.Run("enable bot without permission", func(t *testing.T) {
|
||||||
@@ -937,31 +1028,33 @@ func TestEnableBot(t *testing.T) {
|
|||||||
*cfg.ServiceSettings.EnableBotAccountCreation = true
|
*cfg.ServiceSettings.EnableBotAccountCreation = true
|
||||||
})
|
})
|
||||||
|
|
||||||
bot, resp := th.Client.CreateBot(&model.Bot{
|
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||||
Username: GenerateTestUsername(),
|
bot, resp := th.Client.CreateBot(&model.Bot{
|
||||||
Description: "bot",
|
Username: GenerateTestUsername(),
|
||||||
|
Description: "bot",
|
||||||
|
})
|
||||||
|
CheckCreatedStatus(t, resp)
|
||||||
|
defer th.App.PermanentDeleteBot(bot.UserId)
|
||||||
|
|
||||||
|
_, resp = th.SystemAdminClient.DisableBot(bot.UserId)
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
|
||||||
|
enabledBot1, resp := client.EnableBot(bot.UserId)
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
bot.UpdateAt = enabledBot1.UpdateAt
|
||||||
|
bot.DeleteAt = enabledBot1.DeleteAt
|
||||||
|
require.Equal(t, bot, enabledBot1)
|
||||||
|
|
||||||
|
// Check bot enabled
|
||||||
|
enab, resp := th.SystemAdminClient.GetBotIncludeDeleted(bot.UserId, "")
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
require.Zero(t, enab.DeleteAt)
|
||||||
|
|
||||||
|
// Disabling should be idempotent.
|
||||||
|
enabledBot2, resp := client.EnableBot(bot.UserId)
|
||||||
|
CheckOKStatus(t, resp)
|
||||||
|
require.Equal(t, bot, enabledBot2)
|
||||||
})
|
})
|
||||||
CheckCreatedStatus(t, resp)
|
|
||||||
defer th.App.PermanentDeleteBot(bot.UserId)
|
|
||||||
|
|
||||||
_, resp = th.SystemAdminClient.DisableBot(bot.UserId)
|
|
||||||
CheckOKStatus(t, resp)
|
|
||||||
|
|
||||||
enabledBot1, resp := th.Client.EnableBot(bot.UserId)
|
|
||||||
CheckOKStatus(t, resp)
|
|
||||||
bot.UpdateAt = enabledBot1.UpdateAt
|
|
||||||
bot.DeleteAt = enabledBot1.DeleteAt
|
|
||||||
require.Equal(t, bot, enabledBot1)
|
|
||||||
|
|
||||||
// Check bot enabled
|
|
||||||
enab, resp := th.SystemAdminClient.GetBotIncludeDeleted(bot.UserId, "")
|
|
||||||
CheckOKStatus(t, resp)
|
|
||||||
require.Zero(t, enab.DeleteAt)
|
|
||||||
|
|
||||||
// Disabling should be idempotent.
|
|
||||||
enabledBot2, resp := th.Client.EnableBot(bot.UserId)
|
|
||||||
CheckOKStatus(t, resp)
|
|
||||||
require.Equal(t, bot, enabledBot2)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -970,11 +1063,13 @@ func TestAssignBot(t *testing.T) {
|
|||||||
defer th.TearDown()
|
defer th.TearDown()
|
||||||
|
|
||||||
t.Run("claim non-existent bot", func(t *testing.T) {
|
t.Run("claim non-existent bot", func(t *testing.T) {
|
||||||
_, resp := th.SystemAdminClient.AssignBot(model.NewId(), model.NewId())
|
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||||
CheckNotFoundStatus(t, resp)
|
_, resp := client.AssignBot(model.NewId(), model.NewId())
|
||||||
|
CheckNotFoundStatus(t, resp)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("system admin assign bot", func(t *testing.T) {
|
t.Run("system admin and local mode assign bot", func(t *testing.T) {
|
||||||
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
||||||
|
|
||||||
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.SYSTEM_USER_ROLE_ID)
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.SYSTEM_USER_ROLE_ID)
|
||||||
@@ -1007,8 +1102,8 @@ func TestAssignBot(t *testing.T) {
|
|||||||
CheckOKStatus(t, resp)
|
CheckOKStatus(t, resp)
|
||||||
require.Equal(t, th.SystemAdminUser.Id, after.OwnerId)
|
require.Equal(t, th.SystemAdminUser.Id, after.OwnerId)
|
||||||
|
|
||||||
// Assign back to user without permissions to manage
|
// Assign back to user without permissions to manage, using local mode
|
||||||
_, resp = th.SystemAdminClient.AssignBot(bot.UserId, th.BasicUser.Id)
|
_, resp = th.LocalClient.AssignBot(bot.UserId, th.BasicUser.Id)
|
||||||
CheckOKStatus(t, resp)
|
CheckOKStatus(t, resp)
|
||||||
|
|
||||||
after, resp = th.SystemAdminClient.GetBot(bot.UserId, "")
|
after, resp = th.SystemAdminClient.GetBot(bot.UserId, "")
|
||||||
|
|||||||
@@ -232,6 +232,9 @@ func (a *App) SessionHasPermissionToManageBot(session model.Session, botUserId s
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if session.IsUnrestricted() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if existingBot.OwnerId == session.UserId {
|
if existingBot.OwnerId == session.UserId {
|
||||||
if !a.SessionHasPermissionTo(session, model.PERMISSION_MANAGE_BOTS) {
|
if !a.SessionHasPermissionTo(session, model.PERMISSION_MANAGE_BOTS) {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user