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