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