]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/plugins/plugin-helpers.ts
26f66b0b13958f5947fd2927dde2a10511067240
[github/Chocobozzz/PeerTube.git] / server / tests / plugins / plugin-helpers.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import { expect } from 'chai'
5 import { pathExists } from 'fs-extra'
6 import {
7 checkVideoFilesWereRemoved,
8 cleanupTests,
9 createMultipleServers,
10 doubleFollow,
11 makeGetRequest,
12 makePostBodyRequest,
13 makeRawRequest,
14 PeerTubeServer,
15 PluginsCommand,
16 setAccessTokensToServers,
17 waitJobs
18 } from '@shared/extra-utils'
19 import { HttpStatusCode, ThumbnailType } from '@shared/models'
20
21 function postCommand (server: PeerTubeServer, command: string, bodyArg?: object) {
22 const body = { command }
23 if (bodyArg) Object.assign(body, bodyArg)
24
25 return makePostBodyRequest({
26 url: server.url,
27 path: '/plugins/test-four/router/commander',
28 fields: body,
29 expectedStatus: HttpStatusCode.NO_CONTENT_204
30 })
31 }
32
33 describe('Test plugin helpers', function () {
34 let servers: PeerTubeServer[]
35
36 before(async function () {
37 this.timeout(60000)
38
39 servers = await createMultipleServers(2)
40 await setAccessTokensToServers(servers)
41
42 await doubleFollow(servers[0], servers[1])
43
44 await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath('-four') })
45 })
46
47 describe('Logger', function () {
48
49 it('Should have logged things', async function () {
50 await servers[0].servers.waitUntilLog('localhost:' + servers[0].port + ' peertube-plugin-test-four', 1, false)
51 await servers[0].servers.waitUntilLog('Hello world from plugin four', 1)
52 })
53 })
54
55 describe('Database', function () {
56
57 it('Should have made a query', async function () {
58 await servers[0].servers.waitUntilLog(`root email is admin${servers[0].internalServerNumber}@example.com`)
59 })
60 })
61
62 describe('Config', function () {
63
64 it('Should have the correct webserver url', async function () {
65 await servers[0].servers.waitUntilLog(`server url is http://localhost:${servers[0].port}`)
66 })
67
68 it('Should have the correct config', async function () {
69 const res = await makeGetRequest({
70 url: servers[0].url,
71 path: '/plugins/test-four/router/server-config',
72 expectedStatus: HttpStatusCode.OK_200
73 })
74
75 expect(res.body.serverConfig).to.exist
76 expect(res.body.serverConfig.instance.name).to.equal('PeerTube')
77 })
78 })
79
80 describe('Server', function () {
81
82 it('Should get the server actor', async function () {
83 await servers[0].servers.waitUntilLog('server actor name is peertube')
84 })
85 })
86
87 describe('Plugin', function () {
88
89 it('Should get the base static route', async function () {
90 const res = await makeGetRequest({
91 url: servers[0].url,
92 path: '/plugins/test-four/router/static-route',
93 expectedStatus: HttpStatusCode.OK_200
94 })
95
96 expect(res.body.staticRoute).to.equal('/plugins/test-four/0.0.1/static/')
97 })
98
99 it('Should get the base static route', async function () {
100 const baseRouter = '/plugins/test-four/0.0.1/router/'
101
102 const res = await makeGetRequest({
103 url: servers[0].url,
104 path: baseRouter + 'router-route',
105 expectedStatus: HttpStatusCode.OK_200
106 })
107
108 expect(res.body.routerRoute).to.equal(baseRouter)
109 })
110 })
111
112 describe('User', function () {
113
114 it('Should not get a user if not authenticated', async function () {
115 await makeGetRequest({
116 url: servers[0].url,
117 path: '/plugins/test-four/router/user',
118 expectedStatus: HttpStatusCode.NOT_FOUND_404
119 })
120 })
121
122 it('Should get a user if authenticated', async function () {
123 const res = await makeGetRequest({
124 url: servers[0].url,
125 token: servers[0].accessToken,
126 path: '/plugins/test-four/router/user',
127 expectedStatus: HttpStatusCode.OK_200
128 })
129
130 expect(res.body.username).to.equal('root')
131 expect(res.body.displayName).to.equal('root')
132 expect(res.body.isAdmin).to.be.true
133 expect(res.body.isModerator).to.be.false
134 expect(res.body.isUser).to.be.false
135 })
136 })
137
138 describe('Moderation', function () {
139 let videoUUIDServer1: string
140
141 before(async function () {
142 this.timeout(60000)
143
144 {
145 const res = await servers[0].videos.quickUpload({ name: 'video server 1' })
146 videoUUIDServer1 = res.uuid
147 }
148
149 {
150 await servers[1].videos.quickUpload({ name: 'video server 2' })
151 }
152
153 await waitJobs(servers)
154
155 const { data } = await servers[0].videos.list()
156
157 expect(data).to.have.lengthOf(2)
158 })
159
160 it('Should mute server 2', async function () {
161 this.timeout(10000)
162 await postCommand(servers[0], 'blockServer', { hostToBlock: `localhost:${servers[1].port}` })
163
164 const { data } = await servers[0].videos.list()
165
166 expect(data).to.have.lengthOf(1)
167 expect(data[0].name).to.equal('video server 1')
168 })
169
170 it('Should unmute server 2', async function () {
171 await postCommand(servers[0], 'unblockServer', { hostToUnblock: `localhost:${servers[1].port}` })
172
173 const { data } = await servers[0].videos.list()
174
175 expect(data).to.have.lengthOf(2)
176 })
177
178 it('Should mute account of server 2', async function () {
179 await postCommand(servers[0], 'blockAccount', { handleToBlock: `root@localhost:${servers[1].port}` })
180
181 const { data } = await servers[0].videos.list()
182
183 expect(data).to.have.lengthOf(1)
184 expect(data[0].name).to.equal('video server 1')
185 })
186
187 it('Should unmute account of server 2', async function () {
188 await postCommand(servers[0], 'unblockAccount', { handleToUnblock: `root@localhost:${servers[1].port}` })
189
190 const { data } = await servers[0].videos.list()
191
192 expect(data).to.have.lengthOf(2)
193 })
194
195 it('Should blacklist video', async function () {
196 this.timeout(10000)
197
198 await postCommand(servers[0], 'blacklist', { videoUUID: videoUUIDServer1, unfederate: true })
199
200 await waitJobs(servers)
201
202 for (const server of servers) {
203 const { data } = await server.videos.list()
204
205 expect(data).to.have.lengthOf(1)
206 expect(data[0].name).to.equal('video server 2')
207 }
208 })
209
210 it('Should unblacklist video', async function () {
211 this.timeout(10000)
212
213 await postCommand(servers[0], 'unblacklist', { videoUUID: videoUUIDServer1 })
214
215 await waitJobs(servers)
216
217 for (const server of servers) {
218 const { data } = await server.videos.list()
219
220 expect(data).to.have.lengthOf(2)
221 }
222 })
223 })
224
225 describe('Videos', function () {
226 let videoUUID: string
227
228 before(async () => {
229 this.timeout(240000)
230
231 await servers[0].config.enableTranscoding()
232
233 const res = await servers[0].videos.quickUpload({ name: 'video1' })
234 videoUUID = res.uuid
235
236 await waitJobs(servers)
237 })
238
239 it('Should get video files', async function () {
240 const { body } = await makeGetRequest({
241 url: servers[0].url,
242 path: '/plugins/test-four/router/video-files/' + videoUUID,
243 expectedStatus: HttpStatusCode.OK_200
244 })
245
246 // Video files check
247 {
248 expect(body.webtorrent.videoFiles).to.be.an('array')
249 expect(body.hls.videoFiles).to.be.an('array')
250
251 for (const resolution of [ 144, 240, 360, 480, 720 ]) {
252 for (const files of [ body.webtorrent.videoFiles, body.hls.videoFiles ]) {
253 const file = files.find(f => f.resolution === resolution)
254 expect(file).to.exist
255
256 expect(file.size).to.be.a('number')
257 expect(file.fps).to.equal(25)
258
259 expect(await pathExists(file.path)).to.be.true
260 await makeRawRequest(file.url, HttpStatusCode.OK_200)
261 }
262 }
263 }
264
265 // Thumbnails check
266 {
267 expect(body.thumbnails).to.be.an('array')
268
269 const miniature = body.thumbnails.find(t => t.type === ThumbnailType.MINIATURE)
270 expect(miniature).to.exist
271 expect(await pathExists(miniature.path)).to.be.true
272 await makeRawRequest(miniature.url, HttpStatusCode.OK_200)
273
274 const preview = body.thumbnails.find(t => t.type === ThumbnailType.PREVIEW)
275 expect(preview).to.exist
276 expect(await pathExists(preview.path)).to.be.true
277 await makeRawRequest(preview.url, HttpStatusCode.OK_200)
278 }
279 })
280
281 it('Should remove a video after a view', async function () {
282 this.timeout(40000)
283
284 // Should not throw -> video exists
285 const video = await servers[0].videos.get({ id: videoUUID })
286 // Should delete the video
287 await servers[0].videos.view({ id: videoUUID })
288
289 await servers[0].servers.waitUntilLog('Video deleted by plugin four.')
290
291 try {
292 // Should throw because the video should have been deleted
293 await servers[0].videos.get({ id: videoUUID })
294 throw new Error('Video exists')
295 } catch (err) {
296 if (err.message.includes('exists')) throw err
297 }
298
299 await checkVideoFilesWereRemoved({ server: servers[0], video })
300 })
301
302 it('Should have fetched the video by URL', async function () {
303 await servers[0].servers.waitUntilLog(`video from DB uuid is ${videoUUID}`)
304 })
305 })
306
307 after(async function () {
308 await cleanupTests(servers)
309 })
310 })