1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { v4 as uuidv4 } from 'uuid'
12 generateUserAccessToken,
15 getVideoCommentThreads,
20 removeVideoFromBlacklist,
24 updateCustomSubConfig,
28 } from '../../../../shared/extra-utils'
29 import { ServerInfo, uploadVideo } from '../../../../shared/extra-utils/index'
30 import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
31 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
33 checkAutoInstanceFollowing,
35 checkNewAccountAbuseForModerators,
36 checkNewBlacklistOnMyVideo,
37 checkNewCommentAbuseForModerators,
38 checkNewInstanceFollower,
39 checkNewVideoAbuseForModerators,
40 checkNewVideoFromSubscription,
42 checkVideoAutoBlacklistForModerators,
43 checkVideoIsPublished,
44 prepareNotificationsTest,
45 checkAbuseStateChange,
47 } from '../../../../shared/extra-utils/users/user-notifications'
48 import { addUserSubscription, removeUserSubscription } from '../../../../shared/extra-utils/users/user-subscriptions'
49 import { CustomConfig } from '../../../../shared/models/server'
50 import { UserNotification } from '../../../../shared/models/users'
51 import { VideoPrivacy } from '../../../../shared/models/videos'
52 import { AbuseState } from '@shared/models'
54 describe('Test moderation notifications', function () {
55 let servers: ServerInfo[] = []
56 let userAccessToken: string
57 let userNotifications: UserNotification[] = []
58 let adminNotifications: UserNotification[] = []
59 let adminNotificationsServer2: UserNotification[] = []
60 let emails: object[] = []
62 before(async function () {
65 const res = await prepareNotificationsTest(3)
67 userAccessToken = res.userAccessToken
69 userNotifications = res.userNotifications
70 adminNotifications = res.adminNotifications
71 adminNotificationsServer2 = res.adminNotificationsServer2
74 describe('Abuse for moderators notification', function () {
75 let baseParams: CheckerBaseParams
81 socketNotifications: adminNotifications,
82 token: servers[0].accessToken
86 it('Should send a notification to moderators on local video abuse', async function () {
89 const name = 'video for abuse ' + uuidv4()
90 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
91 const video = resVideo.body.video
93 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, videoId: video.id, reason: 'super reason' })
95 await waitJobs(servers)
96 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
99 it('Should send a notification to moderators on remote video abuse', async function () {
102 const name = 'video for abuse ' + uuidv4()
103 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
104 const video = resVideo.body.video
106 await waitJobs(servers)
108 const videoId = await getVideoIdFromUUID(servers[1].url, video.uuid)
109 await reportAbuse({ url: servers[1].url, token: servers[1].accessToken, videoId, reason: 'super reason' })
111 await waitJobs(servers)
112 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
115 it('Should send a notification to moderators on local comment abuse', async function () {
118 const name = 'video for abuse ' + uuidv4()
119 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
120 const video = resVideo.body.video
121 const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + uuidv4())
122 const comment = resComment.body.comment
124 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, commentId: comment.id, reason: 'super reason' })
126 await waitJobs(servers)
127 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
130 it('Should send a notification to moderators on remote comment abuse', async function () {
133 const name = 'video for abuse ' + uuidv4()
134 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
135 const video = resVideo.body.video
136 await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + uuidv4())
138 await waitJobs(servers)
140 const resComments = await getVideoCommentThreads(servers[1].url, video.uuid, 0, 5)
141 const commentId = resComments.body.data[0].id
142 await reportAbuse({ url: servers[1].url, token: servers[1].accessToken, commentId, reason: 'super reason' })
144 await waitJobs(servers)
145 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
148 it('Should send a notification to moderators on local account abuse', async function () {
151 const username = 'user' + new Date().getTime()
152 const resUser = await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username, password: 'donald' })
153 const accountId = resUser.body.user.account.id
155 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, accountId, reason: 'super reason' })
157 await waitJobs(servers)
158 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
161 it('Should send a notification to moderators on remote account abuse', async function () {
164 const username = 'user' + new Date().getTime()
165 const tmpToken = await generateUserAccessToken(servers[0], username)
166 await uploadVideo(servers[0].url, tmpToken, { name: 'super video' })
168 await waitJobs(servers)
170 const resAccount = await getAccount(servers[1].url, username + '@' + servers[0].host)
171 await reportAbuse({ url: servers[1].url, token: servers[1].accessToken, accountId: resAccount.body.id, reason: 'super reason' })
173 await waitJobs(servers)
174 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
178 describe('Abuse state change notification', function () {
179 let baseParams: CheckerBaseParams
182 before(async function () {
186 socketNotifications: userNotifications,
187 token: userAccessToken
190 const name = 'abuse ' + uuidv4()
191 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
192 const video = resVideo.body.video
194 const res = await reportAbuse({ url: servers[0].url, token: userAccessToken, videoId: video.id, reason: 'super reason' })
195 abuseId = res.body.abuse.id
198 it('Should send a notification to reporter if the abuse has been accepted', async function () {
201 await updateAbuse(servers[0].url, servers[0].accessToken, abuseId, { state: AbuseState.ACCEPTED })
202 await waitJobs(servers)
204 await checkAbuseStateChange(baseParams, abuseId, AbuseState.ACCEPTED, 'presence')
207 it('Should send a notification to reporter if the abuse has been rejected', async function () {
210 await updateAbuse(servers[0].url, servers[0].accessToken, abuseId, { state: AbuseState.REJECTED })
211 await waitJobs(servers)
213 await checkAbuseStateChange(baseParams, abuseId, AbuseState.REJECTED, 'presence')
217 describe('New abuse message notification', function () {
218 let baseParamsUser: CheckerBaseParams
219 let baseParamsAdmin: CheckerBaseParams
223 before(async function () {
227 socketNotifications: userNotifications,
228 token: userAccessToken
234 socketNotifications: adminNotifications,
235 token: servers[0].accessToken
238 const name = 'abuse ' + uuidv4()
239 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
240 const video = resVideo.body.video
243 const res = await reportAbuse({ url: servers[0].url, token: userAccessToken, videoId: video.id, reason: 'super reason' })
244 abuseId = res.body.abuse.id
248 const res = await reportAbuse({ url: servers[0].url, token: userAccessToken, videoId: video.id, reason: 'super reason 2' })
249 abuseId2 = res.body.abuse.id
253 it('Should send a notification to reporter on new message', async function () {
256 const message = 'my super message to users'
257 await addAbuseMessage(servers[0].url, servers[0].accessToken, abuseId, message)
258 await waitJobs(servers)
260 await checkNewAbuseMessage(baseParamsUser, abuseId, message, 'user_1@example.com', 'presence')
263 it('Should not send a notification to the admin if sent by the admin', async function () {
266 const message = 'my super message that should not be sent to the admin'
267 await addAbuseMessage(servers[0].url, servers[0].accessToken, abuseId, message)
268 await waitJobs(servers)
270 await checkNewAbuseMessage(baseParamsAdmin, abuseId, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'absence')
273 it('Should send a notification to moderators', async function () {
276 const message = 'my super message to moderators'
277 await addAbuseMessage(servers[0].url, userAccessToken, abuseId2, message)
278 await waitJobs(servers)
280 await checkNewAbuseMessage(baseParamsAdmin, abuseId2, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'presence')
283 it('Should not send a notification to reporter if sent by the reporter', async function () {
286 const message = 'my super message that should not be sent to reporter'
287 await addAbuseMessage(servers[0].url, userAccessToken, abuseId2, message)
288 await waitJobs(servers)
290 await checkNewAbuseMessage(baseParamsUser, abuseId2, message, 'user_1@example.com', 'absence')
294 describe('Video blacklist on my video', function () {
295 let baseParams: CheckerBaseParams
301 socketNotifications: userNotifications,
302 token: userAccessToken
306 it('Should send a notification to video owner on blacklist', async function () {
309 const name = 'video for abuse ' + uuidv4()
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 checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist')
319 it('Should send a notification to video owner on unblacklist', async function () {
322 const name = 'video for abuse ' + uuidv4()
323 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
324 const uuid = resVideo.body.video.uuid
326 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
328 await waitJobs(servers)
329 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
330 await waitJobs(servers)
333 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist')
337 describe('New registration', function () {
338 let baseParams: CheckerBaseParams
344 socketNotifications: adminNotifications,
345 token: servers[0].accessToken
349 it('Should send a notification only to moderators when a user registers on the instance', async function () {
352 await registerUser(servers[0].url, 'user_45', 'password')
354 await waitJobs(servers)
356 await checkUserRegistered(baseParams, 'user_45', 'presence')
358 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
359 await checkUserRegistered(immutableAssign(baseParams, userOverride), 'user_45', 'absence')
363 describe('New instance follows', function () {
364 const instanceIndexServer = new MockInstancesIndex()
369 indexUrl: 'http://localhost:42101/api/v1/instances/hosts',
375 let baseParams: CheckerBaseParams
381 socketNotifications: adminNotifications,
382 token: servers[0].accessToken
385 await instanceIndexServer.initialize()
386 instanceIndexServer.addInstance(servers[1].host)
389 it('Should send a notification only to admin when there is a new instance follower', async function () {
392 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
394 await waitJobs(servers)
396 await checkNewInstanceFollower(baseParams, 'localhost:' + servers[2].port, 'presence')
398 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
399 await checkNewInstanceFollower(immutableAssign(baseParams, userOverride), 'localhost:' + servers[2].port, 'absence')
402 it('Should send a notification on auto follow back', async function () {
405 await unfollow(servers[2].url, servers[2].accessToken, servers[0])
406 await waitJobs(servers)
411 autoFollowBack: { enabled: true }
415 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
417 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
419 await waitJobs(servers)
421 const followerHost = servers[0].host
422 const followingHost = servers[2].host
423 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
425 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
426 await checkAutoInstanceFollowing(immutableAssign(baseParams, userOverride), followerHost, followingHost, 'absence')
428 config.followings.instance.autoFollowBack.enabled = false
429 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
430 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
431 await unfollow(servers[2].url, servers[2].accessToken, servers[0])
434 it('Should send a notification on auto instances index follow', async function () {
436 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
438 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
441 await waitJobs(servers)
443 const followerHost = servers[0].host
444 const followingHost = servers[1].host
445 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
447 config.followings.instance.autoFollowIndex.enabled = false
448 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
449 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
453 describe('Video-related notifications when video auto-blacklist is enabled', function () {
454 let userBaseParams: CheckerBaseParams
455 let adminBaseParamsServer1: CheckerBaseParams
456 let adminBaseParamsServer2: CheckerBaseParams
457 let videoUUID: string
458 let videoName: string
459 let currentCustomConfig: CustomConfig
463 adminBaseParamsServer1 = {
466 socketNotifications: adminNotifications,
467 token: servers[0].accessToken
470 adminBaseParamsServer2 = {
473 socketNotifications: adminNotificationsServer2,
474 token: servers[1].accessToken
480 socketNotifications: userNotifications,
481 token: userAccessToken
484 const resCustomConfig = await getCustomConfig(servers[0].url, servers[0].accessToken)
485 currentCustomConfig = resCustomConfig.body
486 const autoBlacklistTestsCustomConfig = immutableAssign(currentCustomConfig, {
495 // enable transcoding otherwise own publish notification after transcoding not expected
496 autoBlacklistTestsCustomConfig.transcoding.enabled = true
497 await updateCustomConfig(servers[0].url, servers[0].accessToken, autoBlacklistTestsCustomConfig)
499 await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
500 await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
504 it('Should send notification to moderators on new video with auto-blacklist', async function () {
507 videoName = 'video with auto-blacklist ' + uuidv4()
508 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: videoName })
509 videoUUID = resVideo.body.video.uuid
511 await waitJobs(servers)
512 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence')
515 it('Should not send video publish notification if auto-blacklisted', async function () {
516 await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'absence')
519 it('Should not send a local user subscription notification if auto-blacklisted', async function () {
520 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'absence')
523 it('Should not send a remote user subscription notification if auto-blacklisted', async function () {
524 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'absence')
527 it('Should send video published and unblacklist after video unblacklisted', async function () {
530 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoUUID)
532 await waitJobs(servers)
534 // FIXME: Can't test as two notifications sent to same user and util only checks last one
535 // One notification might be better anyways
536 // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist')
537 // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence')
540 it('Should send a local user subscription notification after removed from blacklist', async function () {
541 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'presence')
544 it('Should send a remote user subscription notification after removed from blacklist', async function () {
545 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'presence')
548 it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () {
551 const updateAt = new Date(new Date().getTime() + 1000000)
553 const name = 'video with auto-blacklist and future schedule ' + uuidv4()
557 privacy: VideoPrivacy.PRIVATE,
559 updateAt: updateAt.toISOString(),
560 privacy: VideoPrivacy.PUBLIC
564 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
565 const uuid = resVideo.body.video.uuid
567 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
569 await waitJobs(servers)
570 await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist')
572 // FIXME: Can't test absence as two notifications sent to same user and util only checks last one
573 // One notification might be better anyways
574 // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
576 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
577 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
580 it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () {
584 const updateAt = new Date(new Date().getTime() + 2000)
586 const name = 'video with schedule done and still auto-blacklisted ' + uuidv4()
590 privacy: VideoPrivacy.PRIVATE,
592 updateAt: updateAt.toISOString(),
593 privacy: VideoPrivacy.PUBLIC
597 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
598 const uuid = resVideo.body.video.uuid
601 await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
602 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
603 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
606 it('Should not send a notification to moderators on new video without auto-blacklist', async function () {
609 const name = 'video without auto-blacklist ' + uuidv4()
611 // admin with blacklist right will not be auto-blacklisted
612 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name })
613 const uuid = resVideo.body.video.uuid
615 await waitJobs(servers)
616 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence')
620 await updateCustomConfig(servers[0].url, servers[0].accessToken, currentCustomConfig)
622 await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
623 await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
627 after(async function () {
628 MockSmtpServer.Instance.kill()
630 await cleanupTests(servers)