]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/config.ts
Introduce server commands
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / config.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '@shared/core-utils'
6 import {
7 cleanupTests,
8 flushAndRunServer,
9 killallServers,
10 makeGetRequest,
11 parallelTests,
12 registerUser,
13 reRunServer,
14 ServerInfo,
15 setAccessTokensToServers,
16 uploadVideo
17 } from '@shared/extra-utils'
18 import { CustomConfig } from '@shared/models'
19
20 const expect = chai.expect
21
22 function checkInitialConfig (server: ServerInfo, data: CustomConfig) {
23 expect(data.instance.name).to.equal('PeerTube')
24 expect(data.instance.shortDescription).to.equal(
25 'PeerTube, an ActivityPub-federated video streaming platform using P2P directly in your web browser.'
26 )
27 expect(data.instance.description).to.equal('Welcome to this PeerTube instance!')
28
29 expect(data.instance.terms).to.equal('No terms for now.')
30 expect(data.instance.creationReason).to.be.empty
31 expect(data.instance.codeOfConduct).to.be.empty
32 expect(data.instance.moderationInformation).to.be.empty
33 expect(data.instance.administrator).to.be.empty
34 expect(data.instance.maintenanceLifetime).to.be.empty
35 expect(data.instance.businessModel).to.be.empty
36 expect(data.instance.hardwareInformation).to.be.empty
37
38 expect(data.instance.languages).to.have.lengthOf(0)
39 expect(data.instance.categories).to.have.lengthOf(0)
40
41 expect(data.instance.defaultClientRoute).to.equal('/videos/trending')
42 expect(data.instance.isNSFW).to.be.false
43 expect(data.instance.defaultNSFWPolicy).to.equal('display')
44 expect(data.instance.customizations.css).to.be.empty
45 expect(data.instance.customizations.javascript).to.be.empty
46
47 expect(data.services.twitter.username).to.equal('@Chocobozzz')
48 expect(data.services.twitter.whitelisted).to.be.false
49
50 expect(data.cache.previews.size).to.equal(1)
51 expect(data.cache.captions.size).to.equal(1)
52 expect(data.cache.torrents.size).to.equal(1)
53
54 expect(data.signup.enabled).to.be.true
55 expect(data.signup.limit).to.equal(4)
56 expect(data.signup.minimumAge).to.equal(16)
57 expect(data.signup.requiresEmailVerification).to.be.false
58
59 expect(data.admin.email).to.equal('admin' + server.internalServerNumber + '@example.com')
60 expect(data.contactForm.enabled).to.be.true
61
62 expect(data.user.videoQuota).to.equal(5242880)
63 expect(data.user.videoQuotaDaily).to.equal(-1)
64
65 expect(data.transcoding.enabled).to.be.false
66 expect(data.transcoding.allowAdditionalExtensions).to.be.false
67 expect(data.transcoding.allowAudioFiles).to.be.false
68 expect(data.transcoding.threads).to.equal(2)
69 expect(data.transcoding.concurrency).to.equal(2)
70 expect(data.transcoding.profile).to.equal('default')
71 expect(data.transcoding.resolutions['240p']).to.be.true
72 expect(data.transcoding.resolutions['360p']).to.be.true
73 expect(data.transcoding.resolutions['480p']).to.be.true
74 expect(data.transcoding.resolutions['720p']).to.be.true
75 expect(data.transcoding.resolutions['1080p']).to.be.true
76 expect(data.transcoding.resolutions['1440p']).to.be.true
77 expect(data.transcoding.resolutions['2160p']).to.be.true
78 expect(data.transcoding.webtorrent.enabled).to.be.true
79 expect(data.transcoding.hls.enabled).to.be.true
80
81 expect(data.live.enabled).to.be.false
82 expect(data.live.allowReplay).to.be.false
83 expect(data.live.maxDuration).to.equal(-1)
84 expect(data.live.maxInstanceLives).to.equal(20)
85 expect(data.live.maxUserLives).to.equal(3)
86 expect(data.live.transcoding.enabled).to.be.false
87 expect(data.live.transcoding.threads).to.equal(2)
88 expect(data.live.transcoding.profile).to.equal('default')
89 expect(data.live.transcoding.resolutions['240p']).to.be.false
90 expect(data.live.transcoding.resolutions['360p']).to.be.false
91 expect(data.live.transcoding.resolutions['480p']).to.be.false
92 expect(data.live.transcoding.resolutions['720p']).to.be.false
93 expect(data.live.transcoding.resolutions['1080p']).to.be.false
94 expect(data.live.transcoding.resolutions['1440p']).to.be.false
95 expect(data.live.transcoding.resolutions['2160p']).to.be.false
96
97 expect(data.import.videos.concurrency).to.equal(2)
98 expect(data.import.videos.http.enabled).to.be.true
99 expect(data.import.videos.torrent.enabled).to.be.true
100 expect(data.autoBlacklist.videos.ofUsers.enabled).to.be.false
101
102 expect(data.followers.instance.enabled).to.be.true
103 expect(data.followers.instance.manualApproval).to.be.false
104
105 expect(data.followings.instance.autoFollowBack.enabled).to.be.false
106 expect(data.followings.instance.autoFollowIndex.enabled).to.be.false
107 expect(data.followings.instance.autoFollowIndex.indexUrl).to.equal('')
108
109 expect(data.broadcastMessage.enabled).to.be.false
110 expect(data.broadcastMessage.level).to.equal('info')
111 expect(data.broadcastMessage.message).to.equal('')
112 expect(data.broadcastMessage.dismissable).to.be.false
113 }
114
115 function checkUpdatedConfig (data: CustomConfig) {
116 expect(data.instance.name).to.equal('PeerTube updated')
117 expect(data.instance.shortDescription).to.equal('my short description')
118 expect(data.instance.description).to.equal('my super description')
119
120 expect(data.instance.terms).to.equal('my super terms')
121 expect(data.instance.creationReason).to.equal('my super creation reason')
122 expect(data.instance.codeOfConduct).to.equal('my super coc')
123 expect(data.instance.moderationInformation).to.equal('my super moderation information')
124 expect(data.instance.administrator).to.equal('Kuja')
125 expect(data.instance.maintenanceLifetime).to.equal('forever')
126 expect(data.instance.businessModel).to.equal('my super business model')
127 expect(data.instance.hardwareInformation).to.equal('2vCore 3GB RAM')
128
129 expect(data.instance.languages).to.deep.equal([ 'en', 'es' ])
130 expect(data.instance.categories).to.deep.equal([ 1, 2 ])
131
132 expect(data.instance.defaultClientRoute).to.equal('/videos/recently-added')
133 expect(data.instance.isNSFW).to.be.true
134 expect(data.instance.defaultNSFWPolicy).to.equal('blur')
135 expect(data.instance.customizations.javascript).to.equal('alert("coucou")')
136 expect(data.instance.customizations.css).to.equal('body { background-color: red; }')
137
138 expect(data.services.twitter.username).to.equal('@Kuja')
139 expect(data.services.twitter.whitelisted).to.be.true
140
141 expect(data.cache.previews.size).to.equal(2)
142 expect(data.cache.captions.size).to.equal(3)
143 expect(data.cache.torrents.size).to.equal(4)
144
145 expect(data.signup.enabled).to.be.false
146 expect(data.signup.limit).to.equal(5)
147 expect(data.signup.requiresEmailVerification).to.be.false
148 expect(data.signup.minimumAge).to.equal(10)
149
150 // We override admin email in parallel tests, so skip this exception
151 if (parallelTests() === false) {
152 expect(data.admin.email).to.equal('superadmin1@example.com')
153 }
154
155 expect(data.contactForm.enabled).to.be.false
156
157 expect(data.user.videoQuota).to.equal(5242881)
158 expect(data.user.videoQuotaDaily).to.equal(318742)
159
160 expect(data.transcoding.enabled).to.be.true
161 expect(data.transcoding.threads).to.equal(1)
162 expect(data.transcoding.concurrency).to.equal(3)
163 expect(data.transcoding.allowAdditionalExtensions).to.be.true
164 expect(data.transcoding.allowAudioFiles).to.be.true
165 expect(data.transcoding.profile).to.equal('vod_profile')
166 expect(data.transcoding.resolutions['240p']).to.be.false
167 expect(data.transcoding.resolutions['360p']).to.be.true
168 expect(data.transcoding.resolutions['480p']).to.be.true
169 expect(data.transcoding.resolutions['720p']).to.be.false
170 expect(data.transcoding.resolutions['1080p']).to.be.false
171 expect(data.transcoding.resolutions['2160p']).to.be.false
172 expect(data.transcoding.hls.enabled).to.be.false
173 expect(data.transcoding.webtorrent.enabled).to.be.true
174
175 expect(data.live.enabled).to.be.true
176 expect(data.live.allowReplay).to.be.true
177 expect(data.live.maxDuration).to.equal(5000)
178 expect(data.live.maxInstanceLives).to.equal(-1)
179 expect(data.live.maxUserLives).to.equal(10)
180 expect(data.live.transcoding.enabled).to.be.true
181 expect(data.live.transcoding.threads).to.equal(4)
182 expect(data.live.transcoding.profile).to.equal('live_profile')
183 expect(data.live.transcoding.resolutions['240p']).to.be.true
184 expect(data.live.transcoding.resolutions['360p']).to.be.true
185 expect(data.live.transcoding.resolutions['480p']).to.be.true
186 expect(data.live.transcoding.resolutions['720p']).to.be.true
187 expect(data.live.transcoding.resolutions['1080p']).to.be.true
188 expect(data.live.transcoding.resolutions['2160p']).to.be.true
189
190 expect(data.import.videos.concurrency).to.equal(4)
191 expect(data.import.videos.http.enabled).to.be.false
192 expect(data.import.videos.torrent.enabled).to.be.false
193 expect(data.autoBlacklist.videos.ofUsers.enabled).to.be.true
194
195 expect(data.followers.instance.enabled).to.be.false
196 expect(data.followers.instance.manualApproval).to.be.true
197
198 expect(data.followings.instance.autoFollowBack.enabled).to.be.true
199 expect(data.followings.instance.autoFollowIndex.enabled).to.be.true
200 expect(data.followings.instance.autoFollowIndex.indexUrl).to.equal('https://updated.example.com')
201
202 expect(data.broadcastMessage.enabled).to.be.true
203 expect(data.broadcastMessage.level).to.equal('error')
204 expect(data.broadcastMessage.message).to.equal('super bad message')
205 expect(data.broadcastMessage.dismissable).to.be.true
206 }
207
208 describe('Test config', function () {
209 let server: ServerInfo = null
210
211 before(async function () {
212 this.timeout(30000)
213
214 server = await flushAndRunServer(1)
215 await setAccessTokensToServers([ server ])
216 })
217
218 it('Should have a correct config on a server with registration enabled', async function () {
219 const data = await server.configCommand.getConfig()
220
221 expect(data.signup.allowed).to.be.true
222 })
223
224 it('Should have a correct config on a server with registration enabled and a users limit', async function () {
225 this.timeout(5000)
226
227 await Promise.all([
228 registerUser(server.url, 'user1', 'super password'),
229 registerUser(server.url, 'user2', 'super password'),
230 registerUser(server.url, 'user3', 'super password')
231 ])
232
233 const data = await server.configCommand.getConfig()
234
235 expect(data.signup.allowed).to.be.false
236 })
237
238 it('Should have the correct video allowed extensions', async function () {
239 const data = await server.configCommand.getConfig()
240
241 expect(data.video.file.extensions).to.have.lengthOf(3)
242 expect(data.video.file.extensions).to.contain('.mp4')
243 expect(data.video.file.extensions).to.contain('.webm')
244 expect(data.video.file.extensions).to.contain('.ogv')
245
246 await uploadVideo(server.url, server.accessToken, { fixture: 'video_short.mkv' }, HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415)
247 await uploadVideo(server.url, server.accessToken, { fixture: 'sample.ogg' }, HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415)
248
249 expect(data.contactForm.enabled).to.be.true
250 })
251
252 it('Should get the customized configuration', async function () {
253 const data = await server.configCommand.getCustomConfig()
254
255 checkInitialConfig(server, data)
256 })
257
258 it('Should update the customized configuration', async function () {
259 const newCustomConfig: CustomConfig = {
260 instance: {
261 name: 'PeerTube updated',
262 shortDescription: 'my short description',
263 description: 'my super description',
264 terms: 'my super terms',
265 codeOfConduct: 'my super coc',
266
267 creationReason: 'my super creation reason',
268 moderationInformation: 'my super moderation information',
269 administrator: 'Kuja',
270 maintenanceLifetime: 'forever',
271 businessModel: 'my super business model',
272 hardwareInformation: '2vCore 3GB RAM',
273
274 languages: [ 'en', 'es' ],
275 categories: [ 1, 2 ],
276
277 isNSFW: true,
278 defaultNSFWPolicy: 'blur' as 'blur',
279
280 defaultClientRoute: '/videos/recently-added',
281
282 customizations: {
283 javascript: 'alert("coucou")',
284 css: 'body { background-color: red; }'
285 }
286 },
287 theme: {
288 default: 'default'
289 },
290 services: {
291 twitter: {
292 username: '@Kuja',
293 whitelisted: true
294 }
295 },
296 cache: {
297 previews: {
298 size: 2
299 },
300 captions: {
301 size: 3
302 },
303 torrents: {
304 size: 4
305 }
306 },
307 signup: {
308 enabled: false,
309 limit: 5,
310 requiresEmailVerification: false,
311 minimumAge: 10
312 },
313 admin: {
314 email: 'superadmin1@example.com'
315 },
316 contactForm: {
317 enabled: false
318 },
319 user: {
320 videoQuota: 5242881,
321 videoQuotaDaily: 318742
322 },
323 transcoding: {
324 enabled: true,
325 allowAdditionalExtensions: true,
326 allowAudioFiles: true,
327 threads: 1,
328 concurrency: 3,
329 profile: 'vod_profile',
330 resolutions: {
331 '0p': false,
332 '240p': false,
333 '360p': true,
334 '480p': true,
335 '720p': false,
336 '1080p': false,
337 '1440p': false,
338 '2160p': false
339 },
340 webtorrent: {
341 enabled: true
342 },
343 hls: {
344 enabled: false
345 }
346 },
347 live: {
348 enabled: true,
349 allowReplay: true,
350 maxDuration: 5000,
351 maxInstanceLives: -1,
352 maxUserLives: 10,
353 transcoding: {
354 enabled: true,
355 threads: 4,
356 profile: 'live_profile',
357 resolutions: {
358 '240p': true,
359 '360p': true,
360 '480p': true,
361 '720p': true,
362 '1080p': true,
363 '1440p': true,
364 '2160p': true
365 }
366 }
367 },
368 import: {
369 videos: {
370 concurrency: 4,
371 http: {
372 enabled: false
373 },
374 torrent: {
375 enabled: false
376 }
377 }
378 },
379 trending: {
380 videos: {
381 algorithms: {
382 enabled: [ 'best', 'hot', 'most-viewed', 'most-liked' ],
383 default: 'hot'
384 }
385 }
386 },
387 autoBlacklist: {
388 videos: {
389 ofUsers: {
390 enabled: true
391 }
392 }
393 },
394 followers: {
395 instance: {
396 enabled: false,
397 manualApproval: true
398 }
399 },
400 followings: {
401 instance: {
402 autoFollowBack: {
403 enabled: true
404 },
405 autoFollowIndex: {
406 enabled: true,
407 indexUrl: 'https://updated.example.com'
408 }
409 }
410 },
411 broadcastMessage: {
412 enabled: true,
413 level: 'error',
414 message: 'super bad message',
415 dismissable: true
416 },
417 search: {
418 remoteUri: {
419 anonymous: true,
420 users: true
421 },
422 searchIndex: {
423 enabled: true,
424 url: 'https://search.joinpeertube.org',
425 disableLocalSearch: true,
426 isDefaultSearch: true
427 }
428 }
429 }
430 await server.configCommand.updateCustomConfig({ newCustomConfig })
431
432 const data = await server.configCommand.getCustomConfig()
433 checkUpdatedConfig(data)
434 })
435
436 it('Should have the correct updated video allowed extensions', async function () {
437 this.timeout(10000)
438
439 const data = await server.configCommand.getConfig()
440
441 expect(data.video.file.extensions).to.have.length.above(4)
442 expect(data.video.file.extensions).to.contain('.mp4')
443 expect(data.video.file.extensions).to.contain('.webm')
444 expect(data.video.file.extensions).to.contain('.ogv')
445 expect(data.video.file.extensions).to.contain('.flv')
446 expect(data.video.file.extensions).to.contain('.wmv')
447 expect(data.video.file.extensions).to.contain('.mkv')
448 expect(data.video.file.extensions).to.contain('.mp3')
449 expect(data.video.file.extensions).to.contain('.ogg')
450 expect(data.video.file.extensions).to.contain('.flac')
451
452 await uploadVideo(server.url, server.accessToken, { fixture: 'video_short.mkv' }, HttpStatusCode.OK_200)
453 await uploadVideo(server.url, server.accessToken, { fixture: 'sample.ogg' }, HttpStatusCode.OK_200)
454 })
455
456 it('Should have the configuration updated after a restart', async function () {
457 this.timeout(10000)
458
459 await killallServers([ server ])
460
461 await reRunServer(server)
462
463 const data = await server.configCommand.getCustomConfig()
464
465 checkUpdatedConfig(data)
466 })
467
468 it('Should fetch the about information', async function () {
469 const data = await server.configCommand.getAbout()
470
471 expect(data.instance.name).to.equal('PeerTube updated')
472 expect(data.instance.shortDescription).to.equal('my short description')
473 expect(data.instance.description).to.equal('my super description')
474 expect(data.instance.terms).to.equal('my super terms')
475 expect(data.instance.codeOfConduct).to.equal('my super coc')
476
477 expect(data.instance.creationReason).to.equal('my super creation reason')
478 expect(data.instance.moderationInformation).to.equal('my super moderation information')
479 expect(data.instance.administrator).to.equal('Kuja')
480 expect(data.instance.maintenanceLifetime).to.equal('forever')
481 expect(data.instance.businessModel).to.equal('my super business model')
482 expect(data.instance.hardwareInformation).to.equal('2vCore 3GB RAM')
483
484 expect(data.instance.languages).to.deep.equal([ 'en', 'es' ])
485 expect(data.instance.categories).to.deep.equal([ 1, 2 ])
486 })
487
488 it('Should remove the custom configuration', async function () {
489 this.timeout(10000)
490
491 await server.configCommand.deleteCustomConfig()
492
493 const data = await server.configCommand.getCustomConfig()
494 checkInitialConfig(server, data)
495 })
496
497 it('Should enable frameguard', async function () {
498 this.timeout(25000)
499
500 {
501 const res = await makeGetRequest({
502 url: server.url,
503 path: '/api/v1/config',
504 statusCodeExpected: 200
505 })
506
507 expect(res.headers['x-frame-options']).to.exist
508 }
509
510 await killallServers([ server ])
511
512 const config = {
513 security: {
514 frameguard: { enabled: false }
515 }
516 }
517 server = await reRunServer(server, config)
518
519 {
520 const res = await makeGetRequest({
521 url: server.url,
522 path: '/api/v1/config',
523 statusCodeExpected: 200
524 })
525
526 expect(res.headers['x-frame-options']).to.not.exist
527 }
528 })
529
530 after(async function () {
531 await cleanupTests([ server ])
532 })
533 })