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