1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { buildUUID } from '@server/helpers/uuid'
10 checkAutoInstanceFollowing,
13 checkNewAccountAbuseForModerators,
14 checkNewBlacklistOnMyVideo,
15 checkNewCommentAbuseForModerators,
16 checkNewInstanceFollower,
17 checkNewVideoAbuseForModerators,
18 checkNewVideoFromSubscription,
20 checkVideoAutoBlacklistForModerators,
21 checkVideoIsPublished,
24 generateUserAccessToken,
25 getVideoCommentThreads,
30 prepareNotificationsTest,
32 removeUserSubscription,
33 removeVideoFromBlacklist,
38 } from '@shared/extra-utils'
39 import { AbuseState, CustomConfig, UserNotification, VideoPrivacy } from '@shared/models'
41 describe('Test moderation notifications', function () {
42 let servers: ServerInfo[] = []
43 let userAccessToken: string
44 let userNotifications: UserNotification[] = []
45 let adminNotifications: UserNotification[] = []
46 let adminNotificationsServer2: UserNotification[] = []
47 let emails: object[] = []
49 before(async function () {
52 const res = await prepareNotificationsTest(3)
54 userAccessToken = res.userAccessToken
56 userNotifications = res.userNotifications
57 adminNotifications = res.adminNotifications
58 adminNotificationsServer2 = res.adminNotificationsServer2
61 describe('Abuse for moderators notification', function () {
62 let baseParams: CheckerBaseParams
68 socketNotifications: adminNotifications,
69 token: servers[0].accessToken
73 it('Should send a notification to moderators on local video abuse', async function () {
76 const name = 'video for abuse ' + buildUUID()
77 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
78 const video = resVideo.body.video
80 await servers[0].abusesCommand.report({ videoId: video.id, reason: 'super reason' })
82 await waitJobs(servers)
83 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
86 it('Should send a notification to moderators on remote video abuse', async function () {
89 const name = 'video for abuse ' + buildUUID()
90 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
91 const video = resVideo.body.video
93 await waitJobs(servers)
95 const videoId = await getVideoIdFromUUID(servers[1].url, video.uuid)
96 await servers[1].abusesCommand.report({ videoId, reason: 'super reason' })
98 await waitJobs(servers)
99 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
102 it('Should send a notification to moderators on local comment abuse', async function () {
105 const name = 'video for abuse ' + buildUUID()
106 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
107 const video = resVideo.body.video
108 const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + buildUUID())
109 const comment = resComment.body.comment
111 await waitJobs(servers)
113 await servers[0].abusesCommand.report({ commentId: comment.id, reason: 'super reason' })
115 await waitJobs(servers)
116 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
119 it('Should send a notification to moderators on remote comment abuse', async function () {
122 const name = 'video for abuse ' + buildUUID()
123 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
124 const video = resVideo.body.video
125 await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + buildUUID())
127 await waitJobs(servers)
129 const resComments = await getVideoCommentThreads(servers[1].url, video.uuid, 0, 5)
130 const commentId = resComments.body.data[0].id
131 await servers[1].abusesCommand.report({ commentId, reason: 'super reason' })
133 await waitJobs(servers)
134 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
137 it('Should send a notification to moderators on local account abuse', async function () {
140 const username = 'user' + new Date().getTime()
141 const resUser = await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username, password: 'donald' })
142 const accountId = resUser.body.user.account.id
144 await servers[0].abusesCommand.report({ accountId, reason: 'super reason' })
146 await waitJobs(servers)
147 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
150 it('Should send a notification to moderators on remote account abuse', async function () {
153 const username = 'user' + new Date().getTime()
154 const tmpToken = await generateUserAccessToken(servers[0], username)
155 await uploadVideo(servers[0].url, tmpToken, { name: 'super video' })
157 await waitJobs(servers)
159 const account = await servers[1].accountsCommand.get({ accountName: username + '@' + servers[0].host })
160 await servers[1].abusesCommand.report({ accountId: account.id, reason: 'super reason' })
162 await waitJobs(servers)
163 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
167 describe('Abuse state change notification', function () {
168 let baseParams: CheckerBaseParams
171 before(async function () {
175 socketNotifications: userNotifications,
176 token: userAccessToken
179 const name = 'abuse ' + buildUUID()
180 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
181 const video = resVideo.body.video
183 const body = await servers[0].abusesCommand.report({ token: userAccessToken, videoId: video.id, reason: 'super reason' })
184 abuseId = body.abuse.id
187 it('Should send a notification to reporter if the abuse has been accepted', async function () {
190 await servers[0].abusesCommand.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
191 await waitJobs(servers)
193 await checkAbuseStateChange(baseParams, abuseId, AbuseState.ACCEPTED, 'presence')
196 it('Should send a notification to reporter if the abuse has been rejected', async function () {
199 await servers[0].abusesCommand.update({ abuseId, body: { state: AbuseState.REJECTED } })
200 await waitJobs(servers)
202 await checkAbuseStateChange(baseParams, abuseId, AbuseState.REJECTED, 'presence')
206 describe('New abuse message notification', function () {
207 let baseParamsUser: CheckerBaseParams
208 let baseParamsAdmin: CheckerBaseParams
212 before(async function () {
216 socketNotifications: userNotifications,
217 token: userAccessToken
223 socketNotifications: adminNotifications,
224 token: servers[0].accessToken
227 const name = 'abuse ' + buildUUID()
228 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
229 const video = resVideo.body.video
232 const body = await servers[0].abusesCommand.report({ token: userAccessToken, videoId: video.id, reason: 'super reason' })
233 abuseId = body.abuse.id
237 const body = await servers[0].abusesCommand.report({ token: userAccessToken, videoId: video.id, reason: 'super reason 2' })
238 abuseId2 = body.abuse.id
242 it('Should send a notification to reporter on new message', async function () {
245 const message = 'my super message to users'
246 await servers[0].abusesCommand.addMessage({ abuseId, message })
247 await waitJobs(servers)
249 await checkNewAbuseMessage(baseParamsUser, abuseId, message, 'user_1@example.com', 'presence')
252 it('Should not send a notification to the admin if sent by the admin', async function () {
255 const message = 'my super message that should not be sent to the admin'
256 await servers[0].abusesCommand.addMessage({ abuseId, message })
257 await waitJobs(servers)
259 await checkNewAbuseMessage(baseParamsAdmin, abuseId, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'absence')
262 it('Should send a notification to moderators', async function () {
265 const message = 'my super message to moderators'
266 await servers[0].abusesCommand.addMessage({ token: userAccessToken, abuseId: abuseId2, message })
267 await waitJobs(servers)
269 await checkNewAbuseMessage(baseParamsAdmin, abuseId2, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'presence')
272 it('Should not send a notification to reporter if sent by the reporter', async function () {
275 const message = 'my super message that should not be sent to reporter'
276 await servers[0].abusesCommand.addMessage({ token: userAccessToken, abuseId: abuseId2, message })
277 await waitJobs(servers)
279 await checkNewAbuseMessage(baseParamsUser, abuseId2, message, 'user_1@example.com', 'absence')
283 describe('Video blacklist on my video', function () {
284 let baseParams: CheckerBaseParams
290 socketNotifications: userNotifications,
291 token: userAccessToken
295 it('Should send a notification to video owner on blacklist', async function () {
298 const name = 'video for abuse ' + buildUUID()
299 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
300 const uuid = resVideo.body.video.uuid
302 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
304 await waitJobs(servers)
305 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist')
308 it('Should send a notification to video owner on unblacklist', async function () {
311 const name = 'video for abuse ' + buildUUID()
312 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
313 const uuid = resVideo.body.video.uuid
315 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
317 await waitJobs(servers)
318 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
319 await waitJobs(servers)
322 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist')
326 describe('New registration', function () {
327 let baseParams: CheckerBaseParams
333 socketNotifications: adminNotifications,
334 token: servers[0].accessToken
338 it('Should send a notification only to moderators when a user registers on the instance', async function () {
341 await registerUser(servers[0].url, 'user_45', 'password')
343 await waitJobs(servers)
345 await checkUserRegistered(baseParams, 'user_45', 'presence')
347 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
348 await checkUserRegistered(immutableAssign(baseParams, userOverride), 'user_45', 'absence')
352 describe('New instance follows', function () {
353 const instanceIndexServer = new MockInstancesIndex()
355 let baseParams: CheckerBaseParams
361 socketNotifications: adminNotifications,
362 token: servers[0].accessToken
365 const port = await instanceIndexServer.initialize()
366 instanceIndexServer.addInstance(servers[1].host)
372 indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
380 it('Should send a notification only to admin when there is a new instance follower', async function () {
383 await servers[2].followsCommand.follow({ targets: [ servers[0].url ] })
385 await waitJobs(servers)
387 await checkNewInstanceFollower(baseParams, 'localhost:' + servers[2].port, 'presence')
389 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
390 await checkNewInstanceFollower(immutableAssign(baseParams, userOverride), 'localhost:' + servers[2].port, 'absence')
393 it('Should send a notification on auto follow back', async function () {
396 await servers[2].followsCommand.unfollow({ target: servers[0] })
397 await waitJobs(servers)
402 autoFollowBack: { enabled: true }
406 await servers[0].configCommand.updateCustomSubConfig({ newConfig: config })
408 await servers[2].followsCommand.follow({ targets: [ servers[0].url ] })
410 await waitJobs(servers)
412 const followerHost = servers[0].host
413 const followingHost = servers[2].host
414 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
416 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
417 await checkAutoInstanceFollowing(immutableAssign(baseParams, userOverride), followerHost, followingHost, 'absence')
419 config.followings.instance.autoFollowBack.enabled = false
420 await servers[0].configCommand.updateCustomSubConfig({ newConfig: config })
421 await servers[0].followsCommand.unfollow({ target: servers[2] })
422 await servers[2].followsCommand.unfollow({ target: servers[0] })
425 it('Should send a notification on auto instances index follow', async function () {
427 await servers[0].followsCommand.unfollow({ target: servers[1] })
429 await servers[0].configCommand.updateCustomSubConfig({ newConfig: config })
432 await waitJobs(servers)
434 const followerHost = servers[0].host
435 const followingHost = servers[1].host
436 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
438 config.followings.instance.autoFollowIndex.enabled = false
439 await servers[0].configCommand.updateCustomSubConfig({ newConfig: config })
440 await servers[0].followsCommand.unfollow({ target: servers[1] })
444 describe('Video-related notifications when video auto-blacklist is enabled', function () {
445 let userBaseParams: CheckerBaseParams
446 let adminBaseParamsServer1: CheckerBaseParams
447 let adminBaseParamsServer2: CheckerBaseParams
448 let videoUUID: string
449 let videoName: string
450 let currentCustomConfig: CustomConfig
454 adminBaseParamsServer1 = {
457 socketNotifications: adminNotifications,
458 token: servers[0].accessToken
461 adminBaseParamsServer2 = {
464 socketNotifications: adminNotificationsServer2,
465 token: servers[1].accessToken
471 socketNotifications: userNotifications,
472 token: userAccessToken
475 currentCustomConfig = await servers[0].configCommand.getCustomConfig()
477 const autoBlacklistTestsCustomConfig = immutableAssign(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 addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
492 await addUserSubscription(servers[1].url, servers[1].accessToken, '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 removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, 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 removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, 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 removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
615 await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
619 after(async function () {
620 MockSmtpServer.Instance.kill()
622 await cleanupTests(servers)