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