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'
8 function updateMyNotificationSettings (url: string, token: string, settings: UserNotificationSetting, statusCodeExpected = 204) {
9 const path = '/api/v1/users/me/notification-settings'
11 return makePutBodyRequest({
20 function getUserNotifications (url: string, token: string, start: number, count: number, sort = '-createdAt', statusCodeExpected = 200) {
21 const path = '/api/v1/users/me/notifications'
23 return makeGetRequest({
36 function markAsReadNotifications (url: string, token: string, ids: number[], statusCodeExpected = 204) {
37 const path = '/api/v1/users/me/notifications/read'
39 return makePostBodyRequest({
48 async function getLastNotification (serverUrl: string, accessToken: string) {
49 const res = await getUserNotifications(serverUrl, accessToken, 0, 1, '-createdAt')
51 if (res.body.total === 0) return undefined
53 return res.body.data[0] as UserNotification
56 type CheckerBaseParams = {
59 socketNotifications: UserNotification[]
61 check?: { web: boolean, mail: boolean }
64 type CheckerType = 'presence' | 'absence'
66 async function checkNotification (
67 base: CheckerBaseParams,
68 lastNotificationChecker: (notification: UserNotification) => void,
69 socketNotificationFinder: (notification: UserNotification) => boolean,
70 emailNotificationFinder: (email: object) => boolean,
71 checkType: 'presence' | 'absence'
73 const check = base.check || { web: true, mail: true }
76 const notification = await getLastNotification(base.server.url, base.token)
77 lastNotificationChecker(notification)
79 const socketNotification = base.socketNotifications.find(n => socketNotificationFinder(n))
81 if (checkType === 'presence') expect(socketNotification, 'The socket notification is absent.').to.not.be.undefined
82 else expect(socketNotification, 'The socket notification is present.').to.be.undefined
87 const email = base.emails
90 .find(e => emailNotificationFinder(e))
92 if (checkType === 'presence') expect(email, 'The email is present.').to.not.be.undefined
93 else expect(email, 'The email is absent.').to.be.undefined
97 async function checkNewVideoFromSubscription (base: CheckerBaseParams, videoName: string, videoUUID: string, type: CheckerType) {
98 const notificationType = UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION
100 function lastNotificationChecker (notification: UserNotification) {
101 if (type === 'presence') {
102 expect(notification).to.not.be.undefined
103 expect(notification.type).to.equal(notificationType)
104 expect(notification.video.name).to.equal(videoName)
106 expect(notification.video).to.satisfy(v => v === undefined || v.name !== videoName)
110 function socketFinder (notification: UserNotification) {
111 return notification.type === notificationType && notification.video.name === videoName
114 function emailFinder (email: object) {
115 return email[ 'text' ].indexOf(videoUUID) !== -1
118 await checkNotification(base, lastNotificationChecker, socketFinder, emailFinder, type)
121 let lastEmailCount = 0
122 async function checkNewCommentOnMyVideo (base: CheckerBaseParams, uuid: string, commentId: number, threadId: number, type: CheckerType) {
123 const notificationType = UserNotificationType.NEW_COMMENT_ON_MY_VIDEO
125 function lastNotificationChecker (notification: UserNotification) {
126 if (type === 'presence') {
127 expect(notification).to.not.be.undefined
128 expect(notification.type).to.equal(notificationType)
129 expect(notification.comment.id).to.equal(commentId)
130 expect(notification.comment.account.displayName).to.equal('root')
132 expect(notification).to.satisfy((n: UserNotification) => {
133 return n === undefined || n.comment === undefined || n.comment.id !== commentId
138 function socketFinder (notification: UserNotification) {
139 return notification.type === notificationType &&
140 notification.comment.id === commentId &&
141 notification.comment.account.displayName === 'root'
144 const commentUrl = `http://localhost:9001/videos/watch/${uuid};threadId=${threadId}`
145 function emailFinder (email: object) {
146 return email[ 'text' ].indexOf(commentUrl) !== -1
149 await checkNotification(base, lastNotificationChecker, socketFinder, emailFinder, type)
151 if (type === 'presence') {
152 // We cannot detect email duplicates, so check we received another email
153 expect(base.emails).to.have.length.above(lastEmailCount)
154 lastEmailCount = base.emails.length
158 async function checkNewVideoAbuseForModerators (base: CheckerBaseParams, videoUUID: string, videoName: string, type: CheckerType) {
159 const notificationType = UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS
161 function lastNotificationChecker (notification: UserNotification) {
162 if (type === 'presence') {
163 expect(notification).to.not.be.undefined
164 expect(notification.type).to.equal(notificationType)
165 expect(notification.videoAbuse.video.uuid).to.equal(videoUUID)
166 expect(notification.videoAbuse.video.name).to.equal(videoName)
168 expect(notification).to.satisfy((n: UserNotification) => {
169 return n === undefined || n.videoAbuse === undefined || n.videoAbuse.video.uuid !== videoUUID
174 function socketFinder (notification: UserNotification) {
175 return notification.type === notificationType && notification.videoAbuse.video.uuid === videoUUID
178 function emailFinder (email: object) {
179 const text = email[ 'text' ]
180 return text.indexOf(videoUUID) !== -1 && text.indexOf('abuse') !== -1
183 await checkNotification(base, lastNotificationChecker, socketFinder, emailFinder, type)
186 async function checkNewBlacklistOnMyVideo (
187 base: CheckerBaseParams,
190 blacklistType: 'blacklist' | 'unblacklist'
192 const notificationType = blacklistType === 'blacklist'
193 ? UserNotificationType.BLACKLIST_ON_MY_VIDEO
194 : UserNotificationType.UNBLACKLIST_ON_MY_VIDEO
196 function lastNotificationChecker (notification: UserNotification) {
197 expect(notification).to.not.be.undefined
198 expect(notification.type).to.equal(notificationType)
200 const video = blacklistType === 'blacklist' ? notification.videoBlacklist.video : notification.video
202 expect(video.uuid).to.equal(videoUUID)
203 expect(video.name).to.equal(videoName)
206 function socketFinder (notification: UserNotification) {
207 return notification.type === notificationType && (notification.video || notification.videoBlacklist.video).uuid === videoUUID
210 function emailFinder (email: object) {
211 const text = email[ 'text' ]
212 return text.indexOf(videoUUID) !== -1 && text.indexOf(' ' + blacklistType) !== -1
215 await checkNotification(base, lastNotificationChecker, socketFinder, emailFinder, 'presence')
218 // ---------------------------------------------------------------------------
224 checkNewVideoFromSubscription,
225 checkNewCommentOnMyVideo,
226 checkNewBlacklistOnMyVideo,
227 updateMyNotificationSettings,
228 checkNewVideoAbuseForModerators,
229 getUserNotifications,
230 markAsReadNotifications,