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