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,
14 checkVideoAutoBlacklistForModerators,
15 checkVideoIsPublished,
18 prepareNotificationsTest
19 } from '@server/tests/shared'
20 import { wait } from '@shared/core-utils'
21 import { buildUUID } from '@shared/extra-utils'
22 import { AbuseState, CustomConfig, UserNotification, UserRole, VideoPrivacy } from '@shared/models'
23 import { cleanupTests, PeerTubeServer, waitJobs } from '@shared/server-commands'
25 describe('Test moderation notifications', function () {
26 let servers: PeerTubeServer[] = []
27 let userToken1: string
28 let userToken2: string
30 let userNotifications: UserNotification[] = []
31 let adminNotifications: UserNotification[] = []
32 let adminNotificationsServer2: UserNotification[] = []
33 let emails: object[] = []
35 before(async function () {
38 const res = await prepareNotificationsTest(3)
40 userToken1 = res.userAccessToken
42 userNotifications = res.userNotifications
43 adminNotifications = res.adminNotifications
44 adminNotificationsServer2 = res.adminNotificationsServer2
46 userToken2 = await servers[1].users.generateUserAndToken('user2', UserRole.USER)
49 describe('Abuse for moderators notification', function () {
50 let baseParams: CheckerBaseParams
56 socketNotifications: adminNotifications,
57 token: servers[0].accessToken
61 it('Should not send a notification to moderators on local abuse reported by an admin', async function () {
64 const name = 'video for abuse ' + buildUUID()
65 const video = await servers[0].videos.upload({ token: userToken1, 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: 'absence' })
73 it('Should send a notification to moderators on local video abuse', async function () {
76 const name = 'video for abuse ' + buildUUID()
77 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
79 await servers[0].abuses.report({ token: userToken1, videoId: video.id, reason: 'super reason' })
81 await waitJobs(servers)
82 await checkNewVideoAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
85 it('Should send a notification to moderators on remote video abuse', async function () {
88 const name = 'video for abuse ' + buildUUID()
89 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
91 await waitJobs(servers)
93 const videoId = await servers[1].videos.getId({ uuid: video.uuid })
94 await servers[1].abuses.report({ token: userToken2, videoId, reason: 'super reason' })
96 await waitJobs(servers)
97 await checkNewVideoAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
100 it('Should send a notification to moderators on local comment abuse', async function () {
103 const name = 'video for abuse ' + buildUUID()
104 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
105 const comment = await servers[0].comments.createThread({
108 text: 'comment abuse ' + buildUUID()
111 await waitJobs(servers)
113 await servers[0].abuses.report({ token: userToken1, commentId: comment.id, reason: 'super reason' })
115 await waitJobs(servers)
116 await checkNewCommentAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
119 it('Should send a notification to moderators on remote comment abuse', async function () {
122 const name = 'video for abuse ' + buildUUID()
123 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
125 await servers[0].comments.createThread({
128 text: 'comment abuse ' + buildUUID()
131 await waitJobs(servers)
133 const { data } = await servers[1].comments.listThreads({ videoId: video.uuid })
134 const commentId = data[0].id
135 await servers[1].abuses.report({ token: userToken2, commentId, reason: 'super reason' })
137 await waitJobs(servers)
138 await checkNewCommentAbuseForModerators({ ...baseParams, shortUUID: video.shortUUID, videoName: name, checkType: 'presence' })
141 it('Should send a notification to moderators on local account abuse', async function () {
144 const username = 'user' + new Date().getTime()
145 const { account } = await servers[0].users.create({ username, password: 'donald' })
146 const accountId = account.id
148 await servers[0].abuses.report({ token: userToken1, accountId, reason: 'super reason' })
150 await waitJobs(servers)
151 await checkNewAccountAbuseForModerators({ ...baseParams, displayName: username, checkType: 'presence' })
154 it('Should send a notification to moderators on remote account abuse', async function () {
157 const username = 'user' + new Date().getTime()
158 const tmpToken = await servers[0].users.generateUserAndToken(username)
159 await servers[0].videos.upload({ token: tmpToken, attributes: { name: 'super video' } })
161 await waitJobs(servers)
163 const account = await servers[1].accounts.get({ accountName: username + '@' + servers[0].host })
164 await servers[1].abuses.report({ token: userToken2, accountId: account.id, reason: 'super reason' })
166 await waitJobs(servers)
167 await checkNewAccountAbuseForModerators({ ...baseParams, displayName: username, checkType: 'presence' })
171 describe('Abuse state change notification', function () {
172 let baseParams: CheckerBaseParams
175 before(async function () {
179 socketNotifications: userNotifications,
183 const name = 'abuse ' + buildUUID()
184 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
186 const body = await servers[0].abuses.report({ token: userToken1, videoId: video.id, reason: 'super reason' })
187 abuseId = body.abuse.id
190 it('Should send a notification to reporter if the abuse has been accepted', async function () {
193 await servers[0].abuses.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
194 await waitJobs(servers)
196 await checkAbuseStateChange({ ...baseParams, abuseId, state: AbuseState.ACCEPTED, checkType: 'presence' })
199 it('Should send a notification to reporter if the abuse has been rejected', async function () {
202 await servers[0].abuses.update({ abuseId, body: { state: AbuseState.REJECTED } })
203 await waitJobs(servers)
205 await checkAbuseStateChange({ ...baseParams, abuseId, state: AbuseState.REJECTED, checkType: 'presence' })
209 describe('New abuse message notification', function () {
210 let baseParamsUser: CheckerBaseParams
211 let baseParamsAdmin: CheckerBaseParams
215 before(async function () {
219 socketNotifications: userNotifications,
226 socketNotifications: adminNotifications,
227 token: servers[0].accessToken
230 const name = 'abuse ' + buildUUID()
231 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
234 const body = await servers[0].abuses.report({ token: userToken1, videoId: video.id, reason: 'super reason' })
235 abuseId = body.abuse.id
239 const body = await servers[0].abuses.report({ token: userToken1, videoId: video.id, reason: 'super reason 2' })
240 abuseId2 = body.abuse.id
244 it('Should send a notification to reporter on new message', async function () {
247 const message = 'my super message to users'
248 await servers[0].abuses.addMessage({ abuseId, message })
249 await waitJobs(servers)
251 await checkNewAbuseMessage({ ...baseParamsUser, abuseId, message, toEmail: 'user_1@example.com', checkType: 'presence' })
254 it('Should not send a notification to the admin if sent by the admin', async function () {
257 const message = 'my super message that should not be sent to the admin'
258 await servers[0].abuses.addMessage({ abuseId, message })
259 await waitJobs(servers)
261 const toEmail = 'admin' + servers[0].internalServerNumber + '@example.com'
262 await checkNewAbuseMessage({ ...baseParamsAdmin, abuseId, message, toEmail, checkType: 'absence' })
265 it('Should send a notification to moderators', async function () {
268 const message = 'my super message to moderators'
269 await servers[0].abuses.addMessage({ token: userToken1, abuseId: abuseId2, message })
270 await waitJobs(servers)
272 const toEmail = 'admin' + servers[0].internalServerNumber + '@example.com'
273 await checkNewAbuseMessage({ ...baseParamsAdmin, abuseId: abuseId2, message, toEmail, checkType: 'presence' })
276 it('Should not send a notification to reporter if sent by the reporter', async function () {
279 const message = 'my super message that should not be sent to reporter'
280 await servers[0].abuses.addMessage({ token: userToken1, abuseId: abuseId2, message })
281 await waitJobs(servers)
283 const toEmail = 'user_1@example.com'
284 await checkNewAbuseMessage({ ...baseParamsUser, abuseId: abuseId2, message, toEmail, checkType: 'absence' })
288 describe('Video blacklist on my video', function () {
289 let baseParams: CheckerBaseParams
295 socketNotifications: userNotifications,
300 it('Should send a notification to video owner on blacklist', async function () {
303 const name = 'video for abuse ' + buildUUID()
304 const { uuid, shortUUID } = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
306 await servers[0].blacklist.add({ videoId: uuid })
308 await waitJobs(servers)
309 await checkNewBlacklistOnMyVideo({ ...baseParams, shortUUID, videoName: name, blacklistType: 'blacklist' })
312 it('Should send a notification to video owner on unblacklist', async function () {
315 const name = 'video for abuse ' + buildUUID()
316 const { uuid, shortUUID } = await servers[0].videos.upload({ token: userToken1, attributes: { name } })
318 await servers[0].blacklist.add({ videoId: uuid })
320 await waitJobs(servers)
321 await servers[0].blacklist.remove({ videoId: uuid })
322 await waitJobs(servers)
325 await checkNewBlacklistOnMyVideo({ ...baseParams, shortUUID, videoName: name, blacklistType: 'unblacklist' })
329 describe('New instance follows', function () {
330 const instanceIndexServer = new MockInstancesIndex()
332 let baseParams: CheckerBaseParams
338 socketNotifications: adminNotifications,
339 token: servers[0].accessToken
342 const port = await instanceIndexServer.initialize()
343 instanceIndexServer.addInstance(servers[1].host)
349 indexUrl: `http://127.0.0.1:${port}/api/v1/instances/hosts`,
357 it('Should send a notification only to admin when there is a new instance follower', async function () {
360 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
362 await waitJobs(servers)
364 await checkNewInstanceFollower({ ...baseParams, followerHost: servers[2].host, checkType: 'presence' })
366 const userOverride = { socketNotifications: userNotifications, token: userToken1, check: { web: true, mail: false } }
367 await checkNewInstanceFollower({ ...baseParams, ...userOverride, followerHost: servers[2].host, checkType: 'absence' })
370 it('Should send a notification on auto follow back', async function () {
373 await servers[2].follows.unfollow({ target: servers[0] })
374 await waitJobs(servers)
379 autoFollowBack: { enabled: true }
383 await servers[0].config.updateCustomSubConfig({ newConfig: config })
385 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
387 await waitJobs(servers)
389 const followerHost = servers[0].host
390 const followingHost = servers[2].host
391 await checkAutoInstanceFollowing({ ...baseParams, followerHost, followingHost, checkType: 'presence' })
393 const userOverride = { socketNotifications: userNotifications, token: userToken1, check: { web: true, mail: false } }
394 await checkAutoInstanceFollowing({ ...baseParams, ...userOverride, followerHost, followingHost, checkType: 'absence' })
396 config.followings.instance.autoFollowBack.enabled = false
397 await servers[0].config.updateCustomSubConfig({ newConfig: config })
398 await servers[0].follows.unfollow({ target: servers[2] })
399 await servers[2].follows.unfollow({ target: servers[0] })
402 it('Should send a notification on auto instances index follow', async function () {
404 await servers[0].follows.unfollow({ target: servers[1] })
406 await servers[0].config.updateCustomSubConfig({ newConfig: config })
409 await waitJobs(servers)
411 const followerHost = servers[0].host
412 const followingHost = servers[1].host
413 await checkAutoInstanceFollowing({ ...baseParams, followerHost, followingHost, checkType: 'presence' })
415 config.followings.instance.autoFollowIndex.enabled = false
416 await servers[0].config.updateCustomSubConfig({ newConfig: config })
417 await servers[0].follows.unfollow({ target: servers[1] })
421 describe('Video-related notifications when video auto-blacklist is enabled', function () {
422 let userBaseParams: CheckerBaseParams
423 let adminBaseParamsServer1: CheckerBaseParams
424 let adminBaseParamsServer2: CheckerBaseParams
426 let shortUUID: string
427 let videoName: string
428 let currentCustomConfig: CustomConfig
432 adminBaseParamsServer1 = {
435 socketNotifications: adminNotifications,
436 token: servers[0].accessToken
439 adminBaseParamsServer2 = {
442 socketNotifications: adminNotificationsServer2,
443 token: servers[1].accessToken
449 socketNotifications: userNotifications,
453 currentCustomConfig = await servers[0].config.getCustomConfig()
455 const autoBlacklistTestsCustomConfig = {
456 ...currentCustomConfig,
467 // enable transcoding otherwise own publish notification after transcoding not expected
468 autoBlacklistTestsCustomConfig.transcoding.enabled = true
469 await servers[0].config.updateCustomConfig({ newCustomConfig: autoBlacklistTestsCustomConfig })
471 await servers[0].subscriptions.add({ targetUri: 'user_1_channel@' + servers[0].host })
472 await servers[1].subscriptions.add({ targetUri: 'user_1_channel@' + servers[0].host })
475 it('Should send notification to moderators on new video with auto-blacklist', async function () {
478 videoName = 'video with auto-blacklist ' + buildUUID()
479 const video = await servers[0].videos.upload({ token: userToken1, attributes: { name: videoName } })
480 shortUUID = video.shortUUID
483 await waitJobs(servers)
484 await checkVideoAutoBlacklistForModerators({ ...adminBaseParamsServer1, shortUUID, videoName, checkType: 'presence' })
487 it('Should not send video publish notification if auto-blacklisted', async function () {
490 await checkVideoIsPublished({ ...userBaseParams, videoName, shortUUID, checkType: 'absence' })
493 it('Should not send a local user subscription notification if auto-blacklisted', async function () {
496 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName, shortUUID, checkType: 'absence' })
499 it('Should not send a remote user subscription notification if auto-blacklisted', async function () {
500 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName, shortUUID, checkType: 'absence' })
503 it('Should send video published and unblacklist after video unblacklisted', async function () {
506 await servers[0].blacklist.remove({ videoId: uuid })
508 await waitJobs(servers)
510 // FIXME: Can't test as two notifications sent to same user and util only checks last one
511 // One notification might be better anyways
512 // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist')
513 // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence')
516 it('Should send a local user subscription notification after removed from blacklist', async function () {
519 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName, shortUUID, checkType: 'presence' })
522 it('Should send a remote user subscription notification after removed from blacklist', async function () {
525 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName, shortUUID, checkType: 'presence' })
528 it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () {
531 const updateAt = new Date(new Date().getTime() + 1000000)
533 const name = 'video with auto-blacklist and future schedule ' + buildUUID()
537 privacy: VideoPrivacy.PRIVATE,
539 updateAt: updateAt.toISOString(),
540 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
544 const { shortUUID, uuid } = await servers[0].videos.upload({ token: userToken1, attributes })
546 await servers[0].blacklist.remove({ videoId: uuid })
548 await waitJobs(servers)
549 await checkNewBlacklistOnMyVideo({ ...userBaseParams, shortUUID, videoName: name, blacklistType: 'unblacklist' })
551 // FIXME: Can't test absence as two notifications sent to same user and util only checks last one
552 // One notification might be better anyways
553 // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence')
555 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName: name, shortUUID, checkType: 'absence' })
556 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName: name, shortUUID, checkType: 'absence' })
559 it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () {
563 const updateAt = new Date(new Date().getTime() + 2000)
565 const name = 'video with schedule done and still auto-blacklisted ' + buildUUID()
569 privacy: VideoPrivacy.PRIVATE,
571 updateAt: updateAt.toISOString(),
572 privacy: VideoPrivacy.PUBLIC as VideoPrivacy.PUBLIC
576 const { shortUUID } = await servers[0].videos.upload({ token: userToken1, attributes })
579 await checkVideoIsPublished({ ...userBaseParams, videoName: name, shortUUID, checkType: 'absence' })
580 await checkNewVideoFromSubscription({ ...adminBaseParamsServer1, videoName: name, shortUUID, checkType: 'absence' })
581 await checkNewVideoFromSubscription({ ...adminBaseParamsServer2, videoName: name, shortUUID, checkType: 'absence' })
584 it('Should not send a notification to moderators on new video without auto-blacklist', async function () {
587 const name = 'video without auto-blacklist ' + buildUUID()
589 // admin with blacklist right will not be auto-blacklisted
590 const { shortUUID } = await servers[0].videos.upload({ attributes: { name } })
592 await waitJobs(servers)
593 await checkVideoAutoBlacklistForModerators({ ...adminBaseParamsServer1, shortUUID, videoName: name, checkType: 'absence' })
597 await servers[0].config.updateCustomConfig({ newCustomConfig: currentCustomConfig })
599 await servers[0].subscriptions.remove({ uri: 'user_1_channel@' + servers[0].host })
600 await servers[1].subscriptions.remove({ uri: 'user_1_channel@' + servers[0].host })
604 after(async function () {
605 MockSmtpServer.Instance.kill()
607 await cleanupTests(servers)