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