]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/shared/notifications.ts
Remove old migration files
[github/Chocobozzz/PeerTube.git] / server / tests / shared / notifications.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
cef534ed 2
cef534ed 3import { expect } from 'chai'
dc133480 4import { inspect } from 'util'
c55e3d72
C
5import {
6 AbuseState,
7 PluginType,
8 UserNotification,
9 UserNotificationSetting,
10 UserNotificationSettingValue,
11 UserNotificationType
12} from '@shared/models'
d0800f76 13import {
14 createMultipleServers,
15 doubleFollow,
16 PeerTubeServer,
17 setAccessTokensToServers,
18 setDefaultAccountAvatar,
19 setDefaultChannelAvatar
20} from '@shared/server-commands'
c55e3d72 21import { MockSmtpServer } from './mock-servers'
cef534ed 22
29837f88
C
23type CheckerBaseParams = {
24 server: PeerTubeServer
25 emails: any[]
26 socketNotifications: UserNotification[]
27 token: string
28 check?: { web: boolean, mail: boolean }
29}
30
31type CheckerType = 'presence' | 'absence'
32
dd0ebb71
C
33function getAllNotificationsSettings (): UserNotificationSetting {
34 return {
35 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
36 newCommentOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
37 abuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
38 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
39 blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
40 myVideoImportFinished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
41 myVideoPublished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
42 commentMention: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
43 newFollow: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
44 newUserRegistration: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
45 newInstanceFollower: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
46 abuseNewMessage: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
47 abuseStateChange: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
48 autoInstanceFollowing: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
49 newPeerTubeVersion: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
50 newPluginVersion: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
51 }
cef534ed
C
52}
53
29837f88
C
54async function checkNewVideoFromSubscription (options: CheckerBaseParams & {
55 videoName: string
56 shortUUID: string
dc133480 57 checkType: CheckerType
29837f88
C
58}) {
59 const { videoName, shortUUID } = options
cef534ed
C
60 const notificationType = UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION
61
29837f88
C
62 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
63 if (checkType === 'presence') {
cef534ed
C
64 expect(notification).to.not.be.undefined
65 expect(notification.type).to.equal(notificationType)
dc133480 66
29837f88 67 checkVideo(notification.video, videoName, shortUUID)
dc133480 68 checkActor(notification.video.channel)
cef534ed 69 } else {
7ccddd7b
JM
70 expect(notification).to.satisfy((n: UserNotification) => {
71 return n === undefined || n.type !== UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION || n.video.name !== videoName
72 })
cef534ed
C
73 }
74 }
75
df4c603d 76 function emailNotificationFinder (email: object) {
a1587156 77 const text = email['text']
29837f88 78 return text.indexOf(shortUUID) !== -1 && text.indexOf('Your subscription') !== -1
dc133480
C
79 }
80
29837f88 81 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
dc133480
C
82}
83
29837f88
C
84async function checkVideoIsPublished (options: CheckerBaseParams & {
85 videoName: string
86 shortUUID: string
87 checkType: CheckerType
88}) {
89 const { videoName, shortUUID } = options
dc133480
C
90 const notificationType = UserNotificationType.MY_VIDEO_PUBLISHED
91
29837f88
C
92 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
93 if (checkType === 'presence') {
dc133480
C
94 expect(notification).to.not.be.undefined
95 expect(notification.type).to.equal(notificationType)
96
29837f88 97 checkVideo(notification.video, videoName, shortUUID)
dc133480
C
98 checkActor(notification.video.channel)
99 } else {
100 expect(notification.video).to.satisfy(v => v === undefined || v.name !== videoName)
101 }
cef534ed
C
102 }
103
df4c603d 104 function emailNotificationFinder (email: object) {
a1587156 105 const text: string = email['text']
29837f88 106 return text.includes(shortUUID) && text.includes('Your video')
cef534ed
C
107 }
108
29837f88 109 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
dc133480
C
110}
111
29837f88
C
112async function checkMyVideoImportIsFinished (options: CheckerBaseParams & {
113 videoName: string
114 shortUUID: string
115 url: string
116 success: boolean
117 checkType: CheckerType
118}) {
119 const { videoName, shortUUID, url, success } = options
120
dc133480
C
121 const notificationType = success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR
122
29837f88
C
123 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
124 if (checkType === 'presence') {
dc133480
C
125 expect(notification).to.not.be.undefined
126 expect(notification.type).to.equal(notificationType)
127
128 expect(notification.videoImport.targetUrl).to.equal(url)
129
29837f88 130 if (success) checkVideo(notification.videoImport.video, videoName, shortUUID)
dc133480
C
131 } else {
132 expect(notification.videoImport).to.satisfy(i => i === undefined || i.targetUrl !== url)
133 }
134 }
135
df4c603d 136 function emailNotificationFinder (email: object) {
a1587156 137 const text: string = email['text']
dc133480
C
138 const toFind = success ? ' finished' : ' error'
139
140 return text.includes(url) && text.includes(toFind)
141 }
142
29837f88 143 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
cef534ed
C
144}
145
29837f88
C
146async function checkUserRegistered (options: CheckerBaseParams & {
147 username: string
148 checkType: CheckerType
149}) {
150 const { username } = options
f7cc67b4
C
151 const notificationType = UserNotificationType.NEW_USER_REGISTRATION
152
29837f88
C
153 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
154 if (checkType === 'presence') {
f7cc67b4
C
155 expect(notification).to.not.be.undefined
156 expect(notification.type).to.equal(notificationType)
157
158 checkActor(notification.account)
159 expect(notification.account.name).to.equal(username)
160 } else {
161 expect(notification).to.satisfy(n => n.type !== notificationType || n.account.name !== username)
162 }
163 }
164
df4c603d 165 function emailNotificationFinder (email: object) {
a1587156 166 const text: string = email['text']
f7cc67b4 167
df4c603d 168 return text.includes(' registered.') && text.includes(username)
f7cc67b4
C
169 }
170
29837f88 171 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
f7cc67b4
C
172}
173
29837f88
C
174async function checkNewActorFollow (options: CheckerBaseParams & {
175 followType: 'channel' | 'account'
176 followerName: string
177 followerDisplayName: string
178 followingDisplayName: string
179 checkType: CheckerType
180}) {
181 const { followType, followerName, followerDisplayName, followingDisplayName } = options
f7cc67b4
C
182 const notificationType = UserNotificationType.NEW_FOLLOW
183
29837f88
C
184 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
185 if (checkType === 'presence') {
f7cc67b4
C
186 expect(notification).to.not.be.undefined
187 expect(notification.type).to.equal(notificationType)
188
189 checkActor(notification.actorFollow.follower)
190 expect(notification.actorFollow.follower.displayName).to.equal(followerDisplayName)
191 expect(notification.actorFollow.follower.name).to.equal(followerName)
ebff55d8 192 expect(notification.actorFollow.follower.host).to.not.be.undefined
f7cc67b4 193
8424c402
C
194 const following = notification.actorFollow.following
195 expect(following.displayName).to.equal(followingDisplayName)
196 expect(following.type).to.equal(followType)
f7cc67b4
C
197 } else {
198 expect(notification).to.satisfy(n => {
199 return n.type !== notificationType ||
200 (n.actorFollow.follower.name !== followerName && n.actorFollow.following !== followingDisplayName)
201 })
202 }
203 }
204
df4c603d 205 function emailNotificationFinder (email: object) {
a1587156 206 const text: string = email['text']
f7cc67b4 207
df4c603d 208 return text.includes(followType) && text.includes(followingDisplayName) && text.includes(followerDisplayName)
f7cc67b4
C
209 }
210
29837f88 211 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
f7cc67b4
C
212}
213
29837f88
C
214async function checkNewInstanceFollower (options: CheckerBaseParams & {
215 followerHost: string
216 checkType: CheckerType
217}) {
218 const { followerHost } = options
883993c8
C
219 const notificationType = UserNotificationType.NEW_INSTANCE_FOLLOWER
220
29837f88
C
221 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
222 if (checkType === 'presence') {
883993c8
C
223 expect(notification).to.not.be.undefined
224 expect(notification.type).to.equal(notificationType)
225
226 checkActor(notification.actorFollow.follower)
227 expect(notification.actorFollow.follower.name).to.equal('peertube')
228 expect(notification.actorFollow.follower.host).to.equal(followerHost)
229
230 expect(notification.actorFollow.following.name).to.equal('peertube')
231 } else {
232 expect(notification).to.satisfy(n => {
233 return n.type !== notificationType || n.actorFollow.follower.host !== followerHost
234 })
235 }
236 }
237
df4c603d 238 function emailNotificationFinder (email: object) {
a1587156 239 const text: string = email['text']
883993c8
C
240
241 return text.includes('instance has a new follower') && text.includes(followerHost)
242 }
243
29837f88 244 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
883993c8
C
245}
246
29837f88
C
247async function checkAutoInstanceFollowing (options: CheckerBaseParams & {
248 followerHost: string
249 followingHost: string
250 checkType: CheckerType
251}) {
252 const { followerHost, followingHost } = options
8424c402
C
253 const notificationType = UserNotificationType.AUTO_INSTANCE_FOLLOWING
254
29837f88
C
255 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
256 if (checkType === 'presence') {
8424c402
C
257 expect(notification).to.not.be.undefined
258 expect(notification.type).to.equal(notificationType)
259
260 const following = notification.actorFollow.following
261 checkActor(following)
262 expect(following.name).to.equal('peertube')
263 expect(following.host).to.equal(followingHost)
264
265 expect(notification.actorFollow.follower.name).to.equal('peertube')
266 expect(notification.actorFollow.follower.host).to.equal(followerHost)
267 } else {
268 expect(notification).to.satisfy(n => {
269 return n.type !== notificationType || n.actorFollow.following.host !== followingHost
270 })
271 }
272 }
273
df4c603d 274 function emailNotificationFinder (email: object) {
a1587156 275 const text: string = email['text']
8424c402
C
276
277 return text.includes(' automatically followed a new instance') && text.includes(followingHost)
278 }
279
29837f88 280 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
8424c402
C
281}
282
29837f88
C
283async function checkCommentMention (options: CheckerBaseParams & {
284 shortUUID: string
285 commentId: number
286 threadId: number
287 byAccountDisplayName: string
288 checkType: CheckerType
289}) {
290 const { shortUUID, commentId, threadId, byAccountDisplayName } = options
f7cc67b4
C
291 const notificationType = UserNotificationType.COMMENT_MENTION
292
29837f88
C
293 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
294 if (checkType === 'presence') {
f7cc67b4
C
295 expect(notification).to.not.be.undefined
296 expect(notification.type).to.equal(notificationType)
297
298 checkComment(notification.comment, commentId, threadId)
299 checkActor(notification.comment.account)
300 expect(notification.comment.account.displayName).to.equal(byAccountDisplayName)
301
29837f88 302 checkVideo(notification.comment.video, undefined, shortUUID)
f7cc67b4
C
303 } else {
304 expect(notification).to.satisfy(n => n.type !== notificationType || n.comment.id !== commentId)
305 }
306 }
307
df4c603d 308 function emailNotificationFinder (email: object) {
a1587156 309 const text: string = email['text']
f7cc67b4 310
29837f88 311 return text.includes(' mentioned ') && text.includes(shortUUID) && text.includes(byAccountDisplayName)
f7cc67b4
C
312 }
313
29837f88 314 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
f7cc67b4
C
315}
316
cef534ed 317let lastEmailCount = 0
a1587156 318
29837f88
C
319async function checkNewCommentOnMyVideo (options: CheckerBaseParams & {
320 shortUUID: string
321 commentId: number
322 threadId: number
323 checkType: CheckerType
324}) {
325 const { server, shortUUID, commentId, threadId, checkType, emails } = options
cef534ed
C
326 const notificationType = UserNotificationType.NEW_COMMENT_ON_MY_VIDEO
327
29837f88
C
328 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
329 if (checkType === 'presence') {
cef534ed
C
330 expect(notification).to.not.be.undefined
331 expect(notification.type).to.equal(notificationType)
dc133480
C
332
333 checkComment(notification.comment, commentId, threadId)
334 checkActor(notification.comment.account)
29837f88 335 checkVideo(notification.comment.video, undefined, shortUUID)
cef534ed
C
336 } else {
337 expect(notification).to.satisfy((n: UserNotification) => {
338 return n === undefined || n.comment === undefined || n.comment.id !== commentId
339 })
340 }
341 }
342
29837f88 343 const commentUrl = `http://localhost:${server.port}/w/${shortUUID};threadId=${threadId}`
a1587156 344
df4c603d 345 function emailNotificationFinder (email: object) {
a1587156 346 return email['text'].indexOf(commentUrl) !== -1
cef534ed
C
347 }
348
29837f88 349 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
cef534ed 350
29837f88 351 if (checkType === 'presence') {
cef534ed 352 // We cannot detect email duplicates, so check we received another email
29837f88
C
353 expect(emails).to.have.length.above(lastEmailCount)
354 lastEmailCount = emails.length
cef534ed
C
355 }
356}
357
29837f88
C
358async function checkNewVideoAbuseForModerators (options: CheckerBaseParams & {
359 shortUUID: string
360 videoName: string
361 checkType: CheckerType
362}) {
363 const { shortUUID, videoName } = options
310b5219 364 const notificationType = UserNotificationType.NEW_ABUSE_FOR_MODERATORS
cef534ed 365
29837f88
C
366 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
367 if (checkType === 'presence') {
cef534ed
C
368 expect(notification).to.not.be.undefined
369 expect(notification.type).to.equal(notificationType)
dc133480 370
d95d1559 371 expect(notification.abuse.id).to.be.a('number')
29837f88 372 checkVideo(notification.abuse.video, videoName, shortUUID)
cef534ed
C
373 } else {
374 expect(notification).to.satisfy((n: UserNotification) => {
29837f88 375 return n === undefined || n.abuse === undefined || n.abuse.video.shortUUID !== shortUUID
cef534ed
C
376 })
377 }
378 }
379
df4c603d 380 function emailNotificationFinder (email: object) {
a1587156 381 const text = email['text']
29837f88 382 return text.indexOf(shortUUID) !== -1 && text.indexOf('abuse') !== -1
cef534ed
C
383 }
384
29837f88 385 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
cef534ed
C
386}
387
29837f88
C
388async function checkNewAbuseMessage (options: CheckerBaseParams & {
389 abuseId: number
390 message: string
391 toEmail: string
392 checkType: CheckerType
393}) {
394 const { abuseId, message, toEmail } = options
594d3e48
C
395 const notificationType = UserNotificationType.ABUSE_NEW_MESSAGE
396
29837f88
C
397 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
398 if (checkType === 'presence') {
594d3e48
C
399 expect(notification).to.not.be.undefined
400 expect(notification.type).to.equal(notificationType)
401
402 expect(notification.abuse.id).to.equal(abuseId)
403 } else {
404 expect(notification).to.satisfy((n: UserNotification) => {
405 return n === undefined || n.type !== notificationType || n.abuse === undefined || n.abuse.id !== abuseId
406 })
407 }
408 }
409
410 function emailNotificationFinder (email: object) {
411 const text = email['text']
412 const to = email['to'].filter(t => t.address === toEmail)
413
414 return text.indexOf(message) !== -1 && to.length !== 0
415 }
416
29837f88 417 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
594d3e48
C
418}
419
29837f88
C
420async function checkAbuseStateChange (options: CheckerBaseParams & {
421 abuseId: number
422 state: AbuseState
423 checkType: CheckerType
424}) {
425 const { abuseId, state } = options
594d3e48
C
426 const notificationType = UserNotificationType.ABUSE_STATE_CHANGE
427
29837f88
C
428 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
429 if (checkType === 'presence') {
594d3e48
C
430 expect(notification).to.not.be.undefined
431 expect(notification.type).to.equal(notificationType)
432
433 expect(notification.abuse.id).to.equal(abuseId)
434 expect(notification.abuse.state).to.equal(state)
435 } else {
436 expect(notification).to.satisfy((n: UserNotification) => {
437 return n === undefined || n.abuse === undefined || n.abuse.id !== abuseId
438 })
439 }
440 }
441
442 function emailNotificationFinder (email: object) {
443 const text = email['text']
444
445 const contains = state === AbuseState.ACCEPTED
446 ? ' accepted'
447 : ' rejected'
448
449 return text.indexOf(contains) !== -1
450 }
451
29837f88 452 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
594d3e48
C
453}
454
29837f88
C
455async function checkNewCommentAbuseForModerators (options: CheckerBaseParams & {
456 shortUUID: string
457 videoName: string
458 checkType: CheckerType
459}) {
460 const { shortUUID, videoName } = options
310b5219
C
461 const notificationType = UserNotificationType.NEW_ABUSE_FOR_MODERATORS
462
29837f88
C
463 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
464 if (checkType === 'presence') {
310b5219
C
465 expect(notification).to.not.be.undefined
466 expect(notification.type).to.equal(notificationType)
467
468 expect(notification.abuse.id).to.be.a('number')
29837f88 469 checkVideo(notification.abuse.comment.video, videoName, shortUUID)
310b5219
C
470 } else {
471 expect(notification).to.satisfy((n: UserNotification) => {
29837f88 472 return n === undefined || n.abuse === undefined || n.abuse.comment.video.shortUUID !== shortUUID
310b5219
C
473 })
474 }
475 }
476
477 function emailNotificationFinder (email: object) {
478 const text = email['text']
29837f88 479 return text.indexOf(shortUUID) !== -1 && text.indexOf('abuse') !== -1
310b5219
C
480 }
481
29837f88 482 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
310b5219
C
483}
484
29837f88
C
485async function checkNewAccountAbuseForModerators (options: CheckerBaseParams & {
486 displayName: string
487 checkType: CheckerType
488}) {
489 const { displayName } = options
310b5219
C
490 const notificationType = UserNotificationType.NEW_ABUSE_FOR_MODERATORS
491
29837f88
C
492 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
493 if (checkType === 'presence') {
310b5219
C
494 expect(notification).to.not.be.undefined
495 expect(notification.type).to.equal(notificationType)
496
497 expect(notification.abuse.id).to.be.a('number')
498 expect(notification.abuse.account.displayName).to.equal(displayName)
499 } else {
500 expect(notification).to.satisfy((n: UserNotification) => {
501 return n === undefined || n.abuse === undefined || n.abuse.account.displayName !== displayName
502 })
503 }
504 }
505
506 function emailNotificationFinder (email: object) {
507 const text = email['text']
508 return text.indexOf(displayName) !== -1 && text.indexOf('abuse') !== -1
509 }
510
29837f88 511 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
310b5219
C
512}
513
29837f88
C
514async function checkVideoAutoBlacklistForModerators (options: CheckerBaseParams & {
515 shortUUID: string
516 videoName: string
517 checkType: CheckerType
518}) {
519 const { shortUUID, videoName } = options
7ccddd7b
JM
520 const notificationType = UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS
521
29837f88
C
522 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
523 if (checkType === 'presence') {
7ccddd7b
JM
524 expect(notification).to.not.be.undefined
525 expect(notification.type).to.equal(notificationType)
526
8424c402 527 expect(notification.videoBlacklist.video.id).to.be.a('number')
29837f88 528 checkVideo(notification.videoBlacklist.video, videoName, shortUUID)
7ccddd7b
JM
529 } else {
530 expect(notification).to.satisfy((n: UserNotification) => {
29837f88 531 return n === undefined || n.video === undefined || n.video.shortUUID !== shortUUID
7ccddd7b
JM
532 })
533 }
534 }
535
df4c603d 536 function emailNotificationFinder (email: object) {
a1587156 537 const text = email['text']
29837f88 538 return text.indexOf(shortUUID) !== -1 && email['text'].indexOf('video-auto-blacklist/list') !== -1
7ccddd7b
JM
539 }
540
29837f88 541 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
7ccddd7b
JM
542}
543
29837f88
C
544async function checkNewBlacklistOnMyVideo (options: CheckerBaseParams & {
545 shortUUID: string
546 videoName: string
cef534ed 547 blacklistType: 'blacklist' | 'unblacklist'
29837f88
C
548}) {
549 const { videoName, shortUUID, blacklistType } = options
cef534ed
C
550 const notificationType = blacklistType === 'blacklist'
551 ? UserNotificationType.BLACKLIST_ON_MY_VIDEO
552 : UserNotificationType.UNBLACKLIST_ON_MY_VIDEO
553
dc133480 554 function notificationChecker (notification: UserNotification) {
cef534ed
C
555 expect(notification).to.not.be.undefined
556 expect(notification.type).to.equal(notificationType)
557
558 const video = blacklistType === 'blacklist' ? notification.videoBlacklist.video : notification.video
559
29837f88 560 checkVideo(video, videoName, shortUUID)
cef534ed
C
561 }
562
df4c603d 563 function emailNotificationFinder (email: object) {
a1587156 564 const text = email['text']
29837f88
C
565 const blacklistText = blacklistType === 'blacklist'
566 ? 'blacklisted'
567 : 'unblacklisted'
568
569 return text.includes(shortUUID) && text.includes(blacklistText)
cef534ed
C
570 }
571
29837f88 572 await checkNotification({ ...options, notificationChecker, emailNotificationFinder, checkType: 'presence' })
cef534ed
C
573}
574
29837f88
C
575async function checkNewPeerTubeVersion (options: CheckerBaseParams & {
576 latestVersion: string
577 checkType: CheckerType
578}) {
579 const { latestVersion } = options
32a18cbf
C
580 const notificationType = UserNotificationType.NEW_PEERTUBE_VERSION
581
29837f88
C
582 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
583 if (checkType === 'presence') {
32a18cbf
C
584 expect(notification).to.not.be.undefined
585 expect(notification.type).to.equal(notificationType)
586
587 expect(notification.peertube).to.exist
588 expect(notification.peertube.latestVersion).to.equal(latestVersion)
589 } else {
590 expect(notification).to.satisfy((n: UserNotification) => {
591 return n === undefined || n.peertube === undefined || n.peertube.latestVersion !== latestVersion
592 })
593 }
594 }
595
596 function emailNotificationFinder (email: object) {
597 const text = email['text']
598
599 return text.includes(latestVersion)
600 }
601
29837f88 602 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
32a18cbf
C
603}
604
29837f88
C
605async function checkNewPluginVersion (options: CheckerBaseParams & {
606 pluginType: PluginType
607 pluginName: string
608 checkType: CheckerType
609}) {
610 const { pluginName, pluginType } = options
32a18cbf
C
611 const notificationType = UserNotificationType.NEW_PLUGIN_VERSION
612
29837f88
C
613 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
614 if (checkType === 'presence') {
32a18cbf
C
615 expect(notification).to.not.be.undefined
616 expect(notification.type).to.equal(notificationType)
617
618 expect(notification.plugin.name).to.equal(pluginName)
619 expect(notification.plugin.type).to.equal(pluginType)
620 } else {
621 expect(notification).to.satisfy((n: UserNotification) => {
622 return n === undefined || n.plugin === undefined || n.plugin.name !== pluginName
623 })
624 }
625 }
626
627 function emailNotificationFinder (email: object) {
628 const text = email['text']
629
630 return text.includes(pluginName)
631 }
632
29837f88 633 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
32a18cbf
C
634}
635
32a18cbf 636async function prepareNotificationsTest (serversCount = 3, overrideConfigArg: any = {}) {
8eb07b01
C
637 const userNotifications: UserNotification[] = []
638 const adminNotifications: UserNotification[] = []
639 const adminNotificationsServer2: UserNotification[] = []
640 const emails: object[] = []
641
642 const port = await MockSmtpServer.Instance.collectEmails(emails)
643
644 const overrideConfig = {
645 smtp: {
646 hostname: 'localhost',
647 port
310b5219
C
648 },
649 signup: {
650 limit: 20
8eb07b01
C
651 }
652 }
254d3579 653 const servers = await createMultipleServers(serversCount, Object.assign(overrideConfig, overrideConfigArg))
8eb07b01
C
654
655 await setAccessTokensToServers(servers)
d0800f76 656 await setDefaultChannelAvatar(servers)
657 await setDefaultAccountAvatar(servers)
49919ca1
C
658
659 if (serversCount > 1) {
660 await doubleFollow(servers[0], servers[1])
661 }
8eb07b01 662
7926c5f9 663 const user = { username: 'user_1', password: 'super password' }
89d241a7
C
664 await servers[0].users.create({ ...user, videoQuota: 10 * 1000 * 1000 })
665 const userAccessToken = await servers[0].login.getAccessToken(user)
8eb07b01 666
89d241a7
C
667 await servers[0].notifications.updateMySettings({ token: userAccessToken, settings: getAllNotificationsSettings() })
668 await servers[0].notifications.updateMySettings({ settings: getAllNotificationsSettings() })
8eb07b01
C
669
670 if (serversCount > 1) {
89d241a7 671 await servers[1].notifications.updateMySettings({ settings: getAllNotificationsSettings() })
8eb07b01
C
672 }
673
674 {
89d241a7 675 const socket = servers[0].socketIO.getUserNotificationSocket({ token: userAccessToken })
8eb07b01
C
676 socket.on('new-notification', n => userNotifications.push(n))
677 }
678 {
89d241a7 679 const socket = servers[0].socketIO.getUserNotificationSocket()
8eb07b01
C
680 socket.on('new-notification', n => adminNotifications.push(n))
681 }
682
683 if (serversCount > 1) {
89d241a7 684 const socket = servers[1].socketIO.getUserNotificationSocket()
8eb07b01
C
685 socket.on('new-notification', n => adminNotificationsServer2.push(n))
686 }
687
89d241a7 688 const { videoChannels } = await servers[0].users.getMyInfo()
7926c5f9 689 const channelId = videoChannels[0].id
8eb07b01
C
690
691 return {
692 userNotifications,
693 adminNotifications,
694 adminNotificationsServer2,
695 userAccessToken,
696 emails,
697 servers,
698 channelId
699 }
700}
701
cef534ed
C
702// ---------------------------------------------------------------------------
703
704export {
dd0ebb71
C
705 getAllNotificationsSettings,
706
cef534ed
C
707 CheckerBaseParams,
708 CheckerType,
dc133480 709 checkMyVideoImportIsFinished,
f7cc67b4 710 checkUserRegistered,
8424c402 711 checkAutoInstanceFollowing,
dc133480 712 checkVideoIsPublished,
cef534ed 713 checkNewVideoFromSubscription,
f7cc67b4 714 checkNewActorFollow,
cef534ed
C
715 checkNewCommentOnMyVideo,
716 checkNewBlacklistOnMyVideo,
f7cc67b4 717 checkCommentMention,
cef534ed 718 checkNewVideoAbuseForModerators,
7ccddd7b 719 checkVideoAutoBlacklistForModerators,
594d3e48
C
720 checkNewAbuseMessage,
721 checkAbuseStateChange,
8eb07b01 722 checkNewInstanceFollower,
310b5219
C
723 prepareNotificationsTest,
724 checkNewCommentAbuseForModerators,
32a18cbf
C
725 checkNewAccountAbuseForModerators,
726 checkNewPeerTubeVersion,
727 checkNewPluginVersion
cef534ed 728}
29837f88
C
729
730// ---------------------------------------------------------------------------
731
732async function checkNotification (options: CheckerBaseParams & {
733 notificationChecker: (notification: UserNotification, checkType: CheckerType) => void
734 emailNotificationFinder: (email: object) => boolean
735 checkType: CheckerType
736}) {
737 const { server, token, checkType, notificationChecker, emailNotificationFinder, socketNotifications, emails } = options
738
739 const check = options.check || { web: true, mail: true }
740
741 if (check.web) {
5d3c5f27 742 const notification = await server.notifications.getLatest({ token: token })
29837f88
C
743
744 if (notification || checkType !== 'absence') {
745 notificationChecker(notification, checkType)
746 }
747
748 const socketNotification = socketNotifications.find(n => {
749 try {
750 notificationChecker(n, 'presence')
751 return true
752 } catch {
753 return false
754 }
755 })
756
757 if (checkType === 'presence') {
758 const obj = inspect(socketNotifications, { depth: 5 })
759 expect(socketNotification, 'The socket notification is absent when it should be present. ' + obj).to.not.be.undefined
760 } else {
761 const obj = inspect(socketNotification, { depth: 5 })
762 expect(socketNotification, 'The socket notification is present when it should not be present. ' + obj).to.be.undefined
763 }
764 }
765
766 if (check.mail) {
767 // Last email
768 const email = emails
769 .slice()
770 .reverse()
771 .find(e => emailNotificationFinder(e))
772
773 if (checkType === 'presence') {
774 const texts = emails.map(e => e.text)
775 expect(email, 'The email is absent when is should be present. ' + inspect(texts)).to.not.be.undefined
776 } else {
777 expect(email, 'The email is present when is should not be present. ' + inspect(email)).to.be.undefined
778 }
779 }
780}
781
782function checkVideo (video: any, videoName?: string, shortUUID?: string) {
783 if (videoName) {
784 expect(video.name).to.be.a('string')
785 expect(video.name).to.not.be.empty
786 expect(video.name).to.equal(videoName)
787 }
788
789 if (shortUUID) {
790 expect(video.shortUUID).to.be.a('string')
791 expect(video.shortUUID).to.not.be.empty
792 expect(video.shortUUID).to.equal(shortUUID)
793 }
794
795 expect(video.id).to.be.a('number')
796}
797
798function checkActor (actor: any) {
799 expect(actor.displayName).to.be.a('string')
800 expect(actor.displayName).to.not.be.empty
801 expect(actor.host).to.not.be.undefined
802}
803
804function checkComment (comment: any, commentId: number, threadId: number) {
805 expect(comment.id).to.equal(commentId)
806 expect(comment.threadId).to.equal(threadId)
807}