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