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