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