1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { buildUUID } from '@shared/core-utils/uuid'
7 checkAutoInstanceFollowing,
10 checkNewAccountAbuseForModerators,
11 checkNewBlacklistOnMyVideo,
12 checkNewCommentAbuseForModerators,
13 checkNewInstanceFollower,
14 checkNewVideoAbuseForModerators,
15 checkNewVideoFromSubscription,
17 checkVideoAutoBlacklistForModerators,
18 checkVideoIsPublished,
23 prepareNotificationsTest,
26 } from '@shared/server-commands'
27 import { AbuseState, CustomConfig, UserNotification, UserRole, VideoPrivacy } from '@shared/models'
29 describe('Test moderation notifications', function () {
30 let servers: PeerTubeServer[] = []
31 let userToken1: string
32 let userToken2: string
34 let userNotifications: UserNotification[] = []
35 let adminNotifications: UserNotification[] = []
36 let adminNotificationsServer2: UserNotification[] = []
37 let emails: object[] = []
39 before(async function () {
42 const res = await prepareNotificationsTest(3)
44 userToken1 = res.userAccessToken
46 userNotifications = res.userNotifications
47 adminNotifications = res.adminNotifications
48 adminNotificationsServer2 = res.adminNotificationsServer2
50 userToken2 = await servers[1].users.generateUserAndToken('user2', UserRole.USER)
53 describe('Abuse for moderators notification', function () {
54 let baseParams: CheckerBaseParams
60 socketNotifications: adminNotifications,
61 token: servers[0].accessToken
65 it('Should not send a notification to moderators on local abuse reported by an admin', async function () {
68 const name = 'video for abuse ' + buildUUID()
69 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
71 await servers[0].abuses.report({ videoId: video.id, reason: 'super reason' })
73 await waitJobs(servers)
74 await checkNewVideoAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'absence' })
77 it('Should send a notification to moderators on local video abuse', async function () {
80 const name = 'video for abuse ' + buildUUID()
81 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
83 await servers[0].abuses.report({ token: userToken1, videoId: video.id, reason: 'super reason' })
85 await waitJobs(servers)
86 await checkNewVideoAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
89 it('Should send a notification to moderators on remote video abuse', async function () {
92 const name = 'video for abuse ' + buildUUID()
93 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
95 await waitJobs(servers)
97 const videoId = await servers[1].videos.getId({ uuid: video.uuid })
98 await servers[1].abuses.report({ token: userToken2, videoId, reason: 'super reason' })
100 await waitJobs(servers)
101 await checkNewVideoAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
104 it('Should send a notification to moderators on local comment abuse', async function () {
107 const name = 'video for abuse ' + buildUUID()
108 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
109 const comment = await servers[0].comments.createThread({
112 text: 'comment abuse ' + buildUUID()
115 await waitJobs(servers)
117 await servers[0].abuses.report({ token: userToken1, commentId: comment.id, reason: 'super reason' })
119 await waitJobs(servers)
120 await checkNewCommentAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
123 it('Should send a notification to moderators on remote comment abuse', async function () {
126 const name = 'video for abuse ' + buildUUID()
127 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
129 await servers[0].comments.createThread({
132 text: 'comment abuse ' + buildUUID()
135 await waitJobs(servers)
137 const { data } = await servers[1].comments.listThreads({ videoId: video.uuid })
138 const commentId = data[0].id
139 await servers[1].abuses.report({ token: userToken2, commentId, reason: 'super reason' })
141 await waitJobs(servers)
142 await checkNewCommentAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
145 it('Should send a notification to moderators on local account abuse', async function () {
148 const username = 'user' + new Date().getTime()
149 const { account } = await servers[0].users.create({ username, password: 'donald' })
150 const accountId = account.id
152 await servers[0].abuses.report({ token: userToken1, accountId, reason: 'super reason' })
154 await waitJobs(servers)
155 await checkNewAccountAbuseForModerators({ ...baseParams, displayName: username, checkType: 'presence' })
158 it('Should send a notification to moderators on remote account abuse', async function () {
161 const username = 'user' + new Date().getTime()
162 const tmpToken = await servers[0].users.generateUserAndToken(username)
163 await servers[0].videos.upload({ token: tmpToken, attributes: { name: 'super video' } })
165 await waitJobs(servers)
167 const account = await servers[1].accounts.get({ accountName: username + '@' + servers[0].host })
168 await servers[1].abuses.report({ token: userToken2, accountId: account.id, reason: 'super reason' })
170 await waitJobs(servers)
171 await checkNewAccountAbuseForModerators({ ...baseParams, displayName: username, checkType: 'presence' })
175 describe('Abuse state change notification', function () {
176 let baseParams: CheckerBaseParams
179 before(async function () {
183 socketNotifications: userNotifications,
187 const name = 'abuse ' + buildUUID()
188 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
190 const body = await servers[0].abuses.report({ token: userToken1, videoId: video.id, reason: 'super reason' })
191 abuseId = body.abuse.id
194 it('Should send a notification to reporter if the abuse has been accepted', async function () {
197 await servers[0].abuses.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
198 await waitJobs(servers)
200 await checkAbuseStateChange({ ...baseParams, abuseId, state: AbuseState.ACCEPTED, checkType: 'presence' })
203 it('Should send a notification to reporter if the abuse has been rejected', async function () {
206 await servers[0].abuses.update({ abuseId, body: { state: AbuseState.REJECTED } })
207 await waitJobs(servers)
209 await checkAbuseStateChange({ ...baseParams, abuseId, state: AbuseState.REJECTED, checkType: 'presence' })
213 describe('New abuse message notification', function () {
214 let baseParamsUser: CheckerBaseParams
215 let baseParamsAdmin: CheckerBaseParams
219 before(async function () {
223 socketNotifications: userNotifications,
230 socketNotifications: adminNotifications,
231 token: servers[0].accessToken
234 const name = 'abuse ' + buildUUID()
235 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
238 const body = await servers[0].abuses.report({ token: userToken1, videoId: video.id, reason: 'super reason' })
239 abuseId = body.abuse.id
243 const body = await servers[0].abuses.report({ token: userToken1, videoId: video.id, reason: 'super reason 2' })
244 abuseId2 = body.abuse.id
248 it('Should send a notification to reporter on new message', async function () {
251 const message = 'my super message to users'
252 await servers[0].abuses.addMessage({ abuseId, message })
253 await waitJobs(servers)
255 await checkNewAbuseMessage({ ...baseParamsUser, abuseId, message, toEmail: 'user_1@example.com', checkType: 'presence' })
258 it('Should not send a notification to the admin if sent by the admin', async function () {
261 const message = 'my super message that should not be sent to the admin'
262 await servers[0].abuses.addMessage({ abuseId, message })
263 await waitJobs(servers)
265 const toEmail = 'admin' + servers[0].internalServerNumber + '@example.com'
266 await checkNewAbuseMessage({ ...baseParamsAdmin, abuseId, message, toEmail, checkType: 'absence' })
269 it('Should send a notification to moderators', async function () {
272 const message = 'my super message to moderators'
273 await servers[0].abuses.addMessage({ token: userToken1, abuseId: abuseId2, message })
274 await waitJobs(servers)
276 const toEmail = 'admin' + servers[0].internalServerNumber + '@example.com'
277 await checkNewAbuseMessage({ ...baseParamsAdmin, abuseId: abuseId2, message, toEmail, checkType: 'presence' })
280 it('Should not send a notification to reporter if sent by the reporter', async function () {
283 const message = 'my super message that should not be sent to reporter'
284 await servers[0].abuses.addMessage({ token: userToken1, abuseId: abuseId2, message })
285 await waitJobs(servers)
287 const toEmail = 'user_1@example.com'
288 await checkNewAbuseMessage({ ...baseParamsUser, abuseId: abuseId2, message, toEmail, checkType: 'absence' })
292 describe('Video blacklist on my video', function () {
293 let baseParams: CheckerBaseParams
299 socketNotifications: userNotifications,
304 it('Should send a notification to video owner on blacklist', async function () {
307 const name = 'video for abuse ' + buildUUID()
308 const { uuid, shortUUID } = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
310 await servers[0].blacklist.add({ videoId: uuid })
312 await waitJobs(servers)
313 await checkNewBlacklistOnMyVideo({ ...baseParams, shortUUID, videoName: name, blacklistType: 'blacklist' })
316 it('Should send a notification to video owner on unblacklist', async function () {
319 const name = 'video for abuse ' + buildUUID()
320 const { uuid, shortUUID } = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
322 await servers[0].blacklist.add({ videoId: uuid })
324 await waitJobs(servers)
325 await servers[0].blacklist.remove({ videoId: uuid })
326 await waitJobs(servers)
329 await checkNewBlacklistOnMyVideo({ ...baseParams, shortUUID, videoName: name, blacklistType: 'unblacklist' })
333 describe('New registration', function () {
334 let baseParams: CheckerBaseParams
340 socketNotifications: adminNotifications,
341 token: servers[0].accessToken
345 it('Should send a notification only to moderators when a user registers on the instance', async function () {
348 await servers[0].users.register({ username: 'user_45' })
350 await waitJobs(servers)
352 await checkUserRegistered({ ...baseParams, username: 'user_45', checkType: 'presence' })
354 const userOverride = { socketNotifications: userNotifications, token: userToken1, check: { web: true, mail: false } }
355 await checkUserRegistered({ ...baseParams, ...userOverride, username: 'user_45', checkType: 'absence' })
359 describe('New instance follows', function () {
360 const instanceIndexServer = new MockInstancesIndex()
362 let baseParams: CheckerBaseParams
368 socketNotifications: adminNotifications,
369 token: servers[0].accessToken
372 const port = await instanceIndexServer.initialize()
373 instanceIndexServer.addInstance(servers[1].host)
379 indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
387 it('Should send a notification only to admin when there is a new instance follower', async function () {
390 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
392 await waitJobs(servers)
394 await checkNewInstanceFollower({ ...baseParams, followerHost: 'localhost:' + servers[2].port, checkType: 'presence' })
396 const userOverride = { socketNotifications: userNotifications, token: userToken1, check: { web: true, mail: false } }
397 await checkNewInstanceFollower({ ...baseParams, ...userOverride, followerHost: 'localhost:' + servers[2].port, checkType: 'absence' })
400 it('Should send a notification on auto follow back', async function () {
403 await servers[2].follows.unfollow({ target: servers[0] })
404 await waitJobs(servers)
409 autoFollowBack: { enabled: true }
413 await servers[0].config.updateCustomSubConfig({ newConfig: config })
415 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
417 await waitJobs(servers)
419 const followerHost = servers[0].host
420 const followingHost = servers[2].host
421 await checkAutoInstanceFollowing({ ...baseParams, followerHost, followingHost, checkType: 'presence' })
423 const userOverride = { socketNotifications: userNotifications, token: userToken1, check: { web: true, mail: false } }
424 await checkAutoInstanceFollowing({ ...baseParams, ...userOverride, followerHost, followingHost, checkType: 'absence' })
426 config.followings.instance.autoFollowBack.enabled = false
427 await servers[0].config.updateCustomSubConfig({ newConfig: config })
428 await servers[0].follows.unfollow({ target: servers[2] })
429 await servers[2].follows.unfollow({ target: servers[0] })
432 it('Should send a notification on auto instances index follow', async function () {
434 await servers[0].follows.unfollow({ target: servers[1] })
436 await servers[0].config.updateCustomSubConfig({ newConfig: config })
439 await waitJobs(servers)
441 const followerHost = servers[0].host
442 const followingHost = servers[1].host
443 await checkAutoInstanceFollowing({ ...baseParams, followerHost, followingHost, checkType: 'presence' })
445 config.followings.instance.autoFollowIndex.enabled = false
446 await servers[0].config.updateCustomSubConfig({ newConfig: config })
447 await servers[0].follows.unfollow({ target: servers[1] })
451 describe('Video-related notifications when video auto-blacklist is enabled', function () {
452 let userBaseParams: CheckerBaseParams
453 let adminBaseParamsServer1: CheckerBaseParams
454 let adminBaseParamsServer2: CheckerBaseParams
456 let shortUUID: string
457 let videoName: string
458 let currentCustomConfig: CustomConfig
462 adminBaseParamsServer1 = {
465 socketNotifications: adminNotifications,
466 token: servers[0].accessToken
469 adminBaseParamsServer2 = {
472 socketNotifications: adminNotificationsServer2,
473 token: servers[1].accessToken
479 socketNotifications: userNotifications,
483 currentCustomConfig = await servers[0].config.getCustomConfig()
485 const autoBlacklistTestsCustomConfig = {
486 ...currentCustomConfig,
497 // enable transcoding otherwise own publish notification after transcoding not expected
498 autoBlacklistTestsCustomConfig.transcoding.enabled = true
499 await servers[0].config.updateCustomConfig({ newCustomConfig: autoBlacklistTestsCustomConfig })
501 await servers[0].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
502 await servers[1].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
505 it('Should send notification to moderators on new video with auto-blacklist', async function () {
508 videoName = 'video with auto-blacklist ' + buildUUID()
509 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name: videoName } })
510 shortUUID = video.shortUUID
513 await waitJobs(servers)
514 await checkVideoAutoBlacklistForModerators({ ...adminBaseParamsServer1, shortUUID, videoName, checkType: 'presence' })
517 it('Should not send video publish notification if auto-blacklisted', async function () {
518 await checkVideoIsPublished({ ...userBaseParams, videoName, shortUUID, checkType: 'absence' })
521 it('Should not send a local user subscription notification if auto-blacklisted', async function () {
522 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName, shortUUID, checkType: 'absence' })
525 it('Should not send a remote user subscription notification if auto-blacklisted', async function () {
526 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName, shortUUID, checkType: 'absence' })
529 it('Should send video published and unblacklist after video unblacklisted', async function () {
532 await servers[0].blacklist.remove({ videoId: uuid })
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, shortUUID, checkType: 'presence' })
546 it('Should send a remote user subscription notification after removed from blacklist', async function () {
547 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName, shortUUID, checkType: '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 ' + buildUUID()
559 privacy: VideoPrivacy.PRIVATE,
561 updateAt: updateAt.toISOString(),
562 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
566 const { shortUUID, uuid } = await servers[0].videos.upload({ token: userToken1, attributes })
568 await servers[0].blacklist.remove({ videoId: uuid })
570 await waitJobs(servers)
571 await checkNewBlacklistOnMyVideo({ ...userBaseParams, shortUUID, videoName: name, blacklistType: 'unblacklist' })
573 // FIXME: Can't test absence as two notifications sent to same user and util only checks last one
574 // One notification might be better anyways
575 // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
577 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName: name, shortUUID, checkType: 'absence' })
578 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName: name, shortUUID, checkType: 'absence' })
581 it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () {
585 const updateAt = new Date(new Date().getTime() + 2000)
587 const name = 'video with schedule done and still auto-blacklisted ' + buildUUID()
591 privacy: VideoPrivacy.PRIVATE,
593 updateAt: updateAt.toISOString(),
594 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
598 const { shortUUID } = await servers[0].videos.upload({ token: userToken1, attributes })
601 await checkVideoIsPublished({ ...userBaseParams, videoName: name, shortUUID, checkType: 'absence' })
602 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName: name, shortUUID, checkType: 'absence' })
603 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName: name, shortUUID, checkType: '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 ' + buildUUID()
611 // admin with blacklist right will not be auto-blacklisted
612 const { shortUUID } = await servers[0].videos.upload({ attributes: { name } })
614 await waitJobs(servers)
615 await checkVideoAutoBlacklistForModerators({ ...adminBaseParamsServer1, shortUUID, videoName: name, checkType: 'absence' })
619 await servers[0].config.updateCustomConfig({ newCustomConfig: currentCustomConfig })
621 await servers[0].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
622 await servers[1].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
626 after(async function () {
627 MockSmtpServer.Instance.kill()
629 await cleanupTests(servers)