mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* bots model, store and api (#9903)
* bots model, store and api
Fixes: MM-13100, MM-13101, MM-13103, MM-13105, MMM-13119
* uncomment tests incorrectly commented, and fix merge issues
* add etags support
* add missing licenses
* remove unused sqlbuilder.go (for now...)
* rejig permissions
* split out READ_BOTS into READ_BOTS and READ_OTHERS_BOTS, the latter
implicitly allowing the former
* make MANAGE_OTHERS_BOTS imply MANAGE_BOTS
* conform to general rest api pattern
* eliminate redundant http.StatusOK
* Update api4/bot.go
Co-Authored-By: lieut-data <jesse.hallam@gmail.com>
* s/model.UserFromBotModel/model.UserFromBot/g
* Update model/bot.go
Co-Authored-By: lieut-data <jesse.hallam@gmail.com>
* Update model/client4.go
Co-Authored-By: lieut-data <jesse.hallam@gmail.com>
* move sessionHasPermissionToManageBot to app/authorization.go
* use api.ApiSessionRequired for createBot
* introduce BOT_DESCRIPTION_MAX_RUNES constant
* MM-13512 Prevent getting a user by email based on privacy settings (#10021)
* MM-13512 Prevent getting a user by email based on privacy settings
* Add additional config settings to tests
* upgrade db to 5.7 (#10019)
* MM-13526 Add validation when setting a user's Locale field (#10022)
* Fix typos (#10024)
* Fixing first user being created with system admin privilages without being explicity specified. (#10014)
* Revert "Support for Embeded chat (#9129)" (#10017)
This reverts commit 3fcecd521a.
* s/DisableBot/UpdateBotActive
* add permissions on upgrade
* Update NOTICE.txt (#10054)
- add new dependency (text)
- handle switch to forked dependency (go-gomail -> go-mail)
- misc copyright owner updates
* avoid leaking bot knowledge without permission
* [GH-6798] added a new api endpoint to get the bulk reactions for posts (#10049)
* 6798 added a new api to get the bulk reactions for posts
* 6798 added the permsission check before getting the reactions
* GH-6798 added a new app function for the new endpoint
* 6798 added a store method to get reactions for multiple posts
* 6798 connected the app function with the new store function
* 6798 fixed the review comments
* MM-13559 Update model.post.is_valid.file_ids.app_error text per report (#10055)
Ticket: https://mattermost.atlassian.net/browse/MM-13559
Report: https://github.com/mattermost/mattermost-server/issues/10023
* Trigger Login Hooks with OAuth (#10061)
* make BotStore.GetAll deterministic even on duplicate CreateAt
* fix spurious TestMuteCommandSpecificChannel test failure
See
https://community-daily.mattermost.com/core/pl/px9p8s3dzbg1pf3ddrm5cr36uw
* fix race in TestExportUserChannels
* TestExportUserChannels: remove SaveMember call, as it is redundant and used to be silently failing anyway
* MM-13117: bot tokens (#10111)
* eliminate redundant Client/AdminClient declarations
* harden TestUpdateChannelScheme to API failures
* eliminate unnecessary config restoration
* minor cleanup
* make TestGenerateMfaSecret config dependency explicit
* TestCreateUserAccessToken for bots
* TestGetUserAccessToken* for bots
* leverage SessionHasPermissionToUserOrBot for user token APIs
* Test(Revoke|Disable|Enable)UserAccessToken
* make EnableUserAccessTokens explicit, so as to not rely on local config.json
* uncomment TestResetPassword, but still skip
* mark assert(Invalid)Token as helper
* fix whitespace issues
* fix mangled comments
* MM-13116: bot plugin api (#10113)
* MM-13117: expose bot API to plugins
This also changes the `CreatorId` column definition to allow for plugin
ids, as the default unless the plugin overrides is to use the plugin id
here. This branch hasn't hit master yet, so no migration needed.
* gofmt issues
* expunge use of BotList in plugin/client API
* introduce model.BotGetOptions
* use botUserId term for clarity
* MM-13129 Adding functionality to deal with orphaned bots (#10238)
* Add way to list orphaned bots.
* Add /assign route to modify ownership of bot accounts.
* Apply suggestions from code review
Co-Authored-By: crspeller <crspeller@gmail.com>
* MM-13120: add IsBot field to returned user objects (#10103)
* MM-13104: forbid bot login (#10251)
* MM-13104: disallow bot login
* fix shadowing
* MM-13136 Disable user bots when user is disabled. (#10293)
* Disable user bots when user is disabled.
* Grammer.
Co-Authored-By: crspeller <crspeller@gmail.com>
* Fixing bot branch for test changes.
* Don't use external dependancies in bot plugin tests.
* Rename bot CreatorId to OwnerId
* Adding ability to re-enable bots
* Fixing IsBot to not attempt to be saved to DB.
* Adding diagnostics and licencing counting for bot accounts.
* Modifying gorp to allow reading of '-' fields.
* Removing unnessisary nil values from UserCountOptions.
* Changing comment to GoDoc format
* Improving user count SQL
* Some improvments from feedback.
* Omit empty on User.IsBot
939 lines
33 KiB
Go
939 lines
33 KiB
Go
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package api4
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestCreateBot(t *testing.T) {
|
|
t.Run("create bot without permissions", func(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
|
|
_, resp := th.Client.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
DisplayName: "a bot",
|
|
Description: "bot",
|
|
})
|
|
|
|
CheckErrorMessage(t, resp, "api.context.permissions.app_error")
|
|
})
|
|
|
|
t.Run("create bot with permissions", func(t *testing.T) {
|
|
th := Setup().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)
|
|
|
|
bot := &model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
DisplayName: "a bot",
|
|
Description: "bot",
|
|
}
|
|
|
|
createdBot, resp := th.Client.CreateBot(bot)
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(createdBot.UserId)
|
|
require.Equal(t, bot.Username, createdBot.Username)
|
|
require.Equal(t, bot.DisplayName, createdBot.DisplayName)
|
|
require.Equal(t, bot.Description, createdBot.Description)
|
|
require.Equal(t, th.BasicUser.Id, createdBot.OwnerId)
|
|
})
|
|
|
|
t.Run("create invalid bot", func(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
|
|
|
_, resp := th.Client.CreateBot(&model.Bot{
|
|
Username: "username",
|
|
DisplayName: "a bot",
|
|
Description: strings.Repeat("x", 1025),
|
|
})
|
|
|
|
CheckErrorMessage(t, resp, "model.bot.is_valid.description.app_error")
|
|
})
|
|
}
|
|
|
|
func TestPatchBot(t *testing.T) {
|
|
t.Run("patch non-existent bot", func(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
_, resp := th.SystemAdminClient.PatchBot(model.NewId(), &model.BotPatch{})
|
|
CheckNotFoundStatus(t, resp)
|
|
})
|
|
|
|
t.Run("patch someone else's bot without permission", func(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
createdBot, resp := th.SystemAdminClient.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
DisplayName: "a bot",
|
|
Description: "bot",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(createdBot.UserId)
|
|
|
|
_, resp = th.Client.PatchBot(createdBot.UserId, &model.BotPatch{})
|
|
CheckErrorMessage(t, resp, "store.sql_bot.get.missing.app_error")
|
|
})
|
|
|
|
t.Run("patch someone else's bot without permission, but with read others permission", func(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
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)
|
|
|
|
createdBot, resp := th.SystemAdminClient.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
DisplayName: "a bot",
|
|
Description: "bot",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(createdBot.UserId)
|
|
|
|
_, resp = th.Client.PatchBot(createdBot.UserId, &model.BotPatch{})
|
|
CheckErrorMessage(t, resp, "api.context.permissions.app_error")
|
|
})
|
|
|
|
t.Run("patch someone else's bot with permission", func(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_MANAGE_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
|
|
|
createdBot, resp := th.SystemAdminClient.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
DisplayName: "a bot",
|
|
Description: "bot",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(createdBot.UserId)
|
|
|
|
botPatch := &model.BotPatch{
|
|
Username: sToP(GenerateTestUsername()),
|
|
DisplayName: sToP("an updated bot"),
|
|
Description: sToP("updated bot"),
|
|
}
|
|
|
|
patchedBot, resp := th.Client.PatchBot(createdBot.UserId, botPatch)
|
|
CheckOKStatus(t, resp)
|
|
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)
|
|
})
|
|
|
|
t.Run("patch my bot without permission", func(t *testing.T) {
|
|
th := Setup().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)
|
|
|
|
createdBot, resp := th.Client.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
DisplayName: "a bot",
|
|
Description: "bot",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(createdBot.UserId)
|
|
|
|
botPatch := &model.BotPatch{
|
|
Username: sToP(GenerateTestUsername()),
|
|
DisplayName: sToP("an updated bot"),
|
|
Description: sToP("updated bot"),
|
|
}
|
|
|
|
_, resp = th.Client.PatchBot(createdBot.UserId, botPatch)
|
|
CheckErrorMessage(t, resp, "store.sql_bot.get.missing.app_error")
|
|
})
|
|
|
|
t.Run("patch my bot without permission, but with read permission", func(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_READ_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
|
|
|
createdBot, resp := th.Client.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
DisplayName: "a bot",
|
|
Description: "bot",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(createdBot.UserId)
|
|
|
|
botPatch := &model.BotPatch{
|
|
Username: sToP(GenerateTestUsername()),
|
|
DisplayName: sToP("an updated bot"),
|
|
Description: sToP("updated bot"),
|
|
}
|
|
|
|
_, resp = th.Client.PatchBot(createdBot.UserId, botPatch)
|
|
CheckErrorMessage(t, resp, "api.context.permissions.app_error")
|
|
})
|
|
|
|
t.Run("patch my bot with permission", func(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_MANAGE_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
|
|
|
createdBot, resp := th.Client.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
DisplayName: "a bot",
|
|
Description: "bot",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(createdBot.UserId)
|
|
|
|
botPatch := &model.BotPatch{
|
|
Username: sToP(GenerateTestUsername()),
|
|
DisplayName: sToP("an updated bot"),
|
|
Description: sToP("updated bot"),
|
|
}
|
|
|
|
patchedBot, resp := th.Client.PatchBot(createdBot.UserId, botPatch)
|
|
CheckOKStatus(t, resp)
|
|
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)
|
|
})
|
|
|
|
t.Run("partial patch my bot with permission", func(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_MANAGE_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
|
|
|
bot := &model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
DisplayName: "a bot",
|
|
Description: "bot",
|
|
}
|
|
|
|
createdBot, resp := th.Client.CreateBot(bot)
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(createdBot.UserId)
|
|
|
|
botPatch := &model.BotPatch{
|
|
Username: sToP(GenerateTestUsername()),
|
|
}
|
|
|
|
patchedBot, resp := th.Client.PatchBot(createdBot.UserId, botPatch)
|
|
CheckOKStatus(t, resp)
|
|
require.Equal(t, *botPatch.Username, patchedBot.Username)
|
|
require.Equal(t, bot.DisplayName, patchedBot.DisplayName)
|
|
require.Equal(t, bot.Description, patchedBot.Description)
|
|
require.Equal(t, th.BasicUser.Id, patchedBot.OwnerId)
|
|
})
|
|
|
|
t.Run("update bot, internally managed fields ignored", func(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_MANAGE_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
|
|
|
createdBot, resp := th.Client.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
DisplayName: "a bot",
|
|
Description: "bot",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(createdBot.UserId)
|
|
|
|
r, err := th.Client.DoApiPut(th.Client.GetBotRoute(createdBot.UserId), `{"creator_id":"`+th.BasicUser2.Id+`"}`)
|
|
require.Nil(t, err)
|
|
defer func() {
|
|
_, _ = ioutil.ReadAll(r.Body)
|
|
_ = r.Body.Close()
|
|
}()
|
|
patchedBot := model.BotFromJson(r.Body)
|
|
resp = model.BuildResponse(r)
|
|
CheckOKStatus(t, resp)
|
|
|
|
require.Equal(t, th.BasicUser.Id, patchedBot.OwnerId)
|
|
})
|
|
}
|
|
|
|
func TestGetBot(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
|
|
bot1, resp := th.SystemAdminClient.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
DisplayName: "a bot",
|
|
Description: "the first bot",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(bot1.UserId)
|
|
|
|
bot2, resp := th.SystemAdminClient.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
DisplayName: "another bot",
|
|
Description: "the second bot",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(bot2.UserId)
|
|
|
|
deletedBot, resp := th.SystemAdminClient.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
Description: "a deleted bot",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(deletedBot.UserId)
|
|
deletedBot, resp = th.SystemAdminClient.DisableBot(deletedBot.UserId)
|
|
CheckOKStatus(t, resp)
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
|
myBot, resp := th.Client.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
DisplayName: "my bot",
|
|
Description: "a bot created by non-admin",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(myBot.UserId)
|
|
th.RemovePermissionFromRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
|
|
|
t.Run("get unknown bot", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_READ_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)
|
|
|
|
_, resp := th.Client.GetBot(model.NewId(), "")
|
|
CheckNotFoundStatus(t, resp)
|
|
})
|
|
|
|
t.Run("get bot1", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_READ_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)
|
|
|
|
bot, resp := th.Client.GetBot(bot1.UserId, "")
|
|
CheckOKStatus(t, resp)
|
|
require.Equal(t, bot1, bot)
|
|
|
|
bot, resp = th.Client.GetBot(bot1.UserId, bot.Etag())
|
|
CheckEtag(t, bot, resp)
|
|
})
|
|
|
|
t.Run("get bot2", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_READ_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)
|
|
|
|
bot, resp := th.Client.GetBot(bot2.UserId, "")
|
|
CheckOKStatus(t, resp)
|
|
require.Equal(t, bot2, bot)
|
|
|
|
bot, resp = th.Client.GetBot(bot2.UserId, bot.Etag())
|
|
CheckEtag(t, bot, resp)
|
|
})
|
|
|
|
t.Run("get bot1 without READ_OTHERS_BOTS permission", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_READ_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_MANAGE_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_MANAGE_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
|
|
|
_, resp := th.Client.GetBot(bot1.UserId, "")
|
|
CheckErrorMessage(t, resp, "store.sql_bot.get.missing.app_error")
|
|
})
|
|
|
|
t.Run("get myBot without READ_BOTS OR READ_OTHERS_BOTS permissions", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_MANAGE_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_MANAGE_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
|
|
|
_, resp := th.Client.GetBot(myBot.UserId, "")
|
|
CheckErrorMessage(t, resp, "store.sql_bot.get.missing.app_error")
|
|
})
|
|
|
|
t.Run("get deleted bot", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_READ_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)
|
|
|
|
_, resp := th.Client.GetBot(deletedBot.UserId, "")
|
|
CheckNotFoundStatus(t, resp)
|
|
})
|
|
|
|
t.Run("get deleted bot, include deleted", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_READ_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)
|
|
|
|
bot, resp := th.Client.GetBotIncludeDeleted(deletedBot.UserId, "")
|
|
CheckOKStatus(t, resp)
|
|
require.NotEqual(t, 0, bot.DeleteAt)
|
|
deletedBot.UpdateAt = bot.UpdateAt
|
|
deletedBot.DeleteAt = bot.DeleteAt
|
|
require.Equal(t, deletedBot, bot)
|
|
|
|
bot, resp = th.Client.GetBotIncludeDeleted(deletedBot.UserId, bot.Etag())
|
|
CheckEtag(t, bot, resp)
|
|
})
|
|
}
|
|
|
|
func TestGetBots(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
|
|
bot1, resp := th.SystemAdminClient.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
DisplayName: "a bot",
|
|
Description: "the first bot",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(bot1.UserId)
|
|
|
|
deletedBot1, resp := th.SystemAdminClient.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
Description: "a deleted bot",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(deletedBot1.UserId)
|
|
deletedBot1, resp = th.SystemAdminClient.DisableBot(deletedBot1.UserId)
|
|
CheckOKStatus(t, resp)
|
|
|
|
bot2, resp := th.SystemAdminClient.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
DisplayName: "another bot",
|
|
Description: "the second bot",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(bot2.UserId)
|
|
|
|
bot3, resp := th.SystemAdminClient.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
DisplayName: "another bot",
|
|
Description: "the third bot",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(bot3.UserId)
|
|
|
|
deletedBot2, resp := th.SystemAdminClient.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
Description: "a deleted bot",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(deletedBot2.UserId)
|
|
deletedBot2, resp = th.SystemAdminClient.DisableBot(deletedBot2.UserId)
|
|
CheckOKStatus(t, resp)
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
|
th.App.UpdateUserRoles(th.BasicUser2.Id, model.TEAM_USER_ROLE_ID, false)
|
|
th.LoginBasic2()
|
|
orphanedBot, resp := th.Client.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
Description: "an oprphaned bot",
|
|
})
|
|
CheckCreatedStatus(t, resp)
|
|
th.LoginBasic()
|
|
defer th.App.PermanentDeleteBot(orphanedBot.UserId)
|
|
// Automatic deactivation disabled
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.ServiceSettings.DisableBotsWhenOwnerIsDeactivated = false
|
|
})
|
|
_, resp = th.SystemAdminClient.DeleteUser(th.BasicUser2.Id)
|
|
CheckOKStatus(t, resp)
|
|
|
|
t.Run("get bots, page=0, perPage=10", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_READ_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)
|
|
|
|
bots, resp := th.Client.GetBots(0, 10, "")
|
|
CheckOKStatus(t, resp)
|
|
require.Equal(t, []*model.Bot{bot1, bot2, bot3, orphanedBot}, bots)
|
|
|
|
botList := model.BotList(bots)
|
|
bots, resp = th.Client.GetBots(0, 10, botList.Etag())
|
|
CheckEtag(t, bots, resp)
|
|
})
|
|
|
|
t.Run("get bots, page=0, perPage=1", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_READ_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)
|
|
|
|
bots, resp := th.Client.GetBots(0, 1, "")
|
|
CheckOKStatus(t, resp)
|
|
require.Equal(t, []*model.Bot{bot1}, bots)
|
|
|
|
botList := model.BotList(bots)
|
|
bots, resp = th.Client.GetBots(0, 1, botList.Etag())
|
|
CheckEtag(t, bots, resp)
|
|
})
|
|
|
|
t.Run("get bots, page=1, perPage=2", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_READ_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)
|
|
|
|
bots, resp := th.Client.GetBots(1, 2, "")
|
|
CheckOKStatus(t, resp)
|
|
require.Equal(t, []*model.Bot{bot3, orphanedBot}, bots)
|
|
|
|
botList := model.BotList(bots)
|
|
bots, resp = th.Client.GetBots(1, 2, botList.Etag())
|
|
CheckEtag(t, bots, resp)
|
|
})
|
|
|
|
t.Run("get bots, page=2, perPage=2", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_READ_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)
|
|
|
|
bots, resp := th.Client.GetBots(2, 2, "")
|
|
CheckOKStatus(t, resp)
|
|
require.Equal(t, []*model.Bot{}, bots)
|
|
|
|
botList := model.BotList(bots)
|
|
bots, resp = th.Client.GetBots(2, 2, botList.Etag())
|
|
CheckEtag(t, bots, resp)
|
|
})
|
|
|
|
t.Run("get bots, page=0, perPage=10, include deleted", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_READ_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)
|
|
|
|
bots, resp := th.Client.GetBotsIncludeDeleted(0, 10, "")
|
|
CheckOKStatus(t, resp)
|
|
require.Equal(t, []*model.Bot{bot1, deletedBot1, bot2, bot3, deletedBot2, orphanedBot}, bots)
|
|
|
|
botList := model.BotList(bots)
|
|
bots, resp = th.Client.GetBotsIncludeDeleted(0, 10, botList.Etag())
|
|
CheckEtag(t, bots, resp)
|
|
})
|
|
|
|
t.Run("get bots, page=0, perPage=1, include deleted", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_READ_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)
|
|
|
|
bots, resp := th.Client.GetBotsIncludeDeleted(0, 1, "")
|
|
CheckOKStatus(t, resp)
|
|
require.Equal(t, []*model.Bot{bot1}, bots)
|
|
|
|
botList := model.BotList(bots)
|
|
bots, resp = th.Client.GetBotsIncludeDeleted(0, 1, botList.Etag())
|
|
CheckEtag(t, bots, resp)
|
|
})
|
|
|
|
t.Run("get bots, page=1, perPage=2, include deleted", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_READ_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)
|
|
|
|
bots, resp := th.Client.GetBotsIncludeDeleted(1, 2, "")
|
|
CheckOKStatus(t, resp)
|
|
require.Equal(t, []*model.Bot{bot2, bot3}, bots)
|
|
|
|
botList := model.BotList(bots)
|
|
bots, resp = th.Client.GetBotsIncludeDeleted(1, 2, botList.Etag())
|
|
CheckEtag(t, bots, resp)
|
|
})
|
|
|
|
t.Run("get bots, page=2, perPage=2, include deleted", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_READ_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)
|
|
|
|
bots, resp := th.Client.GetBotsIncludeDeleted(2, 2, "")
|
|
CheckOKStatus(t, resp)
|
|
require.Equal(t, []*model.Bot{deletedBot2, orphanedBot}, bots)
|
|
|
|
botList := model.BotList(bots)
|
|
bots, resp = th.Client.GetBotsIncludeDeleted(2, 2, botList.Etag())
|
|
CheckEtag(t, bots, resp)
|
|
})
|
|
|
|
t.Run("get bots, page=0, perPage=10, only orphaned", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_READ_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)
|
|
|
|
bots, resp := th.Client.GetBotsOrphaned(0, 10, "")
|
|
CheckOKStatus(t, resp)
|
|
require.Equal(t, []*model.Bot{orphanedBot}, bots)
|
|
|
|
botList := model.BotList(bots)
|
|
bots, resp = th.Client.GetBotsOrphaned(0, 10, botList.Etag())
|
|
CheckEtag(t, bots, resp)
|
|
})
|
|
|
|
t.Run("get bots without permission", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_MANAGE_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_MANAGE_OTHERS_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
|
|
|
_, resp := th.Client.GetBots(0, 10, "")
|
|
CheckErrorMessage(t, resp, "api.context.permissions.app_error")
|
|
})
|
|
}
|
|
|
|
func TestDisableBot(t *testing.T) {
|
|
t.Run("disable non-existent bot", func(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
|
|
_, resp := th.Client.DisableBot(model.NewId())
|
|
CheckNotFoundStatus(t, resp)
|
|
})
|
|
|
|
t.Run("disable bot without permission", func(t *testing.T) {
|
|
th := Setup().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)
|
|
|
|
bot := &model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
Description: "bot",
|
|
}
|
|
|
|
createdBot, resp := th.Client.CreateBot(bot)
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(createdBot.UserId)
|
|
|
|
_, resp = th.Client.DisableBot(createdBot.UserId)
|
|
CheckErrorMessage(t, resp, "store.sql_bot.get.missing.app_error")
|
|
})
|
|
|
|
t.Run("disable bot without permission, but with read permission", func(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_READ_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
|
|
|
bot := &model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
Description: "bot",
|
|
}
|
|
|
|
createdBot, resp := th.Client.CreateBot(bot)
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(createdBot.UserId)
|
|
|
|
_, resp = th.Client.DisableBot(createdBot.UserId)
|
|
CheckErrorMessage(t, resp, "api.context.permissions.app_error")
|
|
})
|
|
|
|
t.Run("disable bot with permission", func(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_MANAGE_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
|
|
|
bot, resp := th.Client.CreateBot(&model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
Description: "bot",
|
|
})
|
|
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) {
|
|
t.Run("enable non-existent bot", func(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
|
|
_, resp := th.Client.EnableBot(model.NewId())
|
|
CheckNotFoundStatus(t, resp)
|
|
})
|
|
|
|
t.Run("enable bot without permission", func(t *testing.T) {
|
|
th := Setup().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)
|
|
|
|
bot := &model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
Description: "bot",
|
|
}
|
|
|
|
createdBot, resp := th.Client.CreateBot(bot)
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(createdBot.UserId)
|
|
|
|
_, resp = th.SystemAdminClient.DisableBot(createdBot.UserId)
|
|
CheckOKStatus(t, resp)
|
|
|
|
_, resp = th.Client.EnableBot(createdBot.UserId)
|
|
CheckErrorMessage(t, resp, "store.sql_bot.get.missing.app_error")
|
|
})
|
|
|
|
t.Run("enable bot without permission, but with read permission", func(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_READ_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
|
|
|
bot := &model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
Description: "bot",
|
|
}
|
|
|
|
createdBot, resp := th.Client.CreateBot(bot)
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(createdBot.UserId)
|
|
|
|
_, resp = th.SystemAdminClient.DisableBot(createdBot.UserId)
|
|
CheckOKStatus(t, resp)
|
|
|
|
_, resp = th.Client.EnableBot(createdBot.UserId)
|
|
CheckErrorMessage(t, resp, "api.context.permissions.app_error")
|
|
})
|
|
|
|
t.Run("enable bot with permission", func(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_MANAGE_BOTS.Id, model.TEAM_USER_ROLE_ID)
|
|
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID, false)
|
|
|
|
bot, resp := th.Client.CreateBot(&model.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 := 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)
|
|
})
|
|
}
|
|
|
|
func TestAssignBot(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
|
|
t.Run("claim non-existent bot", func(t *testing.T) {
|
|
_, resp := th.SystemAdminClient.AssignBot(model.NewId(), model.NewId())
|
|
CheckNotFoundStatus(t, resp)
|
|
})
|
|
|
|
t.Run("system admin assign bot", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.SYSTEM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_READ_BOTS.Id, model.SYSTEM_USER_ROLE_ID)
|
|
|
|
bot := &model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
Description: "bot",
|
|
}
|
|
bot, resp := th.Client.CreateBot(bot)
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(bot.UserId)
|
|
|
|
before, resp := th.Client.GetBot(bot.UserId, "")
|
|
CheckOKStatus(t, resp)
|
|
require.Equal(t, th.BasicUser.Id, before.OwnerId)
|
|
|
|
_, resp = th.SystemAdminClient.AssignBot(bot.UserId, th.SystemAdminUser.Id)
|
|
CheckOKStatus(t, resp)
|
|
|
|
// Original owner doesn't have read others bots permission, therefore can't see bot anymore
|
|
_, resp = th.Client.GetBot(bot.UserId, "")
|
|
CheckNotFoundStatus(t, resp)
|
|
|
|
// System admin can see creator ID has changed
|
|
after, resp := th.SystemAdminClient.GetBot(bot.UserId, "")
|
|
CheckOKStatus(t, resp)
|
|
require.Equal(t, th.SystemAdminUser.Id, after.OwnerId)
|
|
|
|
// Assign back to user without permissions to manage
|
|
_, resp = th.SystemAdminClient.AssignBot(bot.UserId, th.BasicUser.Id)
|
|
CheckOKStatus(t, resp)
|
|
|
|
after, resp = th.SystemAdminClient.GetBot(bot.UserId, "")
|
|
CheckOKStatus(t, resp)
|
|
require.Equal(t, th.BasicUser.Id, after.OwnerId)
|
|
})
|
|
|
|
t.Run("random user assign bot", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.SYSTEM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_READ_BOTS.Id, model.SYSTEM_USER_ROLE_ID)
|
|
|
|
bot := &model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
Description: "bot",
|
|
}
|
|
createdBot, resp := th.Client.CreateBot(bot)
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(createdBot.UserId)
|
|
|
|
th.LoginBasic2()
|
|
|
|
// Without permission to read others bots it doesn't exist
|
|
_, resp = th.Client.AssignBot(createdBot.UserId, th.BasicUser2.Id)
|
|
CheckErrorMessage(t, resp, "store.sql_bot.get.missing.app_error")
|
|
|
|
// With permissions to read we don't have permissions to modify
|
|
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.SYSTEM_USER_ROLE_ID)
|
|
_, resp = th.Client.AssignBot(createdBot.UserId, th.BasicUser2.Id)
|
|
CheckErrorMessage(t, resp, "api.context.permissions.app_error")
|
|
|
|
th.LoginBasic()
|
|
})
|
|
|
|
t.Run("delegated user assign bot", func(t *testing.T) {
|
|
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
|
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.SYSTEM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_READ_BOTS.Id, model.SYSTEM_USER_ROLE_ID)
|
|
|
|
bot := &model.Bot{
|
|
Username: GenerateTestUsername(),
|
|
Description: "bot",
|
|
}
|
|
bot, resp := th.Client.CreateBot(bot)
|
|
CheckCreatedStatus(t, resp)
|
|
defer th.App.PermanentDeleteBot(bot.UserId)
|
|
|
|
// Simulate custom role by just changing the system user role
|
|
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.SYSTEM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_READ_BOTS.Id, model.SYSTEM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.SYSTEM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_MANAGE_BOTS.Id, model.SYSTEM_USER_ROLE_ID)
|
|
th.AddPermissionToRole(model.PERMISSION_MANAGE_OTHERS_BOTS.Id, model.SYSTEM_USER_ROLE_ID)
|
|
th.LoginBasic2()
|
|
|
|
_, resp = th.Client.AssignBot(bot.UserId, th.BasicUser2.Id)
|
|
CheckOKStatus(t, resp)
|
|
|
|
after, resp := th.SystemAdminClient.GetBot(bot.UserId, "")
|
|
CheckOKStatus(t, resp)
|
|
require.Equal(t, th.BasicUser2.Id, after.OwnerId)
|
|
})
|
|
}
|
|
|
|
func sToP(s string) *string {
|
|
return &s
|
|
}
|