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