1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { inspect } from 'util'
5 import { UserNotification, UserNotificationSetting, UserNotificationSettingValue, UserNotificationType } from '../../models/users'
6 import { MockSmtpServer } from '../miscs/email'
7 import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests'
8 import { doubleFollow } from '../server/follows'
9 import { flushAndRunMultipleServers, ServerInfo } from '../server/servers'
10 import { getUserNotificationSocket } from '../socket/socket-io'
11 import { setAccessTokensToServers, userLogin } from './login'
12 import { createUser, getMyUserInformation } from './users'
14 function updateMyNotificationSettings (url: string, token: string, settings: UserNotificationSetting, statusCodeExpected = 204) {
15 const path = '/api/v1/users/me/notification-settings'
17 return makePutBodyRequest({
26 async function getUserNotifications (
33 statusCodeExpected = 200
35 const path = '/api/v1/users/me/notifications'
37 return makeGetRequest({
51 function markAsReadNotifications (url: string, token: string, ids: number[], statusCodeExpected = 204) {
52 const path = '/api/v1/users/me/notifications/read'
54 return makePostBodyRequest({
63 function markAsReadAllNotifications (url: string, token: string, statusCodeExpected = 204) {
64 const path = '/api/v1/users/me/notifications/read-all'
66 return makePostBodyRequest({
74 async function getLastNotification (serverUrl: string, accessToken: string) {
75 const res = await getUserNotifications(serverUrl, accessToken, 0, 1, undefined, '-createdAt')
77 if (res.body.total === 0) return undefined
79 return res.body.data[0] as UserNotification
82 type CheckerBaseParams = {
85 socketNotifications: UserNotification[]
87 check?: { web: boolean, mail: boolean }
90 type CheckerType = 'presence' | 'absence'
92 async function checkNotification (
93 base: CheckerBaseParams,
94 notificationChecker: (notification: UserNotification, type: CheckerType) => void,
95 emailNotificationFinder: (email: object) => boolean,
96 checkType: CheckerType
98 const check = base.check || { web: true, mail: true }
101 const notification = await getLastNotification(base.server.url, base.token)
103 if (notification || checkType !== 'absence') {
104 notificationChecker(notification, checkType)
107 const socketNotification = base.socketNotifications.find(n => {
109 notificationChecker(n, 'presence')
116 if (checkType === 'presence') {
117 const obj = inspect(base.socketNotifications, { depth: 5 })
118 expect(socketNotification, 'The socket notification is absent when it should be present. ' + obj).to.not.be.undefined
120 const obj = inspect(socketNotification, { depth: 5 })
121 expect(socketNotification, 'The socket notification is present when it should not be present. ' + obj).to.be.undefined
127 const email = base.emails
130 .find(e => emailNotificationFinder(e))
132 if (checkType === 'presence') {
133 const emails = base.emails.map(e => e.text)
134 expect(email, 'The email is absent when is should be present. ' + inspect(emails)).to.not.be.undefined
136 expect(email, 'The email is present when is should not be present. ' + inspect(email)).to.be.undefined
141 function checkVideo (video: any, videoName?: string, videoUUID?: string) {
142 expect(video.name).to.be.a('string')
143 expect(video.name).to.not.be.empty
144 if (videoName) expect(video.name).to.equal(videoName)
146 expect(video.uuid).to.be.a('string')
147 expect(video.uuid).to.not.be.empty
148 if (videoUUID) expect(video.uuid).to.equal(videoUUID)
150 expect(video.id).to.be.a('number')
153 function checkActor (actor: any) {
154 expect(actor.displayName).to.be.a('string')
155 expect(actor.displayName).to.not.be.empty
156 expect(actor.host).to.not.be.undefined
159 function checkComment (comment: any, commentId: number, threadId: number) {
160 expect(comment.id).to.equal(commentId)
161 expect(comment.threadId).to.equal(threadId)
164 async function checkNewVideoFromSubscription (base: CheckerBaseParams, videoName: string, videoUUID: string, type: CheckerType) {
165 const notificationType = UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION
167 function notificationChecker (notification: UserNotification, type: CheckerType) {
168 if (type === 'presence') {
169 expect(notification).to.not.be.undefined
170 expect(notification.type).to.equal(notificationType)
172 checkVideo(notification.video, videoName, videoUUID)
173 checkActor(notification.video.channel)
175 expect(notification).to.satisfy((n: UserNotification) => {
176 return n === undefined || n.type !== UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION || n.video.name !== videoName
181 function emailNotificationFinder (email: object) {
182 const text = email['text']
183 return text.indexOf(videoUUID) !== -1 && text.indexOf('Your subscription') !== -1
186 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
189 async function checkVideoIsPublished (base: CheckerBaseParams, videoName: string, videoUUID: string, type: CheckerType) {
190 const notificationType = UserNotificationType.MY_VIDEO_PUBLISHED
192 function notificationChecker (notification: UserNotification, type: CheckerType) {
193 if (type === 'presence') {
194 expect(notification).to.not.be.undefined
195 expect(notification.type).to.equal(notificationType)
197 checkVideo(notification.video, videoName, videoUUID)
198 checkActor(notification.video.channel)
200 expect(notification.video).to.satisfy(v => v === undefined || v.name !== videoName)
204 function emailNotificationFinder (email: object) {
205 const text: string = email['text']
206 return text.includes(videoUUID) && text.includes('Your video')
209 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
212 async function checkMyVideoImportIsFinished (
213 base: CheckerBaseParams,
220 const notificationType = success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR
222 function notificationChecker (notification: UserNotification, type: CheckerType) {
223 if (type === 'presence') {
224 expect(notification).to.not.be.undefined
225 expect(notification.type).to.equal(notificationType)
227 expect(notification.videoImport.targetUrl).to.equal(url)
229 if (success) checkVideo(notification.videoImport.video, videoName, videoUUID)
231 expect(notification.videoImport).to.satisfy(i => i === undefined || i.targetUrl !== url)
235 function emailNotificationFinder (email: object) {
236 const text: string = email['text']
237 const toFind = success ? ' finished' : ' error'
239 return text.includes(url) && text.includes(toFind)
242 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
245 async function checkUserRegistered (base: CheckerBaseParams, username: string, type: CheckerType) {
246 const notificationType = UserNotificationType.NEW_USER_REGISTRATION
248 function notificationChecker (notification: UserNotification, type: CheckerType) {
249 if (type === 'presence') {
250 expect(notification).to.not.be.undefined
251 expect(notification.type).to.equal(notificationType)
253 checkActor(notification.account)
254 expect(notification.account.name).to.equal(username)
256 expect(notification).to.satisfy(n => n.type !== notificationType || n.account.name !== username)
260 function emailNotificationFinder (email: object) {
261 const text: string = email['text']
263 return text.includes(' registered.') && text.includes(username)
266 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
269 async function checkNewActorFollow (
270 base: CheckerBaseParams,
271 followType: 'channel' | 'account',
272 followerName: string,
273 followerDisplayName: string,
274 followingDisplayName: string,
277 const notificationType = UserNotificationType.NEW_FOLLOW
279 function notificationChecker (notification: UserNotification, type: CheckerType) {
280 if (type === 'presence') {
281 expect(notification).to.not.be.undefined
282 expect(notification.type).to.equal(notificationType)
284 checkActor(notification.actorFollow.follower)
285 expect(notification.actorFollow.follower.displayName).to.equal(followerDisplayName)
286 expect(notification.actorFollow.follower.name).to.equal(followerName)
287 expect(notification.actorFollow.follower.host).to.not.be.undefined
289 const following = notification.actorFollow.following
290 expect(following.displayName).to.equal(followingDisplayName)
291 expect(following.type).to.equal(followType)
293 expect(notification).to.satisfy(n => {
294 return n.type !== notificationType ||
295 (n.actorFollow.follower.name !== followerName && n.actorFollow.following !== followingDisplayName)
300 function emailNotificationFinder (email: object) {
301 const text: string = email['text']
303 return text.includes(followType) && text.includes(followingDisplayName) && text.includes(followerDisplayName)
306 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
309 async function checkNewInstanceFollower (base: CheckerBaseParams, followerHost: string, type: CheckerType) {
310 const notificationType = UserNotificationType.NEW_INSTANCE_FOLLOWER
312 function notificationChecker (notification: UserNotification, type: CheckerType) {
313 if (type === 'presence') {
314 expect(notification).to.not.be.undefined
315 expect(notification.type).to.equal(notificationType)
317 checkActor(notification.actorFollow.follower)
318 expect(notification.actorFollow.follower.name).to.equal('peertube')
319 expect(notification.actorFollow.follower.host).to.equal(followerHost)
321 expect(notification.actorFollow.following.name).to.equal('peertube')
323 expect(notification).to.satisfy(n => {
324 return n.type !== notificationType || n.actorFollow.follower.host !== followerHost
329 function emailNotificationFinder (email: object) {
330 const text: string = email['text']
332 return text.includes('instance has a new follower') && text.includes(followerHost)
335 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
338 async function checkAutoInstanceFollowing (base: CheckerBaseParams, followerHost: string, followingHost: string, type: CheckerType) {
339 const notificationType = UserNotificationType.AUTO_INSTANCE_FOLLOWING
341 function notificationChecker (notification: UserNotification, type: CheckerType) {
342 if (type === 'presence') {
343 expect(notification).to.not.be.undefined
344 expect(notification.type).to.equal(notificationType)
346 const following = notification.actorFollow.following
347 checkActor(following)
348 expect(following.name).to.equal('peertube')
349 expect(following.host).to.equal(followingHost)
351 expect(notification.actorFollow.follower.name).to.equal('peertube')
352 expect(notification.actorFollow.follower.host).to.equal(followerHost)
354 expect(notification).to.satisfy(n => {
355 return n.type !== notificationType || n.actorFollow.following.host !== followingHost
360 function emailNotificationFinder (email: object) {
361 const text: string = email['text']
363 return text.includes(' automatically followed a new instance') && text.includes(followingHost)
366 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
369 async function checkCommentMention (
370 base: CheckerBaseParams,
374 byAccountDisplayName: string,
377 const notificationType = UserNotificationType.COMMENT_MENTION
379 function notificationChecker (notification: UserNotification, type: CheckerType) {
380 if (type === 'presence') {
381 expect(notification).to.not.be.undefined
382 expect(notification.type).to.equal(notificationType)
384 checkComment(notification.comment, commentId, threadId)
385 checkActor(notification.comment.account)
386 expect(notification.comment.account.displayName).to.equal(byAccountDisplayName)
388 checkVideo(notification.comment.video, undefined, uuid)
390 expect(notification).to.satisfy(n => n.type !== notificationType || n.comment.id !== commentId)
394 function emailNotificationFinder (email: object) {
395 const text: string = email['text']
397 return text.includes(' mentioned ') && text.includes(uuid) && text.includes(byAccountDisplayName)
400 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
403 let lastEmailCount = 0
405 async function checkNewCommentOnMyVideo (base: CheckerBaseParams, uuid: string, commentId: number, threadId: number, type: CheckerType) {
406 const notificationType = UserNotificationType.NEW_COMMENT_ON_MY_VIDEO
408 function notificationChecker (notification: UserNotification, type: CheckerType) {
409 if (type === 'presence') {
410 expect(notification).to.not.be.undefined
411 expect(notification.type).to.equal(notificationType)
413 checkComment(notification.comment, commentId, threadId)
414 checkActor(notification.comment.account)
415 checkVideo(notification.comment.video, undefined, uuid)
417 expect(notification).to.satisfy((n: UserNotification) => {
418 return n === undefined || n.comment === undefined || n.comment.id !== commentId
423 const commentUrl = `http://localhost:${base.server.port}/videos/watch/${uuid};threadId=${threadId}`
425 function emailNotificationFinder (email: object) {
426 return email['text'].indexOf(commentUrl) !== -1
429 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
431 if (type === 'presence') {
432 // We cannot detect email duplicates, so check we received another email
433 expect(base.emails).to.have.length.above(lastEmailCount)
434 lastEmailCount = base.emails.length
438 async function checkNewVideoAbuseForModerators (base: CheckerBaseParams, videoUUID: string, videoName: string, type: CheckerType) {
439 const notificationType = UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS
441 function notificationChecker (notification: UserNotification, type: CheckerType) {
442 if (type === 'presence') {
443 expect(notification).to.not.be.undefined
444 expect(notification.type).to.equal(notificationType)
446 expect(notification.videoAbuse.id).to.be.a('number')
447 checkVideo(notification.videoAbuse.video, videoName, videoUUID)
449 expect(notification).to.satisfy((n: UserNotification) => {
450 return n === undefined || n.videoAbuse === undefined || n.videoAbuse.video.uuid !== videoUUID
455 function emailNotificationFinder (email: object) {
456 const text = email['text']
457 return text.indexOf(videoUUID) !== -1 && text.indexOf('abuse') !== -1
460 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
463 async function checkVideoAutoBlacklistForModerators (base: CheckerBaseParams, videoUUID: string, videoName: string, type: CheckerType) {
464 const notificationType = UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS
466 function notificationChecker (notification: UserNotification, type: CheckerType) {
467 if (type === 'presence') {
468 expect(notification).to.not.be.undefined
469 expect(notification.type).to.equal(notificationType)
471 expect(notification.videoBlacklist.video.id).to.be.a('number')
472 checkVideo(notification.videoBlacklist.video, videoName, videoUUID)
474 expect(notification).to.satisfy((n: UserNotification) => {
475 return n === undefined || n.video === undefined || n.video.uuid !== videoUUID
480 function emailNotificationFinder (email: object) {
481 const text = email['text']
482 return text.indexOf(videoUUID) !== -1 && email['text'].indexOf('video-auto-blacklist/list') !== -1
485 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
488 async function checkNewBlacklistOnMyVideo (
489 base: CheckerBaseParams,
492 blacklistType: 'blacklist' | 'unblacklist'
494 const notificationType = blacklistType === 'blacklist'
495 ? UserNotificationType.BLACKLIST_ON_MY_VIDEO
496 : UserNotificationType.UNBLACKLIST_ON_MY_VIDEO
498 function notificationChecker (notification: UserNotification) {
499 expect(notification).to.not.be.undefined
500 expect(notification.type).to.equal(notificationType)
502 const video = blacklistType === 'blacklist' ? notification.videoBlacklist.video : notification.video
504 checkVideo(video, videoName, videoUUID)
507 function emailNotificationFinder (email: object) {
508 const text = email['text']
509 return text.indexOf(videoUUID) !== -1 && text.indexOf(' ' + blacklistType) !== -1
512 await checkNotification(base, notificationChecker, emailNotificationFinder, 'presence')
515 function getAllNotificationsSettings () {
517 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
518 newCommentOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
519 videoAbuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
520 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
521 blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
522 myVideoImportFinished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
523 myVideoPublished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
524 commentMention: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
525 newFollow: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
526 newUserRegistration: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
527 newInstanceFollower: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
528 autoInstanceFollowing: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
529 } as UserNotificationSetting
532 async function prepareNotificationsTest (serversCount = 3) {
533 const userNotifications: UserNotification[] = []
534 const adminNotifications: UserNotification[] = []
535 const adminNotificationsServer2: UserNotification[] = []
536 const emails: object[] = []
538 const port = await MockSmtpServer.Instance.collectEmails(emails)
540 const overrideConfig = {
542 hostname: 'localhost',
546 const servers = await flushAndRunMultipleServers(serversCount, overrideConfig)
548 await setAccessTokensToServers(servers)
550 if (serversCount > 1) {
551 await doubleFollow(servers[0], servers[1])
556 password: 'super password'
560 accessToken: servers[0].accessToken,
561 username: user.username,
562 password: user.password,
563 videoQuota: 10 * 1000 * 1000
565 const userAccessToken = await userLogin(servers[0], user)
567 await updateMyNotificationSettings(servers[0].url, userAccessToken, getAllNotificationsSettings())
568 await updateMyNotificationSettings(servers[0].url, servers[0].accessToken, getAllNotificationsSettings())
570 if (serversCount > 1) {
571 await updateMyNotificationSettings(servers[1].url, servers[1].accessToken, getAllNotificationsSettings())
575 const socket = getUserNotificationSocket(servers[0].url, userAccessToken)
576 socket.on('new-notification', n => userNotifications.push(n))
579 const socket = getUserNotificationSocket(servers[0].url, servers[0].accessToken)
580 socket.on('new-notification', n => adminNotifications.push(n))
583 if (serversCount > 1) {
584 const socket = getUserNotificationSocket(servers[1].url, servers[1].accessToken)
585 socket.on('new-notification', n => adminNotificationsServer2.push(n))
588 const resChannel = await getMyUserInformation(servers[0].url, servers[0].accessToken)
589 const channelId = resChannel.body.videoChannels[0].id
594 adminNotificationsServer2,
602 // ---------------------------------------------------------------------------
607 getAllNotificationsSettings,
609 markAsReadAllNotifications,
610 checkMyVideoImportIsFinished,
612 checkAutoInstanceFollowing,
613 checkVideoIsPublished,
614 checkNewVideoFromSubscription,
616 checkNewCommentOnMyVideo,
617 checkNewBlacklistOnMyVideo,
619 updateMyNotificationSettings,
620 checkNewVideoAbuseForModerators,
621 checkVideoAutoBlacklistForModerators,
622 getUserNotifications,
623 markAsReadNotifications,
625 checkNewInstanceFollower,
626 prepareNotificationsTest