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