]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/plugins/plugin-helpers.ts
Merge branch 'release/5.0.0' into develop
[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(servers[0].host + ' 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 ${servers[0].url}`)
65 })
66
67 it('Should have the correct listening config', async function () {
68 const res = await makeGetRequest({
69 url: servers[0].url,
70 path: '/plugins/test-four/router/server-listening-config',
71 expectedStatus: HttpStatusCode.OK_200
72 })
73
74 expect(res.body.config).to.exist
75 expect(res.body.config.hostname).to.equal('::')
76 expect(res.body.config.port).to.equal(servers[0].port)
77 })
78
79 it('Should have the correct config', async function () {
80 const res = await makeGetRequest({
81 url: servers[0].url,
82 path: '/plugins/test-four/router/server-config',
83 expectedStatus: HttpStatusCode.OK_200
84 })
85
86 expect(res.body.serverConfig).to.exist
87 expect(res.body.serverConfig.instance.name).to.equal('PeerTube')
88 })
89 })
90
91 describe('Server', function () {
92
93 it('Should get the server actor', async function () {
94 await servers[0].servers.waitUntilLog('server actor name is peertube')
95 })
96 })
97
98 describe('Socket', function () {
99
100 it('Should sendNotification without any exceptions', async () => {
101 const user = await servers[0].users.create({ username: 'notis_redding', password: 'secret1234?' })
102 await makePostBodyRequest({
103 url: servers[0].url,
104 path: '/plugins/test-four/router/send-notification',
105 fields: {
106 userId: user.id
107 },
108 expectedStatus: HttpStatusCode.CREATED_201
109 })
110 })
111
112 it('Should sendVideoLiveNewState without any exceptions', async () => {
113 const res = await servers[0].videos.quickUpload({ name: 'video server 1' })
114
115 await makePostBodyRequest({
116 url: servers[0].url,
117 path: '/plugins/test-four/router/send-video-live-new-state/' + res.uuid,
118 expectedStatus: HttpStatusCode.CREATED_201
119 })
120
121 await servers[0].videos.remove({ id: res.uuid })
122 })
123 })
124
125 describe('Plugin', function () {
126
127 it('Should get the base static route', async function () {
128 const res = await makeGetRequest({
129 url: servers[0].url,
130 path: '/plugins/test-four/router/static-route',
131 expectedStatus: HttpStatusCode.OK_200
132 })
133
134 expect(res.body.staticRoute).to.equal('/plugins/test-four/0.0.1/static/')
135 })
136
137 it('Should get the base static route', async function () {
138 const baseRouter = '/plugins/test-four/0.0.1/router/'
139
140 const res = await makeGetRequest({
141 url: servers[0].url,
142 path: baseRouter + 'router-route',
143 expectedStatus: HttpStatusCode.OK_200
144 })
145
146 expect(res.body.routerRoute).to.equal(baseRouter)
147 })
148 })
149
150 describe('User', function () {
151 let rootId: number
152
153 it('Should not get a user if not authenticated', async function () {
154 await makeGetRequest({
155 url: servers[0].url,
156 path: '/plugins/test-four/router/user',
157 expectedStatus: HttpStatusCode.NOT_FOUND_404
158 })
159 })
160
161 it('Should get a user if authenticated', async function () {
162 const res = await makeGetRequest({
163 url: servers[0].url,
164 token: servers[0].accessToken,
165 path: '/plugins/test-four/router/user',
166 expectedStatus: HttpStatusCode.OK_200
167 })
168
169 expect(res.body.username).to.equal('root')
170 expect(res.body.displayName).to.equal('root')
171 expect(res.body.isAdmin).to.be.true
172 expect(res.body.isModerator).to.be.false
173 expect(res.body.isUser).to.be.false
174
175 rootId = res.body.id
176 })
177
178 it('Should load a user by id', async function () {
179 {
180 const res = await makeGetRequest({
181 url: servers[0].url,
182 path: '/plugins/test-four/router/user/' + rootId,
183 expectedStatus: HttpStatusCode.OK_200
184 })
185
186 expect(res.body.username).to.equal('root')
187 }
188
189 {
190 await makeGetRequest({
191 url: servers[0].url,
192 path: '/plugins/test-four/router/user/42',
193 expectedStatus: HttpStatusCode.NOT_FOUND_404
194 })
195 }
196 })
197 })
198
199 describe('Moderation', function () {
200 let videoUUIDServer1: string
201
202 before(async function () {
203 this.timeout(60000)
204
205 {
206 const res = await servers[0].videos.quickUpload({ name: 'video server 1' })
207 videoUUIDServer1 = res.uuid
208 }
209
210 {
211 await servers[1].videos.quickUpload({ name: 'video server 2' })
212 }
213
214 await waitJobs(servers)
215
216 const { data } = await servers[0].videos.list()
217
218 expect(data).to.have.lengthOf(2)
219 })
220
221 it('Should mute server 2', async function () {
222 this.timeout(10000)
223 await postCommand(servers[0], 'blockServer', { hostToBlock: servers[1].host })
224
225 const { data } = await servers[0].videos.list()
226
227 expect(data).to.have.lengthOf(1)
228 expect(data[0].name).to.equal('video server 1')
229 })
230
231 it('Should unmute server 2', async function () {
232 await postCommand(servers[0], 'unblockServer', { hostToUnblock: servers[1].host })
233
234 const { data } = await servers[0].videos.list()
235
236 expect(data).to.have.lengthOf(2)
237 })
238
239 it('Should mute account of server 2', async function () {
240 await postCommand(servers[0], 'blockAccount', { handleToBlock: `root@${servers[1].host}` })
241
242 const { data } = await servers[0].videos.list()
243
244 expect(data).to.have.lengthOf(1)
245 expect(data[0].name).to.equal('video server 1')
246 })
247
248 it('Should unmute account of server 2', async function () {
249 await postCommand(servers[0], 'unblockAccount', { handleToUnblock: `root@${servers[1].host}` })
250
251 const { data } = await servers[0].videos.list()
252
253 expect(data).to.have.lengthOf(2)
254 })
255
256 it('Should blacklist video', async function () {
257 this.timeout(10000)
258
259 await postCommand(servers[0], 'blacklist', { videoUUID: videoUUIDServer1, unfederate: true })
260
261 await waitJobs(servers)
262
263 for (const server of servers) {
264 const { data } = await server.videos.list()
265
266 expect(data).to.have.lengthOf(1)
267 expect(data[0].name).to.equal('video server 2')
268 }
269 })
270
271 it('Should unblacklist video', async function () {
272 this.timeout(10000)
273
274 await postCommand(servers[0], 'unblacklist', { videoUUID: videoUUIDServer1 })
275
276 await waitJobs(servers)
277
278 for (const server of servers) {
279 const { data } = await server.videos.list()
280
281 expect(data).to.have.lengthOf(2)
282 }
283 })
284 })
285
286 describe('Videos', function () {
287 let videoUUID: string
288 let videoPath: string
289
290 before(async () => {
291 this.timeout(240000)
292
293 await servers[0].config.enableTranscoding()
294
295 const res = await servers[0].videos.quickUpload({ name: 'video1' })
296 videoUUID = res.uuid
297
298 await waitJobs(servers)
299 })
300
301 it('Should get video files', async function () {
302 const { body } = await makeGetRequest({
303 url: servers[0].url,
304 path: '/plugins/test-four/router/video-files/' + videoUUID,
305 expectedStatus: HttpStatusCode.OK_200
306 })
307
308 // Video files check
309 {
310 expect(body.webtorrent.videoFiles).to.be.an('array')
311 expect(body.hls.videoFiles).to.be.an('array')
312
313 for (const resolution of [ 144, 240, 360, 480, 720 ]) {
314 for (const files of [ body.webtorrent.videoFiles, body.hls.videoFiles ]) {
315 const file = files.find(f => f.resolution === resolution)
316 expect(file).to.exist
317
318 expect(file.size).to.be.a('number')
319 expect(file.fps).to.equal(25)
320
321 expect(await pathExists(file.path)).to.be.true
322 await makeRawRequest({ url: file.url, expectedStatus: HttpStatusCode.OK_200 })
323 }
324 }
325
326 videoPath = body.webtorrent.videoFiles[0].path
327 }
328
329 // Thumbnails check
330 {
331 expect(body.thumbnails).to.be.an('array')
332
333 const miniature = body.thumbnails.find(t => t.type === ThumbnailType.MINIATURE)
334 expect(miniature).to.exist
335 expect(await pathExists(miniature.path)).to.be.true
336 await makeRawRequest({ url: miniature.url, expectedStatus: HttpStatusCode.OK_200 })
337
338 const preview = body.thumbnails.find(t => t.type === ThumbnailType.PREVIEW)
339 expect(preview).to.exist
340 expect(await pathExists(preview.path)).to.be.true
341 await makeRawRequest({ url: preview.url, expectedStatus: HttpStatusCode.OK_200 })
342 }
343 })
344
345 it('Should probe a file', async function () {
346 const { body } = await makeGetRequest({
347 url: servers[0].url,
348 path: '/plugins/test-four/router/ffprobe',
349 query: {
350 path: videoPath
351 },
352 expectedStatus: HttpStatusCode.OK_200
353 })
354
355 expect(body.streams).to.be.an('array')
356 expect(body.streams).to.have.lengthOf(2)
357 })
358
359 it('Should remove a video after a view', async function () {
360 this.timeout(40000)
361
362 // Should not throw -> video exists
363 const video = await servers[0].videos.get({ id: videoUUID })
364 // Should delete the video
365 await servers[0].views.simulateView({ id: videoUUID })
366
367 await servers[0].servers.waitUntilLog('Video deleted by plugin four.')
368
369 try {
370 // Should throw because the video should have been deleted
371 await servers[0].videos.get({ id: videoUUID })
372 throw new Error('Video exists')
373 } catch (err) {
374 if (err.message.includes('exists')) throw err
375 }
376
377 await checkVideoFilesWereRemoved({ server: servers[0], video })
378 })
379
380 it('Should have fetched the video by URL', async function () {
381 await servers[0].servers.waitUntilLog(`video from DB uuid is ${videoUUID}`)
382 })
383 })
384
385 after(async function () {
386 await cleanupTests(servers)
387 })
388 })