1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
5 import { Video, VideoBlacklist } from '@shared/models'
8 getBlacklistedVideosList,
12 removeVideoFromBlacklist,
13 setAccessTokensToServers,
17 } from '../../../shared/extra-utils'
20 flushAndRunMultipleServers,
24 } from '../../../shared/extra-utils/server/servers'
26 async function check (server: ServerInfo, videoUUID: string, exists = true) {
27 const res = await getVideosList(server.url)
29 const video = res.body.data.find(v => v.uuid === videoUUID)
32 expect(video).to.not.be.undefined
34 expect(video).to.be.undefined
38 describe('Official plugin auto-block videos', function () {
39 let servers: ServerInfo[]
40 let blocklistServer: MockBlocklist
41 let server1Videos: Video[] = []
42 let server2Videos: Video[] = []
44 before(async function () {
47 servers = await flushAndRunMultipleServers(2)
48 await setAccessTokensToServers(servers)
50 for (const server of servers) {
53 accessToken: server.accessToken,
54 npmName: 'peertube-plugin-auto-block-videos'
58 blocklistServer = new MockBlocklist()
59 await blocklistServer.initialize()
61 await uploadVideoAndGetId({ server: servers[0], videoName: 'video server 1' })
62 await uploadVideoAndGetId({ server: servers[1], videoName: 'video server 2' })
63 await uploadVideoAndGetId({ server: servers[1], videoName: 'video 2 server 2' })
64 await uploadVideoAndGetId({ server: servers[1], videoName: 'video 3 server 2' })
67 const res = await getVideosList(servers[0].url)
68 server1Videos = res.body.data.map(v => Object.assign(v, { url: servers[0].url + '/videos/watch/' + v.uuid }))
72 const res = await getVideosList(servers[1].url)
73 server2Videos = res.body.data.map(v => Object.assign(v, { url: servers[1].url + '/videos/watch/' + v.uuid }))
76 await doubleFollow(servers[0], servers[1])
79 it('Should update plugin settings', async function () {
80 await updatePluginSettings({
82 accessToken: servers[0].accessToken,
83 npmName: 'peertube-plugin-auto-block-videos',
85 'blocklist-urls': 'http://localhost:42100/blocklist',
86 'check-seconds-interval': 1
91 it('Should auto block a video', async function () {
94 await check(servers[0], server2Videos[0].uuid, true)
96 blocklistServer.replace({
99 value: server2Videos[0].url
106 await check(servers[0], server2Videos[0].uuid, false)
109 it('Should have video in blacklists', async function () {
110 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken })
112 const videoBlacklists = res.body.data as VideoBlacklist[]
114 expect(videoBlacklists).to.have.lengthOf(1)
115 expect(videoBlacklists[0].reason).to.contains('Automatically blocked from auto block plugin')
116 expect(videoBlacklists[0].video.name).to.equal(server2Videos[0].name)
119 it('Should not block a local video', async function () {
122 await check(servers[0], server1Videos[0].uuid, true)
124 blocklistServer.replace({
127 value: server1Videos[0].url
134 await check(servers[0], server1Videos[0].uuid, true)
137 it('Should remove a video block', async function () {
140 await check(servers[0], server2Videos[0].uuid, false)
142 blocklistServer.replace({
145 value: server2Videos[0].url,
153 await check(servers[0], server2Videos[0].uuid, true)
156 it('Should auto block a video, manually unblock it and do not reblock it automatically', async function () {
159 const video = server2Videos[1]
161 await check(servers[0], video.uuid, true)
163 blocklistServer.replace({
167 updatedAt: new Date().toISOString()
174 await check(servers[0], video.uuid, false)
176 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, video.uuid)
178 await check(servers[0], video.uuid, true)
180 killallServers([ servers[0] ])
181 await reRunServer(servers[0])
184 await check(servers[0], video.uuid, true)
187 after(async function () {
188 await blocklistServer.terminate()
190 await cleanupTests(servers)