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