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 waitJobs(servers)
126 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, commentId: comment.id, reason: 'super reason' })
128 await waitJobs(servers)
129 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
132 it('Should send a notification to moderators on remote comment abuse', async function () {
135 const name = 'video for abuse ' + uuidv4()
136 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
137 const video = resVideo.body.video
138 await addVideoCommentThread(servers[0].url, userAccessToken, video.id, 'comment abuse ' + uuidv4())
140 await waitJobs(servers)
142 const resComments = await getVideoCommentThreads(servers[1].url, video.uuid, 0, 5)
143 const commentId = resComments.body.data[0].id
144 await reportAbuse({ url: servers[1].url, token: servers[1].accessToken, commentId, reason: 'super reason' })
146 await waitJobs(servers)
147 await checkNewCommentAbuseForModerators(baseParams, video.uuid, name, 'presence')
150 it('Should send a notification to moderators on local account abuse', async function () {
153 const username = 'user' + new Date().getTime()
154 const resUser = await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username, password: 'donald' })
155 const accountId = resUser.body.user.account.id
157 await reportAbuse({ url: servers[0].url, token: servers[0].accessToken, accountId, reason: 'super reason' })
159 await waitJobs(servers)
160 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
163 it('Should send a notification to moderators on remote account abuse', async function () {
166 const username = 'user' + new Date().getTime()
167 const tmpToken = await generateUserAccessToken(servers[0], username)
168 await uploadVideo(servers[0].url, tmpToken, { name: 'super video' })
170 await waitJobs(servers)
172 const resAccount = await getAccount(servers[1].url, username + '@' + servers[0].host)
173 await reportAbuse({ url: servers[1].url, token: servers[1].accessToken, accountId: resAccount.body.id, reason: 'super reason' })
175 await waitJobs(servers)
176 await checkNewAccountAbuseForModerators(baseParams, username, 'presence')
180 describe('Abuse state change notification', function () {
181 let baseParams: CheckerBaseParams
184 before(async function () {
188 socketNotifications: userNotifications,
189 token: userAccessToken
192 const name = 'abuse ' + uuidv4()
193 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
194 const video = resVideo.body.video
196 const res = await reportAbuse({ url: servers[0].url, token: userAccessToken, videoId: video.id, reason: 'super reason' })
197 abuseId = res.body.abuse.id
200 it('Should send a notification to reporter if the abuse has been accepted', async function () {
203 await updateAbuse(servers[0].url, servers[0].accessToken, abuseId, { state: AbuseState.ACCEPTED })
204 await waitJobs(servers)
206 await checkAbuseStateChange(baseParams, abuseId, AbuseState.ACCEPTED, 'presence')
209 it('Should send a notification to reporter if the abuse has been rejected', async function () {
212 await updateAbuse(servers[0].url, servers[0].accessToken, abuseId, { state: AbuseState.REJECTED })
213 await waitJobs(servers)
215 await checkAbuseStateChange(baseParams, abuseId, AbuseState.REJECTED, 'presence')
219 describe('New abuse message notification', function () {
220 let baseParamsUser: CheckerBaseParams
221 let baseParamsAdmin: CheckerBaseParams
225 before(async function () {
229 socketNotifications: userNotifications,
230 token: userAccessToken
236 socketNotifications: adminNotifications,
237 token: servers[0].accessToken
240 const name = 'abuse ' + uuidv4()
241 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
242 const video = resVideo.body.video
245 const res = await reportAbuse({ url: servers[0].url, token: userAccessToken, videoId: video.id, reason: 'super reason' })
246 abuseId = res.body.abuse.id
250 const res = await reportAbuse({ url: servers[0].url, token: userAccessToken, videoId: video.id, reason: 'super reason 2' })
251 abuseId2 = res.body.abuse.id
255 it('Should send a notification to reporter on new message', async function () {
258 const message = 'my super message to users'
259 await addAbuseMessage(servers[0].url, servers[0].accessToken, abuseId, message)
260 await waitJobs(servers)
262 await checkNewAbuseMessage(baseParamsUser, abuseId, message, 'user_1@example.com', 'presence')
265 it('Should not send a notification to the admin if sent by the admin', async function () {
268 const message = 'my super message that should not be sent to the admin'
269 await addAbuseMessage(servers[0].url, servers[0].accessToken, abuseId, message)
270 await waitJobs(servers)
272 await checkNewAbuseMessage(baseParamsAdmin, abuseId, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'absence')
275 it('Should send a notification to moderators', async function () {
278 const message = 'my super message to moderators'
279 await addAbuseMessage(servers[0].url, userAccessToken, abuseId2, message)
280 await waitJobs(servers)
282 await checkNewAbuseMessage(baseParamsAdmin, abuseId2, message, 'admin' + servers[0].internalServerNumber + '@example.com', 'presence')
285 it('Should not send a notification to reporter if sent by the reporter', async function () {
288 const message = 'my super message that should not be sent to reporter'
289 await addAbuseMessage(servers[0].url, userAccessToken, abuseId2, message)
290 await waitJobs(servers)
292 await checkNewAbuseMessage(baseParamsUser, abuseId2, message, 'user_1@example.com', 'absence')
296 describe('Video blacklist on my video', function () {
297 let baseParams: CheckerBaseParams
303 socketNotifications: userNotifications,
304 token: userAccessToken
308 it('Should send a notification to video owner on blacklist', async function () {
311 const name = 'video for abuse ' + uuidv4()
312 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
313 const uuid = resVideo.body.video.uuid
315 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
317 await waitJobs(servers)
318 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist')
321 it('Should send a notification to video owner on unblacklist', async function () {
324 const name = 'video for abuse ' + uuidv4()
325 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name })
326 const uuid = resVideo.body.video.uuid
328 await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid)
330 await waitJobs(servers)
331 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
332 await waitJobs(servers)
335 await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist')
339 describe('New registration', function () {
340 let baseParams: CheckerBaseParams
346 socketNotifications: adminNotifications,
347 token: servers[0].accessToken
351 it('Should send a notification only to moderators when a user registers on the instance', async function () {
354 await registerUser(servers[0].url, 'user_45', 'password')
356 await waitJobs(servers)
358 await checkUserRegistered(baseParams, 'user_45', 'presence')
360 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
361 await checkUserRegistered(immutableAssign(baseParams, userOverride), 'user_45', 'absence')
365 describe('New instance follows', function () {
366 const instanceIndexServer = new MockInstancesIndex()
371 indexUrl: 'http://localhost:42101/api/v1/instances/hosts',
377 let baseParams: CheckerBaseParams
383 socketNotifications: adminNotifications,
384 token: servers[0].accessToken
387 await instanceIndexServer.initialize()
388 instanceIndexServer.addInstance(servers[1].host)
391 it('Should send a notification only to admin when there is a new instance follower', async function () {
394 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
396 await waitJobs(servers)
398 await checkNewInstanceFollower(baseParams, 'localhost:' + servers[2].port, 'presence')
400 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
401 await checkNewInstanceFollower(immutableAssign(baseParams, userOverride), 'localhost:' + servers[2].port, 'absence')
404 it('Should send a notification on auto follow back', async function () {
407 await unfollow(servers[2].url, servers[2].accessToken, servers[0])
408 await waitJobs(servers)
413 autoFollowBack: { enabled: true }
417 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
419 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
421 await waitJobs(servers)
423 const followerHost = servers[0].host
424 const followingHost = servers[2].host
425 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
427 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
428 await checkAutoInstanceFollowing(immutableAssign(baseParams, userOverride), followerHost, followingHost, 'absence')
430 config.followings.instance.autoFollowBack.enabled = false
431 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
432 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
433 await unfollow(servers[2].url, servers[2].accessToken, servers[0])
436 it('Should send a notification on auto instances index follow', async function () {
438 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
440 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
443 await waitJobs(servers)
445 const followerHost = servers[0].host
446 const followingHost = servers[1].host
447 await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence')
449 config.followings.instance.autoFollowIndex.enabled = false
450 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
451 await unfollow(servers[0].url, servers[0].accessToken, servers[1])
455 describe('Video-related notifications when video auto-blacklist is enabled', function () {
456 let userBaseParams: CheckerBaseParams
457 let adminBaseParamsServer1: CheckerBaseParams
458 let adminBaseParamsServer2: CheckerBaseParams
459 let videoUUID: string
460 let videoName: string
461 let currentCustomConfig: CustomConfig
465 adminBaseParamsServer1 = {
468 socketNotifications: adminNotifications,
469 token: servers[0].accessToken
472 adminBaseParamsServer2 = {
475 socketNotifications: adminNotificationsServer2,
476 token: servers[1].accessToken
482 socketNotifications: userNotifications,
483 token: userAccessToken
486 const resCustomConfig = await getCustomConfig(servers[0].url, servers[0].accessToken)
487 currentCustomConfig = resCustomConfig.body
488 const autoBlacklistTestsCustomConfig = immutableAssign(currentCustomConfig, {
497 // enable transcoding otherwise own publish notification after transcoding not expected
498 autoBlacklistTestsCustomConfig.transcoding.enabled = true
499 await updateCustomConfig(servers[0].url, servers[0].accessToken, autoBlacklistTestsCustomConfig)
501 await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
502 await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
506 it('Should send notification to moderators on new video with auto-blacklist', async function () {
509 videoName = 'video with auto-blacklist ' + uuidv4()
510 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: videoName })
511 videoUUID = resVideo.body.video.uuid
513 await waitJobs(servers)
514 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence')
517 it('Should not send video publish notification if auto-blacklisted', async function () {
518 await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'absence')
521 it('Should not send a local user subscription notification if auto-blacklisted', async function () {
522 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'absence')
525 it('Should not send a remote user subscription notification if auto-blacklisted', async function () {
526 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'absence')
529 it('Should send video published and unblacklist after video unblacklisted', async function () {
532 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoUUID)
534 await waitJobs(servers)
536 // FIXME: Can't test as two notifications sent to same user and util only checks last one
537 // One notification might be better anyways
538 // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist')
539 // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence')
542 it('Should send a local user subscription notification after removed from blacklist', async function () {
543 await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'presence')
546 it('Should send a remote user subscription notification after removed from blacklist', async function () {
547 await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'presence')
550 it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () {
553 const updateAt = new Date(new Date().getTime() + 1000000)
555 const name = 'video with auto-blacklist and future schedule ' + uuidv4()
559 privacy: VideoPrivacy.PRIVATE,
561 updateAt: updateAt.toISOString(),
562 privacy: VideoPrivacy.PUBLIC
566 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
567 const uuid = resVideo.body.video.uuid
569 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid)
571 await waitJobs(servers)
572 await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist')
574 // FIXME: Can't test absence as two notifications sent to same user and util only checks last one
575 // One notification might be better anyways
576 // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
578 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
579 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
582 it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () {
586 const updateAt = new Date(new Date().getTime() + 2000)
588 const name = 'video with schedule done and still auto-blacklisted ' + uuidv4()
592 privacy: VideoPrivacy.PRIVATE,
594 updateAt: updateAt.toISOString(),
595 privacy: VideoPrivacy.PUBLIC
599 const resVideo = await uploadVideo(servers[0].url, userAccessToken, data)
600 const uuid = resVideo.body.video.uuid
603 await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
604 await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence')
605 await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence')
608 it('Should not send a notification to moderators on new video without auto-blacklist', async function () {
611 const name = 'video without auto-blacklist ' + uuidv4()
613 // admin with blacklist right will not be auto-blacklisted
614 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name })
615 const uuid = resVideo.body.video.uuid
617 await waitJobs(servers)
618 await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence')
622 await updateCustomConfig(servers[0].url, servers[0].accessToken, currentCustomConfig)
624 await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port)
625 await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port)
629 after(async function () {
630 MockSmtpServer.Instance.kill()
632 await cleanupTests(servers)