1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
9 flushAndRunMultipleServers,
13 removeVideoFromBlacklist,
18 } from '../../../../shared/utils'
19 import { killallServers, ServerInfo, uploadVideo } from '../../../../shared/utils/index'
20 import { setAccessTokensToServers } from '../../../../shared/utils/users/login'
21 import { waitJobs } from '../../../../shared/utils/server/jobs'
22 import { getUserNotificationSocket } from '../../../../shared/utils/socket/socket-io'
25 checkNewBlacklistOnMyVideo,
26 checkNewCommentOnMyVideo,
27 checkNewVideoAbuseForModerators,
28 checkNewVideoFromSubscription,
31 markAsReadNotifications,
32 updateMyNotificationSettings,
33 checkVideoIsPublished, checkMyVideoImportIsFinished
34 } from '../../../../shared/utils/users/user-notifications'
38 UserNotificationSetting,
39 UserNotificationSettingValue,
41 } from '../../../../shared/models/users'
42 import { MockSmtpServer } from '../../../../shared/utils/miscs/email'
43 import { addUserSubscription } from '../../../../shared/utils/users/user-subscriptions'
44 import { VideoPrivacy } from '../../../../shared/models/videos'
45 import { getYoutubeVideoUrl, importVideo, getBadVideoUrl } from '../../../../shared/utils/videos/video-imports'
46 import { addVideoCommentReply, addVideoCommentThread } from '../../../../shared/utils/videos/video-comments'
47 import * as uuidv4 from 'uuid/v4'
48 import { addAccountToAccountBlocklist, removeAccountFromAccountBlocklist } from '../../../../shared/utils/users/blocklist'
50 const expect = chai.expect
52 async function uploadVideoByRemoteAccount (servers: ServerInfo[], additionalParams: any = {}) {
53 const name = 'remote video ' + uuidv4()
55 const data = Object.assign({ name }, additionalParams)
56 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, data)
58 await waitJobs(servers)
60 return { uuid: res.body.video.uuid, name }
63 async function uploadVideoByLocalAccount (servers: ServerInfo[], additionalParams: any = {}) {
64 const name = 'local video ' + uuidv4()
66 const data = Object.assign({ name }, additionalParams)
67 const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, data)
69 await waitJobs(servers)
71 return { uuid: res.body.video.uuid, name }
74 describe('Test users notifications', function () {
75 let servers: ServerInfo[] = []
76 let userAccessToken: string
77 let userNotifications: UserNotification[] = []
78 let adminNotifications: UserNotification[] = []
79 let adminNotificationsServer2: UserNotification[] = []
80 const emails: object[] = []
83 const allNotificationSettings: UserNotificationSetting = {
84 myVideoPublished: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL,
85 myVideoImportFinished: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL,
86 newCommentOnMyVideo: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL,
87 newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL,
88 videoAbuseAsModerator: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL,
89 blacklistOnMyVideo: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL
92 before(async function () {
95 await MockSmtpServer.Instance.collectEmails(emails)
99 const overrideConfig = {
101 hostname: 'localhost'
104 servers = await flushAndRunMultipleServers(2, overrideConfig)
106 // Get the access tokens
107 await setAccessTokensToServers(servers)
109 // Server 1 and server 2 follow each other
110 await doubleFollow(servers[0], servers[1])
112 await waitJobs(servers)
116 password: 'super password'
118 await createUser(servers[0].url, servers[0].accessToken, user.username, user.password, 10 * 1000 * 1000)
119 userAccessToken = await userLogin(servers[0], user)
121 await updateMyNotificationSettings(servers[0].url, userAccessToken, allNotificationSettings)
122 await updateMyNotificationSettings(servers[0].url, servers[0].accessToken, allNotificationSettings)
123 await updateMyNotificationSettings(servers[1].url, servers[1].accessToken, allNotificationSettings)
126 const socket = getUserNotificationSocket(servers[ 0 ].url, userAccessToken)
127 socket.on('new-notification', n => userNotifications.push(n))
130 const socket = getUserNotificationSocket(servers[ 0 ].url, servers[0].accessToken)
131 socket.on('new-notification', n => adminNotifications.push(n))
134 const socket = getUserNotificationSocket(servers[ 1 ].url, servers[1].accessToken)
135 socket.on('new-notification', n => adminNotificationsServer2.push(n))
139 const resChannel = await getMyUserInformation(servers[0].url, servers[0].accessToken)
140 channelId = resChannel.body.videoChannels[0].id
144 describe('New video from my subscription notification', function () {
145 let baseParams: CheckerBaseParams
151 socketNotifications: userNotifications,
152 token: userAccessToken
156 it('Should not send notifications if the user does not follow the video publisher', async function () {
157 await uploadVideoByLocalAccount(servers)
159 const notification = await getLastNotification(servers[ 0 ].url, userAccessToken)
160 expect(notification).to.be.undefined
162 expect(emails).to.have.lengthOf(0)
163 expect(userNotifications).to.have.lengthOf(0)
166 it('Should send a new video notification if the user follows the local video publisher', async function () {
167 await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:9001')
169 const { name, uuid } = await uploadVideoByLocalAccount(servers)
170 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
173 it('Should send a new video notification from a remote account', async function () {
174 this.timeout(50000) // Server 2 has transcoding enabled
176 await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:9002')
178 const { name, uuid } = await uploadVideoByRemoteAccount(servers)
179 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
182 it('Should send a new video notification on a scheduled publication', async function () {
186 let updateAt = new Date(new Date().getTime() + 2000)
189 privacy: VideoPrivacy.PRIVATE,
191 updateAt: updateAt.toISOString(),
192 privacy: VideoPrivacy.PUBLIC
195 const { name, uuid } = await uploadVideoByLocalAccount(servers, data)
198 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
201 it('Should send a new video notification on a remote scheduled publication', async function () {
205 let updateAt = new Date(new Date().getTime() + 2000)
208 privacy: VideoPrivacy.PRIVATE,
210 updateAt: updateAt.toISOString(),
211 privacy: VideoPrivacy.PUBLIC
214 const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
215 await waitJobs(servers)
218 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
221 it('Should not send a notification before the video is published', async function () {
224 let updateAt = new Date(new Date().getTime() + 100000)
227 privacy: VideoPrivacy.PRIVATE,
229 updateAt: updateAt.toISOString(),
230 privacy: VideoPrivacy.PUBLIC
233 const { name, uuid } = await uploadVideoByLocalAccount(servers, data)
236 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
239 it('Should send a new video notification when a video becomes public', async function () {
242 const data = { privacy: VideoPrivacy.PRIVATE }
243 const { name, uuid } = await uploadVideoByLocalAccount(servers, data)
245 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
247 await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC })
250 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
253 it('Should send a new video notification when a remote video becomes public', async function () {
256 const data = { privacy: VideoPrivacy.PRIVATE }
257 const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
259 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
261 await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC })
263 await waitJobs(servers)
264 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
267 it('Should not send a new video notification when a video becomes unlisted', async function () {
270 const data = { privacy: VideoPrivacy.PRIVATE }
271 const { name, uuid } = await uploadVideoByLocalAccount(servers, data)
273 await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED })
275 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
278 it('Should not send a new video notification when a remote video becomes unlisted', async function () {
281 const data = { privacy: VideoPrivacy.PRIVATE }
282 const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
284 await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED })
286 await waitJobs(servers)
287 await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence')
290 it('Should send a new video notification after a video import', async function () {
293 const name = 'video import ' + uuidv4()
298 privacy: VideoPrivacy.PUBLIC,
299 targetUrl: getYoutubeVideoUrl()
301 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
302 const uuid = res.body.video.uuid
304 await waitJobs(servers)
306 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
310 describe('Comment on my video notifications', function () {
311 let baseParams: CheckerBaseParams
317 socketNotifications: userNotifications,
318 token: userAccessToken
322 it('Should not send a new comment notification after a comment on another video', async function () {
325 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
326 const uuid = resVideo.body.video.uuid
328 const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
329 const commentId = resComment.body.comment.id
332 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
335 it('Should not send a new comment notification if I comment my own video', async function () {
338 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
339 const uuid = resVideo.body.video.uuid
341 const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, 'comment')
342 const commentId = resComment.body.comment.id
345 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
348 it('Should not send a new comment notification if the account is muted', async function () {
351 await addAccountToAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root')
353 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
354 const uuid = resVideo.body.video.uuid
356 const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
357 const commentId = resComment.body.comment.id
360 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
362 await removeAccountFromAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root')
365 it('Should send a new comment notification after a local comment on my video', async function () {
368 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
369 const uuid = resVideo.body.video.uuid
371 const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
372 const commentId = resComment.body.comment.id
375 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence')
378 it('Should send a new comment notification after a remote comment on my video', async function () {
381 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
382 const uuid = resVideo.body.video.uuid
384 await waitJobs(servers)
386 const resComment = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment')
387 const commentId = resComment.body.comment.id
389 await waitJobs(servers)
390 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence')
393 it('Should send a new comment notification after a local reply on my video', async function () {
396 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
397 const uuid = resVideo.body.video.uuid
399 const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
400 const threadId = resThread.body.comment.id
402 const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'reply')
403 const commentId = resComment.body.comment.id
406 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence')
409 it('Should send a new comment notification after a remote reply on my video', async function () {
412 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
413 const uuid = resVideo.body.video.uuid
414 await waitJobs(servers)
416 const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment')
417 const threadId = resThread.body.comment.id
419 const resComment = await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, threadId, 'reply')
420 const commentId = resComment.body.comment.id
422 await waitJobs(servers)
423 await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence')
427 describe('Video abuse for moderators notification' , function () {
428 let baseParams: CheckerBaseParams
434 socketNotifications: adminNotifications,
435 token: servers[0].accessToken
439 it('Should send a notification to moderators on local video abuse', async function () {
442 const name = 'video for abuse ' + uuidv4()
443 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
444 const uuid = resVideo.body.video.uuid
446 await reportVideoAbuse(servers[0].url, servers[0].accessToken, uuid, 'super reason')
448 await waitJobs(servers)
449 await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence')
452 it('Should send a notification to moderators on remote video abuse', async function () {
455 const name = 'video for abuse ' + uuidv4()
456 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
457 const uuid = resVideo.body.video.uuid
459 await waitJobs(servers)
461 await reportVideoAbuse(servers[1].url, servers[1].accessToken, uuid, 'super reason')
463 await waitJobs(servers)
464 await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence')
468 describe('Video blacklist on my video', function () {
469 let baseParams: CheckerBaseParams
475 socketNotifications: userNotifications,
476 token: userAccessToken
480 it('Should send a notification to video owner on blacklist', async function () {
483 const name = 'video for abuse ' + uuidv4()
484 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
485 const uuid = resVideo.body.video.uuid
487 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
489 await waitJobs(servers)
490 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist')
493 it('Should send a notification to video owner on unblacklist', async function () {
496 const name = 'video for abuse ' + uuidv4()
497 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
498 const uuid = resVideo.body.video.uuid
500 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
502 await waitJobs(servers)
503 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
504 await waitJobs(servers)
507 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist')
511 describe('My video is published', function () {
512 let baseParams: CheckerBaseParams
518 socketNotifications: adminNotificationsServer2,
519 token: servers[1].accessToken
523 it('Should not send a notification if transcoding is not enabled', async function () {
524 const { name, uuid } = await uploadVideoByLocalAccount(servers)
525 await waitJobs(servers)
527 await checkVideoIsPublished(baseParams, name, uuid, 'absence')
530 it('Should not send a notification if the wait transcoding is false', async function () {
533 await uploadVideoByRemoteAccount(servers, { waitTranscoding: false })
534 await waitJobs(servers)
536 const notification = await getLastNotification(servers[ 0 ].url, userAccessToken)
538 expect(notification.type).to.not.equal(UserNotificationType.MY_VIDEO_PUBLISHED)
542 it('Should send a notification even if the video is not transcoded in other resolutions', async function () {
545 const { name, uuid } = await uploadVideoByRemoteAccount(servers, { waitTranscoding: true, fixture: 'video_short_240p.mp4' })
546 await waitJobs(servers)
548 await checkVideoIsPublished(baseParams, name, uuid, 'presence')
551 it('Should send a notification with a transcoded video', async function () {
554 const { name, uuid } = await uploadVideoByRemoteAccount(servers, { waitTranscoding: true })
555 await waitJobs(servers)
557 await checkVideoIsPublished(baseParams, name, uuid, 'presence')
560 it('Should send a notification when an imported video is transcoded', async function () {
563 const name = 'video import ' + uuidv4()
568 privacy: VideoPrivacy.PUBLIC,
569 targetUrl: getYoutubeVideoUrl(),
570 waitTranscoding: true
572 const res = await importVideo(servers[1].url, servers[1].accessToken, attributes)
573 const uuid = res.body.video.uuid
575 await waitJobs(servers)
576 await checkVideoIsPublished(baseParams, name, uuid, 'presence')
579 it('Should send a notification when the scheduled update has been proceeded', async function () {
583 let updateAt = new Date(new Date().getTime() + 2000)
586 privacy: VideoPrivacy.PRIVATE,
588 updateAt: updateAt.toISOString(),
589 privacy: VideoPrivacy.PUBLIC
592 const { name, uuid } = await uploadVideoByRemoteAccount(servers, data)
595 await checkVideoIsPublished(baseParams, name, uuid, 'presence')
599 describe('My video is imported', function () {
600 let baseParams: CheckerBaseParams
606 socketNotifications: adminNotifications,
607 token: servers[0].accessToken
611 it('Should send a notification when the video import failed', async function () {
614 const name = 'video import ' + uuidv4()
619 privacy: VideoPrivacy.PRIVATE,
620 targetUrl: getBadVideoUrl()
622 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
623 const uuid = res.body.video.uuid
625 await waitJobs(servers)
626 await checkMyVideoImportIsFinished(baseParams, name, uuid, getBadVideoUrl(), false, 'presence')
629 it('Should send a notification when the video import succeeded', async function () {
632 const name = 'video import ' + uuidv4()
637 privacy: VideoPrivacy.PRIVATE,
638 targetUrl: getYoutubeVideoUrl()
640 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
641 const uuid = res.body.video.uuid
643 await waitJobs(servers)
644 await checkMyVideoImportIsFinished(baseParams, name, uuid, getYoutubeVideoUrl(), true, 'presence')
648 describe('Mark as read', function () {
649 it('Should mark as read some notifications', async function () {
650 const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 2, 3)
651 const ids = res.body.data.map(n => n.id)
653 await markAsReadNotifications(servers[ 0 ].url, userAccessToken, ids)
656 it('Should have the notifications marked as read', async function () {
657 const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10)
659 const notifications = res.body.data as UserNotification[]
660 expect(notifications[ 0 ].read).to.be.false
661 expect(notifications[ 1 ].read).to.be.false
662 expect(notifications[ 2 ].read).to.be.true
663 expect(notifications[ 3 ].read).to.be.true
664 expect(notifications[ 4 ].read).to.be.true
665 expect(notifications[ 5 ].read).to.be.false
668 it('Should only list read notifications', async function () {
669 const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10, false)
671 const notifications = res.body.data as UserNotification[]
672 for (const notification of notifications) {
673 expect(notification.read).to.be.true
677 it('Should only list unread notifications', async function () {
678 const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10, true)
680 const notifications = res.body.data as UserNotification[]
681 for (const notification of notifications) {
682 expect(notification.read).to.be.false
687 describe('Notification settings', function () {
688 let baseParams: CheckerBaseParams
694 socketNotifications: userNotifications,
695 token: userAccessToken
699 it('Should not have notifications', async function () {
700 await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, {
701 newVideoFromSubscription: UserNotificationSettingValue.NONE
705 const res = await getMyUserInformation(servers[0].url, userAccessToken)
706 const info = res.body as User
707 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
710 const { name, uuid } = await uploadVideoByLocalAccount(servers)
712 const check = { web: true, mail: true }
713 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
716 it('Should only have web notifications', async function () {
717 await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, {
718 newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION
722 const res = await getMyUserInformation(servers[0].url, userAccessToken)
723 const info = res.body as User
724 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB_NOTIFICATION)
727 const { name, uuid } = await uploadVideoByLocalAccount(servers)
730 const check = { mail: true, web: false }
731 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
735 const check = { mail: false, web: true }
736 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
740 it('Should only have mail notifications', async function () {
741 await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, {
742 newVideoFromSubscription: UserNotificationSettingValue.EMAIL
746 const res = await getMyUserInformation(servers[0].url, userAccessToken)
747 const info = res.body as User
748 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
751 const { name, uuid } = await uploadVideoByLocalAccount(servers)
754 const check = { mail: false, web: true }
755 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence')
759 const check = { mail: true, web: false }
760 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence')
764 it('Should have email and web notifications', async function () {
765 await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, {
766 newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL
770 const res = await getMyUserInformation(servers[0].url, userAccessToken)
771 const info = res.body as User
772 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL)
775 const { name, uuid } = await uploadVideoByLocalAccount(servers)
777 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
781 after(async function () {
782 killallServers(servers)