1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { wait } from '@shared/core-utils'
5 import { Video } from '@shared/models'
12 setAccessTokensToServers
13 } from '@shared/server-commands'
14 import { MockBlocklist } from '../shared'
16 async function check (server: PeerTubeServer, videoUUID: string, exists = true) {
17 const { data } = await server.videos.list()
19 const video = data.find(v => v.uuid === videoUUID)
21 if (exists) expect(video).to.not.be.undefined
22 else expect(video).to.be.undefined
25 describe('Official plugin auto-block videos', function () {
26 let servers: PeerTubeServer[]
27 let blocklistServer: MockBlocklist
28 let server1Videos: Video[] = []
29 let server2Videos: Video[] = []
32 before(async function () {
35 servers = await createMultipleServers(2)
36 await setAccessTokensToServers(servers)
38 for (const server of servers) {
39 await server.plugins.install({ npmName: 'peertube-plugin-auto-block-videos' })
42 blocklistServer = new MockBlocklist()
43 port = await blocklistServer.initialize()
45 await servers[0].videos.quickUpload({ name: 'video server 1' })
46 await servers[1].videos.quickUpload({ name: 'video server 2' })
47 await servers[1].videos.quickUpload({ name: 'video 2 server 2' })
48 await servers[1].videos.quickUpload({ name: 'video 3 server 2' })
51 const { data } = await servers[0].videos.list()
52 server1Videos = data.map(v => Object.assign(v, { url: servers[0].url + '/videos/watch/' + v.uuid }))
56 const { data } = await servers[1].videos.list()
57 server2Videos = data.map(v => Object.assign(v, { url: servers[1].url + '/videos/watch/' + v.uuid }))
60 await doubleFollow(servers[0], servers[1])
63 it('Should update plugin settings', async function () {
64 await servers[0].plugins.updateSettings({
65 npmName: 'peertube-plugin-auto-block-videos',
67 'blocklist-urls': `http://127.0.0.1:${port}/blocklist`,
68 'check-seconds-interval': 1
73 it('Should auto block a video', async function () {
76 await check(servers[0], server2Videos[0].uuid, true)
78 blocklistServer.replace({
81 value: server2Videos[0].url
88 await check(servers[0], server2Videos[0].uuid, false)
91 it('Should have video in blacklists', async function () {
92 const body = await servers[0].blacklist.list()
94 const videoBlacklists = body.data
95 expect(videoBlacklists).to.have.lengthOf(1)
96 expect(videoBlacklists[0].reason).to.contains('Automatically blocked from auto block plugin')
97 expect(videoBlacklists[0].video.name).to.equal(server2Videos[0].name)
100 it('Should not block a local video', async function () {
103 await check(servers[0], server1Videos[0].uuid, true)
105 blocklistServer.replace({
108 value: server1Videos[0].url
115 await check(servers[0], server1Videos[0].uuid, true)
118 it('Should remove a video block', async function () {
121 await check(servers[0], server2Videos[0].uuid, false)
123 blocklistServer.replace({
126 value: server2Videos[0].url,
134 await check(servers[0], server2Videos[0].uuid, true)
137 it('Should auto block a video, manually unblock it and do not reblock it automatically', async function () {
140 const video = server2Videos[1]
142 await check(servers[0], video.uuid, true)
144 blocklistServer.replace({
148 updatedAt: new Date().toISOString()
155 await check(servers[0], video.uuid, false)
157 await servers[0].blacklist.remove({ videoId: video.uuid })
159 await check(servers[0], video.uuid, true)
161 await killallServers([ servers[0] ])
162 await servers[0].run()
165 await check(servers[0], video.uuid, true)
168 after(async function () {
169 await blocklistServer.terminate()
171 await cleanupTests(servers)