]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/notifications/moderation-notifications.ts
Introduce videos command
[github/Chocobozzz/PeerTube.git] / server / tests / api / notifications / moderation-notifications.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import { buildUUID } from '@server/helpers/uuid'
5 import {
6 checkAbuseStateChange,
7 checkAutoInstanceFollowing,
8 CheckerBaseParams,
9 checkNewAbuseMessage,
10 checkNewAccountAbuseForModerators,
11 checkNewBlacklistOnMyVideo,
12 checkNewCommentAbuseForModerators,
13 checkNewInstanceFollower,
14 checkNewVideoAbuseForModerators,
15 checkNewVideoFromSubscription,
16 checkUserRegistered,
17 checkVideoAutoBlacklistForModerators,
18 checkVideoIsPublished,
19 cleanupTests,
20 MockInstancesIndex,
21 MockSmtpServer,
22 prepareNotificationsTest,
23 ServerInfo,
24 wait,
25 waitJobs
26 } from '@shared/extra-utils'
27 import { AbuseState, CustomConfig, UserNotification, VideoPrivacy } from '@shared/models'
28
29 describe('Test moderation notifications', function () {
30 let servers: ServerInfo[] = []
31 let userAccessToken: string
32 let userNotifications: UserNotification[] = []
33 let adminNotifications: UserNotification[] = []
34 let adminNotificationsServer2: UserNotification[] = []
35 let emails: object[] = []
36
37 before(async function () {
38 this.timeout(120000)
39
40 const res = await prepareNotificationsTest(3)
41 emails = res.emails
42 userAccessToken = res.userAccessToken
43 servers = res.servers
44 userNotifications = res.userNotifications
45 adminNotifications = res.adminNotifications
46 adminNotificationsServer2 = res.adminNotificationsServer2
47 })
48
49 describe('Abuse for moderators notification', function () {
50 let baseParams: CheckerBaseParams
51
52 before(() => {
53 baseParams = {
54 server: servers[0],
55 emails,
56 socketNotifications: adminNotifications,
57 token: servers[0].accessToken
58 }
59 })
60
61 it('Should send a notification to moderators on local video abuse', async function () {
62 this.timeout(20000)
63
64 const name = 'video for abuse ' + buildUUID()
65 const video = await servers[0].videosCommand.upload({ token: userAccessToken, attributes: { name } })
66
67 await servers[0].abusesCommand.report({ videoId: video.id, reason: 'super reason' })
68
69 await waitJobs(servers)
70 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
71 })
72
73 it('Should send a notification to moderators on remote video abuse', async function () {
74 this.timeout(20000)
75
76 const name = 'video for abuse ' + buildUUID()
77 const video = await servers[0].videosCommand.upload({ token: userAccessToken, attributes: { name } })
78
79 await waitJobs(servers)
80
81 const videoId = await servers[1].videosCommand.getId({ uuid: video.uuid })
82 await servers[1].abusesCommand.report({ videoId, reason: 'super reason' })
83
84 await waitJobs(servers)
85 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
86 })
87
88 it('Should send a notification to moderators on local comment abuse', async function () {
89 this.timeout(20000)
90
91 const name = 'video for abuse ' + buildUUID()
92 const video = await servers[0].videosCommand.upload({ token: userAccessToken, attributes: { name } })
93 const comment = await servers[0].commentsCommand.createThread({
94 token: userAccessToken,
95 videoId: video.id,
96 text: 'comment abuse ' + buildUUID()
97 })
98
99 await waitJobs(servers)
100
101 await servers[0].abusesCommand.report({ commentId: comment.id, reason: 'super reason' })
102
103 await waitJobs(servers)
104 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
105 })
106
107 it('Should send a notification to moderators on remote comment abuse', async function () {
108 this.timeout(20000)
109
110 const name = 'video for abuse ' + buildUUID()
111 const video = await servers[0].videosCommand.upload({ token: userAccessToken, attributes: { name } })
112
113 await servers[0].commentsCommand.createThread({
114 token: userAccessToken,
115 videoId: video.id,
116 text: 'comment abuse ' + buildUUID()
117 })
118
119 await waitJobs(servers)
120
121 const { data } = await servers[1].commentsCommand.listThreads({ videoId: video.uuid })
122 const commentId = data[0].id
123 await servers[1].abusesCommand.report({ commentId, reason: 'super reason' })
124
125 await waitJobs(servers)
126 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
127 })
128
129 it('Should send a notification to moderators on local account abuse', async function () {
130 this.timeout(20000)
131
132 const username = 'user' + new Date().getTime()
133 const { account } = await servers[0].usersCommand.create({ username, password: 'donald' })
134 const accountId = account.id
135
136 await servers[0].abusesCommand.report({ accountId, reason: 'super reason' })
137
138 await waitJobs(servers)
139 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
140 })
141
142 it('Should send a notification to moderators on remote account abuse', async function () {
143 this.timeout(20000)
144
145 const username = 'user' + new Date().getTime()
146 const tmpToken = await servers[0].usersCommand.generateUserAndToken(username)
147 await servers[0].videosCommand.upload({ token: tmpToken, attributes: { name: 'super video' } })
148
149 await waitJobs(servers)
150
151 const account = await servers[1].accountsCommand.get({ accountName: username + '@' + servers[0].host })
152 await servers[1].abusesCommand.report({ accountId: account.id, reason: 'super reason' })
153
154 await waitJobs(servers)
155 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
156 })
157 })
158
159 describe('Abuse state change notification', function () {
160 let baseParams: CheckerBaseParams
161 let abuseId: number
162
163 before(async function () {
164 baseParams = {
165 server: servers[0],
166 emails,
167 socketNotifications: userNotifications,
168 token: userAccessToken
169 }
170
171 const name = 'abuse ' + buildUUID()
172 const video = await servers[0].videosCommand.upload({ token: userAccessToken, attributes: { name } })
173
174 const body = await servers[0].abusesCommand.report({ token: userAccessToken, videoId: video.id, reason: 'super reason' })
175 abuseId = body.abuse.id
176 })
177
178 it('Should send a notification to reporter if the abuse has been accepted', async function () {
179 this.timeout(10000)
180
181 await servers[0].abusesCommand.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
182 await waitJobs(servers)
183
184 await checkAbuseStateChange(baseParams, abuseId, AbuseState.ACCEPTED, 'presence')
185 })
186
187 it('Should send a notification to reporter if the abuse has been rejected', async function () {
188 this.timeout(10000)
189
190 await servers[0].abusesCommand.update({ abuseId, body: { state: AbuseState.REJECTED } })
191 await waitJobs(servers)
192
193 await checkAbuseStateChange(baseParams, abuseId, AbuseState.REJECTED, 'presence')
194 })
195 })
196
197 describe('New abuse message notification', function () {
198 let baseParamsUser: CheckerBaseParams
199 let baseParamsAdmin: CheckerBaseParams
200 let abuseId: number
201 let abuseId2: number
202
203 before(async function () {
204 baseParamsUser = {
205 server: servers[0],
206 emails,
207 socketNotifications: userNotifications,
208 token: userAccessToken
209 }
210
211 baseParamsAdmin = {
212 server: servers[0],
213 emails,
214 socketNotifications: adminNotifications,
215 token: servers[0].accessToken
216 }
217
218 const name = 'abuse ' + buildUUID()
219 const video = await servers[0].videosCommand.upload({ token: userAccessToken, attributes: { name } })
220
221 {
222 const body = await servers[0].abusesCommand.report({ token: userAccessToken, videoId: video.id, reason: 'super reason' })
223 abuseId = body.abuse.id
224 }
225
226 {
227 const body = await servers[0].abusesCommand.report({ token: userAccessToken, videoId: video.id, reason: 'super reason 2' })
228 abuseId2 = body.abuse.id
229 }
230 })
231
232 it('Should send a notification to reporter on new message', async function () {
233 this.timeout(10000)
234
235 const message = 'my super message to users'
236 await servers[0].abusesCommand.addMessage({ abuseId, message })
237 await waitJobs(servers)
238
239 await checkNewAbuseMessage(baseParamsUser, abuseId, message, 'user_1@example.com', 'presence')
240 })
241
242 it('Should not send a notification to the admin if sent by the admin', async function () {
243 this.timeout(10000)
244
245 const message = 'my super message that should not be sent to the admin'
246 await servers[0].abusesCommand.addMessage({ abuseId, message })
247 await waitJobs(servers)
248
249 await checkNewAbuseMessage(baseParamsAdmin, abuseId, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'absence')
250 })
251
252 it('Should send a notification to moderators', async function () {
253 this.timeout(10000)
254
255 const message = 'my super message to moderators'
256 await servers[0].abusesCommand.addMessage({ token: userAccessToken, abuseId: abuseId2, message })
257 await waitJobs(servers)
258
259 await checkNewAbuseMessage(baseParamsAdmin, abuseId2, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'presence')
260 })
261
262 it('Should not send a notification to reporter if sent by the reporter', async function () {
263 this.timeout(10000)
264
265 const message = 'my super message that should not be sent to reporter'
266 await servers[0].abusesCommand.addMessage({ token: userAccessToken, abuseId: abuseId2, message })
267 await waitJobs(servers)
268
269 await checkNewAbuseMessage(baseParamsUser, abuseId2, message, 'user_1@example.com', 'absence')
270 })
271 })
272
273 describe('Video blacklist on my video', function () {
274 let baseParams: CheckerBaseParams
275
276 before(() => {
277 baseParams = {
278 server: servers[0],
279 emails,
280 socketNotifications: userNotifications,
281 token: userAccessToken
282 }
283 })
284
285 it('Should send a notification to video owner on blacklist', async function () {
286 this.timeout(10000)
287
288 const name = 'video for abuse ' + buildUUID()
289 const { uuid } = await servers[0].videosCommand.upload({ token: userAccessToken, attributes: { name } })
290
291 await servers[0].blacklistCommand.add({ videoId: uuid })
292
293 await waitJobs(servers)
294 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist')
295 })
296
297 it('Should send a notification to video owner on unblacklist', async function () {
298 this.timeout(10000)
299
300 const name = 'video for abuse ' + buildUUID()
301 const { uuid } = await servers[0].videosCommand.upload({ token: userAccessToken, attributes: { name } })
302
303 await servers[0].blacklistCommand.add({ videoId: uuid })
304
305 await waitJobs(servers)
306 await servers[0].blacklistCommand.remove({ videoId: uuid })
307 await waitJobs(servers)
308
309 await wait(500)
310 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist')
311 })
312 })
313
314 describe('New registration', function () {
315 let baseParams: CheckerBaseParams
316
317 before(() => {
318 baseParams = {
319 server: servers[0],
320 emails,
321 socketNotifications: adminNotifications,
322 token: servers[0].accessToken
323 }
324 })
325
326 it('Should send a notification only to moderators when a user registers on the instance', async function () {
327 this.timeout(10000)
328
329 await servers[0].usersCommand.register({ username: 'user_45' })
330
331 await waitJobs(servers)
332
333 await checkUserRegistered(baseParams, 'user_45', 'presence')
334
335 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
336 await checkUserRegistered({ ...baseParams, ...userOverride }, 'user_45', 'absence')
337 })
338 })
339
340 describe('New instance follows', function () {
341 const instanceIndexServer = new MockInstancesIndex()
342 let config: any
343 let baseParams: CheckerBaseParams
344
345 before(async () => {
346 baseParams = {
347 server: servers[0],
348 emails,
349 socketNotifications: adminNotifications,
350 token: servers[0].accessToken
351 }
352
353 const port = await instanceIndexServer.initialize()
354 instanceIndexServer.addInstance(servers[1].host)
355
356 config = {
357 followings: {
358 instance: {
359 autoFollowIndex: {
360 indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
361 enabled: true
362 }
363 }
364 }
365 }
366 })
367
368 it('Should send a notification only to admin when there is a new instance follower', async function () {
369 this.timeout(20000)
370
371 await servers[2].followsCommand.follow({ targets: [ servers[0].url ] })
372
373 await waitJobs(servers)
374
375 await checkNewInstanceFollower(baseParams, 'localhost:' + servers[2].port, 'presence')
376
377 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
378 await checkNewInstanceFollower({ ...baseParams, ...userOverride }, 'localhost:' + servers[2].port, 'absence')
379 })
380
381 it('Should send a notification on auto follow back', async function () {
382 this.timeout(40000)
383
384 await servers[2].followsCommand.unfollow({ target: servers[0] })
385 await waitJobs(servers)
386
387 const config = {
388 followings: {
389 instance: {
390 autoFollowBack: { enabled: true }
391 }
392 }
393 }
394 await servers[0].configCommand.updateCustomSubConfig({ newConfig: config })
395
396 await servers[2].followsCommand.follow({ targets: [ servers[0].url ] })
397
398 await waitJobs(servers)
399
400 const followerHost = servers[0].host
401 const followingHost = servers[2].host
402 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
403
404 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
405 await checkAutoInstanceFollowing({ ...baseParams, ...userOverride }, followerHost, followingHost, 'absence')
406
407 config.followings.instance.autoFollowBack.enabled = false
408 await servers[0].configCommand.updateCustomSubConfig({ newConfig: config })
409 await servers[0].followsCommand.unfollow({ target: servers[2] })
410 await servers[2].followsCommand.unfollow({ target: servers[0] })
411 })
412
413 it('Should send a notification on auto instances index follow', async function () {
414 this.timeout(30000)
415 await servers[0].followsCommand.unfollow({ target: servers[1] })
416
417 await servers[0].configCommand.updateCustomSubConfig({ newConfig: config })
418
419 await wait(5000)
420 await waitJobs(servers)
421
422 const followerHost = servers[0].host
423 const followingHost = servers[1].host
424 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
425
426 config.followings.instance.autoFollowIndex.enabled = false
427 await servers[0].configCommand.updateCustomSubConfig({ newConfig: config })
428 await servers[0].followsCommand.unfollow({ target: servers[1] })
429 })
430 })
431
432 describe('Video-related notifications when video auto-blacklist is enabled', function () {
433 let userBaseParams: CheckerBaseParams
434 let adminBaseParamsServer1: CheckerBaseParams
435 let adminBaseParamsServer2: CheckerBaseParams
436 let videoUUID: string
437 let videoName: string
438 let currentCustomConfig: CustomConfig
439
440 before(async () => {
441
442 adminBaseParamsServer1 = {
443 server: servers[0],
444 emails,
445 socketNotifications: adminNotifications,
446 token: servers[0].accessToken
447 }
448
449 adminBaseParamsServer2 = {
450 server: servers[1],
451 emails,
452 socketNotifications: adminNotificationsServer2,
453 token: servers[1].accessToken
454 }
455
456 userBaseParams = {
457 server: servers[0],
458 emails,
459 socketNotifications: userNotifications,
460 token: userAccessToken
461 }
462
463 currentCustomConfig = await servers[0].configCommand.getCustomConfig()
464
465 const autoBlacklistTestsCustomConfig = {
466 ...currentCustomConfig,
467
468 autoBlacklist: {
469 videos: {
470 ofUsers: {
471 enabled: true
472 }
473 }
474 }
475 }
476
477 // enable transcoding otherwise own publish notification after transcoding not expected
478 autoBlacklistTestsCustomConfig.transcoding.enabled = true
479 await servers[0].configCommand.updateCustomConfig({ newCustomConfig: autoBlacklistTestsCustomConfig })
480
481 await servers[0].subscriptionsCommand.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
482 await servers[1].subscriptionsCommand.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
483
484 })
485
486 it('Should send notification to moderators on new video with auto-blacklist', async function () {
487 this.timeout(40000)
488
489 videoName = 'video with auto-blacklist ' + buildUUID()
490 const { uuid } = await servers[0].videosCommand.upload({ token: userAccessToken, attributes: { name: videoName } })
491 videoUUID = uuid
492
493 await waitJobs(servers)
494 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence')
495 })
496
497 it('Should not send video publish notification if auto-blacklisted', async function () {
498 await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'absence')
499 })
500
501 it('Should not send a local user subscription notification if auto-blacklisted', async function () {
502 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'absence')
503 })
504
505 it('Should not send a remote user subscription notification if auto-blacklisted', async function () {
506 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'absence')
507 })
508
509 it('Should send video published and unblacklist after video unblacklisted', async function () {
510 this.timeout(40000)
511
512 await servers[0].blacklistCommand.remove({ videoId: videoUUID })
513
514 await waitJobs(servers)
515
516 // FIXME: Can't test as two notifications sent to same user and util only checks last one
517 // One notification might be better anyways
518 // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist')
519 // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence')
520 })
521
522 it('Should send a local user subscription notification after removed from blacklist', async function () {
523 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'presence')
524 })
525
526 it('Should send a remote user subscription notification after removed from blacklist', async function () {
527 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'presence')
528 })
529
530 it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () {
531 this.timeout(40000)
532
533 const updateAt = new Date(new Date().getTime() + 1000000)
534
535 const name = 'video with auto-blacklist and future schedule ' + buildUUID()
536
537 const attributes = {
538 name,
539 privacy: VideoPrivacy.PRIVATE,
540 scheduleUpdate: {
541 updateAt: updateAt.toISOString(),
542 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
543 }
544 }
545
546 const { uuid } = await servers[0].videosCommand.upload({ token: userAccessToken, attributes })
547
548 await servers[0].blacklistCommand.remove({ videoId: uuid })
549
550 await waitJobs(servers)
551 await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist')
552
553 // FIXME: Can't test absence as two notifications sent to same user and util only checks last one
554 // One notification might be better anyways
555 // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
556
557 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
558 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
559 })
560
561 it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () {
562 this.timeout(40000)
563
564 // In 2 seconds
565 const updateAt = new Date(new Date().getTime() + 2000)
566
567 const name = 'video with schedule done and still auto-blacklisted ' + buildUUID()
568
569 const attributes = {
570 name,
571 privacy: VideoPrivacy.PRIVATE,
572 scheduleUpdate: {
573 updateAt: updateAt.toISOString(),
574 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
575 }
576 }
577
578 const { uuid } = await servers[0].videosCommand.upload({ token: userAccessToken, attributes })
579
580 await wait(6000)
581 await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
582 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
583 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
584 })
585
586 it('Should not send a notification to moderators on new video without auto-blacklist', async function () {
587 this.timeout(60000)
588
589 const name = 'video without auto-blacklist ' + buildUUID()
590
591 // admin with blacklist right will not be auto-blacklisted
592 const { uuid } = await servers[0].videosCommand.upload({ attributes: { name } })
593
594 await waitJobs(servers)
595 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence')
596 })
597
598 after(async () => {
599 await servers[0].configCommand.updateCustomConfig({ newCustomConfig: currentCustomConfig })
600
601 await servers[0].subscriptionsCommand.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
602 await servers[1].subscriptionsCommand.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
603 })
604 })
605
606 after(async function () {
607 MockSmtpServer.Instance.kill()
608
609 await cleanupTests(servers)
610 })
611 })