1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { buildUUID } from '@server/helpers/uuid'
7 checkAutoInstanceFollowing,
10 checkNewAccountAbuseForModerators,
11 checkNewBlacklistOnMyVideo,
12 checkNewCommentAbuseForModerators,
13 checkNewInstanceFollower,
14 checkNewVideoAbuseForModerators,
15 checkNewVideoFromSubscription,
17 checkVideoAutoBlacklistForModerators,
18 checkVideoIsPublished,
23 prepareNotificationsTest,
28 } from '@shared/extra-utils'
29 import { AbuseState, CustomConfig, UserNotification, VideoPrivacy } from '@shared/models'
31 describe('Test moderation notifications', function () {
32 let servers: ServerInfo[] = []
33 let userAccessToken: string
34 let userNotifications: UserNotification[] = []
35 let adminNotifications: UserNotification[] = []
36 let adminNotificationsServer2: UserNotification[] = []
37 let emails: object[] = []
39 before(async function () {
42 const res = await prepareNotificationsTest(3)
44 userAccessToken = res.userAccessToken
46 userNotifications = res.userNotifications
47 adminNotifications = res.adminNotifications
48 adminNotificationsServer2 = res.adminNotificationsServer2
51 describe('Abuse for moderators notification', function () {
52 let baseParams: CheckerBaseParams
58 socketNotifications: adminNotifications,
59 token: servers[0].accessToken
63 it('Should send a notification to moderators on local video abuse', async function () {
66 const name = 'video for abuse ' + buildUUID()
67 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
68 const video = resVideo.body.video
70 await servers[0].abusesCommand.report({ videoId: video.id, reason: 'super reason' })
72 await waitJobs(servers)
73 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
76 it('Should send a notification to moderators on remote video abuse', async function () {
79 const name = 'video for abuse ' + buildUUID()
80 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
81 const video = resVideo.body.video
83 await waitJobs(servers)
85 const videoId = await getVideoIdFromUUID(servers[1].url, video.uuid)
86 await servers[1].abusesCommand.report({ videoId, reason: 'super reason' })
88 await waitJobs(servers)
89 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
92 it('Should send a notification to moderators on local comment abuse', async function () {
95 const name = 'video for abuse ' + buildUUID()
96 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
97 const video = resVideo.body.video
98 const comment = await servers[0].commentsCommand.createThread({
99 token: userAccessToken,
101 text: 'comment abuse ' + buildUUID()
104 await waitJobs(servers)
106 await servers[0].abusesCommand.report({ commentId: comment.id, reason: 'super reason' })
108 await waitJobs(servers)
109 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
112 it('Should send a notification to moderators on remote comment abuse', async function () {
115 const name = 'video for abuse ' + buildUUID()
116 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
117 const video = resVideo.body.video
119 await servers[0].commentsCommand.createThread({
120 token: userAccessToken,
122 text: 'comment abuse ' + buildUUID()
125 await waitJobs(servers)
127 const { data } = await servers[1].commentsCommand.listThreads({ videoId: video.uuid })
128 const commentId = data[0].id
129 await servers[1].abusesCommand.report({ commentId, reason: 'super reason' })
131 await waitJobs(servers)
132 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
135 it('Should send a notification to moderators on local account abuse', async function () {
138 const username = 'user' + new Date().getTime()
139 const { account } = await servers[0].usersCommand.create({ username, password: 'donald' })
140 const accountId = account.id
142 await servers[0].abusesCommand.report({ accountId, reason: 'super reason' })
144 await waitJobs(servers)
145 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
148 it('Should send a notification to moderators on remote account abuse', async function () {
151 const username = 'user' + new Date().getTime()
152 const tmpToken = await servers[0].usersCommand.generateUserAndToken(username)
153 await uploadVideo(servers[0].url, tmpToken, { name: 'super video' })
155 await waitJobs(servers)
157 const account = await servers[1].accountsCommand.get({ accountName: username + '@' + servers[0].host })
158 await servers[1].abusesCommand.report({ accountId: account.id, reason: 'super reason' })
160 await waitJobs(servers)
161 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
165 describe('Abuse state change notification', function () {
166 let baseParams: CheckerBaseParams
169 before(async function () {
173 socketNotifications: userNotifications,
174 token: userAccessToken
177 const name = 'abuse ' + buildUUID()
178 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
179 const video = resVideo.body.video
181 const body = await servers[0].abusesCommand.report({ token: userAccessToken, videoId: video.id, reason: 'super reason' })
182 abuseId = body.abuse.id
185 it('Should send a notification to reporter if the abuse has been accepted', async function () {
188 await servers[0].abusesCommand.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
189 await waitJobs(servers)
191 await checkAbuseStateChange(baseParams, abuseId, AbuseState.ACCEPTED, 'presence')
194 it('Should send a notification to reporter if the abuse has been rejected', async function () {
197 await servers[0].abusesCommand.update({ abuseId, body: { state: AbuseState.REJECTED } })
198 await waitJobs(servers)
200 await checkAbuseStateChange(baseParams, abuseId, AbuseState.REJECTED, 'presence')
204 describe('New abuse message notification', function () {
205 let baseParamsUser: CheckerBaseParams
206 let baseParamsAdmin: CheckerBaseParams
210 before(async function () {
214 socketNotifications: userNotifications,
215 token: userAccessToken
221 socketNotifications: adminNotifications,
222 token: servers[0].accessToken
225 const name = 'abuse ' + buildUUID()
226 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
227 const video = resVideo.body.video
230 const body = await servers[0].abusesCommand.report({ token: userAccessToken, videoId: video.id, reason: 'super reason' })
231 abuseId = body.abuse.id
235 const body = await servers[0].abusesCommand.report({ token: userAccessToken, videoId: video.id, reason: 'super reason 2' })
236 abuseId2 = body.abuse.id
240 it('Should send a notification to reporter on new message', async function () {
243 const message = 'my super message to users'
244 await servers[0].abusesCommand.addMessage({ abuseId, message })
245 await waitJobs(servers)
247 await checkNewAbuseMessage(baseParamsUser, abuseId, message, 'user_1@example.com', 'presence')
250 it('Should not send a notification to the admin if sent by the admin', async function () {
253 const message = 'my super message that should not be sent to the admin'
254 await servers[0].abusesCommand.addMessage({ abuseId, message })
255 await waitJobs(servers)
257 await checkNewAbuseMessage(baseParamsAdmin, abuseId, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'absence')
260 it('Should send a notification to moderators', async function () {
263 const message = 'my super message to moderators'
264 await servers[0].abusesCommand.addMessage({ token: userAccessToken, abuseId: abuseId2, message })
265 await waitJobs(servers)
267 await checkNewAbuseMessage(baseParamsAdmin, abuseId2, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'presence')
270 it('Should not send a notification to reporter if sent by the reporter', async function () {
273 const message = 'my super message that should not be sent to reporter'
274 await servers[0].abusesCommand.addMessage({ token: userAccessToken, abuseId: abuseId2, message })
275 await waitJobs(servers)
277 await checkNewAbuseMessage(baseParamsUser, abuseId2, message, 'user_1@example.com', 'absence')
281 describe('Video blacklist on my video', function () {
282 let baseParams: CheckerBaseParams
288 socketNotifications: userNotifications,
289 token: userAccessToken
293 it('Should send a notification to video owner on blacklist', async function () {
296 const name = 'video for abuse ' + buildUUID()
297 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
298 const uuid = resVideo.body.video.uuid
300 await servers[0].blacklistCommand.add({ videoId: uuid })
302 await waitJobs(servers)
303 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist')
306 it('Should send a notification to video owner on unblacklist', async function () {
309 const name = 'video for abuse ' + buildUUID()
310 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
311 const uuid = resVideo.body.video.uuid
313 await servers[0].blacklistCommand.add({ videoId: uuid })
315 await waitJobs(servers)
316 await servers[0].blacklistCommand.remove({ videoId: uuid })
317 await waitJobs(servers)
320 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist')
324 describe('New registration', function () {
325 let baseParams: CheckerBaseParams
331 socketNotifications: adminNotifications,
332 token: servers[0].accessToken
336 it('Should send a notification only to moderators when a user registers on the instance', async function () {
339 await servers[0].usersCommand.register({ username: 'user_45' })
341 await waitJobs(servers)
343 await checkUserRegistered(baseParams, 'user_45', 'presence')
345 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
346 await checkUserRegistered({ ...baseParams, ...userOverride }, 'user_45', 'absence')
350 describe('New instance follows', function () {
351 const instanceIndexServer = new MockInstancesIndex()
353 let baseParams: CheckerBaseParams
359 socketNotifications: adminNotifications,
360 token: servers[0].accessToken
363 const port = await instanceIndexServer.initialize()
364 instanceIndexServer.addInstance(servers[1].host)
370 indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
378 it('Should send a notification only to admin when there is a new instance follower', async function () {
381 await servers[2].followsCommand.follow({ targets: [ servers[0].url ] })
383 await waitJobs(servers)
385 await checkNewInstanceFollower(baseParams, 'localhost:' + servers[2].port, 'presence')
387 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
388 await checkNewInstanceFollower({ ...baseParams, ...userOverride }, 'localhost:' + servers[2].port, 'absence')
391 it('Should send a notification on auto follow back', async function () {
394 await servers[2].followsCommand.unfollow({ target: servers[0] })
395 await waitJobs(servers)
400 autoFollowBack: { enabled: true }
404 await servers[0].configCommand.updateCustomSubConfig({ newConfig: config })
406 await servers[2].followsCommand.follow({ targets: [ servers[0].url ] })
408 await waitJobs(servers)
410 const followerHost = servers[0].host
411 const followingHost = servers[2].host
412 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
414 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
415 await checkAutoInstanceFollowing({ ...baseParams, ...userOverride }, followerHost, followingHost, 'absence')
417 config.followings.instance.autoFollowBack.enabled = false
418 await servers[0].configCommand.updateCustomSubConfig({ newConfig: config })
419 await servers[0].followsCommand.unfollow({ target: servers[2] })
420 await servers[2].followsCommand.unfollow({ target: servers[0] })
423 it('Should send a notification on auto instances index follow', async function () {
425 await servers[0].followsCommand.unfollow({ target: servers[1] })
427 await servers[0].configCommand.updateCustomSubConfig({ newConfig: config })
430 await waitJobs(servers)
432 const followerHost = servers[0].host
433 const followingHost = servers[1].host
434 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
436 config.followings.instance.autoFollowIndex.enabled = false
437 await servers[0].configCommand.updateCustomSubConfig({ newConfig: config })
438 await servers[0].followsCommand.unfollow({ target: servers[1] })
442 describe('Video-related notifications when video auto-blacklist is enabled', function () {
443 let userBaseParams: CheckerBaseParams
444 let adminBaseParamsServer1: CheckerBaseParams
445 let adminBaseParamsServer2: CheckerBaseParams
446 let videoUUID: string
447 let videoName: string
448 let currentCustomConfig: CustomConfig
452 adminBaseParamsServer1 = {
455 socketNotifications: adminNotifications,
456 token: servers[0].accessToken
459 adminBaseParamsServer2 = {
462 socketNotifications: adminNotificationsServer2,
463 token: servers[1].accessToken
469 socketNotifications: userNotifications,
470 token: userAccessToken
473 currentCustomConfig = await servers[0].configCommand.getCustomConfig()
475 const autoBlacklistTestsCustomConfig = {
476 ...currentCustomConfig,
487 // enable transcoding otherwise own publish notification after transcoding not expected
488 autoBlacklistTestsCustomConfig.transcoding.enabled = true
489 await servers[0].configCommand.updateCustomConfig({ newCustomConfig: autoBlacklistTestsCustomConfig })
491 await servers[0].subscriptionsCommand.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
492 await servers[1].subscriptionsCommand.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
496 it('Should send notification to moderators on new video with auto-blacklist', async function () {
499 videoName = 'video with auto-blacklist ' + buildUUID()
500 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: videoName })
501 videoUUID = resVideo.body.video.uuid
503 await waitJobs(servers)
504 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence')
507 it('Should not send video publish notification if auto-blacklisted', async function () {
508 await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'absence')
511 it('Should not send a local user subscription notification if auto-blacklisted', async function () {
512 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'absence')
515 it('Should not send a remote user subscription notification if auto-blacklisted', async function () {
516 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'absence')
519 it('Should send video published and unblacklist after video unblacklisted', async function () {
522 await servers[0].blacklistCommand.remove({ videoId: videoUUID })
524 await waitJobs(servers)
526 // FIXME: Can't test as two notifications sent to same user and util only checks last one
527 // One notification might be better anyways
528 // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist')
529 // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence')
532 it('Should send a local user subscription notification after removed from blacklist', async function () {
533 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'presence')
536 it('Should send a remote user subscription notification after removed from blacklist', async function () {
537 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'presence')
540 it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () {
543 const updateAt = new Date(new Date().getTime() + 1000000)
545 const name = 'video with auto-blacklist and future schedule ' + buildUUID()
549 privacy: VideoPrivacy.PRIVATE,
551 updateAt: updateAt.toISOString(),
552 privacy: VideoPrivacy.PUBLIC
556 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
557 const uuid = resVideo.body.video.uuid
559 await servers[0].blacklistCommand.remove({ videoId: uuid })
561 await waitJobs(servers)
562 await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist')
564 // FIXME: Can't test absence as two notifications sent to same user and util only checks last one
565 // One notification might be better anyways
566 // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
568 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
569 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
572 it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () {
576 const updateAt = new Date(new Date().getTime() + 2000)
578 const name = 'video with schedule done and still auto-blacklisted ' + buildUUID()
582 privacy: VideoPrivacy.PRIVATE,
584 updateAt: updateAt.toISOString(),
585 privacy: VideoPrivacy.PUBLIC
589 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
590 const uuid = resVideo.body.video.uuid
593 await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
594 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
595 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
598 it('Should not send a notification to moderators on new video without auto-blacklist', async function () {
601 const name = 'video without auto-blacklist ' + buildUUID()
603 // admin with blacklist right will not be auto-blacklisted
604 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name })
605 const uuid = resVideo.body.video.uuid
607 await waitJobs(servers)
608 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence')
612 await servers[0].configCommand.updateCustomConfig({ newCustomConfig: currentCustomConfig })
614 await servers[0].subscriptionsCommand.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
615 await servers[1].subscriptionsCommand.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
619 after(async function () {
620 MockSmtpServer.Instance.kill()
622 await cleanupTests(servers)