1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
5 import { wait } from '@shared/core-utils'
6 import { Video } from '@shared/models'
13 setAccessTokensToServers
14 } from '@shared/server-commands'
15 import { MockBlocklist } from '../shared'
17 async function check (server: PeerTubeServer, videoUUID: string, exists = true) {
18 const { data } = await server.videos.list()
20 const video = data.find(v => v.uuid === videoUUID)
22 if (exists) expect(video).to.not.be.undefined
23 else expect(video).to.be.undefined
26 describe('Official plugin auto-block videos', function () {
27 let servers: PeerTubeServer[]
28 let blocklistServer: MockBlocklist
29 let server1Videos: Video[] = []
30 let server2Videos: Video[] = []
33 before(async function () {
36 servers = await createMultipleServers(2)
37 await setAccessTokensToServers(servers)
39 for (const server of servers) {
40 await server.plugins.install({ npmName: 'peertube-plugin-auto-block-videos' })
43 blocklistServer = new MockBlocklist()
44 port = await blocklistServer.initialize()
46 await servers[0].videos.quickUpload({ name: 'video server 1' })
47 await servers[1].videos.quickUpload({ name: 'video server 2' })
48 await servers[1].videos.quickUpload({ name: 'video 2 server 2' })
49 await servers[1].videos.quickUpload({ name: 'video 3 server 2' })
52 const { data } = await servers[0].videos.list()
53 server1Videos = data.map(v => Object.assign(v, { url: servers[0].url + '/videos/watch/' + v.uuid }))
57 const { data } = await servers[1].videos.list()
58 server2Videos = data.map(v => Object.assign(v, { url: servers[1].url + '/videos/watch/' + v.uuid }))
61 await doubleFollow(servers[0], servers[1])
64 it('Should update plugin settings', async function () {
65 await servers[0].plugins.updateSettings({
66 npmName: 'peertube-plugin-auto-block-videos',
68 'blocklist-urls': `http://localhost:${port}/blocklist`,
69 'check-seconds-interval': 1
74 it('Should auto block a video', async function () {
77 await check(servers[0], server2Videos[0].uuid, true)
79 blocklistServer.replace({
82 value: server2Videos[0].url
89 await check(servers[0], server2Videos[0].uuid, false)
92 it('Should have video in blacklists', async function () {
93 const body = await servers[0].blacklist.list()
95 const videoBlacklists = body.data
96 expect(videoBlacklists).to.have.lengthOf(1)
97 expect(videoBlacklists[0].reason).to.contains('Automatically blocked from auto block plugin')
98 expect(videoBlacklists[0].video.name).to.equal(server2Videos[0].name)
101 it('Should not block a local video', async function () {
104 await check(servers[0], server1Videos[0].uuid, true)
106 blocklistServer.replace({
109 value: server1Videos[0].url
116 await check(servers[0], server1Videos[0].uuid, true)
119 it('Should remove a video block', async function () {
122 await check(servers[0], server2Videos[0].uuid, false)
124 blocklistServer.replace({
127 value: server2Videos[0].url,
135 await check(servers[0], server2Videos[0].uuid, true)
138 it('Should auto block a video, manually unblock it and do not reblock it automatically', async function () {
141 const video = server2Videos[1]
143 await check(servers[0], video.uuid, true)
145 blocklistServer.replace({
149 updatedAt: new Date().toISOString()
156 await check(servers[0], video.uuid, false)
158 await servers[0].blacklist.remove({ videoId: video.uuid })
160 await check(servers[0], video.uuid, true)
162 await killallServers([ servers[0] ])
163 await servers[0].run()
166 await check(servers[0], video.uuid, true)
169 after(async function () {
170 await blocklistServer.terminate()
172 await cleanupTests(servers)