MCPcopy
hub / github.com/mattermost/mattermost / TestAssignBot

Function TestAssignBot

server/channels/api4/bot_test.go:1315–1493  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1313}
1314
1315func TestAssignBot(t *testing.T) {
1316 mainHelper.Parallel(t)
1317 th := Setup(t).InitBasic(t)
1318
1319 t.Run("claim non-existent bot", func(t *testing.T) {
1320 th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
1321 _, resp, err := client.AssignBot(context.Background(), model.NewId(), model.NewId())
1322 require.Error(t, err)
1323 CheckNotFoundStatus(t, resp)
1324 })
1325 })
1326
1327 t.Run("system admin and local mode assign bot", func(t *testing.T) {
1328 defaultPerms := th.SaveDefaultRolePermissions(t)
1329 defer th.RestoreDefaultRolePermissions(t, defaultPerms)
1330
1331 th.AddPermissionToRole(t, model.PermissionCreateBot.Id, model.SystemUserRoleId)
1332 th.AddPermissionToRole(t, model.PermissionReadBots.Id, model.SystemUserRoleId)
1333 th.App.UpdateConfig(func(cfg *model.Config) {
1334 *cfg.ServiceSettings.EnableBotAccountCreation = true
1335 })
1336
1337 bot := &model.Bot{
1338 Username: GenerateTestUsername(),
1339 Description: "bot",
1340 }
1341 bot, resp, err := th.Client.CreateBot(context.Background(), bot)
1342 require.NoError(t, err)
1343 CheckCreatedStatus(t, resp)
1344 defer func() {
1345 appErr := th.App.PermanentDeleteBot(th.Context, bot.UserId)
1346 assert.Nil(t, appErr)
1347 }()
1348
1349 before, resp, err := th.Client.GetBot(context.Background(), bot.UserId, "")
1350 require.NoError(t, err)
1351 CheckOKStatus(t, resp)
1352 require.Equal(t, th.BasicUser.Id, before.OwnerId)
1353
1354 _, resp, err = th.SystemAdminClient.AssignBot(context.Background(), bot.UserId, th.SystemAdminUser.Id)
1355 require.NoError(t, err)
1356 CheckOKStatus(t, resp)
1357
1358 // Original owner doesn't have read others bots permission, therefore can't see bot anymore
1359 _, resp, err = th.Client.GetBot(context.Background(), bot.UserId, "")
1360 require.Error(t, err)
1361 CheckNotFoundStatus(t, resp)
1362
1363 // System admin can see creator ID has changed
1364 after, resp, err := th.SystemAdminClient.GetBot(context.Background(), bot.UserId, "")
1365 require.NoError(t, err)
1366 CheckOKStatus(t, resp)
1367 require.Equal(t, th.SystemAdminUser.Id, after.OwnerId)
1368
1369 // Assign back to user without permissions to manage, using local mode
1370 _, resp, err = th.LocalClient.AssignBot(context.Background(), bot.UserId, th.BasicUser.Id)
1371 require.NoError(t, err)
1372 CheckOKStatus(t, resp)

Callers

nothing calls this directly

Calls 15

GenerateTestUsernameFunction · 0.85
CheckCreatedStatusFunction · 0.85
CheckOKStatusFunction · 0.85
CheckErrorIDFunction · 0.85
TestForAllClientsMethod · 0.80
LoginBasic2Method · 0.80
LoginBasicMethod · 0.80
SetupFunction · 0.70
CheckNotFoundStatusFunction · 0.70
RunMethod · 0.65
AssignBotMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…