1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { buildUUID } from '@server/helpers/uuid'
7 checkAutoInstanceFollowing,
10 checkNewAccountAbuseForModerators,
11 checkNewBlacklistOnMyVideo,
12 checkNewCommentAbuseForModerators,
13 checkNewInstanceFollower,
14 checkNewVideoAbuseForModerators,
15 checkNewVideoFromSubscription,
17 checkVideoAutoBlacklistForModerators,
18 checkVideoIsPublished,
23 prepareNotificationsTest,
26 } from '@shared/extra-utils'
27 import { AbuseState, CustomConfig, UserNotification, VideoPrivacy } from '@shared/models'
29 describe('Test moderation notifications', function () {
30 let servers: PeerTubeServer[] = []
31 let userAccessToken: string
32 let userNotifications: UserNotification[] = []
33 let adminNotifications: UserNotification[] = []
34 let adminNotificationsServer2: UserNotification[] = []
35 let emails: object[] = []
37 before(async function () {
40 const res = await prepareNotificationsTest(3)
42 userAccessToken = res.userAccessToken
44 userNotifications = res.userNotifications
45 adminNotifications = res.adminNotifications
46 adminNotificationsServer2 = res.adminNotificationsServer2
49 describe('Abuse for moderators notification', function () {
50 let baseParams: CheckerBaseParams
56 socketNotifications: adminNotifications,
57 token: servers[0].accessToken
61 it('Should send a notification to moderators on local video abuse', async function () {
64 const name = 'video for abuse ' + buildUUID()
65 const video = await servers[0].videos.upload({ token: userAccessToken, attributes: { name } })
67 await servers[0].abuses.report({ videoId: video.id, reason: 'super reason' })
69 await waitJobs(servers)
70 await checkNewVideoAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
73 it('Should send a notification to moderators on remote video abuse', async function () {
76 const name = 'video for abuse ' + buildUUID()
77 const video = await servers[0].videos.upload({ token: userAccessToken, attributes: { name } })
79 await waitJobs(servers)
81 const videoId = await servers[1].videos.getId({ uuid: video.uuid })
82 await servers[1].abuses.report({ videoId, reason: 'super reason' })
84 await waitJobs(servers)
85 await checkNewVideoAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
88 it('Should send a notification to moderators on local comment abuse', async function () {
91 const name = 'video for abuse ' + buildUUID()
92 const video = await servers[0].videos.upload({ token: userAccessToken, attributes: { name } })
93 const comment = await servers[0].comments.createThread({
94 token: userAccessToken,
96 text: 'comment abuse ' + buildUUID()
99 await waitJobs(servers)
101 await servers[0].abuses.report({ commentId: comment.id, reason: 'super reason' })
103 await waitJobs(servers)
104 await checkNewCommentAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
107 it('Should send a notification to moderators on remote comment abuse', async function () {
110 const name = 'video for abuse ' + buildUUID()
111 const video = await servers[0].videos.upload({ token: userAccessToken, attributes: { name } })
113 await servers[0].comments.createThread({
114 token: userAccessToken,
116 text: 'comment abuse ' + buildUUID()
119 await waitJobs(servers)
121 const { data } = await servers[1].comments.listThreads({ videoId: video.uuid })
122 const commentId = data[0].id
123 await servers[1].abuses.report({ commentId, reason: 'super reason' })
125 await waitJobs(servers)
126 await checkNewCommentAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
129 it('Should send a notification to moderators on local account abuse', async function () {
132 const username = 'user' + new Date().getTime()
133 const { account } = await servers[0].users.create({ username, password: 'donald' })
134 const accountId = account.id
136 await servers[0].abuses.report({ accountId, reason: 'super reason' })
138 await waitJobs(servers)
139 await checkNewAccountAbuseForModerators({ ...baseParams, displayName: username, checkType: 'presence' })
142 it('Should send a notification to moderators on remote account abuse', async function () {
145 const username = 'user' + new Date().getTime()
146 const tmpToken = await servers[0].users.generateUserAndToken(username)
147 await servers[0].videos.upload({ token: tmpToken, attributes: { name: 'super video' } })
149 await waitJobs(servers)
151 const account = await servers[1].accounts.get({ accountName: username + '@' + servers[0].host })
152 await servers[1].abuses.report({ accountId: account.id, reason: 'super reason' })
154 await waitJobs(servers)
155 await checkNewAccountAbuseForModerators({ ...baseParams, displayName: username, checkType: 'presence' })
159 describe('Abuse state change notification', function () {
160 let baseParams: CheckerBaseParams
163 before(async function () {
167 socketNotifications: userNotifications,
168 token: userAccessToken
171 const name = 'abuse ' + buildUUID()
172 const video = await servers[0].videos.upload({ token: userAccessToken, attributes: { name } })
174 const body = await servers[0].abuses.report({ token: userAccessToken, videoId: video.id, reason: 'super reason' })
175 abuseId = body.abuse.id
178 it('Should send a notification to reporter if the abuse has been accepted', async function () {
181 await servers[0].abuses.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
182 await waitJobs(servers)
184 await checkAbuseStateChange({ ...baseParams, abuseId, state: AbuseState.ACCEPTED, checkType: 'presence' })
187 it('Should send a notification to reporter if the abuse has been rejected', async function () {
190 await servers[0].abuses.update({ abuseId, body: { state: AbuseState.REJECTED } })
191 await waitJobs(servers)
193 await checkAbuseStateChange({ ...baseParams, abuseId, state: AbuseState.REJECTED, checkType: 'presence' })
197 describe('New abuse message notification', function () {
198 let baseParamsUser: CheckerBaseParams
199 let baseParamsAdmin: CheckerBaseParams
203 before(async function () {
207 socketNotifications: userNotifications,
208 token: userAccessToken
214 socketNotifications: adminNotifications,
215 token: servers[0].accessToken
218 const name = 'abuse ' + buildUUID()
219 const video = await servers[0].videos.upload({ token: userAccessToken, attributes: { name } })
222 const body = await servers[0].abuses.report({ token: userAccessToken, videoId: video.id, reason: 'super reason' })
223 abuseId = body.abuse.id
227 const body = await servers[0].abuses.report({ token: userAccessToken, videoId: video.id, reason: 'super reason 2' })
228 abuseId2 = body.abuse.id
232 it('Should send a notification to reporter on new message', async function () {
235 const message = 'my super message to users'
236 await servers[0].abuses.addMessage({ abuseId, message })
237 await waitJobs(servers)
239 await checkNewAbuseMessage({ ...baseParamsUser, abuseId, message, toEmail: 'user_1@example.com', checkType: 'presence' })
242 it('Should not send a notification to the admin if sent by the admin', async function () {
245 const message = 'my super message that should not be sent to the admin'
246 await servers[0].abuses.addMessage({ abuseId, message })
247 await waitJobs(servers)
249 const toEmail = 'admin' + servers[0].internalServerNumber + '@example.com'
250 await checkNewAbuseMessage({ ...baseParamsAdmin, abuseId, message, toEmail, checkType: 'absence' })
253 it('Should send a notification to moderators', async function () {
256 const message = 'my super message to moderators'
257 await servers[0].abuses.addMessage({ token: userAccessToken, abuseId: abuseId2, message })
258 await waitJobs(servers)
260 const toEmail = 'admin' + servers[0].internalServerNumber + '@example.com'
261 await checkNewAbuseMessage({ ...baseParamsAdmin, abuseId: abuseId2, message, toEmail, checkType: 'presence' })
264 it('Should not send a notification to reporter if sent by the reporter', async function () {
267 const message = 'my super message that should not be sent to reporter'
268 await servers[0].abuses.addMessage({ token: userAccessToken, abuseId: abuseId2, message })
269 await waitJobs(servers)
271 const toEmail = 'user_1@example.com'
272 await checkNewAbuseMessage({ ...baseParamsUser, abuseId: abuseId2, message, toEmail, checkType: 'absence' })
276 describe('Video blacklist on my video', function () {
277 let baseParams: CheckerBaseParams
283 socketNotifications: userNotifications,
284 token: userAccessToken
288 it('Should send a notification to video owner on blacklist', async function () {
291 const name = 'video for abuse ' + buildUUID()
292 const { uuid, shortUUID } = await servers[0].videos.upload({ token: userAccessToken, attributes: { name } })
294 await servers[0].blacklist.add({ videoId: uuid })
296 await waitJobs(servers)
297 await checkNewBlacklistOnMyVideo({ ...baseParams, shortUUID, videoName: name, blacklistType: 'blacklist' })
300 it('Should send a notification to video owner on unblacklist', async function () {
303 const name = 'video for abuse ' + buildUUID()
304 const { uuid, shortUUID } = await servers[0].videos.upload({ token: userAccessToken, attributes: { name } })
306 await servers[0].blacklist.add({ videoId: uuid })
308 await waitJobs(servers)
309 await servers[0].blacklist.remove({ videoId: uuid })
310 await waitJobs(servers)
313 await checkNewBlacklistOnMyVideo({ ...baseParams, shortUUID, videoName: name, blacklistType: 'unblacklist' })
317 describe('New registration', function () {
318 let baseParams: CheckerBaseParams
324 socketNotifications: adminNotifications,
325 token: servers[0].accessToken
329 it('Should send a notification only to moderators when a user registers on the instance', async function () {
332 await servers[0].users.register({ username: 'user_45' })
334 await waitJobs(servers)
336 await checkUserRegistered({ ...baseParams, username: 'user_45', checkType: 'presence' })
338 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
339 await checkUserRegistered({ ...baseParams, ...userOverride, username: 'user_45', checkType: 'absence' })
343 describe('New instance follows', function () {
344 const instanceIndexServer = new MockInstancesIndex()
346 let baseParams: CheckerBaseParams
352 socketNotifications: adminNotifications,
353 token: servers[0].accessToken
356 const port = await instanceIndexServer.initialize()
357 instanceIndexServer.addInstance(servers[1].host)
363 indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
371 it('Should send a notification only to admin when there is a new instance follower', async function () {
374 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
376 await waitJobs(servers)
378 await checkNewInstanceFollower({ ...baseParams, followerHost: 'localhost:' + servers[2].port, checkType: 'presence' })
380 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
381 await checkNewInstanceFollower({ ...baseParams, ...userOverride, followerHost: 'localhost:' + servers[2].port, checkType: 'absence' })
384 it('Should send a notification on auto follow back', async function () {
387 await servers[2].follows.unfollow({ target: servers[0] })
388 await waitJobs(servers)
393 autoFollowBack: { enabled: true }
397 await servers[0].config.updateCustomSubConfig({ newConfig: config })
399 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
401 await waitJobs(servers)
403 const followerHost = servers[0].host
404 const followingHost = servers[2].host
405 await checkAutoInstanceFollowing({ ...baseParams, followerHost, followingHost, checkType: 'presence' })
407 const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } }
408 await checkAutoInstanceFollowing({ ...baseParams, ...userOverride, followerHost, followingHost, checkType: 'absence' })
410 config.followings.instance.autoFollowBack.enabled = false
411 await servers[0].config.updateCustomSubConfig({ newConfig: config })
412 await servers[0].follows.unfollow({ target: servers[2] })
413 await servers[2].follows.unfollow({ target: servers[0] })
416 it('Should send a notification on auto instances index follow', async function () {
418 await servers[0].follows.unfollow({ target: servers[1] })
420 await servers[0].config.updateCustomSubConfig({ newConfig: config })
423 await waitJobs(servers)
425 const followerHost = servers[0].host
426 const followingHost = servers[1].host
427 await checkAutoInstanceFollowing({ ...baseParams, followerHost, followingHost, checkType: 'presence' })
429 config.followings.instance.autoFollowIndex.enabled = false
430 await servers[0].config.updateCustomSubConfig({ newConfig: config })
431 await servers[0].follows.unfollow({ target: servers[1] })
435 describe('Video-related notifications when video auto-blacklist is enabled', function () {
436 let userBaseParams: CheckerBaseParams
437 let adminBaseParamsServer1: CheckerBaseParams
438 let adminBaseParamsServer2: CheckerBaseParams
440 let shortUUID: string
441 let videoName: string
442 let currentCustomConfig: CustomConfig
446 adminBaseParamsServer1 = {
449 socketNotifications: adminNotifications,
450 token: servers[0].accessToken
453 adminBaseParamsServer2 = {
456 socketNotifications: adminNotificationsServer2,
457 token: servers[1].accessToken
463 socketNotifications: userNotifications,
464 token: userAccessToken
467 currentCustomConfig = await servers[0].config.getCustomConfig()
469 const autoBlacklistTestsCustomConfig = {
470 ...currentCustomConfig,
481 // enable transcoding otherwise own publish notification after transcoding not expected
482 autoBlacklistTestsCustomConfig.transcoding.enabled = true
483 await servers[0].config.updateCustomConfig({ newCustomConfig: autoBlacklistTestsCustomConfig })
485 await servers[0].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
486 await servers[1].subscriptions.add({ targetUri: 'user_1_channel@localhost:' + servers[0].port })
489 it('Should send notification to moderators on new video with auto-blacklist', async function () {
492 videoName = 'video with auto-blacklist ' + buildUUID()
493 const video = await servers[0].videos.upload({ token: userAccessToken, attributes: { name: videoName } })
494 shortUUID = video.shortUUID
497 await waitJobs(servers)
498 await checkVideoAutoBlacklistForModerators({ ...adminBaseParamsServer1, shortUUID, videoName, checkType: 'presence' })
501 it('Should not send video publish notification if auto-blacklisted', async function () {
502 await checkVideoIsPublished({ ...userBaseParams, videoName, shortUUID, checkType: 'absence' })
505 it('Should not send a local user subscription notification if auto-blacklisted', async function () {
506 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName, shortUUID, checkType: 'absence' })
509 it('Should not send a remote user subscription notification if auto-blacklisted', async function () {
510 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName, shortUUID, checkType: 'absence' })
513 it('Should send video published and unblacklist after video unblacklisted', async function () {
516 await servers[0].blacklist.remove({ videoId: uuid })
518 await waitJobs(servers)
520 // FIXME: Can't test as two notifications sent to same user and util only checks last one
521 // One notification might be better anyways
522 // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist')
523 // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence')
526 it('Should send a local user subscription notification after removed from blacklist', async function () {
527 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName, shortUUID, checkType: 'presence' })
530 it('Should send a remote user subscription notification after removed from blacklist', async function () {
531 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName, shortUUID, checkType: 'presence' })
534 it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () {
537 const updateAt = new Date(new Date().getTime() + 1000000)
539 const name = 'video with auto-blacklist and future schedule ' + buildUUID()
543 privacy: VideoPrivacy.PRIVATE,
545 updateAt: updateAt.toISOString(),
546 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
550 const { shortUUID, uuid } = await servers[0].videos.upload({ token: userAccessToken, attributes })
552 await servers[0].blacklist.remove({ videoId: uuid })
554 await waitJobs(servers)
555 await checkNewBlacklistOnMyVideo({ ...userBaseParams, shortUUID, videoName: name, blacklistType: 'unblacklist' })
557 // FIXME: Can't test absence as two notifications sent to same user and util only checks last one
558 // One notification might be better anyways
559 // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
561 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName: name, shortUUID, checkType: 'absence' })
562 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName: name, shortUUID, checkType: 'absence' })
565 it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () {
569 const updateAt = new Date(new Date().getTime() + 2000)
571 const name = 'video with schedule done and still auto-blacklisted ' + buildUUID()
575 privacy: VideoPrivacy.PRIVATE,
577 updateAt: updateAt.toISOString(),
578 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
582 const { shortUUID } = await servers[0].videos.upload({ token: userAccessToken, attributes })
585 await checkVideoIsPublished({ ...userBaseParams, videoName: name, shortUUID, checkType: 'absence' })
586 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName: name, shortUUID, checkType: 'absence' })
587 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName: name, shortUUID, checkType: 'absence' })
590 it('Should not send a notification to moderators on new video without auto-blacklist', async function () {
593 const name = 'video without auto-blacklist ' + buildUUID()
595 // admin with blacklist right will not be auto-blacklisted
596 const { shortUUID } = await servers[0].videos.upload({ attributes: { name } })
598 await waitJobs(servers)
599 await checkVideoAutoBlacklistForModerators({ ...adminBaseParamsServer1, shortUUID, videoName: name, checkType: 'absence' })
603 await servers[0].config.updateCustomConfig({ newCustomConfig: currentCustomConfig })
605 await servers[0].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
606 await servers[1].subscriptions.remove({ uri: 'user_1_channel@localhost:' + servers[0].port })
610 after(async function () {
611 MockSmtpServer.Instance.kill()
613 await cleanupTests(servers)