Shared utils -> extra-utils
[oweals/peertube.git] / server / tests / api / notifications / user-notifications.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   addVideoToBlacklist,
7   createUser,
8   doubleFollow,
9   flushAndRunMultipleServers,
10   flushTests,
11   getMyUserInformation,
12   immutableAssign,
13   registerUser,
14   removeVideoFromBlacklist,
15   reportVideoAbuse,
16   updateMyUser,
17   updateVideo,
18   updateVideoChannel,
19   userLogin,
20   wait,
21   getCustomConfig,
22   updateCustomConfig, getVideoThreadComments, getVideoCommentThreads, follow
23 } from '../../../../shared/extra-utils'
24 import { killallServers, ServerInfo, uploadVideo } from '../../../../shared/extra-utils/index'
25 import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
26 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
27 import { getUserNotificationSocket } from '../../../../shared/extra-utils/socket/socket-io'
28 import {
29   checkCommentMention,
30   CheckerBaseParams,
31   checkMyVideoImportIsFinished,
32   checkNewActorFollow,
33   checkNewBlacklistOnMyVideo,
34   checkNewCommentOnMyVideo,
35   checkNewVideoAbuseForModerators,
36   checkVideoAutoBlacklistForModerators,
37   checkNewVideoFromSubscription,
38   checkUserRegistered,
39   checkVideoIsPublished,
40   getLastNotification,
41   getUserNotifications,
42   markAsReadNotifications,
43   updateMyNotificationSettings,
44   markAsReadAllNotifications, checkNewInstanceFollower
45 } from '../../../../shared/extra-utils/users/user-notifications'
46 import {
47   User,
48   UserNotification,
49   UserNotificationSetting,
50   UserNotificationSettingValue,
51   UserNotificationType
52 } from '../../../../shared/models/users'
53 import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
54 import { addUserSubscription, removeUserSubscription } from '../../../../shared/extra-utils/users/user-subscriptions'
55 import { VideoPrivacy } from '../../../../shared/models/videos'
56 import { getBadVideoUrl, getYoutubeVideoUrl, importVideo } from '../../../../shared/extra-utils/videos/video-imports'
57 import { addVideoCommentReply, addVideoCommentThread } from '../../../../shared/extra-utils/videos/video-comments'
58 import * as uuidv4 from 'uuid/v4'
59 import { addAccountToAccountBlocklist, removeAccountFromAccountBlocklist } from '../../../../shared/extra-utils/users/blocklist'
60 import { CustomConfig } from '../../../../shared/models/server'
61 import { VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
62
63 const expect = chai.expect
64
65 async function uploadVideoByRemoteAccount (servers: ServerInfo[], additionalParams: any = {}) {
66   const name = 'remote video ' + uuidv4()
67
68   const data = Object.assign({ name }, additionalParams)
69   const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, data)
70
71   await waitJobs(servers)
72
73   return { uuid: res.body.video.uuid, name }
74 }
75
76 async function uploadVideoByLocalAccount (servers: ServerInfo[], additionalParams: any = {}) {
77   const name = 'local video ' + uuidv4()
78
79   const data = Object.assign({ name }, additionalParams)
80   const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, data)
81
82   await waitJobs(servers)
83
84   return { uuid: res.body.video.uuid, name }
85 }
86
87 describe('Test users notifications', function () {
88   let servers: ServerInfo[] = []
89   let userAccessToken: string
90   let userNotifications: UserNotification[] = []
91   let adminNotifications: UserNotification[] = []
92   let adminNotificationsServer2: UserNotification[] = []
93   const emails: object[] = []
94   let channelId: number
95
96   const allNotificationSettings: UserNotificationSetting = {
97     newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
98     newCommentOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
99     videoAbuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
100     videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
101     blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
102     myVideoImportFinished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
103     myVideoPublished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
104     commentMention: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
105     newFollow: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
106     newUserRegistration: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
107     newInstanceFollower: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
108   }
109
110   before(async function () {
111     this.timeout(120000)
112
113     await MockSmtpServer.Instance.collectEmails(emails)
114
115     await flushTests()
116
117     const overrideConfig = {
118       smtp: {
119         hostname: 'localhost'
120       }
121     }
122     servers = await flushAndRunMultipleServers(3, overrideConfig)
123
124     // Get the access tokens
125     await setAccessTokensToServers(servers)
126
127     // Server 1 and server 2 follow each other
128     await doubleFollow(servers[0], servers[1])
129
130     await waitJobs(servers)
131
132     const user = {
133       username: 'user_1',
134       password: 'super password'
135     }
136     await createUser({
137       url: servers[ 0 ].url,
138       accessToken: servers[ 0 ].accessToken,
139       username: user.username,
140       password: user.password,
141       videoQuota: 10 * 1000 * 1000
142     })
143     userAccessToken = await userLogin(servers[0], user)
144
145     await updateMyNotificationSettings(servers[0].url, userAccessToken, allNotificationSettings)
146     await updateMyNotificationSettings(servers[0].url, servers[0].accessToken, allNotificationSettings)
147     await updateMyNotificationSettings(servers[1].url, servers[1].accessToken, allNotificationSettings)
148
149     {
150       const socket = getUserNotificationSocket(servers[ 0 ].url, userAccessToken)
151       socket.on('new-notification', n => userNotifications.push(n))
152     }
153     {
154       const socket = getUserNotificationSocket(servers[ 0 ].url, servers[0].accessToken)
155       socket.on('new-notification', n => adminNotifications.push(n))
156     }
157     {
158       const socket = getUserNotificationSocket(servers[ 1 ].url, servers[1].accessToken)
159       socket.on('new-notification', n => adminNotificationsServer2.push(n))
160     }
161
162     {
163       const resChannel = await getMyUserInformation(servers[0].url, servers[0].accessToken)
164       channelId = resChannel.body.videoChannels[0].id
165     }
166   })
167
168   describe('New video from my subscription notification', function () {
169     let baseParams: CheckerBaseParams
170
171     before(() => {
172       baseParams = {
173         server: servers[0],
174         emails,
175         socketNotifications: userNotifications,
176         token: userAccessToken
177       }
178     })
179
180     it('Should not send notifications if the user does not follow the video publisher', async function () {
181       this.timeout(10000)
182
183       await uploadVideoByLocalAccount(servers)
184
185       const notification = await getLastNotification(servers[ 0 ].url, userAccessToken)
186       expect(notification).to.be.undefined
187
188       expect(emails).to.have.lengthOf(0)
189       expect(userNotifications).to.have.lengthOf(0)
190     })
191
192     it('Should send a new video notification if the user follows the local video publisher', async function () {
193       this.timeout(15000)
194
195       await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:9001')
196       await waitJobs(servers)
197
198       const { name, uuid } = await uploadVideoByLocalAccount(servers)
199       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
200     })
201
202     it('Should send a new video notification from a remote account', async function () {
203       this.timeout(50000) // Server 2 has transcoding enabled
204
205       await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:9002')
206       await waitJobs(servers)
207
208       const { name, uuid } = await uploadVideoByRemoteAccount(servers)
209       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
210     })
211
212     it('Should send a new video notification on a scheduled publication', async function () {
213       this.timeout(20000)
214
215       // In 2 seconds
216       let updateAt = new Date(new Date().getTime() + 2000)
217
218       const data = {
219         privacy: VideoPrivacy.PRIVATE,
220         scheduleUpdate: {
221           updateAt: updateAt.toISOString(),
222           privacy: VideoPrivacy.PUBLIC
223         }
224       }
225       const { name, uuid } = await uploadVideoByLocalAccount(servers, data)
226
227       await wait(6000)
228       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
229     })
230
231     it('Should send a new video notification on a remote scheduled publication', async function () {
232       this.timeout(50000)
233
234       // In 2 seconds
235       let updateAt = new Date(new Date().getTime() + 2000)
236
237       const data = {
238         privacy: VideoPrivacy.PRIVATE,
239         scheduleUpdate: {
240           updateAt: updateAt.toISOString(),
241           privacy: VideoPrivacy.PUBLIC
242         }
243       }
244       const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
245       await waitJobs(servers)
246
247       await wait(6000)
248       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
249     })
250
251     it('Should not send a notification before the video is published', async function () {
252       this.timeout(20000)
253
254       let updateAt = new Date(new Date().getTime() + 1000000)
255
256       const data = {
257         privacy: VideoPrivacy.PRIVATE,
258         scheduleUpdate: {
259           updateAt: updateAt.toISOString(),
260           privacy: VideoPrivacy.PUBLIC
261         }
262       }
263       const { name, uuid } = await uploadVideoByLocalAccount(servers, data)
264
265       await wait(6000)
266       await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
267     })
268
269     it('Should send a new video notification when a video becomes public', async function () {
270       this.timeout(10000)
271
272       const data = { privacy: VideoPrivacy.PRIVATE }
273       const { name, uuid } = await uploadVideoByLocalAccount(servers, data)
274
275       await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
276
277       await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC })
278
279       await wait(500)
280       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
281     })
282
283     it('Should send a new video notification when a remote video becomes public', async function () {
284       this.timeout(20000)
285
286       const data = { privacy: VideoPrivacy.PRIVATE }
287       const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
288
289       await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
290
291       await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC })
292
293       await waitJobs(servers)
294       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
295     })
296
297     it('Should not send a new video notification when a video becomes unlisted', async function () {
298       this.timeout(20000)
299
300       const data = { privacy: VideoPrivacy.PRIVATE }
301       const { name, uuid } = await uploadVideoByLocalAccount(servers, data)
302
303       await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED })
304
305       await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
306     })
307
308     it('Should not send a new video notification when a remote video becomes unlisted', async function () {
309       this.timeout(20000)
310
311       const data = { privacy: VideoPrivacy.PRIVATE }
312       const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
313
314       await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED })
315
316       await waitJobs(servers)
317       await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
318     })
319
320     it('Should send a new video notification after a video import', async function () {
321       this.timeout(100000)
322
323       const name = 'video import ' + uuidv4()
324
325       const attributes = {
326         name,
327         channelId,
328         privacy: VideoPrivacy.PUBLIC,
329         targetUrl: getYoutubeVideoUrl()
330       }
331       const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
332       const uuid = res.body.video.uuid
333
334       await waitJobs(servers)
335
336       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
337     })
338   })
339
340   describe('Comment on my video notifications', function () {
341     let baseParams: CheckerBaseParams
342
343     before(() => {
344       baseParams = {
345         server: servers[0],
346         emails,
347         socketNotifications: userNotifications,
348         token: userAccessToken
349       }
350     })
351
352     it('Should not send a new comment notification after a comment on another video', async function () {
353       this.timeout(10000)
354
355       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
356       const uuid = resVideo.body.video.uuid
357
358       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
359       const commentId = resComment.body.comment.id
360
361       await wait(500)
362       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
363     })
364
365     it('Should not send a new comment notification if I comment my own video', async function () {
366       this.timeout(10000)
367
368       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
369       const uuid = resVideo.body.video.uuid
370
371       const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, 'comment')
372       const commentId = resComment.body.comment.id
373
374       await wait(500)
375       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
376     })
377
378     it('Should not send a new comment notification if the account is muted', async function () {
379       this.timeout(10000)
380
381       await addAccountToAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root')
382
383       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
384       const uuid = resVideo.body.video.uuid
385
386       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
387       const commentId = resComment.body.comment.id
388
389       await wait(500)
390       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
391
392       await removeAccountFromAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root')
393     })
394
395     it('Should send a new comment notification after a local comment on my video', async function () {
396       this.timeout(10000)
397
398       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
399       const uuid = resVideo.body.video.uuid
400
401       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
402       const commentId = resComment.body.comment.id
403
404       await wait(500)
405       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence')
406     })
407
408     it('Should send a new comment notification after a remote comment on my video', async function () {
409       this.timeout(10000)
410
411       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
412       const uuid = resVideo.body.video.uuid
413
414       await waitJobs(servers)
415
416       await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment')
417
418       await waitJobs(servers)
419
420       const resComment = await getVideoCommentThreads(servers[0].url, uuid, 0, 5)
421       expect(resComment.body.data).to.have.lengthOf(1)
422       const commentId = resComment.body.data[0].id
423
424       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence')
425     })
426
427     it('Should send a new comment notification after a local reply on my video', async function () {
428       this.timeout(10000)
429
430       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
431       const uuid = resVideo.body.video.uuid
432
433       const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
434       const threadId = resThread.body.comment.id
435
436       const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'reply')
437       const commentId = resComment.body.comment.id
438
439       await wait(500)
440       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence')
441     })
442
443     it('Should send a new comment notification after a remote reply on my video', async function () {
444       this.timeout(10000)
445
446       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
447       const uuid = resVideo.body.video.uuid
448       await waitJobs(servers)
449
450       {
451         const resThread = await addVideoCommentThread(servers[ 1 ].url, servers[ 1 ].accessToken, uuid, 'comment')
452         const threadId = resThread.body.comment.id
453         await addVideoCommentReply(servers[ 1 ].url, servers[ 1 ].accessToken, uuid, threadId, 'reply')
454       }
455
456       await waitJobs(servers)
457
458       const resThread = await getVideoCommentThreads(servers[0].url, uuid, 0, 5)
459       expect(resThread.body.data).to.have.lengthOf(1)
460       const threadId = resThread.body.data[0].id
461
462       const resComments = await getVideoThreadComments(servers[0].url, uuid, threadId)
463       const tree = resComments.body as VideoCommentThreadTree
464
465       expect(tree.children).to.have.lengthOf(1)
466       const commentId = tree.children[0].comment.id
467
468       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence')
469     })
470   })
471
472   describe('Mention notifications', function () {
473     let baseParams: CheckerBaseParams
474
475     before(async () => {
476       baseParams = {
477         server: servers[0],
478         emails,
479         socketNotifications: userNotifications,
480         token: userAccessToken
481       }
482
483       await updateMyUser({
484         url: servers[0].url,
485         accessToken: servers[0].accessToken,
486         displayName: 'super root name'
487       })
488
489       await updateMyUser({
490         url: servers[1].url,
491         accessToken: servers[1].accessToken,
492         displayName: 'super root 2 name'
493       })
494     })
495
496     it('Should not send a new mention comment notification if I mention the video owner', async function () {
497       this.timeout(10000)
498
499       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
500       const uuid = resVideo.body.video.uuid
501
502       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello')
503       const commentId = resComment.body.comment.id
504
505       await wait(500)
506       await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
507     })
508
509     it('Should not send a new mention comment notification if I mention myself', async function () {
510       this.timeout(10000)
511
512       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
513       const uuid = resVideo.body.video.uuid
514
515       const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, '@user_1 hello')
516       const commentId = resComment.body.comment.id
517
518       await wait(500)
519       await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
520     })
521
522     it('Should not send a new mention notification if the account is muted', async function () {
523       this.timeout(10000)
524
525       await addAccountToAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root')
526
527       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
528       const uuid = resVideo.body.video.uuid
529
530       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello')
531       const commentId = resComment.body.comment.id
532
533       await wait(500)
534       await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
535
536       await removeAccountFromAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root')
537     })
538
539     it('Should not send a new mention notification if the remote account mention a local account', async function () {
540       this.timeout(20000)
541
542       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
543       const uuid = resVideo.body.video.uuid
544
545       await waitJobs(servers)
546       const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, '@user_1 hello')
547       const threadId = resThread.body.comment.id
548
549       await waitJobs(servers)
550       await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root 2 name', 'absence')
551     })
552
553     it('Should send a new mention notification after local comments', async function () {
554       this.timeout(10000)
555
556       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
557       const uuid = resVideo.body.video.uuid
558
559       const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello 1')
560       const threadId = resThread.body.comment.id
561
562       await wait(500)
563       await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root name', 'presence')
564
565       const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'hello 2 @user_1')
566       const commentId = resComment.body.comment.id
567
568       await wait(500)
569       await checkCommentMention(baseParams, uuid, commentId, threadId, 'super root name', 'presence')
570     })
571
572     it('Should send a new mention notification after remote comments', async function () {
573       this.timeout(20000)
574
575       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
576       const uuid = resVideo.body.video.uuid
577
578       await waitJobs(servers)
579       const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'hello @user_1@localhost:9001 1')
580       const server2ThreadId = resThread.body.comment.id
581
582       await waitJobs(servers)
583
584       const resThread2 = await getVideoCommentThreads(servers[0].url, uuid, 0, 5)
585       expect(resThread2.body.data).to.have.lengthOf(1)
586       const server1ThreadId = resThread2.body.data[0].id
587       await checkCommentMention(baseParams, uuid, server1ThreadId, server1ThreadId, 'super root 2 name', 'presence')
588
589       const text = '@user_1@localhost:9001 hello 2 @root@localhost:9001'
590       await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, server2ThreadId, text)
591
592       await waitJobs(servers)
593
594       const resComments = await getVideoThreadComments(servers[0].url, uuid, server1ThreadId)
595       const tree = resComments.body as VideoCommentThreadTree
596
597       expect(tree.children).to.have.lengthOf(1)
598       const commentId = tree.children[0].comment.id
599
600       await checkCommentMention(baseParams, uuid, commentId, server1ThreadId, 'super root 2 name', 'presence')
601     })
602   })
603
604   describe('Video abuse for moderators notification' , function () {
605     let baseParams: CheckerBaseParams
606
607     before(() => {
608       baseParams = {
609         server: servers[0],
610         emails,
611         socketNotifications: adminNotifications,
612         token: servers[0].accessToken
613       }
614     })
615
616     it('Should send a notification to moderators on local video abuse', async function () {
617       this.timeout(10000)
618
619       const name = 'video for abuse ' + uuidv4()
620       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
621       const uuid = resVideo.body.video.uuid
622
623       await reportVideoAbuse(servers[0].url, servers[0].accessToken, uuid, 'super reason')
624
625       await waitJobs(servers)
626       await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence')
627     })
628
629     it('Should send a notification to moderators on remote video abuse', async function () {
630       this.timeout(10000)
631
632       const name = 'video for abuse ' + uuidv4()
633       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
634       const uuid = resVideo.body.video.uuid
635
636       await waitJobs(servers)
637
638       await reportVideoAbuse(servers[1].url, servers[1].accessToken, uuid, 'super reason')
639
640       await waitJobs(servers)
641       await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence')
642     })
643   })
644
645   describe('Video blacklist on my video', function () {
646     let baseParams: CheckerBaseParams
647
648     before(() => {
649       baseParams = {
650         server: servers[0],
651         emails,
652         socketNotifications: userNotifications,
653         token: userAccessToken
654       }
655     })
656
657     it('Should send a notification to video owner on blacklist', async function () {
658       this.timeout(10000)
659
660       const name = 'video for abuse ' + uuidv4()
661       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
662       const uuid = resVideo.body.video.uuid
663
664       await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
665
666       await waitJobs(servers)
667       await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist')
668     })
669
670     it('Should send a notification to video owner on unblacklist', async function () {
671       this.timeout(10000)
672
673       const name = 'video for abuse ' + uuidv4()
674       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
675       const uuid = resVideo.body.video.uuid
676
677       await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
678
679       await waitJobs(servers)
680       await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
681       await waitJobs(servers)
682
683       await wait(500)
684       await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist')
685     })
686   })
687
688   describe('My video is published', function () {
689     let baseParams: CheckerBaseParams
690
691     before(() => {
692       baseParams = {
693         server: servers[1],
694         emails,
695         socketNotifications: adminNotificationsServer2,
696         token: servers[1].accessToken
697       }
698     })
699
700     it('Should not send a notification if transcoding is not enabled', async function () {
701       this.timeout(10000)
702
703       const { name, uuid } = await uploadVideoByLocalAccount(servers)
704       await waitJobs(servers)
705
706       await checkVideoIsPublished(baseParams, name, uuid, 'absence')
707     })
708
709     it('Should not send a notification if the wait transcoding is false', async function () {
710       this.timeout(50000)
711
712       await uploadVideoByRemoteAccount(servers, { waitTranscoding: false })
713       await waitJobs(servers)
714
715       const notification = await getLastNotification(servers[ 0 ].url, userAccessToken)
716       if (notification) {
717         expect(notification.type).to.not.equal(UserNotificationType.MY_VIDEO_PUBLISHED)
718       }
719     })
720
721     it('Should send a notification even if the video is not transcoded in other resolutions', async function () {
722       this.timeout(50000)
723
724       const { name, uuid } = await uploadVideoByRemoteAccount(servers, { waitTranscoding: true, fixture: 'video_short_240p.mp4' })
725       await waitJobs(servers)
726
727       await checkVideoIsPublished(baseParams, name, uuid, 'presence')
728     })
729
730     it('Should send a notification with a transcoded video', async function () {
731       this.timeout(50000)
732
733       const { name, uuid } = await uploadVideoByRemoteAccount(servers, { waitTranscoding: true })
734       await waitJobs(servers)
735
736       await checkVideoIsPublished(baseParams, name, uuid, 'presence')
737     })
738
739     it('Should send a notification when an imported video is transcoded', async function () {
740       this.timeout(50000)
741
742       const name = 'video import ' + uuidv4()
743
744       const attributes = {
745         name,
746         channelId,
747         privacy: VideoPrivacy.PUBLIC,
748         targetUrl: getYoutubeVideoUrl(),
749         waitTranscoding: true
750       }
751       const res = await importVideo(servers[1].url, servers[1].accessToken, attributes)
752       const uuid = res.body.video.uuid
753
754       await waitJobs(servers)
755       await checkVideoIsPublished(baseParams, name, uuid, 'presence')
756     })
757
758     it('Should send a notification when the scheduled update has been proceeded', async function () {
759       this.timeout(70000)
760
761       // In 2 seconds
762       let updateAt = new Date(new Date().getTime() + 2000)
763
764       const data = {
765         privacy: VideoPrivacy.PRIVATE,
766         scheduleUpdate: {
767           updateAt: updateAt.toISOString(),
768           privacy: VideoPrivacy.PUBLIC
769         }
770       }
771       const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
772
773       await wait(6000)
774       await checkVideoIsPublished(baseParams, name, uuid, 'presence')
775     })
776
777     it('Should not send a notification before the video is published', async function () {
778       this.timeout(20000)
779
780       let updateAt = new Date(new Date().getTime() + 100000)
781
782       const data = {
783         privacy: VideoPrivacy.PRIVATE,
784         scheduleUpdate: {
785           updateAt: updateAt.toISOString(),
786           privacy: VideoPrivacy.PUBLIC
787         }
788       }
789       const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
790
791       await wait(6000)
792       await checkVideoIsPublished(baseParams, name, uuid, 'absence')
793     })
794   })
795
796   describe('My video is imported', function () {
797     let baseParams: CheckerBaseParams
798
799     before(() => {
800       baseParams = {
801         server: servers[0],
802         emails,
803         socketNotifications: adminNotifications,
804         token: servers[0].accessToken
805       }
806     })
807
808     it('Should send a notification when the video import failed', async function () {
809       this.timeout(70000)
810
811       const name = 'video import ' + uuidv4()
812
813       const attributes = {
814         name,
815         channelId,
816         privacy: VideoPrivacy.PRIVATE,
817         targetUrl: getBadVideoUrl()
818       }
819       const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
820       const uuid = res.body.video.uuid
821
822       await waitJobs(servers)
823       await checkMyVideoImportIsFinished(baseParams, name, uuid, getBadVideoUrl(), false, 'presence')
824     })
825
826     it('Should send a notification when the video import succeeded', async function () {
827       this.timeout(70000)
828
829       const name = 'video import ' + uuidv4()
830
831       const attributes = {
832         name,
833         channelId,
834         privacy: VideoPrivacy.PRIVATE,
835         targetUrl: getYoutubeVideoUrl()
836       }
837       const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
838       const uuid = res.body.video.uuid
839
840       await waitJobs(servers)
841       await checkMyVideoImportIsFinished(baseParams, name, uuid, getYoutubeVideoUrl(), true, 'presence')
842     })
843   })
844
845   describe('New registration', function () {
846     let baseParams: CheckerBaseParams
847
848     before(() => {
849       baseParams = {
850         server: servers[0],
851         emails,
852         socketNotifications: adminNotifications,
853         token: servers[0].accessToken
854       }
855     })
856
857     it('Should send a notification only to moderators when a user registers on the instance', async function () {
858       this.timeout(10000)
859
860       await registerUser(servers[0].url, 'user_45', 'password')
861
862       await waitJobs(servers)
863
864       await checkUserRegistered(baseParams, 'user_45', 'presence')
865
866       const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
867       await checkUserRegistered(immutableAssign(baseParams, userOverride), 'user_45', 'absence')
868     })
869   })
870
871   describe('New instance follower', function () {
872     let baseParams: CheckerBaseParams
873
874     before(async () => {
875       baseParams = {
876         server: servers[0],
877         emails,
878         socketNotifications: adminNotifications,
879         token: servers[0].accessToken
880       }
881     })
882
883     it('Should send a notification only to admin when there is a new instance follower', async function () {
884       this.timeout(10000)
885
886       await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
887
888       await waitJobs(servers)
889
890       await checkNewInstanceFollower(baseParams, 'localhost:9003', 'presence')
891
892       const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
893       await checkNewInstanceFollower(immutableAssign(baseParams, userOverride), 'localhost:9003', 'absence')
894     })
895   })
896
897   describe('New actor follow', function () {
898     let baseParams: CheckerBaseParams
899     let myChannelName = 'super channel name'
900     let myUserName = 'super user name'
901
902     before(async () => {
903       baseParams = {
904         server: servers[0],
905         emails,
906         socketNotifications: userNotifications,
907         token: userAccessToken
908       }
909
910       await updateMyUser({
911         url: servers[0].url,
912         accessToken: servers[0].accessToken,
913         displayName: 'super root name'
914       })
915
916       await updateMyUser({
917         url: servers[0].url,
918         accessToken: userAccessToken,
919         displayName: myUserName
920       })
921
922       await updateMyUser({
923         url: servers[1].url,
924         accessToken: servers[1].accessToken,
925         displayName: 'super root 2 name'
926       })
927
928       await updateVideoChannel(servers[0].url, userAccessToken, 'user_1_channel', { displayName: myChannelName })
929     })
930
931     it('Should notify when a local channel is following one of our channel', async function () {
932       this.timeout(10000)
933
934       await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:9001')
935       await waitJobs(servers)
936
937       await checkNewActorFollow(baseParams, 'channel', 'root', 'super root name', myChannelName, 'presence')
938
939       await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:9001')
940     })
941
942     it('Should notify when a remote channel is following one of our channel', async function () {
943       this.timeout(10000)
944
945       await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:9001')
946       await waitJobs(servers)
947
948       await checkNewActorFollow(baseParams, 'channel', 'root', 'super root 2 name', myChannelName, 'presence')
949
950       await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:9001')
951     })
952
953     it('Should notify when a local account is following one of our channel', async function () {
954       this.timeout(10000)
955
956       await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1@localhost:9001')
957
958       await waitJobs(servers)
959
960       await checkNewActorFollow(baseParams, 'account', 'root', 'super root name', myUserName, 'presence')
961     })
962
963     it('Should notify when a remote account is following one of our channel', async function () {
964       this.timeout(10000)
965
966       await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1@localhost:9001')
967
968       await waitJobs(servers)
969
970       await checkNewActorFollow(baseParams, 'account', 'root', 'super root 2 name', myUserName, 'presence')
971     })
972   })
973
974   describe('Video-related notifications when video auto-blacklist is enabled', function () {
975     let userBaseParams: CheckerBaseParams
976     let adminBaseParamsServer1: CheckerBaseParams
977     let adminBaseParamsServer2: CheckerBaseParams
978     let videoUUID: string
979     let videoName: string
980     let currentCustomConfig: CustomConfig
981
982     before(async () => {
983
984       adminBaseParamsServer1 = {
985         server: servers[0],
986         emails,
987         socketNotifications: adminNotifications,
988         token: servers[0].accessToken
989       }
990
991       adminBaseParamsServer2 = {
992         server: servers[1],
993         emails,
994         socketNotifications: adminNotificationsServer2,
995         token: servers[1].accessToken
996       }
997
998       userBaseParams = {
999         server: servers[0],
1000         emails,
1001         socketNotifications: userNotifications,
1002         token: userAccessToken
1003       }
1004
1005       const resCustomConfig = await getCustomConfig(servers[0].url, servers[0].accessToken)
1006       currentCustomConfig = resCustomConfig.body
1007       const autoBlacklistTestsCustomConfig = immutableAssign(currentCustomConfig, {
1008         autoBlacklist: {
1009           videos: {
1010             ofUsers: {
1011               enabled: true
1012             }
1013           }
1014         }
1015       })
1016       // enable transcoding otherwise own publish notification after transcoding not expected
1017       autoBlacklistTestsCustomConfig.transcoding.enabled = true
1018       await updateCustomConfig(servers[0].url, servers[0].accessToken, autoBlacklistTestsCustomConfig)
1019
1020       await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:9001')
1021       await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:9001')
1022
1023     })
1024
1025     it('Should send notification to moderators on new video with auto-blacklist', async function () {
1026       this.timeout(20000)
1027
1028       videoName = 'video with auto-blacklist ' + uuidv4()
1029       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: videoName })
1030       videoUUID = resVideo.body.video.uuid
1031
1032       await waitJobs(servers)
1033       await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence')
1034     })
1035
1036     it('Should not send video publish notification if auto-blacklisted', async function () {
1037       await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'absence')
1038     })
1039
1040     it('Should not send a local user subscription notification if auto-blacklisted', async function () {
1041       await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'absence')
1042     })
1043
1044     it('Should not send a remote user subscription notification if auto-blacklisted', async function () {
1045       await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'absence')
1046     })
1047
1048     it('Should send video published and unblacklist after video unblacklisted', async function () {
1049       this.timeout(20000)
1050
1051       await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoUUID)
1052
1053       await waitJobs(servers)
1054
1055       // FIXME: Can't test as two notifications sent to same user and util only checks last one
1056       // One notification might be better anyways
1057       // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist')
1058       // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence')
1059     })
1060
1061     it('Should send a local user subscription notification after removed from blacklist', async function () {
1062       await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'presence')
1063     })
1064
1065     it('Should send a remote user subscription notification after removed from blacklist', async function () {
1066       await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'presence')
1067     })
1068
1069     it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () {
1070       this.timeout(20000)
1071
1072       let updateAt = new Date(new Date().getTime() + 100000)
1073
1074       const name = 'video with auto-blacklist and future schedule ' + uuidv4()
1075
1076       const data = {
1077         name,
1078         privacy: VideoPrivacy.PRIVATE,
1079         scheduleUpdate: {
1080           updateAt: updateAt.toISOString(),
1081           privacy: VideoPrivacy.PUBLIC
1082         }
1083       }
1084
1085       const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
1086       const uuid = resVideo.body.video.uuid
1087
1088       await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
1089
1090       await waitJobs(servers)
1091       await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist')
1092
1093       // FIXME: Can't test absence as two notifications sent to same user and util only checks last one
1094       // One notification might be better anyways
1095       // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
1096
1097       await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
1098       await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
1099     })
1100
1101     it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () {
1102       this.timeout(20000)
1103
1104       // In 2 seconds
1105       let updateAt = new Date(new Date().getTime() + 2000)
1106
1107       const name = 'video with schedule done and still auto-blacklisted ' + uuidv4()
1108
1109       const data = {
1110         name,
1111         privacy: VideoPrivacy.PRIVATE,
1112         scheduleUpdate: {
1113           updateAt: updateAt.toISOString(),
1114           privacy: VideoPrivacy.PUBLIC
1115         }
1116       }
1117
1118       const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
1119       const uuid = resVideo.body.video.uuid
1120
1121       await wait(6000)
1122       await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
1123       await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
1124       await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
1125     })
1126
1127     it('Should not send a notification to moderators on new video without auto-blacklist', async function () {
1128       this.timeout(20000)
1129
1130       const name = 'video without auto-blacklist ' + uuidv4()
1131
1132       // admin with blacklist right will not be auto-blacklisted
1133       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name })
1134       const uuid = resVideo.body.video.uuid
1135
1136       await waitJobs(servers)
1137       await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence')
1138     })
1139
1140     after(async () => {
1141       await updateCustomConfig(servers[0].url, servers[0].accessToken, currentCustomConfig)
1142
1143       await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:9001')
1144       await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:9001')
1145     })
1146   })
1147
1148   describe('Mark as read', function () {
1149     it('Should mark as read some notifications', async function () {
1150       const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 2, 3)
1151       const ids = res.body.data.map(n => n.id)
1152
1153       await markAsReadNotifications(servers[ 0 ].url, userAccessToken, ids)
1154     })
1155
1156     it('Should have the notifications marked as read', async function () {
1157       const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10)
1158
1159       const notifications = res.body.data as UserNotification[]
1160       expect(notifications[ 0 ].read).to.be.false
1161       expect(notifications[ 1 ].read).to.be.false
1162       expect(notifications[ 2 ].read).to.be.true
1163       expect(notifications[ 3 ].read).to.be.true
1164       expect(notifications[ 4 ].read).to.be.true
1165       expect(notifications[ 5 ].read).to.be.false
1166     })
1167
1168     it('Should only list read notifications', async function () {
1169       const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10, false)
1170
1171       const notifications = res.body.data as UserNotification[]
1172       for (const notification of notifications) {
1173         expect(notification.read).to.be.true
1174       }
1175     })
1176
1177     it('Should only list unread notifications', async function () {
1178       const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10, true)
1179
1180       const notifications = res.body.data as UserNotification[]
1181       for (const notification of notifications) {
1182         expect(notification.read).to.be.false
1183       }
1184     })
1185
1186     it('Should mark as read all notifications', async function () {
1187       await markAsReadAllNotifications(servers[ 0 ].url, userAccessToken)
1188
1189       const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10, true)
1190
1191       expect(res.body.total).to.equal(0)
1192       expect(res.body.data).to.have.lengthOf(0)
1193     })
1194   })
1195
1196   describe('Notification settings', function () {
1197     let baseParams: CheckerBaseParams
1198
1199     before(() => {
1200       baseParams = {
1201         server: servers[0],
1202         emails,
1203         socketNotifications: userNotifications,
1204         token: userAccessToken
1205       }
1206     })
1207
1208     it('Should not have notifications', async function () {
1209       this.timeout(20000)
1210
1211       await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, {
1212         newVideoFromSubscription: UserNotificationSettingValue.NONE
1213       }))
1214
1215       {
1216         const res = await getMyUserInformation(servers[0].url, userAccessToken)
1217         const info = res.body as User
1218         expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
1219       }
1220
1221       const { name, uuid } = await uploadVideoByLocalAccount(servers)
1222
1223       const check = { web: true, mail: true }
1224       await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
1225     })
1226
1227     it('Should only have web notifications', async function () {
1228       this.timeout(20000)
1229
1230       await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, {
1231         newVideoFromSubscription: UserNotificationSettingValue.WEB
1232       }))
1233
1234       {
1235         const res = await getMyUserInformation(servers[0].url, userAccessToken)
1236         const info = res.body as User
1237         expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
1238       }
1239
1240       const { name, uuid } = await uploadVideoByLocalAccount(servers)
1241
1242       {
1243         const check = { mail: true, web: false }
1244         await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
1245       }
1246
1247       {
1248         const check = { mail: false, web: true }
1249         await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
1250       }
1251     })
1252
1253     it('Should only have mail notifications', async function () {
1254       this.timeout(20000)
1255
1256       await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, {
1257         newVideoFromSubscription: UserNotificationSettingValue.EMAIL
1258       }))
1259
1260       {
1261         const res = await getMyUserInformation(servers[0].url, userAccessToken)
1262         const info = res.body as User
1263         expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
1264       }
1265
1266       const { name, uuid } = await uploadVideoByLocalAccount(servers)
1267
1268       {
1269         const check = { mail: false, web: true }
1270         await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
1271       }
1272
1273       {
1274         const check = { mail: true, web: false }
1275         await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
1276       }
1277     })
1278
1279     it('Should have email and web notifications', async function () {
1280       this.timeout(20000)
1281
1282       await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, {
1283         newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
1284       }))
1285
1286       {
1287         const res = await getMyUserInformation(servers[0].url, userAccessToken)
1288         const info = res.body as User
1289         expect(info.notificationSettings.newVideoFromSubscription).to.equal(
1290           UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
1291         )
1292       }
1293
1294       const { name, uuid } = await uploadVideoByLocalAccount(servers)
1295
1296       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
1297     })
1298   })
1299
1300   after(async function () {
1301     MockSmtpServer.Instance.kill()
1302
1303     killallServers(servers)
1304   })
1305 })