1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { v4 as uuidv4 } from 'uuid'
13 removeVideoFromBlacklist,
17 updateCustomSubConfig,
19 } from '../../../../shared/extra-utils'
20 import { ServerInfo, uploadVideo } from '../../../../shared/extra-utils/index'
21 import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
22 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
24 checkAutoInstanceFollowing,
26 checkNewBlacklistOnMyVideo,
27 checkNewInstanceFollower,
28 checkNewVideoAbuseForModerators,
29 checkNewVideoFromSubscription,
31 checkVideoAutoBlacklistForModerators,
32 checkVideoIsPublished,
33 prepareNotificationsTest
34 } from '../../../../shared/extra-utils/users/user-notifications'
35 import { addUserSubscription, removeUserSubscription } from '../../../../shared/extra-utils/users/user-subscriptions'
36 import { CustomConfig } from '../../../../shared/models/server'
37 import { UserNotification } from '../../../../shared/models/users'
38 import { VideoPrivacy } from '../../../../shared/models/videos'
40 describe('Test moderation notifications', function () {
41 let servers: ServerInfo[] = []
42 let userAccessToken: string
43 let userNotifications: UserNotification[] = []
44 let adminNotifications: UserNotification[] = []
45 let adminNotificationsServer2: UserNotification[] = []
46 let emails: object[] = []
48 before(async function () {
51 const res = await prepareNotificationsTest(3)
53 userAccessToken = res.userAccessToken
55 userNotifications = res.userNotifications
56 adminNotifications = res.adminNotifications
57 adminNotificationsServer2 = res.adminNotificationsServer2
60 describe('Video abuse for moderators notification', function () {
61 let baseParams: CheckerBaseParams
67 socketNotifications: adminNotifications,
68 token: servers[0].accessToken
72 it('Should send a notification to moderators on local video abuse', async function () {
75 const name = 'video for abuse ' + uuidv4()
76 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
77 const uuid = resVideo.body.video.uuid
79 await reportVideoAbuse(servers[0].url, servers[0].accessToken, uuid, 'super reason')
81 await waitJobs(servers)
82 await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence')
85 it('Should send a notification to moderators on remote video abuse', async function () {
88 const name = 'video for abuse ' + uuidv4()
89 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
90 const uuid = resVideo.body.video.uuid
92 await waitJobs(servers)
94 await reportVideoAbuse(servers[1].url, servers[1].accessToken, uuid, 'super reason')
96 await waitJobs(servers)
97 await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence')
101 describe('Video blacklist on my video', function () {
102 let baseParams: CheckerBaseParams
108 socketNotifications: userNotifications,
109 token: userAccessToken
113 it('Should send a notification to video owner on blacklist', async function () {
116 const name = 'video for abuse ' + uuidv4()
117 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
118 const uuid = resVideo.body.video.uuid
120 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
122 await waitJobs(servers)
123 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist')
126 it('Should send a notification to video owner on unblacklist', async function () {
129 const name = 'video for abuse ' + uuidv4()
130 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
131 const uuid = resVideo.body.video.uuid
133 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
135 await waitJobs(servers)
136 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
137 await waitJobs(servers)
140 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist')
144 describe('New registration', function () {
145 let baseParams: CheckerBaseParams
151 socketNotifications: adminNotifications,
152 token: servers[0].accessToken
156 it('Should send a notification only to moderators when a user registers on the instance', async function () {
159 await registerUser(servers[0].url, 'user_45', 'password')
161 await waitJobs(servers)
163 await checkUserRegistered(baseParams, 'user_45', 'presence')
165 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
166 await checkUserRegistered(immutableAssign(baseParams, userOverride), 'user_45', 'absence')
170 describe('New instance follows', function () {
171 const instanceIndexServer = new MockInstancesIndex()
176 indexUrl: 'http://localhost:42101/api/v1/instances/hosts',
182 let baseParams: CheckerBaseParams
188 socketNotifications: adminNotifications,
189 token: servers[0].accessToken
192 await instanceIndexServer.initialize()
193 instanceIndexServer.addInstance(servers[1].host)
196 it('Should send a notification only to admin when there is a new instance follower', async function () {
199 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
201 await waitJobs(servers)
203 await checkNewInstanceFollower(baseParams, 'localhost:' + servers[2].port, 'presence')
205 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
206 await checkNewInstanceFollower(immutableAssign(baseParams, userOverride), 'localhost:' + servers[2].port, 'absence')
209 it('Should send a notification on auto follow back', async function () {
212 await unfollow(servers[2].url, servers[2].accessToken, servers[0])
213 await waitJobs(servers)
218 autoFollowBack: { enabled: true }
222 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
224 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
226 await waitJobs(servers)
228 const followerHost = servers[0].host
229 const followingHost = servers[2].host
230 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
232 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
233 await checkAutoInstanceFollowing(immutableAssign(baseParams, userOverride), followerHost, followingHost, 'absence')
235 config.followings.instance.autoFollowBack.enabled = false
236 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
237 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
238 await unfollow(servers[2].url, servers[2].accessToken, servers[0])
241 it('Should send a notification on auto instances index follow', async function () {
243 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
245 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
248 await waitJobs(servers)
250 const followerHost = servers[0].host
251 const followingHost = servers[1].host
252 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
254 config.followings.instance.autoFollowIndex.enabled = false
255 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
256 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
260 describe('Video-related notifications when video auto-blacklist is enabled', function () {
261 let userBaseParams: CheckerBaseParams
262 let adminBaseParamsServer1: CheckerBaseParams
263 let adminBaseParamsServer2: CheckerBaseParams
264 let videoUUID: string
265 let videoName: string
266 let currentCustomConfig: CustomConfig
270 adminBaseParamsServer1 = {
273 socketNotifications: adminNotifications,
274 token: servers[0].accessToken
277 adminBaseParamsServer2 = {
280 socketNotifications: adminNotificationsServer2,
281 token: servers[1].accessToken
287 socketNotifications: userNotifications,
288 token: userAccessToken
291 const resCustomConfig = await getCustomConfig(servers[0].url, servers[0].accessToken)
292 currentCustomConfig = resCustomConfig.body
293 const autoBlacklistTestsCustomConfig = immutableAssign(currentCustomConfig, {
302 // enable transcoding otherwise own publish notification after transcoding not expected
303 autoBlacklistTestsCustomConfig.transcoding.enabled = true
304 await updateCustomConfig(servers[0].url, servers[0].accessToken, autoBlacklistTestsCustomConfig)
306 await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
307 await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
311 it('Should send notification to moderators on new video with auto-blacklist', async function () {
314 videoName = 'video with auto-blacklist ' + uuidv4()
315 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: videoName })
316 videoUUID = resVideo.body.video.uuid
318 await waitJobs(servers)
319 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence')
322 it('Should not send video publish notification if auto-blacklisted', async function () {
323 await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'absence')
326 it('Should not send a local user subscription notification if auto-blacklisted', async function () {
327 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'absence')
330 it('Should not send a remote user subscription notification if auto-blacklisted', async function () {
331 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'absence')
334 it('Should send video published and unblacklist after video unblacklisted', async function () {
337 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoUUID)
339 await waitJobs(servers)
341 // FIXME: Can't test as two notifications sent to same user and util only checks last one
342 // One notification might be better anyways
343 // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist')
344 // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence')
347 it('Should send a local user subscription notification after removed from blacklist', async function () {
348 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'presence')
351 it('Should send a remote user subscription notification after removed from blacklist', async function () {
352 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'presence')
355 it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () {
358 const updateAt = new Date(new Date().getTime() + 1000000)
360 const name = 'video with auto-blacklist and future schedule ' + uuidv4()
364 privacy: VideoPrivacy.PRIVATE,
366 updateAt: updateAt.toISOString(),
367 privacy: VideoPrivacy.PUBLIC
371 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
372 const uuid = resVideo.body.video.uuid
374 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
376 await waitJobs(servers)
377 await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist')
379 // FIXME: Can't test absence as two notifications sent to same user and util only checks last one
380 // One notification might be better anyways
381 // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
383 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
384 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
387 it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () {
391 const updateAt = new Date(new Date().getTime() + 2000)
393 const name = 'video with schedule done and still auto-blacklisted ' + uuidv4()
397 privacy: VideoPrivacy.PRIVATE,
399 updateAt: updateAt.toISOString(),
400 privacy: VideoPrivacy.PUBLIC
404 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
405 const uuid = resVideo.body.video.uuid
408 await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
409 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
410 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
413 it('Should not send a notification to moderators on new video without auto-blacklist', async function () {
416 const name = 'video without auto-blacklist ' + uuidv4()
418 // admin with blacklist right will not be auto-blacklisted
419 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name })
420 const uuid = resVideo.body.video.uuid
422 await waitJobs(servers)
423 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence')
427 await updateCustomConfig(servers[0].url, servers[0].accessToken, currentCustomConfig)
429 await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
430 await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
434 after(async function () {
435 MockSmtpServer.Instance.kill()
437 await cleanupTests(servers)