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