]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/shared/notifications.ts
Implement avatar miniatures (#4639)
[github/Chocobozzz/PeerTube.git] / server / tests / shared / notifications.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import { expect } from 'chai'
4 import { inspect } from 'util'
5 import {
6 AbuseState,
7 PluginType,
8 UserNotification,
9 UserNotificationSetting,
10 UserNotificationSettingValue,
11 UserNotificationType
12 } from '@shared/models'
13 import {
14 createMultipleServers,
15 doubleFollow,
16 PeerTubeServer,
17 setAccessTokensToServers,
18 setDefaultAccountAvatar,
19 setDefaultChannelAvatar
20 } from '@shared/server-commands'
21 import { MockSmtpServer } from './mock-servers'
22
23 type CheckerBaseParams = {
24 server: PeerTubeServer
25 emails: any[]
26 socketNotifications: UserNotification[]
27 token: string
28 check?: { web: boolean, mail: boolean }
29 }
30
31 type CheckerType = 'presence' | 'absence'
32
33 function 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 }
52 }
53
54 async function checkNewVideoFromSubscription (options: CheckerBaseParams & {
55 videoName: string
56 shortUUID: string
57 checkType: CheckerType
58 }) {
59 const { videoName, shortUUID } = options
60 const notificationType = UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION
61
62 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
63 if (checkType === 'presence') {
64 expect(notification).to.not.be.undefined
65 expect(notification.type).to.equal(notificationType)
66
67 checkVideo(notification.video, videoName, shortUUID)
68 checkActor(notification.video.channel)
69 } else {
70 expect(notification).to.satisfy((n: UserNotification) => {
71 return n === undefined || n.type !== UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION || n.video.name !== videoName
72 })
73 }
74 }
75
76 function emailNotificationFinder (email: object) {
77 const text = email['text']
78 return text.indexOf(shortUUID) !== -1 && text.indexOf('Your subscription') !== -1
79 }
80
81 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
82 }
83
84 async function checkVideoIsPublished (options: CheckerBaseParams & {
85 videoName: string
86 shortUUID: string
87 checkType: CheckerType
88 }) {
89 const { videoName, shortUUID } = options
90 const notificationType = UserNotificationType.MY_VIDEO_PUBLISHED
91
92 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
93 if (checkType === 'presence') {
94 expect(notification).to.not.be.undefined
95 expect(notification.type).to.equal(notificationType)
96
97 checkVideo(notification.video, videoName, shortUUID)
98 checkActor(notification.video.channel)
99 } else {
100 expect(notification.video).to.satisfy(v => v === undefined || v.name !== videoName)
101 }
102 }
103
104 function emailNotificationFinder (email: object) {
105 const text: string = email['text']
106 return text.includes(shortUUID) && text.includes('Your video')
107 }
108
109 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
110 }
111
112 async 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
121 const notificationType = success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR
122
123 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
124 if (checkType === 'presence') {
125 expect(notification).to.not.be.undefined
126 expect(notification.type).to.equal(notificationType)
127
128 expect(notification.videoImport.targetUrl).to.equal(url)
129
130 if (success) checkVideo(notification.videoImport.video, videoName, shortUUID)
131 } else {
132 expect(notification.videoImport).to.satisfy(i => i === undefined || i.targetUrl !== url)
133 }
134 }
135
136 function emailNotificationFinder (email: object) {
137 const text: string = email['text']
138 const toFind = success ? ' finished' : ' error'
139
140 return text.includes(url) && text.includes(toFind)
141 }
142
143 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
144 }
145
146 async function checkUserRegistered (options: CheckerBaseParams & {
147 username: string
148 checkType: CheckerType
149 }) {
150 const { username } = options
151 const notificationType = UserNotificationType.NEW_USER_REGISTRATION
152
153 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
154 if (checkType === 'presence') {
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
165 function emailNotificationFinder (email: object) {
166 const text: string = email['text']
167
168 return text.includes(' registered.') && text.includes(username)
169 }
170
171 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
172 }
173
174 async 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
182 const notificationType = UserNotificationType.NEW_FOLLOW
183
184 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
185 if (checkType === 'presence') {
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)
192 expect(notification.actorFollow.follower.host).to.not.be.undefined
193
194 const following = notification.actorFollow.following
195 expect(following.displayName).to.equal(followingDisplayName)
196 expect(following.type).to.equal(followType)
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
205 function emailNotificationFinder (email: object) {
206 const text: string = email['text']
207
208 return text.includes(followType) && text.includes(followingDisplayName) && text.includes(followerDisplayName)
209 }
210
211 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
212 }
213
214 async function checkNewInstanceFollower (options: CheckerBaseParams & {
215 followerHost: string
216 checkType: CheckerType
217 }) {
218 const { followerHost } = options
219 const notificationType = UserNotificationType.NEW_INSTANCE_FOLLOWER
220
221 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
222 if (checkType === 'presence') {
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
238 function emailNotificationFinder (email: object) {
239 const text: string = email['text']
240
241 return text.includes('instance has a new follower') && text.includes(followerHost)
242 }
243
244 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
245 }
246
247 async function checkAutoInstanceFollowing (options: CheckerBaseParams & {
248 followerHost: string
249 followingHost: string
250 checkType: CheckerType
251 }) {
252 const { followerHost, followingHost } = options
253 const notificationType = UserNotificationType.AUTO_INSTANCE_FOLLOWING
254
255 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
256 if (checkType === 'presence') {
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
274 function emailNotificationFinder (email: object) {
275 const text: string = email['text']
276
277 return text.includes(' automatically followed a new instance') && text.includes(followingHost)
278 }
279
280 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
281 }
282
283 async 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
291 const notificationType = UserNotificationType.COMMENT_MENTION
292
293 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
294 if (checkType === 'presence') {
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
302 checkVideo(notification.comment.video, undefined, shortUUID)
303 } else {
304 expect(notification).to.satisfy(n => n.type !== notificationType || n.comment.id !== commentId)
305 }
306 }
307
308 function emailNotificationFinder (email: object) {
309 const text: string = email['text']
310
311 return text.includes(' mentioned ') && text.includes(shortUUID) && text.includes(byAccountDisplayName)
312 }
313
314 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
315 }
316
317 let lastEmailCount = 0
318
319 async 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
326 const notificationType = UserNotificationType.NEW_COMMENT_ON_MY_VIDEO
327
328 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
329 if (checkType === 'presence') {
330 expect(notification).to.not.be.undefined
331 expect(notification.type).to.equal(notificationType)
332
333 checkComment(notification.comment, commentId, threadId)
334 checkActor(notification.comment.account)
335 checkVideo(notification.comment.video, undefined, shortUUID)
336 } else {
337 expect(notification).to.satisfy((n: UserNotification) => {
338 return n === undefined || n.comment === undefined || n.comment.id !== commentId
339 })
340 }
341 }
342
343 const commentUrl = `http://localhost:${server.port}/w/${shortUUID};threadId=${threadId}`
344
345 function emailNotificationFinder (email: object) {
346 return email['text'].indexOf(commentUrl) !== -1
347 }
348
349 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
350
351 if (checkType === 'presence') {
352 // We cannot detect email duplicates, so check we received another email
353 expect(emails).to.have.length.above(lastEmailCount)
354 lastEmailCount = emails.length
355 }
356 }
357
358 async function checkNewVideoAbuseForModerators (options: CheckerBaseParams & {
359 shortUUID: string
360 videoName: string
361 checkType: CheckerType
362 }) {
363 const { shortUUID, videoName } = options
364 const notificationType = UserNotificationType.NEW_ABUSE_FOR_MODERATORS
365
366 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
367 if (checkType === 'presence') {
368 expect(notification).to.not.be.undefined
369 expect(notification.type).to.equal(notificationType)
370
371 expect(notification.abuse.id).to.be.a('number')
372 checkVideo(notification.abuse.video, videoName, shortUUID)
373 } else {
374 expect(notification).to.satisfy((n: UserNotification) => {
375 return n === undefined || n.abuse === undefined || n.abuse.video.shortUUID !== shortUUID
376 })
377 }
378 }
379
380 function emailNotificationFinder (email: object) {
381 const text = email['text']
382 return text.indexOf(shortUUID) !== -1 && text.indexOf('abuse') !== -1
383 }
384
385 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
386 }
387
388 async function checkNewAbuseMessage (options: CheckerBaseParams & {
389 abuseId: number
390 message: string
391 toEmail: string
392 checkType: CheckerType
393 }) {
394 const { abuseId, message, toEmail } = options
395 const notificationType = UserNotificationType.ABUSE_NEW_MESSAGE
396
397 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
398 if (checkType === 'presence') {
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
417 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
418 }
419
420 async function checkAbuseStateChange (options: CheckerBaseParams & {
421 abuseId: number
422 state: AbuseState
423 checkType: CheckerType
424 }) {
425 const { abuseId, state } = options
426 const notificationType = UserNotificationType.ABUSE_STATE_CHANGE
427
428 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
429 if (checkType === 'presence') {
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
452 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
453 }
454
455 async function checkNewCommentAbuseForModerators (options: CheckerBaseParams & {
456 shortUUID: string
457 videoName: string
458 checkType: CheckerType
459 }) {
460 const { shortUUID, videoName } = options
461 const notificationType = UserNotificationType.NEW_ABUSE_FOR_MODERATORS
462
463 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
464 if (checkType === 'presence') {
465 expect(notification).to.not.be.undefined
466 expect(notification.type).to.equal(notificationType)
467
468 expect(notification.abuse.id).to.be.a('number')
469 checkVideo(notification.abuse.comment.video, videoName, shortUUID)
470 } else {
471 expect(notification).to.satisfy((n: UserNotification) => {
472 return n === undefined || n.abuse === undefined || n.abuse.comment.video.shortUUID !== shortUUID
473 })
474 }
475 }
476
477 function emailNotificationFinder (email: object) {
478 const text = email['text']
479 return text.indexOf(shortUUID) !== -1 && text.indexOf('abuse') !== -1
480 }
481
482 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
483 }
484
485 async function checkNewAccountAbuseForModerators (options: CheckerBaseParams & {
486 displayName: string
487 checkType: CheckerType
488 }) {
489 const { displayName } = options
490 const notificationType = UserNotificationType.NEW_ABUSE_FOR_MODERATORS
491
492 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
493 if (checkType === 'presence') {
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
511 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
512 }
513
514 async function checkVideoAutoBlacklistForModerators (options: CheckerBaseParams & {
515 shortUUID: string
516 videoName: string
517 checkType: CheckerType
518 }) {
519 const { shortUUID, videoName } = options
520 const notificationType = UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS
521
522 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
523 if (checkType === 'presence') {
524 expect(notification).to.not.be.undefined
525 expect(notification.type).to.equal(notificationType)
526
527 expect(notification.videoBlacklist.video.id).to.be.a('number')
528 checkVideo(notification.videoBlacklist.video, videoName, shortUUID)
529 } else {
530 expect(notification).to.satisfy((n: UserNotification) => {
531 return n === undefined || n.video === undefined || n.video.shortUUID !== shortUUID
532 })
533 }
534 }
535
536 function emailNotificationFinder (email: object) {
537 const text = email['text']
538 return text.indexOf(shortUUID) !== -1 && email['text'].indexOf('video-auto-blacklist/list') !== -1
539 }
540
541 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
542 }
543
544 async function checkNewBlacklistOnMyVideo (options: CheckerBaseParams & {
545 shortUUID: string
546 videoName: string
547 blacklistType: 'blacklist' | 'unblacklist'
548 }) {
549 const { videoName, shortUUID, blacklistType } = options
550 const notificationType = blacklistType === 'blacklist'
551 ? UserNotificationType.BLACKLIST_ON_MY_VIDEO
552 : UserNotificationType.UNBLACKLIST_ON_MY_VIDEO
553
554 function notificationChecker (notification: UserNotification) {
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
560 checkVideo(video, videoName, shortUUID)
561 }
562
563 function emailNotificationFinder (email: object) {
564 const text = email['text']
565 const blacklistText = blacklistType === 'blacklist'
566 ? 'blacklisted'
567 : 'unblacklisted'
568
569 return text.includes(shortUUID) && text.includes(blacklistText)
570 }
571
572 await checkNotification({ ...options, notificationChecker, emailNotificationFinder, checkType: 'presence' })
573 }
574
575 async function checkNewPeerTubeVersion (options: CheckerBaseParams & {
576 latestVersion: string
577 checkType: CheckerType
578 }) {
579 const { latestVersion } = options
580 const notificationType = UserNotificationType.NEW_PEERTUBE_VERSION
581
582 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
583 if (checkType === 'presence') {
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
602 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
603 }
604
605 async function checkNewPluginVersion (options: CheckerBaseParams & {
606 pluginType: PluginType
607 pluginName: string
608 checkType: CheckerType
609 }) {
610 const { pluginName, pluginType } = options
611 const notificationType = UserNotificationType.NEW_PLUGIN_VERSION
612
613 function notificationChecker (notification: UserNotification, checkType: CheckerType) {
614 if (checkType === 'presence') {
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
633 await checkNotification({ ...options, notificationChecker, emailNotificationFinder })
634 }
635
636 async function prepareNotificationsTest (serversCount = 3, overrideConfigArg: any = {}) {
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
648 },
649 signup: {
650 limit: 20
651 }
652 }
653 const servers = await createMultipleServers(serversCount, Object.assign(overrideConfig, overrideConfigArg))
654
655 await setAccessTokensToServers(servers)
656 await setDefaultChannelAvatar(servers)
657 await setDefaultAccountAvatar(servers)
658
659 if (serversCount > 1) {
660 await doubleFollow(servers[0], servers[1])
661 }
662
663 const user = { username: 'user_1', password: 'super password' }
664 await servers[0].users.create({ ...user, videoQuota: 10 * 1000 * 1000 })
665 const userAccessToken = await servers[0].login.getAccessToken(user)
666
667 await servers[0].notifications.updateMySettings({ token: userAccessToken, settings: getAllNotificationsSettings() })
668 await servers[0].notifications.updateMySettings({ settings: getAllNotificationsSettings() })
669
670 if (serversCount > 1) {
671 await servers[1].notifications.updateMySettings({ settings: getAllNotificationsSettings() })
672 }
673
674 {
675 const socket = servers[0].socketIO.getUserNotificationSocket({ token: userAccessToken })
676 socket.on('new-notification', n => userNotifications.push(n))
677 }
678 {
679 const socket = servers[0].socketIO.getUserNotificationSocket()
680 socket.on('new-notification', n => adminNotifications.push(n))
681 }
682
683 if (serversCount > 1) {
684 const socket = servers[1].socketIO.getUserNotificationSocket()
685 socket.on('new-notification', n => adminNotificationsServer2.push(n))
686 }
687
688 const { videoChannels } = await servers[0].users.getMyInfo()
689 const channelId = videoChannels[0].id
690
691 return {
692 userNotifications,
693 adminNotifications,
694 adminNotificationsServer2,
695 userAccessToken,
696 emails,
697 servers,
698 channelId
699 }
700 }
701
702 // ---------------------------------------------------------------------------
703
704 export {
705 getAllNotificationsSettings,
706
707 CheckerBaseParams,
708 CheckerType,
709 checkMyVideoImportIsFinished,
710 checkUserRegistered,
711 checkAutoInstanceFollowing,
712 checkVideoIsPublished,
713 checkNewVideoFromSubscription,
714 checkNewActorFollow,
715 checkNewCommentOnMyVideo,
716 checkNewBlacklistOnMyVideo,
717 checkCommentMention,
718 checkNewVideoAbuseForModerators,
719 checkVideoAutoBlacklistForModerators,
720 checkNewAbuseMessage,
721 checkAbuseStateChange,
722 checkNewInstanceFollower,
723 prepareNotificationsTest,
724 checkNewCommentAbuseForModerators,
725 checkNewAccountAbuseForModerators,
726 checkNewPeerTubeVersion,
727 checkNewPluginVersion
728 }
729
730 // ---------------------------------------------------------------------------
731
732 async 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) {
742 const notification = await server.notifications.getLatest({ token: token })
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
782 function 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
798 function 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
804 function checkComment (comment: any, commentId: number, threadId: number) {
805 expect(comment.id).to.equal(commentId)
806 expect(comment.threadId).to.equal(threadId)
807 }