1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { v4 as uuidv4 } from 'uuid'
5 import { AbuseState } from '@shared/models'
13 generateUserAccessToken,
16 getVideoCommentThreads,
21 removeVideoFromBlacklist,
26 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 checkAbuseStateChange,
34 checkAutoInstanceFollowing,
37 checkNewAccountAbuseForModerators,
38 checkNewBlacklistOnMyVideo,
39 checkNewCommentAbuseForModerators,
40 checkNewInstanceFollower,
41 checkNewVideoAbuseForModerators,
42 checkNewVideoFromSubscription,
44 checkVideoAutoBlacklistForModerators,
45 checkVideoIsPublished,
46 prepareNotificationsTest
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'
53 describe('Test moderation notifications', function () {
54 let servers: ServerInfo[] = []
55 let userAccessToken: string
56 let userNotifications: UserNotification[] = []
57 let adminNotifications: UserNotification[] = []
58 let adminNotificationsServer2: UserNotification[] = []
59 let emails: object[] = []
61 before(async function () {
64 const res = await prepareNotificationsTest(3)
66 userAccessToken = res.userAccessToken
68 userNotifications = res.userNotifications
69 adminNotifications = res.adminNotifications
70 adminNotificationsServer2 = res.adminNotificationsServer2
73 describe('Abuse for moderators notification', function () {
74 let baseParams: CheckerBaseParams
80 socketNotifications: adminNotifications,
81 token: servers[0].accessToken
85 it('Should send a notification to moderators on local video abuse', async function () {
88 const name = 'video for abuse ' + uuidv4()
89 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
90 const video = resVideo.body.video
92 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, videoId: video.id, reason: 'super reason' })
94 await waitJobs(servers)
95 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
98 it('Should send a notification to moderators on remote video abuse', async function () {
101 const name = 'video for abuse ' + uuidv4()
102 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
103 const video = resVideo.body.video
105 await waitJobs(servers)
107 const videoId = await getVideoIdFromUUID(servers[1].url, video.uuid)
108 await reportAbuse({ url: servers[1].url, token: servers[1].accessToken, videoId, reason: 'super reason' })
110 await waitJobs(servers)
111 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
114 it('Should send a notification to moderators on local comment abuse', async function () {
117 const name = 'video for abuse ' + uuidv4()
118 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
119 const video = resVideo.body.video
120 const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + uuidv4())
121 const comment = resComment.body.comment
123 await waitJobs(servers)
125 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, commentId: comment.id, reason: 'super reason' })
127 await waitJobs(servers)
128 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
131 it('Should send a notification to moderators on remote comment abuse', async function () {
134 const name = 'video for abuse ' + uuidv4()
135 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
136 const video = resVideo.body.video
137 await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + uuidv4())
139 await waitJobs(servers)
141 const resComments = await getVideoCommentThreads(servers[1].url, video.uuid, 0, 5)
142 const commentId = resComments.body.data[0].id
143 await reportAbuse({ url: servers[1].url, token: servers[1].accessToken, commentId, reason: 'super reason' })
145 await waitJobs(servers)
146 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
149 it('Should send a notification to moderators on local account abuse', async function () {
152 const username = 'user' + new Date().getTime()
153 const resUser = await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username, password: 'donald' })
154 const accountId = resUser.body.user.account.id
156 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, accountId, reason: 'super reason' })
158 await waitJobs(servers)
159 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
162 it('Should send a notification to moderators on remote account abuse', async function () {
165 const username = 'user' + new Date().getTime()
166 const tmpToken = await generateUserAccessToken(servers[0], username)
167 await uploadVideo(servers[0].url, tmpToken, { name: 'super video' })
169 await waitJobs(servers)
171 const resAccount = await getAccount(servers[1].url, username + '@' + servers[0].host)
172 await reportAbuse({ url: servers[1].url, token: servers[1].accessToken, accountId: resAccount.body.id, reason: 'super reason' })
174 await waitJobs(servers)
175 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
179 describe('Abuse state change notification', function () {
180 let baseParams: CheckerBaseParams
183 before(async function () {
187 socketNotifications: userNotifications,
188 token: userAccessToken
191 const name = 'abuse ' + uuidv4()
192 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
193 const video = resVideo.body.video
195 const res = await reportAbuse({ url: servers[0].url, token: userAccessToken, videoId: video.id, reason: 'super reason' })
196 abuseId = res.body.abuse.id
199 it('Should send a notification to reporter if the abuse has been accepted', async function () {
202 await updateAbuse(servers[0].url, servers[0].accessToken, abuseId, { state: AbuseState.ACCEPTED })
203 await waitJobs(servers)
205 await checkAbuseStateChange(baseParams, abuseId, AbuseState.ACCEPTED, 'presence')
208 it('Should send a notification to reporter if the abuse has been rejected', async function () {
211 await updateAbuse(servers[0].url, servers[0].accessToken, abuseId, { state: AbuseState.REJECTED })
212 await waitJobs(servers)
214 await checkAbuseStateChange(baseParams, abuseId, AbuseState.REJECTED, 'presence')
218 describe('New abuse message notification', function () {
219 let baseParamsUser: CheckerBaseParams
220 let baseParamsAdmin: CheckerBaseParams
224 before(async function () {
228 socketNotifications: userNotifications,
229 token: userAccessToken
235 socketNotifications: adminNotifications,
236 token: servers[0].accessToken
239 const name = 'abuse ' + uuidv4()
240 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
241 const video = resVideo.body.video
244 const res = await reportAbuse({ url: servers[0].url, token: userAccessToken, videoId: video.id, reason: 'super reason' })
245 abuseId = res.body.abuse.id
249 const res = await reportAbuse({ url: servers[0].url, token: userAccessToken, videoId: video.id, reason: 'super reason 2' })
250 abuseId2 = res.body.abuse.id
254 it('Should send a notification to reporter on new message', async function () {
257 const message = 'my super message to users'
258 await addAbuseMessage(servers[0].url, servers[0].accessToken, abuseId, message)
259 await waitJobs(servers)
261 await checkNewAbuseMessage(baseParamsUser, abuseId, message, 'user_1@example.com', 'presence')
264 it('Should not send a notification to the admin if sent by the admin', async function () {
267 const message = 'my super message that should not be sent to the admin'
268 await addAbuseMessage(servers[0].url, servers[0].accessToken, abuseId, message)
269 await waitJobs(servers)
271 await checkNewAbuseMessage(baseParamsAdmin, abuseId, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'absence')
274 it('Should send a notification to moderators', async function () {
277 const message = 'my super message to moderators'
278 await addAbuseMessage(servers[0].url, userAccessToken, abuseId2, message)
279 await waitJobs(servers)
281 await checkNewAbuseMessage(baseParamsAdmin, abuseId2, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'presence')
284 it('Should not send a notification to reporter if sent by the reporter', async function () {
287 const message = 'my super message that should not be sent to reporter'
288 await addAbuseMessage(servers[0].url, userAccessToken, abuseId2, message)
289 await waitJobs(servers)
291 await checkNewAbuseMessage(baseParamsUser, abuseId2, message, 'user_1@example.com', 'absence')
295 describe('Video blacklist on my video', function () {
296 let baseParams: CheckerBaseParams
302 socketNotifications: userNotifications,
303 token: userAccessToken
307 it('Should send a notification to video owner on blacklist', async function () {
310 const name = 'video for abuse ' + uuidv4()
311 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
312 const uuid = resVideo.body.video.uuid
314 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
316 await waitJobs(servers)
317 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist')
320 it('Should send a notification to video owner on unblacklist', async function () {
323 const name = 'video for abuse ' + uuidv4()
324 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
325 const uuid = resVideo.body.video.uuid
327 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
329 await waitJobs(servers)
330 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
331 await waitJobs(servers)
334 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist')
338 describe('New registration', function () {
339 let baseParams: CheckerBaseParams
345 socketNotifications: adminNotifications,
346 token: servers[0].accessToken
350 it('Should send a notification only to moderators when a user registers on the instance', async function () {
353 await registerUser(servers[0].url, 'user_45', 'password')
355 await waitJobs(servers)
357 await checkUserRegistered(baseParams, 'user_45', 'presence')
359 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
360 await checkUserRegistered(immutableAssign(baseParams, userOverride), 'user_45', 'absence')
364 describe('New instance follows', function () {
365 const instanceIndexServer = new MockInstancesIndex()
367 let baseParams: CheckerBaseParams
373 socketNotifications: adminNotifications,
374 token: servers[0].accessToken
377 const port = await instanceIndexServer.initialize()
378 instanceIndexServer.addInstance(servers[1].host)
384 indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
392 it('Should send a notification only to admin when there is a new instance follower', async function () {
395 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
397 await waitJobs(servers)
399 await checkNewInstanceFollower(baseParams, 'localhost:' + servers[2].port, 'presence')
401 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
402 await checkNewInstanceFollower(immutableAssign(baseParams, userOverride), 'localhost:' + servers[2].port, 'absence')
405 it('Should send a notification on auto follow back', async function () {
408 await unfollow(servers[2].url, servers[2].accessToken, servers[0])
409 await waitJobs(servers)
414 autoFollowBack: { enabled: true }
418 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
420 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
422 await waitJobs(servers)
424 const followerHost = servers[0].host
425 const followingHost = servers[2].host
426 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
428 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
429 await checkAutoInstanceFollowing(immutableAssign(baseParams, userOverride), followerHost, followingHost, 'absence')
431 config.followings.instance.autoFollowBack.enabled = false
432 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
433 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
434 await unfollow(servers[2].url, servers[2].accessToken, servers[0])
437 it('Should send a notification on auto instances index follow', async function () {
439 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
441 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
444 await waitJobs(servers)
446 const followerHost = servers[0].host
447 const followingHost = servers[1].host
448 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
450 config.followings.instance.autoFollowIndex.enabled = false
451 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
452 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
456 describe('Video-related notifications when video auto-blacklist is enabled', function () {
457 let userBaseParams: CheckerBaseParams
458 let adminBaseParamsServer1: CheckerBaseParams
459 let adminBaseParamsServer2: CheckerBaseParams
460 let videoUUID: string
461 let videoName: string
462 let currentCustomConfig: CustomConfig
466 adminBaseParamsServer1 = {
469 socketNotifications: adminNotifications,
470 token: servers[0].accessToken
473 adminBaseParamsServer2 = {
476 socketNotifications: adminNotificationsServer2,
477 token: servers[1].accessToken
483 socketNotifications: userNotifications,
484 token: userAccessToken
487 const resCustomConfig = await getCustomConfig(servers[0].url, servers[0].accessToken)
488 currentCustomConfig = resCustomConfig.body
489 const autoBlacklistTestsCustomConfig = immutableAssign(currentCustomConfig, {
498 // enable transcoding otherwise own publish notification after transcoding not expected
499 autoBlacklistTestsCustomConfig.transcoding.enabled = true
500 await updateCustomConfig(servers[0].url, servers[0].accessToken, autoBlacklistTestsCustomConfig)
502 await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
503 await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
507 it('Should send notification to moderators on new video with auto-blacklist', async function () {
510 videoName = 'video with auto-blacklist ' + uuidv4()
511 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: videoName })
512 videoUUID = resVideo.body.video.uuid
514 await waitJobs(servers)
515 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence')
518 it('Should not send video publish notification if auto-blacklisted', async function () {
519 await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'absence')
522 it('Should not send a local user subscription notification if auto-blacklisted', async function () {
523 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'absence')
526 it('Should not send a remote user subscription notification if auto-blacklisted', async function () {
527 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'absence')
530 it('Should send video published and unblacklist after video unblacklisted', async function () {
533 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoUUID)
535 await waitJobs(servers)
537 // FIXME: Can't test as two notifications sent to same user and util only checks last one
538 // One notification might be better anyways
539 // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist')
540 // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence')
543 it('Should send a local user subscription notification after removed from blacklist', async function () {
544 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'presence')
547 it('Should send a remote user subscription notification after removed from blacklist', async function () {
548 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'presence')
551 it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () {
554 const updateAt = new Date(new Date().getTime() + 1000000)
556 const name = 'video with auto-blacklist and future schedule ' + uuidv4()
560 privacy: VideoPrivacy.PRIVATE,
562 updateAt: updateAt.toISOString(),
563 privacy: VideoPrivacy.PUBLIC
567 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
568 const uuid = resVideo.body.video.uuid
570 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
572 await waitJobs(servers)
573 await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist')
575 // FIXME: Can't test absence as two notifications sent to same user and util only checks last one
576 // One notification might be better anyways
577 // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
579 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
580 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
583 it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () {
587 const updateAt = new Date(new Date().getTime() + 2000)
589 const name = 'video with schedule done and still auto-blacklisted ' + uuidv4()
593 privacy: VideoPrivacy.PRIVATE,
595 updateAt: updateAt.toISOString(),
596 privacy: VideoPrivacy.PUBLIC
600 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
601 const uuid = resVideo.body.video.uuid
604 await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
605 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
606 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
609 it('Should not send a notification to moderators on new video without auto-blacklist', async function () {
612 const name = 'video without auto-blacklist ' + uuidv4()
614 // admin with blacklist right will not be auto-blacklisted
615 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name })
616 const uuid = resVideo.body.video.uuid
618 await waitJobs(servers)
619 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence')
623 await updateCustomConfig(servers[0].url, servers[0].accessToken, currentCustomConfig)
625 await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
626 await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
630 after(async function () {
631 MockSmtpServer.Instance.kill()
633 await cleanupTests(servers)