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