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 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({
58 async function getLastNotification (serverUrl: string, accessToken: string) {
59 const res = await getUserNotifications(serverUrl, accessToken, 0, 1, undefined, '-createdAt')
61 if (res.body.total === 0) return undefined
63 return res.body.data[0] as UserNotification
66 type CheckerBaseParams = {
69 socketNotifications: UserNotification[]
71 check?: { web: boolean, mail: boolean }
74 type CheckerType = 'presence' | 'absence'
76 async function checkNotification (
77 base: CheckerBaseParams,
78 notificationChecker: (notification: UserNotification, type: CheckerType) => void,
79 emailNotificationFinder: (email: object) => boolean,
80 checkType: CheckerType
82 const check = base.check || { web: true, mail: true }
85 const notification = await getLastNotification(base.server.url, base.token)
87 if (notification || checkType !== 'absence') {
88 notificationChecker(notification, checkType)
91 const socketNotification = base.socketNotifications.find(n => {
93 notificationChecker(n, 'presence')
100 if (checkType === 'presence') {
101 const obj = inspect(base.socketNotifications, { depth: 5 })
102 expect(socketNotification, 'The socket notification is absent. ' + obj).to.not.be.undefined
104 const obj = inspect(socketNotification, { depth: 5 })
105 expect(socketNotification, 'The socket notification is present. ' + obj).to.be.undefined
111 const email = base.emails
114 .find(e => emailNotificationFinder(e))
116 if (checkType === 'presence') {
117 expect(email, 'The email is absent. ' + inspect(base.emails)).to.not.be.undefined
119 expect(email, 'The email is present. ' + inspect(email)).to.be.undefined
124 function checkVideo (video: any, videoName?: string, videoUUID?: string) {
125 expect(video.name).to.be.a('string')
126 expect(video.name).to.not.be.empty
127 if (videoName) expect(video.name).to.equal(videoName)
129 expect(video.uuid).to.be.a('string')
130 expect(video.uuid).to.not.be.empty
131 if (videoUUID) expect(video.uuid).to.equal(videoUUID)
133 expect(video.id).to.be.a('number')
136 function checkActor (actor: any) {
137 expect(actor.displayName).to.be.a('string')
138 expect(actor.displayName).to.not.be.empty
141 function checkComment (comment: any, commentId: number, threadId: number) {
142 expect(comment.id).to.equal(commentId)
143 expect(comment.threadId).to.equal(threadId)
146 async function checkNewVideoFromSubscription (base: CheckerBaseParams, videoName: string, videoUUID: string, type: CheckerType) {
147 const notificationType = UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION
149 function notificationChecker (notification: UserNotification, type: CheckerType) {
150 if (type === 'presence') {
151 expect(notification).to.not.be.undefined
152 expect(notification.type).to.equal(notificationType)
154 checkVideo(notification.video, videoName, videoUUID)
155 checkActor(notification.video.channel)
157 expect(notification.video).to.satisfy(v => v === undefined || v.name !== videoName)
161 function emailFinder (email: object) {
162 return email[ 'text' ].indexOf(videoUUID) !== -1
165 await checkNotification(base, notificationChecker, emailFinder, type)
168 async function checkVideoIsPublished (base: CheckerBaseParams, videoName: string, videoUUID: string, type: CheckerType) {
169 const notificationType = UserNotificationType.MY_VIDEO_PUBLISHED
171 function notificationChecker (notification: UserNotification, type: CheckerType) {
172 if (type === 'presence') {
173 expect(notification).to.not.be.undefined
174 expect(notification.type).to.equal(notificationType)
176 checkVideo(notification.video, videoName, videoUUID)
177 checkActor(notification.video.channel)
179 expect(notification.video).to.satisfy(v => v === undefined || v.name !== videoName)
183 function emailFinder (email: object) {
184 const text: string = email[ 'text' ]
185 return text.includes(videoUUID) && text.includes('Your video')
188 await checkNotification(base, notificationChecker, emailFinder, type)
191 async function checkMyVideoImportIsFinished (
192 base: CheckerBaseParams,
199 const notificationType = success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR
201 function notificationChecker (notification: UserNotification, type: CheckerType) {
202 if (type === 'presence') {
203 expect(notification).to.not.be.undefined
204 expect(notification.type).to.equal(notificationType)
206 expect(notification.videoImport.targetUrl).to.equal(url)
208 if (success) checkVideo(notification.videoImport.video, videoName, videoUUID)
210 expect(notification.videoImport).to.satisfy(i => i === undefined || i.targetUrl !== url)
214 function emailFinder (email: object) {
215 const text: string = email[ 'text' ]
216 const toFind = success ? ' finished' : ' error'
218 return text.includes(url) && text.includes(toFind)
221 await checkNotification(base, notificationChecker, emailFinder, type)
224 async function checkUserRegistered (base: CheckerBaseParams, username: string, type: CheckerType) {
225 const notificationType = UserNotificationType.NEW_USER_REGISTRATION
227 function notificationChecker (notification: UserNotification, type: CheckerType) {
228 if (type === 'presence') {
229 expect(notification).to.not.be.undefined
230 expect(notification.type).to.equal(notificationType)
232 checkActor(notification.account)
233 expect(notification.account.name).to.equal(username)
235 expect(notification).to.satisfy(n => n.type !== notificationType || n.account.name !== username)
239 function emailFinder (email: object) {
240 const text: string = email[ 'text' ]
242 return text.includes(' registered ') && text.includes(username)
245 await checkNotification(base, notificationChecker, emailFinder, type)
248 async function checkNewActorFollow (
249 base: CheckerBaseParams,
250 followType: 'channel' | 'account',
251 followerName: string,
252 followerDisplayName: string,
253 followingDisplayName: string,
256 const notificationType = UserNotificationType.NEW_FOLLOW
258 function notificationChecker (notification: UserNotification, type: CheckerType) {
259 if (type === 'presence') {
260 expect(notification).to.not.be.undefined
261 expect(notification.type).to.equal(notificationType)
263 checkActor(notification.actorFollow.follower)
264 expect(notification.actorFollow.follower.displayName).to.equal(followerDisplayName)
265 expect(notification.actorFollow.follower.name).to.equal(followerName)
267 checkActor(notification.actorFollow.following)
268 expect(notification.actorFollow.following.displayName).to.equal(followingDisplayName)
269 expect(notification.actorFollow.following.type).to.equal(followType)
271 expect(notification).to.satisfy(n => {
272 return n.type !== notificationType ||
273 (n.actorFollow.follower.name !== followerName && n.actorFollow.following !== followingDisplayName)
278 function emailFinder (email: object) {
279 const text: string = email[ 'text' ]
281 return text.includes('Your ' + followType) && text.includes(followingDisplayName) && text.includes(followerDisplayName)
284 await checkNotification(base, notificationChecker, emailFinder, type)
287 async function checkCommentMention (
288 base: CheckerBaseParams,
292 byAccountDisplayName: string,
295 const notificationType = UserNotificationType.COMMENT_MENTION
297 function notificationChecker (notification: UserNotification, type: CheckerType) {
298 if (type === 'presence') {
299 expect(notification).to.not.be.undefined
300 expect(notification.type).to.equal(notificationType)
302 checkComment(notification.comment, commentId, threadId)
303 checkActor(notification.comment.account)
304 expect(notification.comment.account.displayName).to.equal(byAccountDisplayName)
306 checkVideo(notification.comment.video, undefined, uuid)
308 expect(notification).to.satisfy(n => n.type !== notificationType || n.comment.id !== commentId)
312 function emailFinder (email: object) {
313 const text: string = email[ 'text' ]
315 return text.includes(' mentioned ') && text.includes(uuid) && text.includes(byAccountDisplayName)
318 await checkNotification(base, notificationChecker, emailFinder, type)
321 let lastEmailCount = 0
322 async function checkNewCommentOnMyVideo (base: CheckerBaseParams, uuid: string, commentId: number, threadId: number, type: CheckerType) {
323 const notificationType = UserNotificationType.NEW_COMMENT_ON_MY_VIDEO
325 function notificationChecker (notification: UserNotification, type: CheckerType) {
326 if (type === 'presence') {
327 expect(notification).to.not.be.undefined
328 expect(notification.type).to.equal(notificationType)
330 checkComment(notification.comment, commentId, threadId)
331 checkActor(notification.comment.account)
332 checkVideo(notification.comment.video, undefined, uuid)
334 expect(notification).to.satisfy((n: UserNotification) => {
335 return n === undefined || n.comment === undefined || n.comment.id !== commentId
340 const commentUrl = `http://localhost:9001/videos/watch/${uuid};threadId=${threadId}`
341 function emailFinder (email: object) {
342 return email[ 'text' ].indexOf(commentUrl) !== -1
345 await checkNotification(base, notificationChecker, emailFinder, type)
347 if (type === 'presence') {
348 // We cannot detect email duplicates, so check we received another email
349 expect(base.emails).to.have.length.above(lastEmailCount)
350 lastEmailCount = base.emails.length
354 async function checkNewVideoAbuseForModerators (base: CheckerBaseParams, videoUUID: string, videoName: string, type: CheckerType) {
355 const notificationType = UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS
357 function notificationChecker (notification: UserNotification, type: CheckerType) {
358 if (type === 'presence') {
359 expect(notification).to.not.be.undefined
360 expect(notification.type).to.equal(notificationType)
362 expect(notification.videoAbuse.id).to.be.a('number')
363 checkVideo(notification.videoAbuse.video, videoName, videoUUID)
365 expect(notification).to.satisfy((n: UserNotification) => {
366 return n === undefined || n.videoAbuse === undefined || n.videoAbuse.video.uuid !== videoUUID
371 function emailFinder (email: object) {
372 const text = email[ 'text' ]
373 return text.indexOf(videoUUID) !== -1 && text.indexOf('abuse') !== -1
376 await checkNotification(base, notificationChecker, emailFinder, type)
379 async function checkNewBlacklistOnMyVideo (
380 base: CheckerBaseParams,
383 blacklistType: 'blacklist' | 'unblacklist'
385 const notificationType = blacklistType === 'blacklist'
386 ? UserNotificationType.BLACKLIST_ON_MY_VIDEO
387 : UserNotificationType.UNBLACKLIST_ON_MY_VIDEO
389 function notificationChecker (notification: UserNotification) {
390 expect(notification).to.not.be.undefined
391 expect(notification.type).to.equal(notificationType)
393 const video = blacklistType === 'blacklist' ? notification.videoBlacklist.video : notification.video
395 checkVideo(video, videoName, videoUUID)
398 function emailFinder (email: object) {
399 const text = email[ 'text' ]
400 return text.indexOf(videoUUID) !== -1 && text.indexOf(' ' + blacklistType) !== -1
403 await checkNotification(base, notificationChecker, emailFinder, 'presence')
406 // ---------------------------------------------------------------------------
412 checkMyVideoImportIsFinished,
414 checkVideoIsPublished,
415 checkNewVideoFromSubscription,
417 checkNewCommentOnMyVideo,
418 checkNewBlacklistOnMyVideo,
420 updateMyNotificationSettings,
421 checkNewVideoAbuseForModerators,
422 getUserNotifications,
423 markAsReadNotifications,