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