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