]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/external-plugins/auto-block-videos.ts
6baf3756600b08d0d45c10015311eaf2d258d2a3
[github/Chocobozzz/PeerTube.git] / server / tests / external-plugins / auto-block-videos.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import { expect } from 'chai'
5 import { Video, VideoBlacklist } from '@shared/models'
6 import {
7 doubleFollow,
8 getBlacklistedVideosList,
9 getVideosList,
10 MockBlocklist,
11 removeVideoFromBlacklist,
12 setAccessTokensToServers,
13 uploadVideoAndGetId,
14 wait
15 } from '../../../shared/extra-utils'
16 import {
17 cleanupTests,
18 flushAndRunMultipleServers,
19 killallServers,
20 reRunServer,
21 ServerInfo
22 } from '../../../shared/extra-utils/server/servers'
23
24 async function check (server: ServerInfo, videoUUID: string, exists = true) {
25 const res = await getVideosList(server.url)
26
27 const video = res.body.data.find(v => v.uuid === videoUUID)
28
29 if (exists) expect(video).to.not.be.undefined
30 else expect(video).to.be.undefined
31 }
32
33 describe('Official plugin auto-block videos', function () {
34 let servers: ServerInfo[]
35 let blocklistServer: MockBlocklist
36 let server1Videos: Video[] = []
37 let server2Videos: Video[] = []
38 let port: number
39
40 before(async function () {
41 this.timeout(60000)
42
43 servers = await flushAndRunMultipleServers(2)
44 await setAccessTokensToServers(servers)
45
46 for (const server of servers) {
47 await server.pluginsCommand.install({ npmName: 'peertube-plugin-auto-block-videos' })
48 }
49
50 blocklistServer = new MockBlocklist()
51 port = await blocklistServer.initialize()
52
53 await uploadVideoAndGetId({ server: servers[0], videoName: 'video server 1' })
54 await uploadVideoAndGetId({ server: servers[1], videoName: 'video server 2' })
55 await uploadVideoAndGetId({ server: servers[1], videoName: 'video 2 server 2' })
56 await uploadVideoAndGetId({ server: servers[1], videoName: 'video 3 server 2' })
57
58 {
59 const res = await getVideosList(servers[0].url)
60 server1Videos = res.body.data.map(v => Object.assign(v, { url: servers[0].url + '/videos/watch/' + v.uuid }))
61 }
62
63 {
64 const res = await getVideosList(servers[1].url)
65 server2Videos = res.body.data.map(v => Object.assign(v, { url: servers[1].url + '/videos/watch/' + v.uuid }))
66 }
67
68 await doubleFollow(servers[0], servers[1])
69 })
70
71 it('Should update plugin settings', async function () {
72 await servers[0].pluginsCommand.updateSettings({
73 npmName: 'peertube-plugin-auto-block-videos',
74 settings: {
75 'blocklist-urls': `http://localhost:${port}/blocklist`,
76 'check-seconds-interval': 1
77 }
78 })
79 })
80
81 it('Should auto block a video', async function () {
82 this.timeout(10000)
83
84 await check(servers[0], server2Videos[0].uuid, true)
85
86 blocklistServer.replace({
87 data: [
88 {
89 value: server2Videos[0].url
90 }
91 ]
92 })
93
94 await wait(2000)
95
96 await check(servers[0], server2Videos[0].uuid, false)
97 })
98
99 it('Should have video in blacklists', async function () {
100 const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken })
101
102 const videoBlacklists = res.body.data as VideoBlacklist[]
103
104 expect(videoBlacklists).to.have.lengthOf(1)
105 expect(videoBlacklists[0].reason).to.contains('Automatically blocked from auto block plugin')
106 expect(videoBlacklists[0].video.name).to.equal(server2Videos[0].name)
107 })
108
109 it('Should not block a local video', async function () {
110 this.timeout(10000)
111
112 await check(servers[0], server1Videos[0].uuid, true)
113
114 blocklistServer.replace({
115 data: [
116 {
117 value: server1Videos[0].url
118 }
119 ]
120 })
121
122 await wait(2000)
123
124 await check(servers[0], server1Videos[0].uuid, true)
125 })
126
127 it('Should remove a video block', async function () {
128 this.timeout(10000)
129
130 await check(servers[0], server2Videos[0].uuid, false)
131
132 blocklistServer.replace({
133 data: [
134 {
135 value: server2Videos[0].url,
136 action: 'remove'
137 }
138 ]
139 })
140
141 await wait(2000)
142
143 await check(servers[0], server2Videos[0].uuid, true)
144 })
145
146 it('Should auto block a video, manually unblock it and do not reblock it automatically', async function () {
147 this.timeout(20000)
148
149 const video = server2Videos[1]
150
151 await check(servers[0], video.uuid, true)
152
153 blocklistServer.replace({
154 data: [
155 {
156 value: video.url,
157 updatedAt: new Date().toISOString()
158 }
159 ]
160 })
161
162 await wait(2000)
163
164 await check(servers[0], video.uuid, false)
165
166 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, video.uuid)
167
168 await check(servers[0], video.uuid, true)
169
170 killallServers([ servers[0] ])
171 await reRunServer(servers[0])
172 await wait(2000)
173
174 await check(servers[0], video.uuid, true)
175 })
176
177 after(async function () {
178 await blocklistServer.terminate()
179
180 await cleanupTests(servers)
181 })
182 })