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