1 /* tslint:disable:no-unused-expression */
3 import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests'
4 import { UserNotification, UserNotificationSetting, UserNotificationType } from '../../models/users'
5 import { ServerInfo } from '..'
6 import { expect } from 'chai'
7 import { inspect } from 'util'
9 function updateMyNotificationSettings (url: string, token: string, settings: UserNotificationSetting, statusCodeExpected = 204) {
10 const path = '/api/v1/users/me/notification-settings'
12 return makePutBodyRequest({
21 async function getUserNotifications (
28 statusCodeExpected = 200
30 const path = '/api/v1/users/me/notifications'
32 return makeGetRequest({
46 function markAsReadNotifications (url: string, token: string, ids: number[], statusCodeExpected = 204) {
47 const path = '/api/v1/users/me/notifications/read'
49 return makePostBodyRequest({
57 function markAsReadAllNotifications (url: string, token: string, statusCodeExpected = 204) {
58 const path = '/api/v1/users/me/notifications/read-all'
60 return makePostBodyRequest({
68 async function getLastNotification (serverUrl: string, accessToken: string) {
69 const res = await getUserNotifications(serverUrl, accessToken, 0, 1, undefined, '-createdAt')
71 if (res.body.total === 0) return undefined
73 return res.body.data[0] as UserNotification
76 type CheckerBaseParams = {
79 socketNotifications: UserNotification[]
81 check?: { web: boolean, mail: boolean }
84 type CheckerType = 'presence' | 'absence'
86 async function checkNotification (
87 base: CheckerBaseParams,
88 notificationChecker: (notification: UserNotification, type: CheckerType) => void,
89 emailNotificationFinder: (email: object) => boolean,
90 checkType: CheckerType
92 const check = base.check || { web: true, mail: true }
95 const notification = await getLastNotification(base.server.url, base.token)
97 if (notification || checkType !== 'absence') {
98 notificationChecker(notification, checkType)
101 const socketNotification = base.socketNotifications.find(n => {
103 notificationChecker(n, 'presence')
110 if (checkType === 'presence') {
111 const obj = inspect(base.socketNotifications, { depth: 5 })
112 expect(socketNotification, 'The socket notification is absent. ' + obj).to.not.be.undefined
114 const obj = inspect(socketNotification, { depth: 5 })
115 expect(socketNotification, 'The socket notification is present. ' + obj).to.be.undefined
121 const email = base.emails
124 .find(e => emailNotificationFinder(e))
126 if (checkType === 'presence') {
127 expect(email, 'The email is absent. ' + inspect(base.emails)).to.not.be.undefined
129 expect(email, 'The email is present. ' + inspect(email)).to.be.undefined
134 function checkVideo (video: any, videoName?: string, videoUUID?: string) {
135 expect(video.name).to.be.a('string')
136 expect(video.name).to.not.be.empty
137 if (videoName) expect(video.name).to.equal(videoName)
139 expect(video.uuid).to.be.a('string')
140 expect(video.uuid).to.not.be.empty
141 if (videoUUID) expect(video.uuid).to.equal(videoUUID)
143 expect(video.id).to.be.a('number')
146 function checkActor (actor: any) {
147 expect(actor.displayName).to.be.a('string')
148 expect(actor.displayName).to.not.be.empty
149 expect(actor.host).to.not.be.undefined
152 function checkComment (comment: any, commentId: number, threadId: number) {
153 expect(comment.id).to.equal(commentId)
154 expect(comment.threadId).to.equal(threadId)
157 async function checkNewVideoFromSubscription (base: CheckerBaseParams, videoName: string, videoUUID: string, type: CheckerType) {
158 const notificationType = UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION
160 function notificationChecker (notification: UserNotification, type: CheckerType) {
161 if (type === 'presence') {
162 expect(notification).to.not.be.undefined
163 expect(notification.type).to.equal(notificationType)
165 checkVideo(notification.video, videoName, videoUUID)
166 checkActor(notification.video.channel)
168 expect(notification).to.satisfy((n: UserNotification) => {
169 return n === undefined || n.type !== UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION || n.video.name !== videoName
174 function emailFinder (email: object) {
175 const text = email[ 'text' ]
176 return text.indexOf(videoUUID) !== -1 && text.indexOf('Your subscription') !== -1
179 await checkNotification(base, notificationChecker, emailFinder, type)
182 async function checkVideoIsPublished (base: CheckerBaseParams, videoName: string, videoUUID: string, type: CheckerType) {
183 const notificationType = UserNotificationType.MY_VIDEO_PUBLISHED
185 function notificationChecker (notification: UserNotification, type: CheckerType) {
186 if (type === 'presence') {
187 expect(notification).to.not.be.undefined
188 expect(notification.type).to.equal(notificationType)
190 checkVideo(notification.video, videoName, videoUUID)
191 checkActor(notification.video.channel)
193 expect(notification.video).to.satisfy(v => v === undefined || v.name !== videoName)
197 function emailFinder (email: object) {
198 const text: string = email[ 'text' ]
199 return text.includes(videoUUID) && text.includes('Your video')
202 await checkNotification(base, notificationChecker, emailFinder, type)
205 async function checkMyVideoImportIsFinished (
206 base: CheckerBaseParams,
213 const notificationType = success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR
215 function notificationChecker (notification: UserNotification, type: CheckerType) {
216 if (type === 'presence') {
217 expect(notification).to.not.be.undefined
218 expect(notification.type).to.equal(notificationType)
220 expect(notification.videoImport.targetUrl).to.equal(url)
222 if (success) checkVideo(notification.videoImport.video, videoName, videoUUID)
224 expect(notification.videoImport).to.satisfy(i => i === undefined || i.targetUrl !== url)
228 function emailFinder (email: object) {
229 const text: string = email[ 'text' ]
230 const toFind = success ? ' finished' : ' error'
232 return text.includes(url) && text.includes(toFind)
235 await checkNotification(base, notificationChecker, emailFinder, type)
238 async function checkUserRegistered (base: CheckerBaseParams, username: string, type: CheckerType) {
239 const notificationType = UserNotificationType.NEW_USER_REGISTRATION
241 function notificationChecker (notification: UserNotification, type: CheckerType) {
242 if (type === 'presence') {
243 expect(notification).to.not.be.undefined
244 expect(notification.type).to.equal(notificationType)
246 checkActor(notification.account)
247 expect(notification.account.name).to.equal(username)
249 expect(notification).to.satisfy(n => n.type !== notificationType || n.account.name !== username)
253 function emailFinder (email: object) {
254 const text: string = email[ 'text' ]
256 return text.includes(' registered ') && text.includes(username)
259 await checkNotification(base, notificationChecker, emailFinder, type)
262 async function checkNewActorFollow (
263 base: CheckerBaseParams,
264 followType: 'channel' | 'account',
265 followerName: string,
266 followerDisplayName: string,
267 followingDisplayName: string,
270 const notificationType = UserNotificationType.NEW_FOLLOW
272 function notificationChecker (notification: UserNotification, type: CheckerType) {
273 if (type === 'presence') {
274 expect(notification).to.not.be.undefined
275 expect(notification.type).to.equal(notificationType)
277 checkActor(notification.actorFollow.follower)
278 expect(notification.actorFollow.follower.displayName).to.equal(followerDisplayName)
279 expect(notification.actorFollow.follower.name).to.equal(followerName)
280 expect(notification.actorFollow.follower.host).to.not.be.undefined
282 const following = notification.actorFollow.following
283 expect(following.displayName).to.equal(followingDisplayName)
284 expect(following.type).to.equal(followType)
286 expect(notification).to.satisfy(n => {
287 return n.type !== notificationType ||
288 (n.actorFollow.follower.name !== followerName && n.actorFollow.following !== followingDisplayName)
293 function emailFinder (email: object) {
294 const text: string = email[ 'text' ]
296 return text.includes('Your ' + followType) && text.includes(followingDisplayName) && text.includes(followerDisplayName)
299 await checkNotification(base, notificationChecker, emailFinder, type)
302 async function checkNewInstanceFollower (base: CheckerBaseParams, followerHost: string, type: CheckerType) {
303 const notificationType = UserNotificationType.NEW_INSTANCE_FOLLOWER
305 function notificationChecker (notification: UserNotification, type: CheckerType) {
306 if (type === 'presence') {
307 expect(notification).to.not.be.undefined
308 expect(notification.type).to.equal(notificationType)
310 checkActor(notification.actorFollow.follower)
311 expect(notification.actorFollow.follower.name).to.equal('peertube')
312 expect(notification.actorFollow.follower.host).to.equal(followerHost)
314 expect(notification.actorFollow.following.name).to.equal('peertube')
316 expect(notification).to.satisfy(n => {
317 return n.type !== notificationType || n.actorFollow.follower.host !== followerHost
322 function emailFinder (email: object) {
323 const text: string = email[ 'text' ]
325 return text.includes('instance has a new follower') && text.includes(followerHost)
328 await checkNotification(base, notificationChecker, emailFinder, type)
331 async function checkAutoInstanceFollowing (base: CheckerBaseParams, followerHost: string, followingHost: string, type: CheckerType) {
332 const notificationType = UserNotificationType.AUTO_INSTANCE_FOLLOWING
334 function notificationChecker (notification: UserNotification, type: CheckerType) {
335 if (type === 'presence') {
336 expect(notification).to.not.be.undefined
337 expect(notification.type).to.equal(notificationType)
339 const following = notification.actorFollow.following
340 checkActor(following)
341 expect(following.name).to.equal('peertube')
342 expect(following.host).to.equal(followingHost)
344 expect(notification.actorFollow.follower.name).to.equal('peertube')
345 expect(notification.actorFollow.follower.host).to.equal(followerHost)
347 expect(notification).to.satisfy(n => {
348 return n.type !== notificationType || n.actorFollow.following.host !== followingHost
353 function emailFinder (email: object) {
354 const text: string = email[ 'text' ]
356 return text.includes(' automatically followed a new instance') && text.includes(followingHost)
359 await checkNotification(base, notificationChecker, emailFinder, type)
362 async function checkCommentMention (
363 base: CheckerBaseParams,
367 byAccountDisplayName: string,
370 const notificationType = UserNotificationType.COMMENT_MENTION
372 function notificationChecker (notification: UserNotification, type: CheckerType) {
373 if (type === 'presence') {
374 expect(notification).to.not.be.undefined
375 expect(notification.type).to.equal(notificationType)
377 checkComment(notification.comment, commentId, threadId)
378 checkActor(notification.comment.account)
379 expect(notification.comment.account.displayName).to.equal(byAccountDisplayName)
381 checkVideo(notification.comment.video, undefined, uuid)
383 expect(notification).to.satisfy(n => n.type !== notificationType || n.comment.id !== commentId)
387 function emailFinder (email: object) {
388 const text: string = email[ 'text' ]
390 return text.includes(' mentioned ') && text.includes(uuid) && text.includes(byAccountDisplayName)
393 await checkNotification(base, notificationChecker, emailFinder, type)
396 let lastEmailCount = 0
397 async function checkNewCommentOnMyVideo (base: CheckerBaseParams, uuid: string, commentId: number, threadId: number, type: CheckerType) {
398 const notificationType = UserNotificationType.NEW_COMMENT_ON_MY_VIDEO
400 function notificationChecker (notification: UserNotification, type: CheckerType) {
401 if (type === 'presence') {
402 expect(notification).to.not.be.undefined
403 expect(notification.type).to.equal(notificationType)
405 checkComment(notification.comment, commentId, threadId)
406 checkActor(notification.comment.account)
407 checkVideo(notification.comment.video, undefined, uuid)
409 expect(notification).to.satisfy((n: UserNotification) => {
410 return n === undefined || n.comment === undefined || n.comment.id !== commentId
415 const commentUrl = `http://localhost:${base.server.port}/videos/watch/${uuid};threadId=${threadId}`
416 function emailFinder (email: object) {
417 return email[ 'text' ].indexOf(commentUrl) !== -1
420 await checkNotification(base, notificationChecker, emailFinder, type)
422 if (type === 'presence') {
423 // We cannot detect email duplicates, so check we received another email
424 expect(base.emails).to.have.length.above(lastEmailCount)
425 lastEmailCount = base.emails.length
429 async function checkNewVideoAbuseForModerators (base: CheckerBaseParams, videoUUID: string, videoName: string, type: CheckerType) {
430 const notificationType = UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS
432 function notificationChecker (notification: UserNotification, type: CheckerType) {
433 if (type === 'presence') {
434 expect(notification).to.not.be.undefined
435 expect(notification.type).to.equal(notificationType)
437 expect(notification.videoAbuse.id).to.be.a('number')
438 checkVideo(notification.videoAbuse.video, videoName, videoUUID)
440 expect(notification).to.satisfy((n: UserNotification) => {
441 return n === undefined || n.videoAbuse === undefined || n.videoAbuse.video.uuid !== videoUUID
446 function emailFinder (email: object) {
447 const text = email[ 'text' ]
448 return text.indexOf(videoUUID) !== -1 && text.indexOf('abuse') !== -1
451 await checkNotification(base, notificationChecker, emailFinder, type)
454 async function checkVideoAutoBlacklistForModerators (base: CheckerBaseParams, videoUUID: string, videoName: string, type: CheckerType) {
455 const notificationType = UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS
457 function notificationChecker (notification: UserNotification, type: CheckerType) {
458 if (type === 'presence') {
459 expect(notification).to.not.be.undefined
460 expect(notification.type).to.equal(notificationType)
462 expect(notification.videoBlacklist.video.id).to.be.a('number')
463 checkVideo(notification.videoBlacklist.video, videoName, videoUUID)
465 expect(notification).to.satisfy((n: UserNotification) => {
466 return n === undefined || n.video === undefined || n.video.uuid !== videoUUID
471 function emailFinder (email: object) {
472 const text = email[ 'text' ]
473 return text.indexOf(videoUUID) !== -1 && email[ 'text' ].indexOf('video-auto-blacklist/list') !== -1
476 await checkNotification(base, notificationChecker, emailFinder, type)
479 async function checkNewBlacklistOnMyVideo (
480 base: CheckerBaseParams,
483 blacklistType: 'blacklist' | 'unblacklist'
485 const notificationType = blacklistType === 'blacklist'
486 ? UserNotificationType.BLACKLIST_ON_MY_VIDEO
487 : UserNotificationType.UNBLACKLIST_ON_MY_VIDEO
489 function notificationChecker (notification: UserNotification) {
490 expect(notification).to.not.be.undefined
491 expect(notification.type).to.equal(notificationType)
493 const video = blacklistType === 'blacklist' ? notification.videoBlacklist.video : notification.video
495 checkVideo(video, videoName, videoUUID)
498 function emailFinder (email: object) {
499 const text = email[ 'text' ]
500 return text.indexOf(videoUUID) !== -1 && text.indexOf(' ' + blacklistType) !== -1
503 await checkNotification(base, notificationChecker, emailFinder, 'presence')
506 // ---------------------------------------------------------------------------
512 markAsReadAllNotifications,
513 checkMyVideoImportIsFinished,
515 checkAutoInstanceFollowing,
516 checkVideoIsPublished,
517 checkNewVideoFromSubscription,
519 checkNewCommentOnMyVideo,
520 checkNewBlacklistOnMyVideo,
522 updateMyNotificationSettings,
523 checkNewVideoAbuseForModerators,
524 checkVideoAutoBlacklistForModerators,
525 getUserNotifications,
526 markAsReadNotifications,
528 checkNewInstanceFollower