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,
27 getVideoCommentThreads,
32 prepareNotificationsTest,
34 removeUserSubscription,
35 removeVideoFromBlacklist,
38 updateCustomSubConfig,
42 } from '@shared/extra-utils'
43 import { AbuseState, CustomConfig, UserNotification, VideoPrivacy } from '@shared/models'
45 describe('Test moderation notifications', function () {
46 let servers: ServerInfo[] = []
47 let userAccessToken: string
48 let userNotifications: UserNotification[] = []
49 let adminNotifications: UserNotification[] = []
50 let adminNotificationsServer2: UserNotification[] = []
51 let emails: object[] = []
53 before(async function () {
56 const res = await prepareNotificationsTest(3)
58 userAccessToken = res.userAccessToken
60 userNotifications = res.userNotifications
61 adminNotifications = res.adminNotifications
62 adminNotificationsServer2 = res.adminNotificationsServer2
65 describe('Abuse for moderators notification', function () {
66 let baseParams: CheckerBaseParams
72 socketNotifications: adminNotifications,
73 token: servers[0].accessToken
77 it('Should send a notification to moderators on local video abuse', async function () {
80 const name = 'video for abuse ' + buildUUID()
81 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
82 const video = resVideo.body.video
84 await servers[0].abusesCommand.report({ videoId: video.id, reason: 'super reason' })
86 await waitJobs(servers)
87 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
90 it('Should send a notification to moderators on remote video abuse', async function () {
93 const name = 'video for abuse ' + buildUUID()
94 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
95 const video = resVideo.body.video
97 await waitJobs(servers)
99 const videoId = await getVideoIdFromUUID(servers[1].url, video.uuid)
100 await servers[1].abusesCommand.report({ videoId, reason: 'super reason' })
102 await waitJobs(servers)
103 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
106 it('Should send a notification to moderators on local comment abuse', async function () {
109 const name = 'video for abuse ' + buildUUID()
110 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
111 const video = resVideo.body.video
112 const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + buildUUID())
113 const comment = resComment.body.comment
115 await waitJobs(servers)
117 await servers[0].abusesCommand.report({ commentId: comment.id, reason: 'super reason' })
119 await waitJobs(servers)
120 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
123 it('Should send a notification to moderators on remote comment abuse', async function () {
126 const name = 'video for abuse ' + buildUUID()
127 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
128 const video = resVideo.body.video
129 await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + buildUUID())
131 await waitJobs(servers)
133 const resComments = await getVideoCommentThreads(servers[1].url, video.uuid, 0, 5)
134 const commentId = resComments.body.data[0].id
135 await servers[1].abusesCommand.report({ commentId, reason: 'super reason' })
137 await waitJobs(servers)
138 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
141 it('Should send a notification to moderators on local account abuse', async function () {
144 const username = 'user' + new Date().getTime()
145 const resUser = await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username, password: 'donald' })
146 const accountId = resUser.body.user.account.id
148 await servers[0].abusesCommand.report({ accountId, reason: 'super reason' })
150 await waitJobs(servers)
151 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
154 it('Should send a notification to moderators on remote account abuse', async function () {
157 const username = 'user' + new Date().getTime()
158 const tmpToken = await generateUserAccessToken(servers[0], username)
159 await uploadVideo(servers[0].url, tmpToken, { name: 'super video' })
161 await waitJobs(servers)
163 const resAccount = await getAccount(servers[1].url, username + '@' + servers[0].host)
164 await servers[1].abusesCommand.report({ accountId: resAccount.body.id, reason: 'super reason' })
166 await waitJobs(servers)
167 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
171 describe('Abuse state change notification', function () {
172 let baseParams: CheckerBaseParams
175 before(async function () {
179 socketNotifications: userNotifications,
180 token: userAccessToken
183 const name = 'abuse ' + buildUUID()
184 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
185 const video = resVideo.body.video
187 const body = await servers[0].abusesCommand.report({ token: userAccessToken, videoId: video.id, reason: 'super reason' })
188 abuseId = body.abuse.id
191 it('Should send a notification to reporter if the abuse has been accepted', async function () {
194 await servers[0].abusesCommand.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
195 await waitJobs(servers)
197 await checkAbuseStateChange(baseParams, abuseId, AbuseState.ACCEPTED, 'presence')
200 it('Should send a notification to reporter if the abuse has been rejected', async function () {
203 await servers[0].abusesCommand.update({ abuseId, body: { state: AbuseState.REJECTED } })
204 await waitJobs(servers)
206 await checkAbuseStateChange(baseParams, abuseId, AbuseState.REJECTED, 'presence')
210 describe('New abuse message notification', function () {
211 let baseParamsUser: CheckerBaseParams
212 let baseParamsAdmin: CheckerBaseParams
216 before(async function () {
220 socketNotifications: userNotifications,
221 token: userAccessToken
227 socketNotifications: adminNotifications,
228 token: servers[0].accessToken
231 const name = 'abuse ' + buildUUID()
232 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
233 const video = resVideo.body.video
236 const body = await servers[0].abusesCommand.report({ token: userAccessToken, videoId: video.id, reason: 'super reason' })
237 abuseId = body.abuse.id
241 const body = await servers[0].abusesCommand.report({ token: userAccessToken, 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].abusesCommand.addMessage({ abuseId, message })
251 await waitJobs(servers)
253 await checkNewAbuseMessage(baseParamsUser, abuseId, message, 'user_1@example.com', '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].abusesCommand.addMessage({ abuseId, message })
261 await waitJobs(servers)
263 await checkNewAbuseMessage(baseParamsAdmin, abuseId, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'absence')
266 it('Should send a notification to moderators', async function () {
269 const message = 'my super message to moderators'
270 await servers[0].abusesCommand.addMessage({ token: userAccessToken, abuseId: abuseId2, message })
271 await waitJobs(servers)
273 await checkNewAbuseMessage(baseParamsAdmin, abuseId2, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'presence')
276 it('Should not send a notification to reporter if sent by the reporter', async function () {
279 const message = 'my super message that should not be sent to reporter'
280 await servers[0].abusesCommand.addMessage({ token: userAccessToken, abuseId: abuseId2, message })
281 await waitJobs(servers)
283 await checkNewAbuseMessage(baseParamsUser, abuseId2, message, 'user_1@example.com', 'absence')
287 describe('Video blacklist on my video', function () {
288 let baseParams: CheckerBaseParams
294 socketNotifications: userNotifications,
295 token: userAccessToken
299 it('Should send a notification to video owner on blacklist', async function () {
302 const name = 'video for abuse ' + buildUUID()
303 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
304 const uuid = resVideo.body.video.uuid
306 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
308 await waitJobs(servers)
309 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist')
312 it('Should send a notification to video owner on unblacklist', async function () {
315 const name = 'video for abuse ' + buildUUID()
316 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
317 const uuid = resVideo.body.video.uuid
319 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
321 await waitJobs(servers)
322 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
323 await waitJobs(servers)
326 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist')
330 describe('New registration', function () {
331 let baseParams: CheckerBaseParams
337 socketNotifications: adminNotifications,
338 token: servers[0].accessToken
342 it('Should send a notification only to moderators when a user registers on the instance', async function () {
345 await registerUser(servers[0].url, 'user_45', 'password')
347 await waitJobs(servers)
349 await checkUserRegistered(baseParams, 'user_45', 'presence')
351 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
352 await checkUserRegistered(immutableAssign(baseParams, userOverride), 'user_45', 'absence')
356 describe('New instance follows', function () {
357 const instanceIndexServer = new MockInstancesIndex()
359 let baseParams: CheckerBaseParams
365 socketNotifications: adminNotifications,
366 token: servers[0].accessToken
369 const port = await instanceIndexServer.initialize()
370 instanceIndexServer.addInstance(servers[1].host)
376 indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
384 it('Should send a notification only to admin when there is a new instance follower', async function () {
387 await servers[2].followsCommand.follow({ targets: [ servers[0].url ] })
389 await waitJobs(servers)
391 await checkNewInstanceFollower(baseParams, 'localhost:' + servers[2].port, 'presence')
393 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
394 await checkNewInstanceFollower(immutableAssign(baseParams, userOverride), 'localhost:' + servers[2].port, 'absence')
397 it('Should send a notification on auto follow back', async function () {
400 await servers[2].followsCommand.unfollow({ target: servers[0] })
401 await waitJobs(servers)
406 autoFollowBack: { enabled: true }
410 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
412 await servers[2].followsCommand.follow({ targets: [ servers[0].url ] })
414 await waitJobs(servers)
416 const followerHost = servers[0].host
417 const followingHost = servers[2].host
418 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
420 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
421 await checkAutoInstanceFollowing(immutableAssign(baseParams, userOverride), followerHost, followingHost, 'absence')
423 config.followings.instance.autoFollowBack.enabled = false
424 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
425 await servers[0].followsCommand.unfollow({ target: servers[2] })
426 await servers[2].followsCommand.unfollow({ target: servers[0] })
429 it('Should send a notification on auto instances index follow', async function () {
431 await servers[0].followsCommand.unfollow({ target: servers[1] })
433 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
436 await waitJobs(servers)
438 const followerHost = servers[0].host
439 const followingHost = servers[1].host
440 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
442 config.followings.instance.autoFollowIndex.enabled = false
443 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
444 await servers[0].followsCommand.unfollow({ target: servers[1] })
448 describe('Video-related notifications when video auto-blacklist is enabled', function () {
449 let userBaseParams: CheckerBaseParams
450 let adminBaseParamsServer1: CheckerBaseParams
451 let adminBaseParamsServer2: CheckerBaseParams
452 let videoUUID: string
453 let videoName: string
454 let currentCustomConfig: CustomConfig
458 adminBaseParamsServer1 = {
461 socketNotifications: adminNotifications,
462 token: servers[0].accessToken
465 adminBaseParamsServer2 = {
468 socketNotifications: adminNotificationsServer2,
469 token: servers[1].accessToken
475 socketNotifications: userNotifications,
476 token: userAccessToken
479 const resCustomConfig = await getCustomConfig(servers[0].url, servers[0].accessToken)
480 currentCustomConfig = resCustomConfig.body
481 const autoBlacklistTestsCustomConfig = immutableAssign(currentCustomConfig, {
490 // enable transcoding otherwise own publish notification after transcoding not expected
491 autoBlacklistTestsCustomConfig.transcoding.enabled = true
492 await updateCustomConfig(servers[0].url, servers[0].accessToken, autoBlacklistTestsCustomConfig)
494 await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
495 await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
499 it('Should send notification to moderators on new video with auto-blacklist', async function () {
502 videoName = 'video with auto-blacklist ' + buildUUID()
503 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: videoName })
504 videoUUID = resVideo.body.video.uuid
506 await waitJobs(servers)
507 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence')
510 it('Should not send video publish notification if auto-blacklisted', async function () {
511 await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'absence')
514 it('Should not send a local user subscription notification if auto-blacklisted', async function () {
515 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'absence')
518 it('Should not send a remote user subscription notification if auto-blacklisted', async function () {
519 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'absence')
522 it('Should send video published and unblacklist after video unblacklisted', async function () {
525 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoUUID)
527 await waitJobs(servers)
529 // FIXME: Can't test as two notifications sent to same user and util only checks last one
530 // One notification might be better anyways
531 // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist')
532 // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence')
535 it('Should send a local user subscription notification after removed from blacklist', async function () {
536 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'presence')
539 it('Should send a remote user subscription notification after removed from blacklist', async function () {
540 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'presence')
543 it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () {
546 const updateAt = new Date(new Date().getTime() + 1000000)
548 const name = 'video with auto-blacklist and future schedule ' + buildUUID()
552 privacy: VideoPrivacy.PRIVATE,
554 updateAt: updateAt.toISOString(),
555 privacy: VideoPrivacy.PUBLIC
559 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
560 const uuid = resVideo.body.video.uuid
562 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
564 await waitJobs(servers)
565 await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist')
567 // FIXME: Can't test absence as two notifications sent to same user and util only checks last one
568 // One notification might be better anyways
569 // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
571 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
572 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
575 it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () {
579 const updateAt = new Date(new Date().getTime() + 2000)
581 const name = 'video with schedule done and still auto-blacklisted ' + buildUUID()
585 privacy: VideoPrivacy.PRIVATE,
587 updateAt: updateAt.toISOString(),
588 privacy: VideoPrivacy.PUBLIC
592 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
593 const uuid = resVideo.body.video.uuid
596 await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
597 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
598 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
601 it('Should not send a notification to moderators on new video without auto-blacklist', async function () {
604 const name = 'video without auto-blacklist ' + buildUUID()
606 // admin with blacklist right will not be auto-blacklisted
607 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name })
608 const uuid = resVideo.body.video.uuid
610 await waitJobs(servers)
611 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence')
615 await updateCustomConfig(servers[0].url, servers[0].accessToken, currentCustomConfig)
617 await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
618 await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
622 after(async function () {
623 MockSmtpServer.Instance.kill()
625 await cleanupTests(servers)