1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
8 flushAndRunMultipleServers,
13 setAccessTokensToServers,
15 } from '@shared/extra-utils'
16 import { Video } from '@shared/models'
18 async function check (server: ServerInfo, videoUUID: string, exists = true) {
19 const { data } = await server.videosCommand.list()
21 const video = data.find(v => v.uuid === videoUUID)
23 if (exists) expect(video).to.not.be.undefined
24 else expect(video).to.be.undefined
27 describe('Official plugin auto-block videos', function () {
28 let servers: ServerInfo[]
29 let blocklistServer: MockBlocklist
30 let server1Videos: Video[] = []
31 let server2Videos: Video[] = []
34 before(async function () {
37 servers = await flushAndRunMultipleServers(2)
38 await setAccessTokensToServers(servers)
40 for (const server of servers) {
41 await server.pluginsCommand.install({ npmName: 'peertube-plugin-auto-block-videos' })
44 blocklistServer = new MockBlocklist()
45 port = await blocklistServer.initialize()
47 await await servers[0].videosCommand.quickUpload({ name: 'video server 1' })
48 await await servers[1].videosCommand.quickUpload({ name: 'video server 2' })
49 await await servers[1].videosCommand.quickUpload({ name: 'video 2 server 2' })
50 await await servers[1].videosCommand.quickUpload({ name: 'video 3 server 2' })
53 const { data } = await servers[0].videosCommand.list()
54 server1Videos = data.map(v => Object.assign(v, { url: servers[0].url + '/videos/watch/' + v.uuid }))
58 const { data } = await servers[1].videosCommand.list()
59 server2Videos = data.map(v => Object.assign(v, { url: servers[1].url + '/videos/watch/' + v.uuid }))
62 await doubleFollow(servers[0], servers[1])
65 it('Should update plugin settings', async function () {
66 await servers[0].pluginsCommand.updateSettings({
67 npmName: 'peertube-plugin-auto-block-videos',
69 'blocklist-urls': `http://localhost:${port}/blocklist`,
70 'check-seconds-interval': 1
75 it('Should auto block a video', async function () {
78 await check(servers[0], server2Videos[0].uuid, true)
80 blocklistServer.replace({
83 value: server2Videos[0].url
90 await check(servers[0], server2Videos[0].uuid, false)
93 it('Should have video in blacklists', async function () {
94 const body = await servers[0].blacklistCommand.list()
96 const videoBlacklists = body.data
97 expect(videoBlacklists).to.have.lengthOf(1)
98 expect(videoBlacklists[0].reason).to.contains('Automatically blocked from auto block plugin')
99 expect(videoBlacklists[0].video.name).to.equal(server2Videos[0].name)
102 it('Should not block a local video', async function () {
105 await check(servers[0], server1Videos[0].uuid, true)
107 blocklistServer.replace({
110 value: server1Videos[0].url
117 await check(servers[0], server1Videos[0].uuid, true)
120 it('Should remove a video block', async function () {
123 await check(servers[0], server2Videos[0].uuid, false)
125 blocklistServer.replace({
128 value: server2Videos[0].url,
136 await check(servers[0], server2Videos[0].uuid, true)
139 it('Should auto block a video, manually unblock it and do not reblock it automatically', async function () {
142 const video = server2Videos[1]
144 await check(servers[0], video.uuid, true)
146 blocklistServer.replace({
150 updatedAt: new Date().toISOString()
157 await check(servers[0], video.uuid, false)
159 await servers[0].blacklistCommand.remove({ videoId: video.uuid })
161 await check(servers[0], video.uuid, true)
163 await killallServers([ servers[0] ])
164 await reRunServer(servers[0])
167 await check(servers[0], video.uuid, true)
170 after(async function () {
171 await blocklistServer.terminate()
173 await cleanupTests(servers)