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