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