ae77b4db221bd3087235b562787e2702e82f8012
[oweals/peertube.git] / server / tests / api / users / 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 } from '../../../../shared/utils'
22 import { killallServers, ServerInfo, uploadVideo } from '../../../../shared/utils/index'
23 import { setAccessTokensToServers } from '../../../../shared/utils/users/login'
24 import { waitJobs } from '../../../../shared/utils/server/jobs'
25 import { getUserNotificationSocket } from '../../../../shared/utils/socket/socket-io'
26 import {
27   checkCommentMention,
28   CheckerBaseParams,
29   checkMyVideoImportIsFinished,
30   checkNewActorFollow,
31   checkNewBlacklistOnMyVideo,
32   checkNewCommentOnMyVideo,
33   checkNewVideoAbuseForModerators,
34   checkNewVideoFromSubscription,
35   checkUserRegistered,
36   checkVideoIsPublished,
37   getLastNotification,
38   getUserNotifications,
39   markAsReadNotifications,
40   updateMyNotificationSettings
41 } from '../../../../shared/utils/users/user-notifications'
42 import {
43   User,
44   UserNotification,
45   UserNotificationSetting,
46   UserNotificationSettingValue,
47   UserNotificationType
48 } from '../../../../shared/models/users'
49 import { MockSmtpServer } from '../../../../shared/utils/miscs/email'
50 import { addUserSubscription, removeUserSubscription } from '../../../../shared/utils/users/user-subscriptions'
51 import { VideoPrivacy } from '../../../../shared/models/videos'
52 import { getBadVideoUrl, getYoutubeVideoUrl, importVideo } from '../../../../shared/utils/videos/video-imports'
53 import { addVideoCommentReply, addVideoCommentThread } from '../../../../shared/utils/videos/video-comments'
54 import * as uuidv4 from 'uuid/v4'
55 import { addAccountToAccountBlocklist, removeAccountFromAccountBlocklist } from '../../../../shared/utils/users/blocklist'
56
57 const expect = chai.expect
58
59 async function uploadVideoByRemoteAccount (servers: ServerInfo[], additionalParams: any = {}) {
60   const name = 'remote video ' + uuidv4()
61
62   const data = Object.assign({ name }, additionalParams)
63   const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, data)
64
65   await waitJobs(servers)
66
67   return { uuid: res.body.video.uuid, name }
68 }
69
70 async function uploadVideoByLocalAccount (servers: ServerInfo[], additionalParams: any = {}) {
71   const name = 'local video ' + uuidv4()
72
73   const data = Object.assign({ name }, additionalParams)
74   const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, data)
75
76   await waitJobs(servers)
77
78   return { uuid: res.body.video.uuid, name }
79 }
80
81 describe('Test users notifications', function () {
82   let servers: ServerInfo[] = []
83   let userAccessToken: string
84   let userNotifications: UserNotification[] = []
85   let adminNotifications: UserNotification[] = []
86   let adminNotificationsServer2: UserNotification[] = []
87   const emails: object[] = []
88   let channelId: number
89
90   const allNotificationSettings: UserNotificationSetting = {
91     newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL,
92     newCommentOnMyVideo: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL,
93     videoAbuseAsModerator: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL,
94     blacklistOnMyVideo: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL,
95     myVideoImportFinished: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL,
96     myVideoPublished: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL,
97     commentMention: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL,
98     newFollow: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL,
99     newUserRegistration: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL
100   }
101
102   before(async function () {
103     this.timeout(120000)
104
105     await MockSmtpServer.Instance.collectEmails(emails)
106
107     await flushTests()
108
109     const overrideConfig = {
110       smtp: {
111         hostname: 'localhost'
112       }
113     }
114     servers = await flushAndRunMultipleServers(2, overrideConfig)
115
116     // Get the access tokens
117     await setAccessTokensToServers(servers)
118
119     // Server 1 and server 2 follow each other
120     await doubleFollow(servers[0], servers[1])
121
122     await waitJobs(servers)
123
124     const user = {
125       username: 'user_1',
126       password: 'super password'
127     }
128     await createUser(servers[0].url, servers[0].accessToken, user.username, user.password, 10 * 1000 * 1000)
129     userAccessToken = await userLogin(servers[0], user)
130
131     await updateMyNotificationSettings(servers[0].url, userAccessToken, allNotificationSettings)
132     await updateMyNotificationSettings(servers[0].url, servers[0].accessToken, allNotificationSettings)
133     await updateMyNotificationSettings(servers[1].url, servers[1].accessToken, allNotificationSettings)
134
135     {
136       const socket = getUserNotificationSocket(servers[ 0 ].url, userAccessToken)
137       socket.on('new-notification', n => userNotifications.push(n))
138     }
139     {
140       const socket = getUserNotificationSocket(servers[ 0 ].url, servers[0].accessToken)
141       socket.on('new-notification', n => adminNotifications.push(n))
142     }
143     {
144       const socket = getUserNotificationSocket(servers[ 1 ].url, servers[1].accessToken)
145       socket.on('new-notification', n => adminNotificationsServer2.push(n))
146     }
147
148     {
149       const resChannel = await getMyUserInformation(servers[0].url, servers[0].accessToken)
150       channelId = resChannel.body.videoChannels[0].id
151     }
152   })
153
154   describe('New video from my subscription notification', function () {
155     let baseParams: CheckerBaseParams
156
157     before(() => {
158       baseParams = {
159         server: servers[0],
160         emails,
161         socketNotifications: userNotifications,
162         token: userAccessToken
163       }
164     })
165
166     it('Should not send notifications if the user does not follow the video publisher', async function () {
167       await uploadVideoByLocalAccount(servers)
168
169       const notification = await getLastNotification(servers[ 0 ].url, userAccessToken)
170       expect(notification).to.be.undefined
171
172       expect(emails).to.have.lengthOf(0)
173       expect(userNotifications).to.have.lengthOf(0)
174     })
175
176     it('Should send a new video notification if the user follows the local video publisher', async function () {
177       await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:9001')
178
179       const { name, uuid } = await uploadVideoByLocalAccount(servers)
180       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
181     })
182
183     it('Should send a new video notification from a remote account', async function () {
184       this.timeout(50000) // Server 2 has transcoding enabled
185
186       await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:9002')
187
188       const { name, uuid } = await uploadVideoByRemoteAccount(servers)
189       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
190     })
191
192     it('Should send a new video notification on a scheduled publication', async function () {
193       this.timeout(20000)
194
195       // In 2 seconds
196       let updateAt = new Date(new Date().getTime() + 2000)
197
198       const data = {
199         privacy: VideoPrivacy.PRIVATE,
200         scheduleUpdate: {
201           updateAt: updateAt.toISOString(),
202           privacy: VideoPrivacy.PUBLIC
203         }
204       }
205       const { name, uuid } = await uploadVideoByLocalAccount(servers, data)
206
207       await wait(6000)
208       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
209     })
210
211     it('Should send a new video notification on a remote scheduled publication', async function () {
212       this.timeout(20000)
213
214       // In 2 seconds
215       let updateAt = new Date(new Date().getTime() + 2000)
216
217       const data = {
218         privacy: VideoPrivacy.PRIVATE,
219         scheduleUpdate: {
220           updateAt: updateAt.toISOString(),
221           privacy: VideoPrivacy.PUBLIC
222         }
223       }
224       const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
225       await waitJobs(servers)
226
227       await wait(6000)
228       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
229     })
230
231     it('Should not send a notification before the video is published', async function () {
232       this.timeout(20000)
233
234       let updateAt = new Date(new Date().getTime() + 100000)
235
236       const data = {
237         privacy: VideoPrivacy.PRIVATE,
238         scheduleUpdate: {
239           updateAt: updateAt.toISOString(),
240           privacy: VideoPrivacy.PUBLIC
241         }
242       }
243       const { name, uuid } = await uploadVideoByLocalAccount(servers, data)
244
245       await wait(6000)
246       await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
247     })
248
249     it('Should send a new video notification when a video becomes public', async function () {
250       this.timeout(10000)
251
252       const data = { privacy: VideoPrivacy.PRIVATE }
253       const { name, uuid } = await uploadVideoByLocalAccount(servers, data)
254
255       await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
256
257       await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC })
258
259       await wait(500)
260       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
261     })
262
263     it('Should send a new video notification when a remote video becomes public', async function () {
264       this.timeout(20000)
265
266       const data = { privacy: VideoPrivacy.PRIVATE }
267       const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
268
269       await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
270
271       await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC })
272
273       await waitJobs(servers)
274       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
275     })
276
277     it('Should not send a new video notification when a video becomes unlisted', async function () {
278       this.timeout(20000)
279
280       const data = { privacy: VideoPrivacy.PRIVATE }
281       const { name, uuid } = await uploadVideoByLocalAccount(servers, data)
282
283       await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED })
284
285       await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
286     })
287
288     it('Should not send a new video notification when a remote video becomes unlisted', async function () {
289       this.timeout(20000)
290
291       const data = { privacy: VideoPrivacy.PRIVATE }
292       const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
293
294       await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED })
295
296       await waitJobs(servers)
297       await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
298     })
299
300     it('Should send a new video notification after a video import', async function () {
301       this.timeout(30000)
302
303       const name = 'video import ' + uuidv4()
304
305       const attributes = {
306         name,
307         channelId,
308         privacy: VideoPrivacy.PUBLIC,
309         targetUrl: getYoutubeVideoUrl()
310       }
311       const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
312       const uuid = res.body.video.uuid
313
314       await waitJobs(servers)
315
316       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
317     })
318   })
319
320   describe('Comment on my video notifications', function () {
321     let baseParams: CheckerBaseParams
322
323     before(() => {
324       baseParams = {
325         server: servers[0],
326         emails,
327         socketNotifications: userNotifications,
328         token: userAccessToken
329       }
330     })
331
332     it('Should not send a new comment notification after a comment on another video', async function () {
333       this.timeout(10000)
334
335       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
336       const uuid = resVideo.body.video.uuid
337
338       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
339       const commentId = resComment.body.comment.id
340
341       await wait(500)
342       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
343     })
344
345     it('Should not send a new comment notification if I comment my own video', async function () {
346       this.timeout(10000)
347
348       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
349       const uuid = resVideo.body.video.uuid
350
351       const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, 'comment')
352       const commentId = resComment.body.comment.id
353
354       await wait(500)
355       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
356     })
357
358     it('Should not send a new comment notification if the account is muted', async function () {
359       this.timeout(10000)
360
361       await addAccountToAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root')
362
363       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
364       const uuid = resVideo.body.video.uuid
365
366       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
367       const commentId = resComment.body.comment.id
368
369       await wait(500)
370       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
371
372       await removeAccountFromAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root')
373     })
374
375     it('Should send a new comment notification after a local comment on my video', async function () {
376       this.timeout(10000)
377
378       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
379       const uuid = resVideo.body.video.uuid
380
381       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
382       const commentId = resComment.body.comment.id
383
384       await wait(500)
385       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence')
386     })
387
388     it('Should send a new comment notification after a remote comment on my video', async function () {
389       this.timeout(10000)
390
391       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
392       const uuid = resVideo.body.video.uuid
393
394       await waitJobs(servers)
395
396       const resComment = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment')
397       const commentId = resComment.body.comment.id
398
399       await waitJobs(servers)
400       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence')
401     })
402
403     it('Should send a new comment notification after a local reply on my video', async function () {
404       this.timeout(10000)
405
406       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
407       const uuid = resVideo.body.video.uuid
408
409       const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
410       const threadId = resThread.body.comment.id
411
412       const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'reply')
413       const commentId = resComment.body.comment.id
414
415       await wait(500)
416       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence')
417     })
418
419     it('Should send a new comment notification after a remote reply on my video', async function () {
420       this.timeout(10000)
421
422       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
423       const uuid = resVideo.body.video.uuid
424       await waitJobs(servers)
425
426       const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment')
427       const threadId = resThread.body.comment.id
428
429       const resComment = await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, threadId, 'reply')
430       const commentId = resComment.body.comment.id
431
432       await waitJobs(servers)
433       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence')
434     })
435   })
436
437   describe('Mention notifications', function () {
438     let baseParams: CheckerBaseParams
439
440     before(async () => {
441       baseParams = {
442         server: servers[0],
443         emails,
444         socketNotifications: userNotifications,
445         token: userAccessToken
446       }
447
448       await updateMyUser({
449         url: servers[0].url,
450         accessToken: servers[0].accessToken,
451         displayName: 'super root name'
452       })
453
454       await updateMyUser({
455         url: servers[1].url,
456         accessToken: servers[1].accessToken,
457         displayName: 'super root 2 name'
458       })
459     })
460
461     it('Should not send a new mention comment notification if I mention the video owner', async function () {
462       this.timeout(10000)
463
464       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
465       const uuid = resVideo.body.video.uuid
466
467       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello')
468       const commentId = resComment.body.comment.id
469
470       await wait(500)
471       await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
472     })
473
474     it('Should not send a new mention comment notification if I mention myself', async function () {
475       this.timeout(10000)
476
477       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
478       const uuid = resVideo.body.video.uuid
479
480       const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, '@user_1 hello')
481       const commentId = resComment.body.comment.id
482
483       await wait(500)
484       await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
485     })
486
487     it('Should not send a new mention notification if the account is muted', async function () {
488       this.timeout(10000)
489
490       await addAccountToAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root')
491
492       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
493       const uuid = resVideo.body.video.uuid
494
495       const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello')
496       const commentId = resComment.body.comment.id
497
498       await wait(500)
499       await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
500
501       await removeAccountFromAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root')
502     })
503
504     it('Should send a new mention notification after local comments', async function () {
505       this.timeout(10000)
506
507       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
508       const uuid = resVideo.body.video.uuid
509
510       const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello 1')
511       const threadId = resThread.body.comment.id
512
513       await wait(500)
514       await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root name', 'presence')
515
516       const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'hello 2 @user_1')
517       const commentId = resComment.body.comment.id
518
519       await wait(500)
520       await checkCommentMention(baseParams, uuid, commentId, threadId, 'super root name', 'presence')
521     })
522
523     it('Should send a new mention notification after remote comments', async function () {
524       this.timeout(20000)
525
526       const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
527       const uuid = resVideo.body.video.uuid
528
529       await waitJobs(servers)
530       const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'hello @user_1@localhost:9001 1')
531       const threadId = resThread.body.comment.id
532
533       await waitJobs(servers)
534       await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root 2 name', 'presence')
535
536       const text = '@user_1@localhost:9001 hello 2 @root@localhost:9001'
537       const resComment = await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, threadId, text)
538       const commentId = resComment.body.comment.id
539
540       await waitJobs(servers)
541       await checkCommentMention(baseParams, uuid, commentId, threadId, 'super root 2 name', 'presence')
542     })
543   })
544
545   describe('Video abuse for moderators notification' , function () {
546     let baseParams: CheckerBaseParams
547
548     before(() => {
549       baseParams = {
550         server: servers[0],
551         emails,
552         socketNotifications: adminNotifications,
553         token: servers[0].accessToken
554       }
555     })
556
557     it('Should send a notification to moderators on local video abuse', async function () {
558       this.timeout(10000)
559
560       const name = 'video for abuse ' + uuidv4()
561       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
562       const uuid = resVideo.body.video.uuid
563
564       await reportVideoAbuse(servers[0].url, servers[0].accessToken, uuid, 'super reason')
565
566       await waitJobs(servers)
567       await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence')
568     })
569
570     it('Should send a notification to moderators on remote video abuse', async function () {
571       this.timeout(10000)
572
573       const name = 'video for abuse ' + uuidv4()
574       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
575       const uuid = resVideo.body.video.uuid
576
577       await waitJobs(servers)
578
579       await reportVideoAbuse(servers[1].url, servers[1].accessToken, uuid, 'super reason')
580
581       await waitJobs(servers)
582       await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence')
583     })
584   })
585
586   describe('Video blacklist on my video', function () {
587     let baseParams: CheckerBaseParams
588
589     before(() => {
590       baseParams = {
591         server: servers[0],
592         emails,
593         socketNotifications: userNotifications,
594         token: userAccessToken
595       }
596     })
597
598     it('Should send a notification to video owner on blacklist', async function () {
599       this.timeout(10000)
600
601       const name = 'video for abuse ' + uuidv4()
602       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
603       const uuid = resVideo.body.video.uuid
604
605       await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
606
607       await waitJobs(servers)
608       await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist')
609     })
610
611     it('Should send a notification to video owner on unblacklist', async function () {
612       this.timeout(10000)
613
614       const name = 'video for abuse ' + uuidv4()
615       const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
616       const uuid = resVideo.body.video.uuid
617
618       await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
619
620       await waitJobs(servers)
621       await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
622       await waitJobs(servers)
623
624       await wait(500)
625       await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist')
626     })
627   })
628
629   describe('My video is published', function () {
630     let baseParams: CheckerBaseParams
631
632     before(() => {
633       baseParams = {
634         server: servers[1],
635         emails,
636         socketNotifications: adminNotificationsServer2,
637         token: servers[1].accessToken
638       }
639     })
640
641     it('Should not send a notification if transcoding is not enabled', async function () {
642       const { name, uuid } = await uploadVideoByLocalAccount(servers)
643       await waitJobs(servers)
644
645       await checkVideoIsPublished(baseParams, name, uuid, 'absence')
646     })
647
648     it('Should not send a notification if the wait transcoding is false', async function () {
649       this.timeout(50000)
650
651       await uploadVideoByRemoteAccount(servers, { waitTranscoding: false })
652       await waitJobs(servers)
653
654       const notification = await getLastNotification(servers[ 0 ].url, userAccessToken)
655       if (notification) {
656         expect(notification.type).to.not.equal(UserNotificationType.MY_VIDEO_PUBLISHED)
657       }
658     })
659
660     it('Should send a notification even if the video is not transcoded in other resolutions', async function () {
661       this.timeout(50000)
662
663       const { name, uuid } = await uploadVideoByRemoteAccount(servers, { waitTranscoding: true, fixture: 'video_short_240p.mp4' })
664       await waitJobs(servers)
665
666       await checkVideoIsPublished(baseParams, name, uuid, 'presence')
667     })
668
669     it('Should send a notification with a transcoded video', async function () {
670       this.timeout(50000)
671
672       const { name, uuid } = await uploadVideoByRemoteAccount(servers, { waitTranscoding: true })
673       await waitJobs(servers)
674
675       await checkVideoIsPublished(baseParams, name, uuid, 'presence')
676     })
677
678     it('Should send a notification when an imported video is transcoded', async function () {
679       this.timeout(50000)
680
681       const name = 'video import ' + uuidv4()
682
683       const attributes = {
684         name,
685         channelId,
686         privacy: VideoPrivacy.PUBLIC,
687         targetUrl: getYoutubeVideoUrl(),
688         waitTranscoding: true
689       }
690       const res = await importVideo(servers[1].url, servers[1].accessToken, attributes)
691       const uuid = res.body.video.uuid
692
693       await waitJobs(servers)
694       await checkVideoIsPublished(baseParams, name, uuid, 'presence')
695     })
696
697     it('Should send a notification when the scheduled update has been proceeded', async function () {
698       this.timeout(70000)
699
700       // In 2 seconds
701       let updateAt = new Date(new Date().getTime() + 2000)
702
703       const data = {
704         privacy: VideoPrivacy.PRIVATE,
705         scheduleUpdate: {
706           updateAt: updateAt.toISOString(),
707           privacy: VideoPrivacy.PUBLIC
708         }
709       }
710       const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
711
712       await wait(6000)
713       await checkVideoIsPublished(baseParams, name, uuid, 'presence')
714     })
715   })
716
717   describe('My video is imported', function () {
718     let baseParams: CheckerBaseParams
719
720     before(() => {
721       baseParams = {
722         server: servers[0],
723         emails,
724         socketNotifications: adminNotifications,
725         token: servers[0].accessToken
726       }
727     })
728
729     it('Should send a notification when the video import failed', async function () {
730       this.timeout(70000)
731
732       const name = 'video import ' + uuidv4()
733
734       const attributes = {
735         name,
736         channelId,
737         privacy: VideoPrivacy.PRIVATE,
738         targetUrl: getBadVideoUrl()
739       }
740       const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
741       const uuid = res.body.video.uuid
742
743       await waitJobs(servers)
744       await checkMyVideoImportIsFinished(baseParams, name, uuid, getBadVideoUrl(), false, 'presence')
745     })
746
747     it('Should send a notification when the video import succeeded', async function () {
748       this.timeout(70000)
749
750       const name = 'video import ' + uuidv4()
751
752       const attributes = {
753         name,
754         channelId,
755         privacy: VideoPrivacy.PRIVATE,
756         targetUrl: getYoutubeVideoUrl()
757       }
758       const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
759       const uuid = res.body.video.uuid
760
761       await waitJobs(servers)
762       await checkMyVideoImportIsFinished(baseParams, name, uuid, getYoutubeVideoUrl(), true, 'presence')
763     })
764   })
765
766   describe('New registration', function () {
767     let baseParams: CheckerBaseParams
768
769     before(() => {
770       baseParams = {
771         server: servers[0],
772         emails,
773         socketNotifications: adminNotifications,
774         token: servers[0].accessToken
775       }
776     })
777
778     it('Should send a notification only to moderators when a user registers on the instance', async function () {
779       await registerUser(servers[0].url, 'user_45', 'password')
780
781       await waitJobs(servers)
782
783       await checkUserRegistered(baseParams, 'user_45', 'presence')
784
785       const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
786       await checkUserRegistered(immutableAssign(baseParams, userOverride), 'user_45', 'absence')
787     })
788   })
789
790   describe('New actor follow', function () {
791     let baseParams: CheckerBaseParams
792     let myChannelName = 'super channel name'
793     let myUserName = 'super user name'
794
795     before(async () => {
796       baseParams = {
797         server: servers[0],
798         emails,
799         socketNotifications: userNotifications,
800         token: userAccessToken
801       }
802
803       await updateMyUser({
804         url: servers[0].url,
805         accessToken: servers[0].accessToken,
806         displayName: 'super root name'
807       })
808
809       await updateMyUser({
810         url: servers[0].url,
811         accessToken: userAccessToken,
812         displayName: myUserName
813       })
814
815       await updateMyUser({
816         url: servers[1].url,
817         accessToken: servers[1].accessToken,
818         displayName: 'super root 2 name'
819       })
820
821       await updateVideoChannel(servers[0].url, userAccessToken, 'user_1_channel', { displayName: myChannelName })
822     })
823
824     it('Should notify when a local channel is following one of our channel', async function () {
825       await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:9001')
826
827       await waitJobs(servers)
828
829       await checkNewActorFollow(baseParams, 'channel', 'root', 'super root name', myChannelName, 'presence')
830
831       await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:9001')
832     })
833
834     it('Should notify when a remote channel is following one of our channel', async function () {
835       await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:9001')
836
837       await waitJobs(servers)
838
839       await checkNewActorFollow(baseParams, 'channel', 'root', 'super root 2 name', myChannelName, 'presence')
840
841       await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:9001')
842     })
843
844     it('Should notify when a local account is following one of our channel', async function () {
845       await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1@localhost:9001')
846
847       await waitJobs(servers)
848
849       await checkNewActorFollow(baseParams, 'account', 'root', 'super root name', myUserName, 'presence')
850     })
851
852     it('Should notify when a remote account is following one of our channel', async function () {
853       await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1@localhost:9001')
854
855       await waitJobs(servers)
856
857       await checkNewActorFollow(baseParams, 'account', 'root', 'super root 2 name', myUserName, 'presence')
858     })
859   })
860
861   describe('Mark as read', function () {
862     it('Should mark as read some notifications', async function () {
863       const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 2, 3)
864       const ids = res.body.data.map(n => n.id)
865
866       await markAsReadNotifications(servers[ 0 ].url, userAccessToken, ids)
867     })
868
869     it('Should have the notifications marked as read', async function () {
870       const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10)
871
872       const notifications = res.body.data as UserNotification[]
873       expect(notifications[ 0 ].read).to.be.false
874       expect(notifications[ 1 ].read).to.be.false
875       expect(notifications[ 2 ].read).to.be.true
876       expect(notifications[ 3 ].read).to.be.true
877       expect(notifications[ 4 ].read).to.be.true
878       expect(notifications[ 5 ].read).to.be.false
879     })
880
881     it('Should only list read notifications', async function () {
882       const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10, false)
883
884       const notifications = res.body.data as UserNotification[]
885       for (const notification of notifications) {
886         expect(notification.read).to.be.true
887       }
888     })
889
890     it('Should only list unread notifications', async function () {
891       const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10, true)
892
893       const notifications = res.body.data as UserNotification[]
894       for (const notification of notifications) {
895         expect(notification.read).to.be.false
896       }
897     })
898   })
899
900   describe('Notification settings', function () {
901     let baseParams: CheckerBaseParams
902
903     before(() => {
904       baseParams = {
905         server: servers[0],
906         emails,
907         socketNotifications: userNotifications,
908         token: userAccessToken
909       }
910     })
911
912     it('Should not have notifications', async function () {
913       await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, {
914         newVideoFromSubscription: UserNotificationSettingValue.NONE
915       }))
916
917       {
918         const res = await getMyUserInformation(servers[0].url, userAccessToken)
919         const info = res.body as User
920         expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
921       }
922
923       const { name, uuid } = await uploadVideoByLocalAccount(servers)
924
925       const check = { web: true, mail: true }
926       await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
927     })
928
929     it('Should only have web notifications', async function () {
930       await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, {
931         newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION
932       }))
933
934       {
935         const res = await getMyUserInformation(servers[0].url, userAccessToken)
936         const info = res.body as User
937         expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB_NOTIFICATION)
938       }
939
940       const { name, uuid } = await uploadVideoByLocalAccount(servers)
941
942       {
943         const check = { mail: true, web: false }
944         await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
945       }
946
947       {
948         const check = { mail: false, web: true }
949         await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
950       }
951     })
952
953     it('Should only have mail notifications', async function () {
954       await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, {
955         newVideoFromSubscription: UserNotificationSettingValue.EMAIL
956       }))
957
958       {
959         const res = await getMyUserInformation(servers[0].url, userAccessToken)
960         const info = res.body as User
961         expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
962       }
963
964       const { name, uuid } = await uploadVideoByLocalAccount(servers)
965
966       {
967         const check = { mail: false, web: true }
968         await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
969       }
970
971       {
972         const check = { mail: true, web: false }
973         await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
974       }
975     })
976
977     it('Should have email and web notifications', async function () {
978       await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, {
979         newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL
980       }))
981
982       {
983         const res = await getMyUserInformation(servers[0].url, userAccessToken)
984         const info = res.body as User
985         expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL)
986       }
987
988       const { name, uuid } = await uploadVideoByLocalAccount(servers)
989
990       await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
991     })
992   })
993
994   after(async function () {
995     killallServers(servers)
996   })
997 })