1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
5 checkAutoInstanceFollowing,
8 checkNewAccountAbuseForModerators,
9 checkNewBlacklistOnMyVideo,
10 checkNewCommentAbuseForModerators,
11 checkNewInstanceFollower,
12 checkNewVideoAbuseForModerators,
13 checkNewVideoFromSubscription,
15 checkVideoAutoBlacklistForModerators,
16 checkVideoIsPublished,
19 prepareNotificationsTest
20 } from '@server/tests/shared'
21 import { wait } from '@shared/core-utils'
22 import { buildUUID } from '@shared/extra-utils'
23 import { AbuseState, CustomConfig, UserNotification, UserRole, VideoPrivacy } from '@shared/models'
24 import { cleanupTests, PeerTubeServer, waitJobs } from '@shared/server-commands'
26 describe('Test moderation notifications', function () {
27 let servers: PeerTubeServer[] = []
28 let userToken1: string
29 let userToken2: string
31 let userNotifications: UserNotification[] = []
32 let adminNotifications: UserNotification[] = []
33 let adminNotificationsServer2: UserNotification[] = []
34 let emails: object[] = []
36 before(async function () {
39 const res = await prepareNotificationsTest(3)
41 userToken1 = res.userAccessToken
43 userNotifications = res.userNotifications
44 adminNotifications = res.adminNotifications
45 adminNotificationsServer2 = res.adminNotificationsServer2
47 userToken2 = await servers[1].users.generateUserAndToken('user2', UserRole.USER)
50 describe('Abuse for moderators notification', function () {
51 let baseParams: CheckerBaseParams
57 socketNotifications: adminNotifications,
58 token: servers[0].accessToken
62 it('Should not send a notification to moderators on local abuse reported by an admin', async function () {
65 const name = 'video for abuse ' + buildUUID()
66 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
68 await servers[0].abuses.report({ videoId: video.id, reason: 'super reason' })
70 await waitJobs(servers)
71 await checkNewVideoAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'absence' })
74 it('Should send a notification to moderators on local video abuse', async function () {
77 const name = 'video for abuse ' + buildUUID()
78 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
80 await servers[0].abuses.report({ token: userToken1, videoId: video.id, reason: 'super reason' })
82 await waitJobs(servers)
83 await checkNewVideoAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
86 it('Should send a notification to moderators on remote video abuse', async function () {
89 const name = 'video for abuse ' + buildUUID()
90 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
92 await waitJobs(servers)
94 const videoId = await servers[1].videos.getId({ uuid: video.uuid })
95 await servers[1].abuses.report({ token: userToken2, videoId, reason: 'super reason' })
97 await waitJobs(servers)
98 await checkNewVideoAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
101 it('Should send a notification to moderators on local comment abuse', async function () {
104 const name = 'video for abuse ' + buildUUID()
105 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
106 const comment = await servers[0].comments.createThread({
109 text: 'comment abuse ' + buildUUID()
112 await waitJobs(servers)
114 await servers[0].abuses.report({ token: userToken1, commentId: comment.id, reason: 'super reason' })
116 await waitJobs(servers)
117 await checkNewCommentAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
120 it('Should send a notification to moderators on remote comment abuse', async function () {
123 const name = 'video for abuse ' + buildUUID()
124 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
126 await servers[0].comments.createThread({
129 text: 'comment abuse ' + buildUUID()
132 await waitJobs(servers)
134 const { data } = await servers[1].comments.listThreads({ videoId: video.uuid })
135 const commentId = data[0].id
136 await servers[1].abuses.report({ token: userToken2, commentId, reason: 'super reason' })
138 await waitJobs(servers)
139 await checkNewCommentAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
142 it('Should send a notification to moderators on local account abuse', async function () {
145 const username = 'user' + new Date().getTime()
146 const { account } = await servers[0].users.create({ username, password: 'donald' })
147 const accountId = account.id
149 await servers[0].abuses.report({ token: userToken1, accountId, reason: 'super reason' })
151 await waitJobs(servers)
152 await checkNewAccountAbuseForModerators({ ...baseParams, displayName: username, checkType: '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 servers[0].users.generateUserAndToken(username)
160 await servers[0].videos.upload({ token: tmpToken, attributes: { name: 'super video' } })
162 await waitJobs(servers)
164 const account = await servers[1].accounts.get({ accountName: username + '@' + servers[0].host })
165 await servers[1].abuses.report({ token: userToken2, accountId: account.id, reason: 'super reason' })
167 await waitJobs(servers)
168 await checkNewAccountAbuseForModerators({ ...baseParams, displayName: username, checkType: 'presence' })
172 describe('Abuse state change notification', function () {
173 let baseParams: CheckerBaseParams
176 before(async function () {
180 socketNotifications: userNotifications,
184 const name = 'abuse ' + buildUUID()
185 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
187 const body = await servers[0].abuses.report({ token: userToken1, videoId: video.id, reason: 'super reason' })
188 abuseId = body.abuse.id
191 it('Should send a notification to reporter if the abuse has been accepted', async function () {
194 await servers[0].abuses.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
195 await waitJobs(servers)
197 await checkAbuseStateChange({ ...baseParams, abuseId, state: AbuseState.ACCEPTED, checkType: 'presence' })
200 it('Should send a notification to reporter if the abuse has been rejected', async function () {
203 await servers[0].abuses.update({ abuseId, body: { state: AbuseState.REJECTED } })
204 await waitJobs(servers)
206 await checkAbuseStateChange({ ...baseParams, abuseId, state: AbuseState.REJECTED, checkType: 'presence' })
210 describe('New abuse message notification', function () {
211 let baseParamsUser: CheckerBaseParams
212 let baseParamsAdmin: CheckerBaseParams
216 before(async function () {
220 socketNotifications: userNotifications,
227 socketNotifications: adminNotifications,
228 token: servers[0].accessToken
231 const name = 'abuse ' + buildUUID()
232 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
235 const body = await servers[0].abuses.report({ token: userToken1, videoId: video.id, reason: 'super reason' })
236 abuseId = body.abuse.id
240 const body = await servers[0].abuses.report({ token: userToken1, videoId: video.id, reason: 'super reason 2' })
241 abuseId2 = body.abuse.id
245 it('Should send a notification to reporter on new message', async function () {
248 const message = 'my super message to users'
249 await servers[0].abuses.addMessage({ abuseId, message })
250 await waitJobs(servers)
252 await checkNewAbuseMessage({ ...baseParamsUser, abuseId, message, toEmail: 'user_1@example.com', checkType: 'presence' })
255 it('Should not send a notification to the admin if sent by the admin', async function () {
258 const message = 'my super message that should not be sent to the admin'
259 await servers[0].abuses.addMessage({ abuseId, message })
260 await waitJobs(servers)
262 const toEmail = 'admin' + servers[0].internalServerNumber + '@example.com'
263 await checkNewAbuseMessage({ ...baseParamsAdmin, abuseId, message, toEmail, checkType: 'absence' })
266 it('Should send a notification to moderators', async function () {
269 const message = 'my super message to moderators'
270 await servers[0].abuses.addMessage({ token: userToken1, abuseId: abuseId2, message })
271 await waitJobs(servers)
273 const toEmail = 'admin' + servers[0].internalServerNumber + '@example.com'
274 await checkNewAbuseMessage({ ...baseParamsAdmin, abuseId: abuseId2, message, toEmail, checkType: 'presence' })
277 it('Should not send a notification to reporter if sent by the reporter', async function () {
280 const message = 'my super message that should not be sent to reporter'
281 await servers[0].abuses.addMessage({ token: userToken1, abuseId: abuseId2, message })
282 await waitJobs(servers)
284 const toEmail = 'user_1@example.com'
285 await checkNewAbuseMessage({ ...baseParamsUser, abuseId: abuseId2, message, toEmail, checkType: 'absence' })
289 describe('Video blacklist on my video', function () {
290 let baseParams: CheckerBaseParams
296 socketNotifications: userNotifications,
301 it('Should send a notification to video owner on blacklist', async function () {
304 const name = 'video for abuse ' + buildUUID()
305 const { uuid, shortUUID } = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
307 await servers[0].blacklist.add({ videoId: uuid })
309 await waitJobs(servers)
310 await checkNewBlacklistOnMyVideo({ ...baseParams, shortUUID, videoName: name, blacklistType: 'blacklist' })
313 it('Should send a notification to video owner on unblacklist', async function () {
316 const name = 'video for abuse ' + buildUUID()
317 const { uuid, shortUUID } = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
319 await servers[0].blacklist.add({ videoId: uuid })
321 await waitJobs(servers)
322 await servers[0].blacklist.remove({ videoId: uuid })
323 await waitJobs(servers)
326 await checkNewBlacklistOnMyVideo({ ...baseParams, shortUUID, videoName: name, blacklistType: 'unblacklist' })
330 describe('New registration', function () {
331 let baseParams: CheckerBaseParams
337 socketNotifications: adminNotifications,
338 token: servers[0].accessToken
342 it('Should send a notification only to moderators when a user registers on the instance', async function () {
345 await servers[0].users.register({ username: 'user_45' })
347 await waitJobs(servers)
349 await checkUserRegistered({ ...baseParams, username: 'user_45', checkType: 'presence' })
351 const userOverride = { socketNotifications: userNotifications, token: userToken1, check: { web: true, mail: false } }
352 await checkUserRegistered({ ...baseParams, ...userOverride, username: 'user_45', checkType: 'absence' })
356 describe('New instance follows', function () {
357 const instanceIndexServer = new MockInstancesIndex()
359 let baseParams: CheckerBaseParams
365 socketNotifications: adminNotifications,
366 token: servers[0].accessToken
369 const port = await instanceIndexServer.initialize()
370 instanceIndexServer.addInstance(servers[1].host)
376 indexUrl: `http://127.0.0.1:${port}/api/v1/instances/hosts`,
384 it('Should send a notification only to admin when there is a new instance follower', async function () {
387 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
389 await waitJobs(servers)
391 await checkNewInstanceFollower({ ...baseParams, followerHost: servers[2].host, checkType: 'presence' })
393 const userOverride = { socketNotifications: userNotifications, token: userToken1, check: { web: true, mail: false } }
394 await checkNewInstanceFollower({ ...baseParams, ...userOverride, followerHost: servers[2].host, checkType: 'absence' })
397 it('Should send a notification on auto follow back', async function () {
400 await servers[2].follows.unfollow({ target: servers[0] })
401 await waitJobs(servers)
406 autoFollowBack: { enabled: true }
410 await servers[0].config.updateCustomSubConfig({ newConfig: config })
412 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
414 await waitJobs(servers)
416 const followerHost = servers[0].host
417 const followingHost = servers[2].host
418 await checkAutoInstanceFollowing({ ...baseParams, followerHost, followingHost, checkType: 'presence' })
420 const userOverride = { socketNotifications: userNotifications, token: userToken1, check: { web: true, mail: false } }
421 await checkAutoInstanceFollowing({ ...baseParams, ...userOverride, followerHost, followingHost, checkType: 'absence' })
423 config.followings.instance.autoFollowBack.enabled = false
424 await servers[0].config.updateCustomSubConfig({ newConfig: config })
425 await servers[0].follows.unfollow({ target: servers[2] })
426 await servers[2].follows.unfollow({ target: servers[0] })
429 it('Should send a notification on auto instances index follow', async function () {
431 await servers[0].follows.unfollow({ target: servers[1] })
433 await servers[0].config.updateCustomSubConfig({ newConfig: config })
436 await waitJobs(servers)
438 const followerHost = servers[0].host
439 const followingHost = servers[1].host
440 await checkAutoInstanceFollowing({ ...baseParams, followerHost, followingHost, checkType: 'presence' })
442 config.followings.instance.autoFollowIndex.enabled = false
443 await servers[0].config.updateCustomSubConfig({ newConfig: config })
444 await servers[0].follows.unfollow({ target: servers[1] })
448 describe('Video-related notifications when video auto-blacklist is enabled', function () {
449 let userBaseParams: CheckerBaseParams
450 let adminBaseParamsServer1: CheckerBaseParams
451 let adminBaseParamsServer2: CheckerBaseParams
453 let shortUUID: string
454 let videoName: string
455 let currentCustomConfig: CustomConfig
459 adminBaseParamsServer1 = {
462 socketNotifications: adminNotifications,
463 token: servers[0].accessToken
466 adminBaseParamsServer2 = {
469 socketNotifications: adminNotificationsServer2,
470 token: servers[1].accessToken
476 socketNotifications: userNotifications,
480 currentCustomConfig = await servers[0].config.getCustomConfig()
482 const autoBlacklistTestsCustomConfig = {
483 ...currentCustomConfig,
494 // enable transcoding otherwise own publish notification after transcoding not expected
495 autoBlacklistTestsCustomConfig.transcoding.enabled = true
496 await servers[0].config.updateCustomConfig({ newCustomConfig: autoBlacklistTestsCustomConfig })
498 await servers[0].subscriptions.add({ targetUri: 'user_1_channel@' + servers[0].host })
499 await servers[1].subscriptions.add({ targetUri: 'user_1_channel@' + servers[0].host })
502 it('Should send notification to moderators on new video with auto-blacklist', async function () {
505 videoName = 'video with auto-blacklist ' + buildUUID()
506 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name: videoName } })
507 shortUUID = video.shortUUID
510 await waitJobs(servers)
511 await checkVideoAutoBlacklistForModerators({ ...adminBaseParamsServer1, shortUUID, videoName, checkType: 'presence' })
514 it('Should not send video publish notification if auto-blacklisted', async function () {
515 await checkVideoIsPublished({ ...userBaseParams, videoName, shortUUID, checkType: 'absence' })
518 it('Should not send a local user subscription notification if auto-blacklisted', async function () {
519 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName, shortUUID, checkType: 'absence' })
522 it('Should not send a remote user subscription notification if auto-blacklisted', async function () {
523 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName, shortUUID, checkType: 'absence' })
526 it('Should send video published and unblacklist after video unblacklisted', async function () {
529 await servers[0].blacklist.remove({ videoId: uuid })
531 await waitJobs(servers)
533 // FIXME: Can't test as two notifications sent to same user and util only checks last one
534 // One notification might be better anyways
535 // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist')
536 // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence')
539 it('Should send a local user subscription notification after removed from blacklist', async function () {
540 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName, shortUUID, checkType: 'presence' })
543 it('Should send a remote user subscription notification after removed from blacklist', async function () {
544 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName, shortUUID, checkType: 'presence' })
547 it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () {
550 const updateAt = new Date(new Date().getTime() + 1000000)
552 const name = 'video with auto-blacklist and future schedule ' + buildUUID()
556 privacy: VideoPrivacy.PRIVATE,
558 updateAt: updateAt.toISOString(),
559 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
563 const { shortUUID, uuid } = await servers[0].videos.upload({ token: userToken1, attributes })
565 await servers[0].blacklist.remove({ videoId: uuid })
567 await waitJobs(servers)
568 await checkNewBlacklistOnMyVideo({ ...userBaseParams, shortUUID, videoName: name, blacklistType: 'unblacklist' })
570 // FIXME: Can't test absence as two notifications sent to same user and util only checks last one
571 // One notification might be better anyways
572 // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
574 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName: name, shortUUID, checkType: 'absence' })
575 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName: name, shortUUID, checkType: 'absence' })
578 it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () {
582 const updateAt = new Date(new Date().getTime() + 2000)
584 const name = 'video with schedule done and still auto-blacklisted ' + buildUUID()
588 privacy: VideoPrivacy.PRIVATE,
590 updateAt: updateAt.toISOString(),
591 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
595 const { shortUUID } = await servers[0].videos.upload({ token: userToken1, attributes })
598 await checkVideoIsPublished({ ...userBaseParams, videoName: name, shortUUID, checkType: 'absence' })
599 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName: name, shortUUID, checkType: 'absence' })
600 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName: name, shortUUID, checkType: 'absence' })
603 it('Should not send a notification to moderators on new video without auto-blacklist', async function () {
606 const name = 'video without auto-blacklist ' + buildUUID()
608 // admin with blacklist right will not be auto-blacklisted
609 const { shortUUID } = await servers[0].videos.upload({ attributes: { name } })
611 await waitJobs(servers)
612 await checkVideoAutoBlacklistForModerators({ ...adminBaseParamsServer1, shortUUID, videoName: name, checkType: 'absence' })
616 await servers[0].config.updateCustomConfig({ newCustomConfig: currentCustomConfig })
618 await servers[0].subscriptions.remove({ uri: 'user_1_channel@' + servers[0].host })
619 await servers[1].subscriptions.remove({ uri: 'user_1_channel@' + servers[0].host })
623 after(async function () {
624 MockSmtpServer.Instance.kill()
626 await cleanupTests(servers)