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,
25 generateUserAccessToken,
28 getVideoCommentThreads,
33 prepareNotificationsTest,
35 removeUserSubscription,
36 removeVideoFromBlacklist,
40 updateCustomSubConfig,
44 } from '@shared/extra-utils'
45 import { AbuseState, CustomConfig, UserNotification, VideoPrivacy } from '@shared/models'
47 describe('Test moderation notifications', function () {
48 let servers: ServerInfo[] = []
49 let userAccessToken: string
50 let userNotifications: UserNotification[] = []
51 let adminNotifications: UserNotification[] = []
52 let adminNotificationsServer2: UserNotification[] = []
53 let emails: object[] = []
55 before(async function () {
58 const res = await prepareNotificationsTest(3)
60 userAccessToken = res.userAccessToken
62 userNotifications = res.userNotifications
63 adminNotifications = res.adminNotifications
64 adminNotificationsServer2 = res.adminNotificationsServer2
67 describe('Abuse for moderators notification', function () {
68 let baseParams: CheckerBaseParams
74 socketNotifications: adminNotifications,
75 token: servers[0].accessToken
79 it('Should send a notification to moderators on local video abuse', async function () {
82 const name = 'video for abuse ' + buildUUID()
83 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
84 const video = resVideo.body.video
86 await servers[0].abusesCommand.report({ videoId: video.id, reason: 'super reason' })
88 await waitJobs(servers)
89 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
92 it('Should send a notification to moderators on remote video 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
99 await waitJobs(servers)
101 const videoId = await getVideoIdFromUUID(servers[1].url, video.uuid)
102 await servers[1].abusesCommand.report({ videoId, reason: 'super reason' })
104 await waitJobs(servers)
105 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
108 it('Should send a notification to moderators on local comment abuse', async function () {
111 const name = 'video for abuse ' + buildUUID()
112 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
113 const video = resVideo.body.video
114 const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + buildUUID())
115 const comment = resComment.body.comment
117 await waitJobs(servers)
119 await servers[0].abusesCommand.report({ commentId: comment.id, reason: 'super reason' })
121 await waitJobs(servers)
122 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
125 it('Should send a notification to moderators on remote comment abuse', async function () {
128 const name = 'video for abuse ' + buildUUID()
129 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
130 const video = resVideo.body.video
131 await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + buildUUID())
133 await waitJobs(servers)
135 const resComments = await getVideoCommentThreads(servers[1].url, video.uuid, 0, 5)
136 const commentId = resComments.body.data[0].id
137 await servers[1].abusesCommand.report({ commentId, reason: 'super reason' })
139 await waitJobs(servers)
140 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
143 it('Should send a notification to moderators on local account abuse', async function () {
146 const username = 'user' + new Date().getTime()
147 const resUser = await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username, password: 'donald' })
148 const accountId = resUser.body.user.account.id
150 await servers[0].abusesCommand.report({ accountId, reason: 'super reason' })
152 await waitJobs(servers)
153 await checkNewAccountAbuseForModerators(baseParams, username, '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 generateUserAccessToken(servers[0], username)
161 await uploadVideo(servers[0].url, tmpToken, { name: 'super video' })
163 await waitJobs(servers)
165 const resAccount = await getAccount(servers[1].url, username + '@' + servers[0].host)
166 await servers[1].abusesCommand.report({ accountId: resAccount.body.id, reason: 'super reason' })
168 await waitJobs(servers)
169 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
173 describe('Abuse state change notification', function () {
174 let baseParams: CheckerBaseParams
177 before(async function () {
181 socketNotifications: userNotifications,
182 token: userAccessToken
185 const name = 'abuse ' + buildUUID()
186 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
187 const video = resVideo.body.video
189 const body = await servers[0].abusesCommand.report({ token: userAccessToken, videoId: video.id, reason: 'super reason' })
190 abuseId = body.abuse.id
193 it('Should send a notification to reporter if the abuse has been accepted', async function () {
196 await servers[0].abusesCommand.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
197 await waitJobs(servers)
199 await checkAbuseStateChange(baseParams, abuseId, AbuseState.ACCEPTED, 'presence')
202 it('Should send a notification to reporter if the abuse has been rejected', async function () {
205 await servers[0].abusesCommand.update({ abuseId, body: { state: AbuseState.REJECTED } })
206 await waitJobs(servers)
208 await checkAbuseStateChange(baseParams, abuseId, AbuseState.REJECTED, 'presence')
212 describe('New abuse message notification', function () {
213 let baseParamsUser: CheckerBaseParams
214 let baseParamsAdmin: CheckerBaseParams
218 before(async function () {
222 socketNotifications: userNotifications,
223 token: userAccessToken
229 socketNotifications: adminNotifications,
230 token: servers[0].accessToken
233 const name = 'abuse ' + buildUUID()
234 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
235 const video = resVideo.body.video
238 const body = await servers[0].abusesCommand.report({ token: userAccessToken, videoId: video.id, reason: 'super reason' })
239 abuseId = body.abuse.id
243 const body = await servers[0].abusesCommand.report({ token: userAccessToken, videoId: video.id, reason: 'super reason 2' })
244 abuseId2 = body.abuse.id
248 it('Should send a notification to reporter on new message', async function () {
251 const message = 'my super message to users'
252 await servers[0].abusesCommand.addMessage({ abuseId, message })
253 await waitJobs(servers)
255 await checkNewAbuseMessage(baseParamsUser, abuseId, message, 'user_1@example.com', 'presence')
258 it('Should not send a notification to the admin if sent by the admin', async function () {
261 const message = 'my super message that should not be sent to the admin'
262 await servers[0].abusesCommand.addMessage({ abuseId, message })
263 await waitJobs(servers)
265 await checkNewAbuseMessage(baseParamsAdmin, abuseId, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'absence')
268 it('Should send a notification to moderators', async function () {
271 const message = 'my super message to moderators'
272 await servers[0].abusesCommand.addMessage({ token: userAccessToken, abuseId: abuseId2, message })
273 await waitJobs(servers)
275 await checkNewAbuseMessage(baseParamsAdmin, abuseId2, message, 'admin' + servers[0].internalServerNumber + '@example.com', '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].abusesCommand.addMessage({ token: userAccessToken, abuseId: abuseId2, message })
283 await waitJobs(servers)
285 await checkNewAbuseMessage(baseParamsUser, abuseId2, message, 'user_1@example.com', 'absence')
289 describe('Video blacklist on my video', function () {
290 let baseParams: CheckerBaseParams
296 socketNotifications: userNotifications,
297 token: userAccessToken
301 it('Should send a notification to video owner on blacklist', async function () {
304 const name = 'video for abuse ' + buildUUID()
305 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
306 const uuid = resVideo.body.video.uuid
308 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
310 await waitJobs(servers)
311 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist')
314 it('Should send a notification to video owner on unblacklist', async function () {
317 const name = 'video for abuse ' + buildUUID()
318 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
319 const uuid = resVideo.body.video.uuid
321 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
323 await waitJobs(servers)
324 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
325 await waitJobs(servers)
328 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist')
332 describe('New registration', function () {
333 let baseParams: CheckerBaseParams
339 socketNotifications: adminNotifications,
340 token: servers[0].accessToken
344 it('Should send a notification only to moderators when a user registers on the instance', async function () {
347 await registerUser(servers[0].url, 'user_45', 'password')
349 await waitJobs(servers)
351 await checkUserRegistered(baseParams, 'user_45', 'presence')
353 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
354 await checkUserRegistered(immutableAssign(baseParams, userOverride), 'user_45', 'absence')
358 describe('New instance follows', function () {
359 const instanceIndexServer = new MockInstancesIndex()
361 let baseParams: CheckerBaseParams
367 socketNotifications: adminNotifications,
368 token: servers[0].accessToken
371 const port = await instanceIndexServer.initialize()
372 instanceIndexServer.addInstance(servers[1].host)
378 indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
386 it('Should send a notification only to admin when there is a new instance follower', async function () {
389 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
391 await waitJobs(servers)
393 await checkNewInstanceFollower(baseParams, 'localhost:' + servers[2].port, 'presence')
395 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
396 await checkNewInstanceFollower(immutableAssign(baseParams, userOverride), 'localhost:' + servers[2].port, 'absence')
399 it('Should send a notification on auto follow back', async function () {
402 await unfollow(servers[2].url, servers[2].accessToken, servers[0])
403 await waitJobs(servers)
408 autoFollowBack: { enabled: true }
412 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
414 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
416 await waitJobs(servers)
418 const followerHost = servers[0].host
419 const followingHost = servers[2].host
420 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
422 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
423 await checkAutoInstanceFollowing(immutableAssign(baseParams, userOverride), followerHost, followingHost, 'absence')
425 config.followings.instance.autoFollowBack.enabled = false
426 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
427 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
428 await unfollow(servers[2].url, servers[2].accessToken, servers[0])
431 it('Should send a notification on auto instances index follow', async function () {
433 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
435 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
438 await waitJobs(servers)
440 const followerHost = servers[0].host
441 const followingHost = servers[1].host
442 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
444 config.followings.instance.autoFollowIndex.enabled = false
445 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
446 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
450 describe('Video-related notifications when video auto-blacklist is enabled', function () {
451 let userBaseParams: CheckerBaseParams
452 let adminBaseParamsServer1: CheckerBaseParams
453 let adminBaseParamsServer2: CheckerBaseParams
454 let videoUUID: 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,
478 token: userAccessToken
481 const resCustomConfig = await getCustomConfig(servers[0].url, servers[0].accessToken)
482 currentCustomConfig = resCustomConfig.body
483 const autoBlacklistTestsCustomConfig = immutableAssign(currentCustomConfig, {
492 // enable transcoding otherwise own publish notification after transcoding not expected
493 autoBlacklistTestsCustomConfig.transcoding.enabled = true
494 await updateCustomConfig(servers[0].url, servers[0].accessToken, autoBlacklistTestsCustomConfig)
496 await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
497 await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
501 it('Should send notification to moderators on new video with auto-blacklist', async function () {
504 videoName = 'video with auto-blacklist ' + buildUUID()
505 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: videoName })
506 videoUUID = resVideo.body.video.uuid
508 await waitJobs(servers)
509 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence')
512 it('Should not send video publish notification if auto-blacklisted', async function () {
513 await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'absence')
516 it('Should not send a local user subscription notification if auto-blacklisted', async function () {
517 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'absence')
520 it('Should not send a remote user subscription notification if auto-blacklisted', async function () {
521 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'absence')
524 it('Should send video published and unblacklist after video unblacklisted', async function () {
527 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoUUID)
529 await waitJobs(servers)
531 // FIXME: Can't test as two notifications sent to same user and util only checks last one
532 // One notification might be better anyways
533 // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist')
534 // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence')
537 it('Should send a local user subscription notification after removed from blacklist', async function () {
538 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'presence')
541 it('Should send a remote user subscription notification after removed from blacklist', async function () {
542 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'presence')
545 it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () {
548 const updateAt = new Date(new Date().getTime() + 1000000)
550 const name = 'video with auto-blacklist and future schedule ' + buildUUID()
554 privacy: VideoPrivacy.PRIVATE,
556 updateAt: updateAt.toISOString(),
557 privacy: VideoPrivacy.PUBLIC
561 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
562 const uuid = resVideo.body.video.uuid
564 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
566 await waitJobs(servers)
567 await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist')
569 // FIXME: Can't test absence as two notifications sent to same user and util only checks last one
570 // One notification might be better anyways
571 // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
573 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
574 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
577 it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () {
581 const updateAt = new Date(new Date().getTime() + 2000)
583 const name = 'video with schedule done and still auto-blacklisted ' + buildUUID()
587 privacy: VideoPrivacy.PRIVATE,
589 updateAt: updateAt.toISOString(),
590 privacy: VideoPrivacy.PUBLIC
594 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
595 const uuid = resVideo.body.video.uuid
598 await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
599 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
600 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
603 it('Should not send a notification to moderators on new video without auto-blacklist', async function () {
606 const name = 'video without auto-blacklist ' + buildUUID()
608 // admin with blacklist right will not be auto-blacklisted
609 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name })
610 const uuid = resVideo.body.video.uuid
612 await waitJobs(servers)
613 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence')
617 await updateCustomConfig(servers[0].url, servers[0].accessToken, currentCustomConfig)
619 await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
620 await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
624 after(async function () {
625 MockSmtpServer.Instance.kill()
627 await cleanupTests(servers)