1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
6 checkAutoInstanceFollowing,
9 checkNewAccountAbuseForModerators,
10 checkNewBlacklistOnMyVideo,
11 checkNewCommentAbuseForModerators,
12 checkNewInstanceFollower,
13 checkNewVideoAbuseForModerators,
14 checkNewVideoFromSubscription,
16 checkVideoAutoBlacklistForModerators,
17 checkVideoIsPublished,
20 prepareNotificationsTest
21 } from '@server/tests/shared'
22 import { wait } from '@shared/core-utils'
23 import { buildUUID } from '@shared/extra-utils'
24 import { AbuseState, CustomConfig, UserNotification, UserRole, VideoPrivacy } from '@shared/models'
25 import { cleanupTests, PeerTubeServer, waitJobs } from '@shared/server-commands'
27 describe('Test moderation notifications', function () {
28 let servers: PeerTubeServer[] = []
29 let userToken1: string
30 let userToken2: string
32 let userNotifications: UserNotification[] = []
33 let adminNotifications: UserNotification[] = []
34 let adminNotificationsServer2: UserNotification[] = []
35 let emails: object[] = []
37 before(async function () {
40 const res = await prepareNotificationsTest(3)
42 userToken1 = res.userAccessToken
44 userNotifications = res.userNotifications
45 adminNotifications = res.adminNotifications
46 adminNotificationsServer2 = res.adminNotificationsServer2
48 userToken2 = await servers[1].users.generateUserAndToken('user2', UserRole.USER)
51 describe('Abuse for moderators notification', function () {
52 let baseParams: CheckerBaseParams
58 socketNotifications: adminNotifications,
59 token: servers[0].accessToken
63 it('Should not send a notification to moderators on local abuse reported by an admin', async function () {
66 const name = 'video for abuse ' + buildUUID()
67 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
69 await servers[0].abuses.report({ videoId: video.id, reason: 'super reason' })
71 await waitJobs(servers)
72 await checkNewVideoAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'absence' })
75 it('Should send a notification to moderators on local video abuse', async function () {
78 const name = 'video for abuse ' + buildUUID()
79 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
81 await servers[0].abuses.report({ token: userToken1, videoId: video.id, reason: 'super reason' })
83 await waitJobs(servers)
84 await checkNewVideoAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
87 it('Should send a notification to moderators on remote video abuse', async function () {
90 const name = 'video for abuse ' + buildUUID()
91 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
93 await waitJobs(servers)
95 const videoId = await servers[1].videos.getId({ uuid: video.uuid })
96 await servers[1].abuses.report({ token: userToken2, videoId, reason: 'super reason' })
98 await waitJobs(servers)
99 await checkNewVideoAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
102 it('Should send a notification to moderators on local comment abuse', async function () {
105 const name = 'video for abuse ' + buildUUID()
106 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
107 const comment = await servers[0].comments.createThread({
110 text: 'comment abuse ' + buildUUID()
113 await waitJobs(servers)
115 await servers[0].abuses.report({ token: userToken1, commentId: comment.id, reason: 'super reason' })
117 await waitJobs(servers)
118 await checkNewCommentAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
121 it('Should send a notification to moderators on remote comment abuse', async function () {
124 const name = 'video for abuse ' + buildUUID()
125 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
127 await servers[0].comments.createThread({
130 text: 'comment abuse ' + buildUUID()
133 await waitJobs(servers)
135 const { data } = await servers[1].comments.listThreads({ videoId: video.uuid })
136 const commentId = data[0].id
137 await servers[1].abuses.report({ token: userToken2, commentId, reason: 'super reason' })
139 await waitJobs(servers)
140 await checkNewCommentAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
143 it('Should send a notification to moderators on local account abuse', async function () {
146 const username = 'user' + new Date().getTime()
147 const { account } = await servers[0].users.create({ username, password: 'donald' })
148 const accountId = account.id
150 await servers[0].abuses.report({ token: userToken1, accountId, reason: 'super reason' })
152 await waitJobs(servers)
153 await checkNewAccountAbuseForModerators({ ...baseParams, displayName: username, checkType: 'presence' })
156 it('Should send a notification to moderators on remote account abuse', async function () {
159 const username = 'user' + new Date().getTime()
160 const tmpToken = await servers[0].users.generateUserAndToken(username)
161 await servers[0].videos.upload({ token: tmpToken, attributes: { name: 'super video' } })
163 await waitJobs(servers)
165 const account = await servers[1].accounts.get({ accountName: username + '@' + servers[0].host })
166 await servers[1].abuses.report({ token: userToken2, accountId: account.id, reason: 'super reason' })
168 await waitJobs(servers)
169 await checkNewAccountAbuseForModerators({ ...baseParams, displayName: username, checkType: 'presence' })
173 describe('Abuse state change notification', function () {
174 let baseParams: CheckerBaseParams
177 before(async function () {
181 socketNotifications: userNotifications,
185 const name = 'abuse ' + buildUUID()
186 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
188 const body = await servers[0].abuses.report({ token: userToken1, videoId: video.id, reason: 'super reason' })
189 abuseId = body.abuse.id
192 it('Should send a notification to reporter if the abuse has been accepted', async function () {
195 await servers[0].abuses.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
196 await waitJobs(servers)
198 await checkAbuseStateChange({ ...baseParams, abuseId, state: AbuseState.ACCEPTED, checkType: 'presence' })
201 it('Should send a notification to reporter if the abuse has been rejected', async function () {
204 await servers[0].abuses.update({ abuseId, body: { state: AbuseState.REJECTED } })
205 await waitJobs(servers)
207 await checkAbuseStateChange({ ...baseParams, abuseId, state: AbuseState.REJECTED, checkType: 'presence' })
211 describe('New abuse message notification', function () {
212 let baseParamsUser: CheckerBaseParams
213 let baseParamsAdmin: CheckerBaseParams
217 before(async function () {
221 socketNotifications: userNotifications,
228 socketNotifications: adminNotifications,
229 token: servers[0].accessToken
232 const name = 'abuse ' + buildUUID()
233 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
236 const body = await servers[0].abuses.report({ token: userToken1, videoId: video.id, reason: 'super reason' })
237 abuseId = body.abuse.id
241 const body = await servers[0].abuses.report({ token: userToken1, videoId: video.id, reason: 'super reason 2' })
242 abuseId2 = body.abuse.id
246 it('Should send a notification to reporter on new message', async function () {
249 const message = 'my super message to users'
250 await servers[0].abuses.addMessage({ abuseId, message })
251 await waitJobs(servers)
253 await checkNewAbuseMessage({ ...baseParamsUser, abuseId, message, toEmail: 'user_1@example.com', checkType: 'presence' })
256 it('Should not send a notification to the admin if sent by the admin', async function () {
259 const message = 'my super message that should not be sent to the admin'
260 await servers[0].abuses.addMessage({ abuseId, message })
261 await waitJobs(servers)
263 const toEmail = 'admin' + servers[0].internalServerNumber + '@example.com'
264 await checkNewAbuseMessage({ ...baseParamsAdmin, abuseId, message, toEmail, checkType: 'absence' })
267 it('Should send a notification to moderators', async function () {
270 const message = 'my super message to moderators'
271 await servers[0].abuses.addMessage({ token: userToken1, abuseId: abuseId2, message })
272 await waitJobs(servers)
274 const toEmail = 'admin' + servers[0].internalServerNumber + '@example.com'
275 await checkNewAbuseMessage({ ...baseParamsAdmin, abuseId: abuseId2, message, toEmail, checkType: 'presence' })
278 it('Should not send a notification to reporter if sent by the reporter', async function () {
281 const message = 'my super message that should not be sent to reporter'
282 await servers[0].abuses.addMessage({ token: userToken1, abuseId: abuseId2, message })
283 await waitJobs(servers)
285 const toEmail = 'user_1@example.com'
286 await checkNewAbuseMessage({ ...baseParamsUser, abuseId: abuseId2, message, toEmail, checkType: 'absence' })
290 describe('Video blacklist on my video', function () {
291 let baseParams: CheckerBaseParams
297 socketNotifications: userNotifications,
302 it('Should send a notification to video owner on blacklist', async function () {
305 const name = 'video for abuse ' + buildUUID()
306 const { uuid, shortUUID } = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
308 await servers[0].blacklist.add({ videoId: uuid })
310 await waitJobs(servers)
311 await checkNewBlacklistOnMyVideo({ ...baseParams, shortUUID, videoName: name, blacklistType: 'blacklist' })
314 it('Should send a notification to video owner on unblacklist', async function () {
317 const name = 'video for abuse ' + buildUUID()
318 const { uuid, shortUUID } = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
320 await servers[0].blacklist.add({ videoId: uuid })
322 await waitJobs(servers)
323 await servers[0].blacklist.remove({ videoId: uuid })
324 await waitJobs(servers)
327 await checkNewBlacklistOnMyVideo({ ...baseParams, shortUUID, videoName: name, blacklistType: 'unblacklist' })
331 describe('New registration', function () {
332 let baseParams: CheckerBaseParams
338 socketNotifications: adminNotifications,
339 token: servers[0].accessToken
343 it('Should send a notification only to moderators when a user registers on the instance', async function () {
346 await servers[0].users.register({ username: 'user_45' })
348 await waitJobs(servers)
350 await checkUserRegistered({ ...baseParams, username: 'user_45', checkType: 'presence' })
352 const userOverride = { socketNotifications: userNotifications, token: userToken1, check: { web: true, mail: false } }
353 await checkUserRegistered({ ...baseParams, ...userOverride, username: 'user_45', checkType: 'absence' })
357 describe('New instance follows', function () {
358 const instanceIndexServer = new MockInstancesIndex()
360 let baseParams: CheckerBaseParams
366 socketNotifications: adminNotifications,
367 token: servers[0].accessToken
370 const port = await instanceIndexServer.initialize()
371 instanceIndexServer.addInstance(servers[1].host)
377 indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
385 it('Should send a notification only to admin when there is a new instance follower', async function () {
388 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
390 await waitJobs(servers)
392 await checkNewInstanceFollower({ ...baseParams, followerHost: 'localhost:' + servers[2].port, checkType: 'presence' })
394 const userOverride = { socketNotifications: userNotifications, token: userToken1, check: { web: true, mail: false } }
395 await checkNewInstanceFollower({ ...baseParams, ...userOverride, followerHost: 'localhost:' + servers[2].port, checkType: 'absence' })
398 it('Should send a notification on auto follow back', async function () {
401 await servers[2].follows.unfollow({ target: servers[0] })
402 await waitJobs(servers)
407 autoFollowBack: { enabled: true }
411 await servers[0].config.updateCustomSubConfig({ newConfig: config })
413 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
415 await waitJobs(servers)
417 const followerHost = servers[0].host
418 const followingHost = servers[2].host
419 await checkAutoInstanceFollowing({ ...baseParams, followerHost, followingHost, checkType: 'presence' })
421 const userOverride = { socketNotifications: userNotifications, token: userToken1, check: { web: true, mail: false } }
422 await checkAutoInstanceFollowing({ ...baseParams, ...userOverride, followerHost, followingHost, checkType: 'absence' })
424 config.followings.instance.autoFollowBack.enabled = false
425 await servers[0].config.updateCustomSubConfig({ newConfig: config })
426 await servers[0].follows.unfollow({ target: servers[2] })
427 await servers[2].follows.unfollow({ target: servers[0] })
430 it('Should send a notification on auto instances index follow', async function () {
432 await servers[0].follows.unfollow({ target: servers[1] })
434 await servers[0].config.updateCustomSubConfig({ newConfig: config })
437 await waitJobs(servers)
439 const followerHost = servers[0].host
440 const followingHost = servers[1].host
441 await checkAutoInstanceFollowing({ ...baseParams, followerHost, followingHost, checkType: 'presence' })
443 config.followings.instance.autoFollowIndex.enabled = false
444 await servers[0].config.updateCustomSubConfig({ newConfig: config })
445 await servers[0].follows.unfollow({ target: servers[1] })
449 describe('Video-related notifications when video auto-blacklist is enabled', function () {
450 let userBaseParams: CheckerBaseParams
451 let adminBaseParamsServer1: CheckerBaseParams
452 let adminBaseParamsServer2: CheckerBaseParams
454 let shortUUID: string
455 let videoName: string
456 let currentCustomConfig: CustomConfig
460 adminBaseParamsServer1 = {
463 socketNotifications: adminNotifications,
464 token: servers[0].accessToken
467 adminBaseParamsServer2 = {
470 socketNotifications: adminNotificationsServer2,
471 token: servers[1].accessToken
477 socketNotifications: userNotifications,
481 currentCustomConfig = await servers[0].config.getCustomConfig()
483 const autoBlacklistTestsCustomConfig = {
484 ...currentCustomConfig,
495 // enable transcoding otherwise own publish notification after transcoding not expected
496 autoBlacklistTestsCustomConfig.transcoding.enabled = true
497 await servers[0].config.updateCustomConfig({ newCustomConfig: autoBlacklistTestsCustomConfig })
499 await servers[0].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
500 await servers[1].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
503 it('Should send notification to moderators on new video with auto-blacklist', async function () {
506 videoName = 'video with auto-blacklist ' + buildUUID()
507 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name: videoName } })
508 shortUUID = video.shortUUID
511 await waitJobs(servers)
512 await checkVideoAutoBlacklistForModerators({ ...adminBaseParamsServer1, shortUUID, videoName, checkType: 'presence' })
515 it('Should not send video publish notification if auto-blacklisted', async function () {
516 await checkVideoIsPublished({ ...userBaseParams, videoName, shortUUID, checkType: 'absence' })
519 it('Should not send a local user subscription notification if auto-blacklisted', async function () {
520 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName, shortUUID, checkType: 'absence' })
523 it('Should not send a remote user subscription notification if auto-blacklisted', async function () {
524 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName, shortUUID, checkType: 'absence' })
527 it('Should send video published and unblacklist after video unblacklisted', async function () {
530 await servers[0].blacklist.remove({ videoId: uuid })
532 await waitJobs(servers)
534 // FIXME: Can't test as two notifications sent to same user and util only checks last one
535 // One notification might be better anyways
536 // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist')
537 // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence')
540 it('Should send a local user subscription notification after removed from blacklist', async function () {
541 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName, shortUUID, checkType: 'presence' })
544 it('Should send a remote user subscription notification after removed from blacklist', async function () {
545 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName, shortUUID, checkType: 'presence' })
548 it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () {
551 const updateAt = new Date(new Date().getTime() + 1000000)
553 const name = 'video with auto-blacklist and future schedule ' + buildUUID()
557 privacy: VideoPrivacy.PRIVATE,
559 updateAt: updateAt.toISOString(),
560 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
564 const { shortUUID, uuid } = await servers[0].videos.upload({ token: userToken1, attributes })
566 await servers[0].blacklist.remove({ videoId: uuid })
568 await waitJobs(servers)
569 await checkNewBlacklistOnMyVideo({ ...userBaseParams, shortUUID, videoName: name, blacklistType: 'unblacklist' })
571 // FIXME: Can't test absence as two notifications sent to same user and util only checks last one
572 // One notification might be better anyways
573 // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
575 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName: name, shortUUID, checkType: 'absence' })
576 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName: name, shortUUID, checkType: 'absence' })
579 it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () {
583 const updateAt = new Date(new Date().getTime() + 2000)
585 const name = 'video with schedule done and still auto-blacklisted ' + buildUUID()
589 privacy: VideoPrivacy.PRIVATE,
591 updateAt: updateAt.toISOString(),
592 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
596 const { shortUUID } = await servers[0].videos.upload({ token: userToken1, attributes })
599 await checkVideoIsPublished({ ...userBaseParams, videoName: name, shortUUID, checkType: 'absence' })
600 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName: name, shortUUID, checkType: 'absence' })
601 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName: name, shortUUID, checkType: 'absence' })
604 it('Should not send a notification to moderators on new video without auto-blacklist', async function () {
607 const name = 'video without auto-blacklist ' + buildUUID()
609 // admin with blacklist right will not be auto-blacklisted
610 const { shortUUID } = await servers[0].videos.upload({ attributes: { name } })
612 await waitJobs(servers)
613 await checkVideoAutoBlacklistForModerators({ ...adminBaseParamsServer1, shortUUID, videoName: name, checkType: 'absence' })
617 await servers[0].config.updateCustomConfig({ newCustomConfig: currentCustomConfig })
619 await servers[0].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
620 await servers[1].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
624 after(async function () {
625 MockSmtpServer.Instance.kill()
627 await cleanupTests(servers)