1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { v4 as uuidv4 } from 'uuid'
11 generateUserAccessToken,
14 getVideoCommentThreads,
19 removeVideoFromBlacklist,
23 updateCustomSubConfig,
25 } from '../../../../shared/extra-utils'
26 import { ServerInfo, uploadVideo } from '../../../../shared/extra-utils/index'
27 import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
28 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
30 checkAutoInstanceFollowing,
32 checkNewAccountAbuseForModerators,
33 checkNewBlacklistOnMyVideo,
34 checkNewCommentAbuseForModerators,
35 checkNewInstanceFollower,
36 checkNewVideoAbuseForModerators,
37 checkNewVideoFromSubscription,
39 checkVideoAutoBlacklistForModerators,
40 checkVideoIsPublished,
41 prepareNotificationsTest
42 } from '../../../../shared/extra-utils/users/user-notifications'
43 import { addUserSubscription, removeUserSubscription } from '../../../../shared/extra-utils/users/user-subscriptions'
44 import { CustomConfig } from '../../../../shared/models/server'
45 import { UserNotification } from '../../../../shared/models/users'
46 import { VideoPrivacy } from '../../../../shared/models/videos'
48 describe('Test moderation notifications', function () {
49 let servers: ServerInfo[] = []
50 let userAccessToken: string
51 let userNotifications: UserNotification[] = []
52 let adminNotifications: UserNotification[] = []
53 let adminNotificationsServer2: UserNotification[] = []
54 let emails: object[] = []
56 before(async function () {
59 const res = await prepareNotificationsTest(3)
61 userAccessToken = res.userAccessToken
63 userNotifications = res.userNotifications
64 adminNotifications = res.adminNotifications
65 adminNotificationsServer2 = res.adminNotificationsServer2
68 describe('Video abuse for moderators notification', function () {
69 let baseParams: CheckerBaseParams
75 socketNotifications: adminNotifications,
76 token: servers[0].accessToken
80 it('Should send a notification to moderators on local video abuse', async function () {
83 const name = 'video for abuse ' + uuidv4()
84 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
85 const video = resVideo.body.video
87 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, videoId: video.id, reason: 'super reason' })
89 await waitJobs(servers)
90 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
93 it('Should send a notification to moderators on remote video abuse', async function () {
96 const name = 'video for abuse ' + uuidv4()
97 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
98 const video = resVideo.body.video
100 await waitJobs(servers)
102 const videoId = await getVideoIdFromUUID(servers[1].url, video.uuid)
103 await reportAbuse({ url: servers[1].url, token: servers[1].accessToken, videoId, reason: 'super reason' })
105 await waitJobs(servers)
106 await checkNewVideoAbuseForModerators(baseParams, video.uuid, name, 'presence')
109 it('Should send a notification to moderators on local comment abuse', async function () {
112 const name = 'video for abuse ' + uuidv4()
113 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
114 const video = resVideo.body.video
115 const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + uuidv4())
116 const comment = resComment.body.comment
118 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, commentId: comment.id, reason: 'super reason' })
120 await waitJobs(servers)
121 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
124 it('Should send a notification to moderators on remote comment abuse', async function () {
127 const name = 'video for abuse ' + uuidv4()
128 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
129 const video = resVideo.body.video
130 await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + uuidv4())
132 await waitJobs(servers)
134 const resComments = await getVideoCommentThreads(servers[1].url, video.uuid, 0, 5)
135 const commentId = resComments.body.data[0].id
136 await reportAbuse({ url: servers[1].url, token: servers[1].accessToken, commentId, reason: 'super reason' })
138 await waitJobs(servers)
139 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
142 it('Should send a notification to moderators on local account abuse', async function () {
145 const username = 'user' + new Date().getTime()
146 const resUser = await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username, password: 'donald' })
147 const accountId = resUser.body.user.account.id
149 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, accountId, reason: 'super reason' })
151 await waitJobs(servers)
152 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
155 it('Should send a notification to moderators on remote account abuse', async function () {
158 const username = 'user' + new Date().getTime()
159 const tmpToken = await generateUserAccessToken(servers[0], username)
160 await uploadVideo(servers[0].url, tmpToken, { name: 'super video' })
162 await waitJobs(servers)
164 const resAccount = await getAccount(servers[1].url, username + '@' + servers[0].host)
165 await reportAbuse({ url: servers[1].url, token: servers[1].accessToken, accountId: resAccount.body.id, reason: 'super reason' })
167 await waitJobs(servers)
168 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
172 describe('Video blacklist on my video', function () {
173 let baseParams: CheckerBaseParams
179 socketNotifications: userNotifications,
180 token: userAccessToken
184 it('Should send a notification to video owner on blacklist', async function () {
187 const name = 'video for abuse ' + uuidv4()
188 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
189 const uuid = resVideo.body.video.uuid
191 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
193 await waitJobs(servers)
194 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist')
197 it('Should send a notification to video owner on unblacklist', async function () {
200 const name = 'video for abuse ' + uuidv4()
201 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
202 const uuid = resVideo.body.video.uuid
204 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
206 await waitJobs(servers)
207 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
208 await waitJobs(servers)
211 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist')
215 describe('New registration', function () {
216 let baseParams: CheckerBaseParams
222 socketNotifications: adminNotifications,
223 token: servers[0].accessToken
227 it('Should send a notification only to moderators when a user registers on the instance', async function () {
230 await registerUser(servers[0].url, 'user_45', 'password')
232 await waitJobs(servers)
234 await checkUserRegistered(baseParams, 'user_45', 'presence')
236 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
237 await checkUserRegistered(immutableAssign(baseParams, userOverride), 'user_45', 'absence')
241 describe('New instance follows', function () {
242 const instanceIndexServer = new MockInstancesIndex()
247 indexUrl: 'http://localhost:42101/api/v1/instances/hosts',
253 let baseParams: CheckerBaseParams
259 socketNotifications: adminNotifications,
260 token: servers[0].accessToken
263 await instanceIndexServer.initialize()
264 instanceIndexServer.addInstance(servers[1].host)
267 it('Should send a notification only to admin when there is a new instance follower', async function () {
270 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
272 await waitJobs(servers)
274 await checkNewInstanceFollower(baseParams, 'localhost:' + servers[2].port, 'presence')
276 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
277 await checkNewInstanceFollower(immutableAssign(baseParams, userOverride), 'localhost:' + servers[2].port, 'absence')
280 it('Should send a notification on auto follow back', async function () {
283 await unfollow(servers[2].url, servers[2].accessToken, servers[0])
284 await waitJobs(servers)
289 autoFollowBack: { enabled: true }
293 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
295 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
297 await waitJobs(servers)
299 const followerHost = servers[0].host
300 const followingHost = servers[2].host
301 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
303 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
304 await checkAutoInstanceFollowing(immutableAssign(baseParams, userOverride), followerHost, followingHost, 'absence')
306 config.followings.instance.autoFollowBack.enabled = false
307 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
308 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
309 await unfollow(servers[2].url, servers[2].accessToken, servers[0])
312 it('Should send a notification on auto instances index follow', async function () {
314 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
316 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
319 await waitJobs(servers)
321 const followerHost = servers[0].host
322 const followingHost = servers[1].host
323 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
325 config.followings.instance.autoFollowIndex.enabled = false
326 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
327 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
331 describe('Video-related notifications when video auto-blacklist is enabled', function () {
332 let userBaseParams: CheckerBaseParams
333 let adminBaseParamsServer1: CheckerBaseParams
334 let adminBaseParamsServer2: CheckerBaseParams
335 let videoUUID: string
336 let videoName: string
337 let currentCustomConfig: CustomConfig
341 adminBaseParamsServer1 = {
344 socketNotifications: adminNotifications,
345 token: servers[0].accessToken
348 adminBaseParamsServer2 = {
351 socketNotifications: adminNotificationsServer2,
352 token: servers[1].accessToken
358 socketNotifications: userNotifications,
359 token: userAccessToken
362 const resCustomConfig = await getCustomConfig(servers[0].url, servers[0].accessToken)
363 currentCustomConfig = resCustomConfig.body
364 const autoBlacklistTestsCustomConfig = immutableAssign(currentCustomConfig, {
373 // enable transcoding otherwise own publish notification after transcoding not expected
374 autoBlacklistTestsCustomConfig.transcoding.enabled = true
375 await updateCustomConfig(servers[0].url, servers[0].accessToken, autoBlacklistTestsCustomConfig)
377 await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
378 await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
382 it('Should send notification to moderators on new video with auto-blacklist', async function () {
385 videoName = 'video with auto-blacklist ' + uuidv4()
386 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: videoName })
387 videoUUID = resVideo.body.video.uuid
389 await waitJobs(servers)
390 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence')
393 it('Should not send video publish notification if auto-blacklisted', async function () {
394 await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'absence')
397 it('Should not send a local user subscription notification if auto-blacklisted', async function () {
398 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'absence')
401 it('Should not send a remote user subscription notification if auto-blacklisted', async function () {
402 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'absence')
405 it('Should send video published and unblacklist after video unblacklisted', async function () {
408 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoUUID)
410 await waitJobs(servers)
412 // FIXME: Can't test as two notifications sent to same user and util only checks last one
413 // One notification might be better anyways
414 // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist')
415 // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence')
418 it('Should send a local user subscription notification after removed from blacklist', async function () {
419 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'presence')
422 it('Should send a remote user subscription notification after removed from blacklist', async function () {
423 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'presence')
426 it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () {
429 const updateAt = new Date(new Date().getTime() + 1000000)
431 const name = 'video with auto-blacklist and future schedule ' + uuidv4()
435 privacy: VideoPrivacy.PRIVATE,
437 updateAt: updateAt.toISOString(),
438 privacy: VideoPrivacy.PUBLIC
442 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
443 const uuid = resVideo.body.video.uuid
445 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
447 await waitJobs(servers)
448 await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist')
450 // FIXME: Can't test absence as two notifications sent to same user and util only checks last one
451 // One notification might be better anyways
452 // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
454 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
455 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
458 it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () {
462 const updateAt = new Date(new Date().getTime() + 2000)
464 const name = 'video with schedule done and still auto-blacklisted ' + uuidv4()
468 privacy: VideoPrivacy.PRIVATE,
470 updateAt: updateAt.toISOString(),
471 privacy: VideoPrivacy.PUBLIC
475 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
476 const uuid = resVideo.body.video.uuid
479 await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
480 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
481 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
484 it('Should not send a notification to moderators on new video without auto-blacklist', async function () {
487 const name = 'video without auto-blacklist ' + uuidv4()
489 // admin with blacklist right will not be auto-blacklisted
490 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name })
491 const uuid = resVideo.body.video.uuid
493 await waitJobs(servers)
494 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence')
498 await updateCustomConfig(servers[0].url, servers[0].accessToken, currentCustomConfig)
500 await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
501 await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
505 after(async function () {
506 MockSmtpServer.Instance.kill()
508 await cleanupTests(servers)