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