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