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