forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_api_spec.js
483 lines (388 loc) · 18.7 KB
/
bot_api_spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
// ***************************************************************
// - [#] indicates a test step (e.g. # Go to a page)
// - [*] indicates an assertion (e.g. * Check the title)
// - Use element ID when selecting an element. Create one if none.
// ***************************************************************
// Stage: @prod
// Group: @bot_accounts @mfa
import * as TIMEOUTS from '../../fixtures/timeouts';
describe('Bot accounts ownership and API', () => {
let newTeam;
let newUser;
let newChannel;
let botId;
let botUsername;
let botName;
let adminUser;
beforeEach(() => {
cy.apiAdminLogin().then(({user}) => {
adminUser = user;
});
cy.apiInitSetup().then(({team, user, channel}) => {
newTeam = team;
newUser = user;
newChannel = channel;
});
// # Set ServiceSettings to expected values
const newSettings = {
ServiceSettings: {
EnforceMultifactorAuthentication: false,
EnableBotAccountCreation: true,
},
};
cy.apiUpdateConfig(newSettings);
// # Create a test bot
cy.apiCreateBot().then(({bot}) => {
({user_id: botId, username: botUsername, display_name: botName} = bot);
cy.apiPatchUserRoles(bot.user_id, ['system_admin', 'system_user']);
});
});
it('MM-T1862 Only system admin are able to create bots', () => {
// # Login as admin
cy.apiAdminLogin();
cy.visit(`/${newTeam.name}/channels/town-square`);
// # Open the menu
cy.get('#lhsHeader', {timeout: TIMEOUTS.ONE_MIN}).should('be.visible').within(() => {
cy.get('#sidebarHeaderDropdownButton').click();
cy.get('.dropdown-menu').should('be.visible');
// * Confirm integrations are visible
cy.get('#integrations').should('be.visible');
});
// # Login as a regular user
cy.apiLogin(newUser);
cy.visit(`/${newTeam.name}/channels/town-square`);
// # Open the menu
cy.get('#lhsHeader', {timeout: TIMEOUTS.ONE_MIN}).should('be.visible').within(() => {
cy.get('#sidebarHeaderDropdownButton').click();
cy.get('.dropdown-menu').should('be.visible');
// * Confirm integrations are not visible
cy.get('#integrations').should('not.exist');
});
});
it('MM-T1863 Only System Admin are able to create bots (API)', () => {
// # Login as a regular user
cy.apiLogin(newUser);
// # Try to create a new bot as a regular user
const botName3 = 'stay-enabled-bot-' + Date.now();
cy.request({
headers: {'X-Requested-With': 'XMLHttpRequest'},
url: '/api/v4/bots',
method: 'POST',
failOnStatusCode: false,
body: {
username: botName3,
display_name: 'some text',
description: 'some text',
},
}).then((response) => {
// * Validate that request was denied
expect(response.status).to.equal(403);
});
});
it('MM-T1864 Create bot (API)', () => {
// # Login as admin
cy.apiAdminLogin();
// * This call will fail if bot was not created
cy.apiCreateBot();
});
it('MM-T1865 Create post as bot', () => {
// # Login as admin
cy.apiAdminLogin();
// # Create token for the bot
cy.apiCreateToken(botId).then(({token}) => {
// # Logout to allow posting as bot
cy.apiLogout();
const msg1 = 'this is a bot message ' + botName;
cy.apiCreatePost(newChannel.id, msg1, '', {attachments: [{pretext: 'Look some text', text: 'This is text'}]}, token);
// # Re-login to validate post presence
cy.apiAdminLogin();
cy.visit(`/${newTeam.name}/channels/` + newChannel.name);
// * Validate post was created
cy.findByText(msg1).should('be.visible');
});
});
it('MM-T1866 Create two posts in a row to the same channel', () => {
// # Login as admin
cy.apiAdminLogin();
// # Create token for the bot
cy.apiCreateToken(botId).then(({token}) => {
// # Logout to allow posting as bot
cy.apiLogout();
const msg1 = 'this is a bot message ' + botName;
const msg2 = 'this is a bot message2 ' + botName;
cy.apiCreatePost(newChannel.id, msg1, '', {attachments: [{pretext: 'Look some text', text: 'This is text'}]}, token).then(({body: post1}) => {
cy.apiCreatePost(newChannel.id, msg2, '', {attachments: [{pretext: 'Look some text', text: 'This is text'}]}, token).then(({body: post2}) => {
// # Re-login to validate post presence
cy.apiAdminLogin();
cy.visit(`/${newTeam.name}/channels/` + newChannel.name);
// * Validate posts were created
cy.get(`#postMessageText_${post1.id}`, {timeout: TIMEOUTS.ONE_MIN}).should('contain', msg1);
cy.get(`#postMessageText_${post2.id}`, {timeout: TIMEOUTS.ONE_MIN}).should('contain', msg2);
// * Validate first post has an image
cy.get(`#post_${post1.id}`).find('.Avatar').should('be.visible');
// * Validate that the second one doesn't
cy.get(`#post_${post2.id}`).should('have.class', 'same--user');
});
});
});
});
it('MM-T1867 Post as a bot and include an @ mention', () => {
// # Login as admin
cy.apiAdminLogin();
// # Create token for the bot
cy.apiCreateToken(botId).then(({token}) => {
// # Logout to allow posting as bot
cy.apiLogout();
const msg1 = 'this is a bot message ' + botName;
cy.apiCreatePost(newChannel.id, msg1 + ' to @sysadmin', '', {}, token);
// # Re-login to validate post presence
cy.apiAdminLogin();
cy.visit(`/${newTeam.name}/channels/` + newChannel.name);
cy.getLastPostId().then((postId) => {
// * Validate post was created
cy.get(`#postMessageText_${postId}`, {timeout: TIMEOUTS.ONE_MIN}).should('contain', msg1);
// * Assert that the last message posted contains highlighted mention
cy.get(`#postMessageText_${postId}`, {timeout: TIMEOUTS.ONE_MIN}).find('.mention--highlight').should('be.visible');
});
});
});
it('MM-T1868 BOT has a member role and is not in target channel and team', () => {
// # Login as admin
cy.apiAdminLogin();
// # Create a test bot (member)
cy.apiCreateBot().then(({bot}) => {
// # Create token for the bot
cy.apiCreateToken(bot.user_id).then(({token}) => {
// # Logout to allow posting as bot
cy.apiLogout();
// # Try posting
cy.apiCreatePost(newChannel.id, 'this is a bot message ' + bot.username, '', {}, token, false).then((response) => {
// * Validate that posting was not allowed
expect(response.status).to.equal(403);
});
});
});
});
it('MM-T1869 BOT has a system admin role and is not in target channel and team', () => {
// # Login as admin
cy.apiAdminLogin();
const botName3 = 'stay-enabled-bot-' + Date.now();
// # Create token for the bot
cy.apiCreateToken(botId).then(({token}) => {
// # Logout to allow posting as bot
cy.apiLogout();
// # Try posting
cy.apiCreatePost(newChannel.id, 'this is a bot message ' + botName3, '', {}, token).then((response) => {
// * Validate that posting was allowed
expect(response.status).to.equal(201);
});
});
});
it('MM-T1870 BOT has a system admin role and can also post to private channels they do not belong to', () => {
// # Login as admin
cy.apiAdminLogin();
const channelName = 'channel' + Date.now();
// # Create private channel that bot doesn't belong to
cy.apiCreateChannel(newTeam.id, channelName, channelName, 'P').then(({channel}) => {
// # Create token for the bot
cy.apiCreateToken(botId).then(({token}) => {
// # Logout to allow posting as bot
cy.apiLogout();
const msg1 = 'this is a bot message ' + botName;
// # Create a post
cy.apiCreatePost(channel.id, msg1 + ' to @sysadmin', '', {}, token);
// # Re-login to validate post presence
cy.apiAdminLogin();
cy.visit(`/${newTeam.name}/channels/` + channel.name);
cy.getLastPostId().then((postId) => {
// * Validate post was created
cy.get(`#postMessageText_${postId}`, {timeout: TIMEOUTS.ONE_MIN}).should('contain', msg1);
});
});
});
});
it('MM-T1872 Bot can post to DM channel', () => {
// # Create DM channel that bot doesn't belong to
cy.apiCreateDirectChannel([newUser.id, adminUser.id]).then(({channel}) => {
// # Create token for the bot
cy.apiAccessToken(botId, 'some text').then(({token}) => {
const msg1 = 'this is a bot message ' + botName;
// # Post test message
cy.postBotMessage({message: msg1, token, channelId: channel.id});
// # Validate post presence
cy.visit(`/${newTeam.name}/channels/` + channel.name);
cy.getLastPostId().then((postId) => {
// * Validate post was created
cy.get(`#postMessageText_${postId}`, {timeout: TIMEOUTS.ONE_MIN}).should('contain', msg1);
});
});
});
});
it('MM-T1874 Bots can post when MFA is enforced', () => {
// # Create token for the bot
cy.apiAccessToken(botId, 'some text').then(({token}) => {
const msg1 = 'this is a bot message ' + botName;
cy.postBotMessage({channelId: newChannel.id, message: msg1, props: {attachments: [{pretext: 'Look some text', text: 'This is text'}]}, token});
// # Visit test channel
cy.visit(`/${newTeam.name}/channels/` + newChannel.name);
// * Validate post was created
cy.findByText(msg1).should('be.visible');
const newSettings = {
ServiceSettings: {
EnforceMultifactorAuthentication: true,
},
};
cy.apiUpdateConfig(newSettings);
const msg2 = 'this is a bot message2 ' + botName;
cy.postBotMessage({channelId: newChannel.id, message: msg2, props: {attachments: [{pretext: 'Look some text', text: 'This is text'}]}, token});
cy.visit(`/${newTeam.name}/channels/` + newChannel.name);
// * Validate post was created
cy.findByText(msg2).should('be.visible');
});
});
it('MM-T1875 A bot cannot create another bot', () => {
// # Create token for the bot
cy.apiAccessToken(botId, 'some text').then(({token}) => {
// # Logout to allow posting as bot
cy.apiLogout();
// # Try to create a new bot
cy.request({
headers: {'X-Requested-With': 'XMLHttpRequest', Authorization: `Bearer ${token}`},
url: '/api/v4/bots',
method: 'POST',
failOnStatusCode: false,
body: {
username: botName + '333',
display_name: 'some text',
description: 'some text',
},
}).then((response) => {
// * Validate that request was denied
expect(response.status).to.equal(403);
});
});
});
it('MM-T1877 Reactivate a deactivated bot', () => {
// # Create private channel that bot doesn't belong to
cy.apiCreateDirectChannel([newUser.id, adminUser.id]).then(({channel}) => {
// # Create token for the bot
cy.apiAccessToken(botId, 'some text').then(({token}) => {
const msg1 = 'this is a bot message ' + botName;
// # Create a post
cy.postBotMessage({channelId: channel.id, message: msg1, token});
cy.visit(`/${newTeam.name}/channels/` + channel.name);
cy.getLastPostId().then((postId) => {
// * Validate post was created
cy.get(`#postMessageText_${postId}`, {timeout: TIMEOUTS.ONE_MIN}).should('contain', msg1);
});
// # Disable the bot
cy.visit(`/${newTeam.name}/integrations/bots`);
cy.findByText(`${botName} (@${botUsername})`).scrollIntoView().parent().findByText('Disable').click();
// # Try to post again
const msg2 = 'this is a bot message2 ' + botName;
// # Logout to allow posting as bot
cy.apiLogout();
// # Create a post
cy.postBotMessage({channelId: channel.id, message: msg2, token, failOnStatus: false}).then(({status}) => {
// * Validate that posting failed
expect(status, 403);
});
cy.apiAdminLogin();
// # Enable the bot again
cy.visit(`/${newTeam.name}/integrations/bots`);
cy.findByText(`${botName} (@${botUsername})`).scrollIntoView().parent().findByText('Enable').click();
// # Try to post again
// * Validate that posting works
cy.postBotMessage({channelId: channel.id, message: msg2, token});
// * Validate post presence
cy.visit(`/${newTeam.name}/channels/` + channel.name);
cy.getLastPostId().then((postId) => {
// * Validate post was created
cy.get(`#postMessageText_${postId}`, {timeout: TIMEOUTS.ONE_MIN}).should('contain', msg2);
});
});
});
});
it('MM-T1878 Disable token can not be used to post', () => {
// # Create DM channel that bot doesn't belong to
cy.apiCreateDirectChannel([newUser.id, adminUser.id]).then(({channel}) => {
// # Create token for the bot
cy.apiAccessToken(botId, 'some text').then(({token, id}) => {
const msg1 = 'this is a bot message ' + botName;
// # Create a post
cy.postBotMessage({channelId: channel.id, message: msg1, token});
// # Validate post presence
cy.visit(`/${newTeam.name}/channels/` + channel.name);
cy.getLastPostId().then((postId) => {
// * Validate post was created
cy.get(`#postMessageText_${postId}`, {timeout: TIMEOUTS.ONE_MIN}).should('contain', msg1);
});
// # Disable the bot token
cy.visit(`/${newTeam.name}/integrations/bots`);
cy.findByText(`${botName} (@${botUsername})`).then((el) => {
// # Make sure it's on the screen
cy.wrap(el[0].parentElement.parentElement).scrollIntoView();
cy.get(`#${id}_deactivate`).click();
});
// # Try to post again
const msg2 = 'this is a bot message2 ' + botName;
// # Create a post
cy.postBotMessage({channelId: channel.id, message: msg2, token, failOnStatus: false}).then(({status}) => {
// * Validate that posting failed
expect(status, 403);
});
// # Enable the bot token again
cy.visit(`/${newTeam.name}/integrations/bots`);
cy.findByText(`${botName} (@${botUsername})`).then((el) => {
// # Make sure it's on the screen
cy.wrap(el[0].parentElement.parentElement).scrollIntoView();
cy.get(`#${id}_activate`).click();
});
// # Try to post again
// * Validate that posting works
cy.postBotMessage({channelId: channel.id, message: msg2, token});
// # Validate post presence
cy.visit(`/${newTeam.name}/channels/` + channel.name);
cy.getLastPostId().then((postId) => {
// * Validate post was created
cy.get(`#postMessageText_${postId}`, {timeout: TIMEOUTS.ONE_MIN}).should('contain', msg2);
});
});
});
});
it('MM-T1880 Deleted token can not be used to post', () => {
// # Create private channel that bot doesn't belong to
cy.apiCreateDirectChannel([newUser.id, adminUser.id]).then(({channel}) => {
// # Create token for the bot
cy.apiAccessToken(botId, 'some text').then(({token, id}) => {
const msg1 = 'this is a bot message ' + botName;
// # Create a post
cy.postBotMessage({channelId: channel.id, message: msg1, token});
// # Validate post presence
cy.visit(`/${newTeam.name}/channels/` + channel.name);
cy.getLastPostId().then((postId) => {
// * Validate post was created
cy.get(`#postMessageText_${postId}`, {timeout: TIMEOUTS.ONE_MIN}).should('contain', msg1);
});
// # Disable the bot token
cy.visit(`/${newTeam.name}/integrations/bots`);
cy.findByText(`${botName} (@${botUsername})`).then((el) => {
// # Make sure it's on the screen
cy.wrap(el[0].parentElement.parentElement).scrollIntoView();
// # Delete token
cy.get(`#${id}_delete`).click();
cy.get('#confirmModalButton').click();
// # Try to post again
const msg2 = 'this is a bot message2 ' + botName;
// # Create a post
cy.postBotMessage({channelId: channel.id, message: msg2, token, failOnStatus: false}).then(({status}) => {
// * Validate that posting failed
expect(status, 403);
});
});
});
});
});
});