1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { buildUUID } from '@server/helpers/uuid'
9 checkAutoInstanceFollowing,
12 checkNewAccountAbuseForModerators,
13 checkNewBlacklistOnMyVideo,
14 checkNewCommentAbuseForModerators,
15 checkNewInstanceFollower,
16 checkNewVideoAbuseForModerators,
17 checkNewVideoFromSubscription,
19 checkVideoAutoBlacklistForModerators,
20 checkVideoIsPublished,
23 generateUserAccessToken,
24 getVideoCommentThreads,
29 prepareNotificationsTest,
31 removeVideoFromBlacklist,
36 } from '@shared/extra-utils'
37 import { AbuseState, CustomConfig, UserNotification, VideoPrivacy } from '@shared/models'
39 describe('Test moderation notifications', function () {
40 let servers: ServerInfo[] = []
41 let userAccessToken: string
42 let userNotifications: UserNotification[] = []
43 let adminNotifications: UserNotification[] = []
44 let adminNotificationsServer2: UserNotification[] = []
45 let emails: object[] = []
47 before(async function () {
50 const res = await prepareNotificationsTest(3)
52 userAccessToken = res.userAccessToken
54 userNotifications = res.userNotifications
55 adminNotifications = res.adminNotifications
56 adminNotificationsServer2 = res.adminNotificationsServer2
59 describe('Abuse for moderators notification', function () {
60 let baseParams: CheckerBaseParams
66 socketNotifications: adminNotifications,
67 token: servers[0].accessToken
71 it('Should send a notification to moderators on local video abuse', async function () {
74 const name = 'video for abuse ' + buildUUID()
75 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
76 const video = resVideo.body.video
78 await servers[0].abusesCommand.report({ videoId: video.id, reason: 'super reason' })
80 await waitJobs(servers)
81 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
84 it('Should send a notification to moderators on remote video abuse', async function () {
87 const name = 'video for abuse ' + buildUUID()
88 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
89 const video = resVideo.body.video
91 await waitJobs(servers)
93 const videoId = await getVideoIdFromUUID(servers[1].url, video.uuid)
94 await servers[1].abusesCommand.report({ videoId, reason: 'super reason' })
96 await waitJobs(servers)
97 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
100 it('Should send a notification to moderators on local comment abuse', async function () {
103 const name = 'video for abuse ' + buildUUID()
104 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
105 const video = resVideo.body.video
106 const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + buildUUID())
107 const comment = resComment.body.comment
109 await waitJobs(servers)
111 await servers[0].abusesCommand.report({ commentId: comment.id, reason: 'super reason' })
113 await waitJobs(servers)
114 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
117 it('Should send a notification to moderators on remote comment abuse', async function () {
120 const name = 'video for abuse ' + buildUUID()
121 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
122 const video = resVideo.body.video
123 await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + buildUUID())
125 await waitJobs(servers)
127 const resComments = await getVideoCommentThreads(servers[1].url, video.uuid, 0, 5)
128 const commentId = resComments.body.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 resUser = await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username, password: 'donald' })
140 const accountId = resUser.body.user.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 generateUserAccessToken(servers[0], 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 addVideoToBlacklist(servers[0].url, servers[0].accessToken, 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 addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
315 await waitJobs(servers)
316 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, 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 registerUser(servers[0].url, 'user_45', 'password')
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(immutableAssign(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(immutableAssign(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(immutableAssign(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 = immutableAssign(currentCustomConfig, {
485 // enable transcoding otherwise own publish notification after transcoding not expected
486 autoBlacklistTestsCustomConfig.transcoding.enabled = true
487 await servers[0].configCommand.updateCustomConfig({ newCustomConfig: autoBlacklistTestsCustomConfig })
489 await servers[0].subscriptionsCommand.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
490 await servers[1].subscriptionsCommand.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
494 it('Should send notification to moderators on new video with auto-blacklist', async function () {
497 videoName = 'video with auto-blacklist ' + buildUUID()
498 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: videoName })
499 videoUUID = resVideo.body.video.uuid
501 await waitJobs(servers)
502 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence')
505 it('Should not send video publish notification if auto-blacklisted', async function () {
506 await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'absence')
509 it('Should not send a local user subscription notification if auto-blacklisted', async function () {
510 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'absence')
513 it('Should not send a remote user subscription notification if auto-blacklisted', async function () {
514 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'absence')
517 it('Should send video published and unblacklist after video unblacklisted', async function () {
520 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoUUID)
522 await waitJobs(servers)
524 // FIXME: Can't test as two notifications sent to same user and util only checks last one
525 // One notification might be better anyways
526 // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist')
527 // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence')
530 it('Should send a local user subscription notification after removed from blacklist', async function () {
531 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'presence')
534 it('Should send a remote user subscription notification after removed from blacklist', async function () {
535 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'presence')
538 it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () {
541 const updateAt = new Date(new Date().getTime() + 1000000)
543 const name = 'video with auto-blacklist and future schedule ' + buildUUID()
547 privacy: VideoPrivacy.PRIVATE,
549 updateAt: updateAt.toISOString(),
550 privacy: VideoPrivacy.PUBLIC
554 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
555 const uuid = resVideo.body.video.uuid
557 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
559 await waitJobs(servers)
560 await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist')
562 // FIXME: Can't test absence as two notifications sent to same user and util only checks last one
563 // One notification might be better anyways
564 // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
566 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
567 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
570 it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () {
574 const updateAt = new Date(new Date().getTime() + 2000)
576 const name = 'video with schedule done and still auto-blacklisted ' + buildUUID()
580 privacy: VideoPrivacy.PRIVATE,
582 updateAt: updateAt.toISOString(),
583 privacy: VideoPrivacy.PUBLIC
587 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
588 const uuid = resVideo.body.video.uuid
591 await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
592 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
593 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
596 it('Should not send a notification to moderators on new video without auto-blacklist', async function () {
599 const name = 'video without auto-blacklist ' + buildUUID()
601 // admin with blacklist right will not be auto-blacklisted
602 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name })
603 const uuid = resVideo.body.video.uuid
605 await waitJobs(servers)
606 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence')
610 await servers[0].configCommand.updateCustomConfig({ newCustomConfig: currentCustomConfig })
612 await servers[0].subscriptionsCommand.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
613 await servers[1].subscriptionsCommand.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
617 after(async function () {
618 MockSmtpServer.Instance.kill()
620 await cleanupTests(servers)