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({
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.video).to.satisfy(v => v === undefined || v.name !== videoName)
172 function emailFinder (email: object) {
173 return email[ 'text' ].indexOf(videoUUID) !== -1
176 await checkNotification(base, notificationChecker, emailFinder, type)
179 async function checkVideoIsPublished (base: CheckerBaseParams, videoName: string, videoUUID: string, type: CheckerType) {
180 const notificationType = UserNotificationType.MY_VIDEO_PUBLISHED
182 function notificationChecker (notification: UserNotification, type: CheckerType) {
183 if (type === 'presence') {
184 expect(notification).to.not.be.undefined
185 expect(notification.type).to.equal(notificationType)
187 checkVideo(notification.video, videoName, videoUUID)
188 checkActor(notification.video.channel)
190 expect(notification.video).to.satisfy(v => v === undefined || v.name !== videoName)
194 function emailFinder (email: object) {
195 const text: string = email[ 'text' ]
196 return text.includes(videoUUID) && text.includes('Your video')
199 await checkNotification(base, notificationChecker, emailFinder, type)
202 async function checkMyVideoImportIsFinished (
203 base: CheckerBaseParams,
210 const notificationType = success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR
212 function notificationChecker (notification: UserNotification, type: CheckerType) {
213 if (type === 'presence') {
214 expect(notification).to.not.be.undefined
215 expect(notification.type).to.equal(notificationType)
217 expect(notification.videoImport.targetUrl).to.equal(url)
219 if (success) checkVideo(notification.videoImport.video, videoName, videoUUID)
221 expect(notification.videoImport).to.satisfy(i => i === undefined || i.targetUrl !== url)
225 function emailFinder (email: object) {
226 const text: string = email[ 'text' ]
227 const toFind = success ? ' finished' : ' error'
229 return text.includes(url) && text.includes(toFind)
232 await checkNotification(base, notificationChecker, emailFinder, type)
235 async function checkUserRegistered (base: CheckerBaseParams, username: string, type: CheckerType) {
236 const notificationType = UserNotificationType.NEW_USER_REGISTRATION
238 function notificationChecker (notification: UserNotification, type: CheckerType) {
239 if (type === 'presence') {
240 expect(notification).to.not.be.undefined
241 expect(notification.type).to.equal(notificationType)
243 checkActor(notification.account)
244 expect(notification.account.name).to.equal(username)
246 expect(notification).to.satisfy(n => n.type !== notificationType || n.account.name !== username)
250 function emailFinder (email: object) {
251 const text: string = email[ 'text' ]
253 return text.includes(' registered ') && text.includes(username)
256 await checkNotification(base, notificationChecker, emailFinder, type)
259 async function checkNewActorFollow (
260 base: CheckerBaseParams,
261 followType: 'channel' | 'account',
262 followerName: string,
263 followerDisplayName: string,
264 followingDisplayName: string,
267 const notificationType = UserNotificationType.NEW_FOLLOW
269 function notificationChecker (notification: UserNotification, type: CheckerType) {
270 if (type === 'presence') {
271 expect(notification).to.not.be.undefined
272 expect(notification.type).to.equal(notificationType)
274 checkActor(notification.actorFollow.follower)
275 expect(notification.actorFollow.follower.displayName).to.equal(followerDisplayName)
276 expect(notification.actorFollow.follower.name).to.equal(followerName)
277 expect(notification.actorFollow.follower.host).to.not.be.undefined
279 expect(notification.actorFollow.following.displayName).to.equal(followingDisplayName)
280 expect(notification.actorFollow.following.type).to.equal(followType)
282 expect(notification).to.satisfy(n => {
283 return n.type !== notificationType ||
284 (n.actorFollow.follower.name !== followerName && n.actorFollow.following !== followingDisplayName)
289 function emailFinder (email: object) {
290 const text: string = email[ 'text' ]
292 return text.includes('Your ' + followType) && text.includes(followingDisplayName) && text.includes(followerDisplayName)
295 await checkNotification(base, notificationChecker, emailFinder, type)
298 async function checkCommentMention (
299 base: CheckerBaseParams,
303 byAccountDisplayName: string,
306 const notificationType = UserNotificationType.COMMENT_MENTION
308 function notificationChecker (notification: UserNotification, type: CheckerType) {
309 if (type === 'presence') {
310 expect(notification).to.not.be.undefined
311 expect(notification.type).to.equal(notificationType)
313 checkComment(notification.comment, commentId, threadId)
314 checkActor(notification.comment.account)
315 expect(notification.comment.account.displayName).to.equal(byAccountDisplayName)
317 checkVideo(notification.comment.video, undefined, uuid)
319 expect(notification).to.satisfy(n => n.type !== notificationType || n.comment.id !== commentId)
323 function emailFinder (email: object) {
324 const text: string = email[ 'text' ]
326 return text.includes(' mentioned ') && text.includes(uuid) && text.includes(byAccountDisplayName)
329 await checkNotification(base, notificationChecker, emailFinder, type)
332 let lastEmailCount = 0
333 async function checkNewCommentOnMyVideo (base: CheckerBaseParams, uuid: string, commentId: number, threadId: number, type: CheckerType) {
334 const notificationType = UserNotificationType.NEW_COMMENT_ON_MY_VIDEO
336 function notificationChecker (notification: UserNotification, type: CheckerType) {
337 if (type === 'presence') {
338 expect(notification).to.not.be.undefined
339 expect(notification.type).to.equal(notificationType)
341 checkComment(notification.comment, commentId, threadId)
342 checkActor(notification.comment.account)
343 checkVideo(notification.comment.video, undefined, uuid)
345 expect(notification).to.satisfy((n: UserNotification) => {
346 return n === undefined || n.comment === undefined || n.comment.id !== commentId
351 const commentUrl = `http://localhost:9001/videos/watch/${uuid};threadId=${threadId}`
352 function emailFinder (email: object) {
353 return email[ 'text' ].indexOf(commentUrl) !== -1
356 await checkNotification(base, notificationChecker, emailFinder, type)
358 if (type === 'presence') {
359 // We cannot detect email duplicates, so check we received another email
360 expect(base.emails).to.have.length.above(lastEmailCount)
361 lastEmailCount = base.emails.length
365 async function checkNewVideoAbuseForModerators (base: CheckerBaseParams, videoUUID: string, videoName: string, type: CheckerType) {
366 const notificationType = UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS
368 function notificationChecker (notification: UserNotification, type: CheckerType) {
369 if (type === 'presence') {
370 expect(notification).to.not.be.undefined
371 expect(notification.type).to.equal(notificationType)
373 expect(notification.videoAbuse.id).to.be.a('number')
374 checkVideo(notification.videoAbuse.video, videoName, videoUUID)
376 expect(notification).to.satisfy((n: UserNotification) => {
377 return n === undefined || n.videoAbuse === undefined || n.videoAbuse.video.uuid !== videoUUID
382 function emailFinder (email: object) {
383 const text = email[ 'text' ]
384 return text.indexOf(videoUUID) !== -1 && text.indexOf('abuse') !== -1
387 await checkNotification(base, notificationChecker, emailFinder, type)
390 async function checkNewBlacklistOnMyVideo (
391 base: CheckerBaseParams,
394 blacklistType: 'blacklist' | 'unblacklist'
396 const notificationType = blacklistType === 'blacklist'
397 ? UserNotificationType.BLACKLIST_ON_MY_VIDEO
398 : UserNotificationType.UNBLACKLIST_ON_MY_VIDEO
400 function notificationChecker (notification: UserNotification) {
401 expect(notification).to.not.be.undefined
402 expect(notification.type).to.equal(notificationType)
404 const video = blacklistType === 'blacklist' ? notification.videoBlacklist.video : notification.video
406 checkVideo(video, videoName, videoUUID)
409 function emailFinder (email: object) {
410 const text = email[ 'text' ]
411 return text.indexOf(videoUUID) !== -1 && text.indexOf(' ' + blacklistType) !== -1
414 await checkNotification(base, notificationChecker, emailFinder, 'presence')
417 // ---------------------------------------------------------------------------
423 markAsReadAllNotifications,
424 checkMyVideoImportIsFinished,
426 checkVideoIsPublished,
427 checkNewVideoFromSubscription,
429 checkNewCommentOnMyVideo,
430 checkNewBlacklistOnMyVideo,
432 updateMyNotificationSettings,
433 checkNewVideoAbuseForModerators,
434 getUserNotifications,
435 markAsReadNotifications,