Fix misc tests
[oweals/peertube.git] / server / tests / api / server / config.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { About } from '../../../../shared/models/server/about.model'
6 import { CustomConfig } from '../../../../shared/models/server/custom-config.model'
7 import {
8   cleanupTests,
9   deleteCustomConfig,
10   flushAndRunServer,
11   getAbout,
12   getConfig,
13   getCustomConfig,
14   killallServers, parallelTests,
15   registerUser,
16   reRunServer, ServerInfo,
17   setAccessTokensToServers,
18   updateCustomConfig, uploadVideo
19 } from '../../../../shared/extra-utils'
20 import { ServerConfig } from '../../../../shared/models'
21
22 const expect = chai.expect
23
24 function checkInitialConfig (server: ServerInfo, data: CustomConfig) {
25   expect(data.instance.name).to.equal('PeerTube')
26   expect(data.instance.shortDescription).to.equal(
27     'PeerTube, a federated (ActivityPub) video streaming platform using P2P (BitTorrent) directly in the web browser ' +
28     'with WebTorrent and Angular.'
29   )
30   expect(data.instance.description).to.equal('Welcome to this PeerTube instance!')
31
32   expect(data.instance.terms).to.equal('No terms for now.')
33   expect(data.instance.creationReason).to.be.empty
34   expect(data.instance.codeOfConduct).to.be.empty
35   expect(data.instance.moderationInformation).to.be.empty
36   expect(data.instance.administrator).to.be.empty
37   expect(data.instance.maintenanceLifetime).to.be.empty
38   expect(data.instance.businessModel).to.be.empty
39   expect(data.instance.hardwareInformation).to.be.empty
40
41   expect(data.instance.languages).to.have.lengthOf(0)
42   expect(data.instance.categories).to.have.lengthOf(0)
43
44   expect(data.instance.defaultClientRoute).to.equal('/videos/trending')
45   expect(data.instance.isNSFW).to.be.false
46   expect(data.instance.defaultNSFWPolicy).to.equal('display')
47   expect(data.instance.customizations.css).to.be.empty
48   expect(data.instance.customizations.javascript).to.be.empty
49
50   expect(data.services.twitter.username).to.equal('@Chocobozzz')
51   expect(data.services.twitter.whitelisted).to.be.false
52
53   expect(data.cache.previews.size).to.equal(1)
54   expect(data.cache.captions.size).to.equal(1)
55
56   expect(data.signup.enabled).to.be.true
57   expect(data.signup.limit).to.equal(4)
58   expect(data.signup.requiresEmailVerification).to.be.false
59
60   expect(data.admin.email).to.equal('admin' + server.internalServerNumber + '@example.com')
61   expect(data.contactForm.enabled).to.be.true
62
63   expect(data.user.videoQuota).to.equal(5242880)
64   expect(data.user.videoQuotaDaily).to.equal(-1)
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.resolutions['240p']).to.be.true
70   expect(data.transcoding.resolutions['360p']).to.be.true
71   expect(data.transcoding.resolutions['480p']).to.be.true
72   expect(data.transcoding.resolutions['720p']).to.be.true
73   expect(data.transcoding.resolutions['1080p']).to.be.true
74   expect(data.transcoding.resolutions['2160p']).to.be.true
75   expect(data.transcoding.hls.enabled).to.be.true
76
77   expect(data.import.videos.http.enabled).to.be.true
78   expect(data.import.videos.torrent.enabled).to.be.true
79   expect(data.autoBlacklist.videos.ofUsers.enabled).to.be.false
80
81   expect(data.followers.instance.enabled).to.be.true
82   expect(data.followers.instance.manualApproval).to.be.false
83
84   expect(data.followings.instance.autoFollowBack.enabled).to.be.false
85   expect(data.followings.instance.autoFollowIndex.enabled).to.be.false
86   expect(data.followings.instance.autoFollowIndex.indexUrl).to.equal('https://instances.joinpeertube.org')
87 }
88
89 function checkUpdatedConfig (data: CustomConfig) {
90   expect(data.instance.name).to.equal('PeerTube updated')
91   expect(data.instance.shortDescription).to.equal('my short description')
92   expect(data.instance.description).to.equal('my super description')
93
94   expect(data.instance.terms).to.equal('my super terms')
95   expect(data.instance.creationReason).to.equal('my super creation reason')
96   expect(data.instance.codeOfConduct).to.equal('my super coc')
97   expect(data.instance.moderationInformation).to.equal('my super moderation information')
98   expect(data.instance.administrator).to.equal('Kuja')
99   expect(data.instance.maintenanceLifetime).to.equal('forever')
100   expect(data.instance.businessModel).to.equal('my super business model')
101   expect(data.instance.hardwareInformation).to.equal('2vCore 3GB RAM')
102
103   expect(data.instance.languages).to.deep.equal([ 'en', 'es' ])
104   expect(data.instance.categories).to.deep.equal([ 1, 2 ])
105
106   expect(data.instance.defaultClientRoute).to.equal('/videos/recently-added')
107   expect(data.instance.isNSFW).to.be.true
108   expect(data.instance.defaultNSFWPolicy).to.equal('blur')
109   expect(data.instance.customizations.javascript).to.equal('alert("coucou")')
110   expect(data.instance.customizations.css).to.equal('body { background-color: red; }')
111
112   expect(data.services.twitter.username).to.equal('@Kuja')
113   expect(data.services.twitter.whitelisted).to.be.true
114
115   expect(data.cache.previews.size).to.equal(2)
116   expect(data.cache.captions.size).to.equal(3)
117
118   expect(data.signup.enabled).to.be.false
119   expect(data.signup.limit).to.equal(5)
120   expect(data.signup.requiresEmailVerification).to.be.false
121
122   // We override admin email in parallel tests, so skip this exception
123   if (parallelTests() === false) {
124     expect(data.admin.email).to.equal('superadmin1@example.com')
125   }
126
127   expect(data.contactForm.enabled).to.be.false
128
129   expect(data.user.videoQuota).to.equal(5242881)
130   expect(data.user.videoQuotaDaily).to.equal(318742)
131
132   expect(data.transcoding.enabled).to.be.true
133   expect(data.transcoding.threads).to.equal(1)
134   expect(data.transcoding.allowAdditionalExtensions).to.be.true
135   expect(data.transcoding.allowAudioFiles).to.be.true
136   expect(data.transcoding.resolutions['240p']).to.be.false
137   expect(data.transcoding.resolutions['360p']).to.be.true
138   expect(data.transcoding.resolutions['480p']).to.be.true
139   expect(data.transcoding.resolutions['720p']).to.be.false
140   expect(data.transcoding.resolutions['1080p']).to.be.false
141   expect(data.transcoding.resolutions['2160p']).to.be.false
142   expect(data.transcoding.hls.enabled).to.be.false
143
144   expect(data.import.videos.http.enabled).to.be.false
145   expect(data.import.videos.torrent.enabled).to.be.false
146   expect(data.autoBlacklist.videos.ofUsers.enabled).to.be.true
147
148   expect(data.followers.instance.enabled).to.be.false
149   expect(data.followers.instance.manualApproval).to.be.true
150
151   expect(data.followings.instance.autoFollowBack.enabled).to.be.true
152   expect(data.followings.instance.autoFollowIndex.enabled).to.be.true
153   expect(data.followings.instance.autoFollowIndex.indexUrl).to.equal('https://updated.example.com')
154 }
155
156 describe('Test config', function () {
157   let server = null
158
159   before(async function () {
160     this.timeout(30000)
161
162     server = await flushAndRunServer(1)
163     await setAccessTokensToServers([ server ])
164   })
165
166   it('Should have a correct config on a server with registration enabled', async function () {
167     const res = await getConfig(server.url)
168     const data: ServerConfig = res.body
169
170     expect(data.signup.allowed).to.be.true
171   })
172
173   it('Should have a correct config on a server with registration enabled and a users limit', async function () {
174     this.timeout(5000)
175
176     await Promise.all([
177       registerUser(server.url, 'user1', 'super password'),
178       registerUser(server.url, 'user2', 'super password'),
179       registerUser(server.url, 'user3', 'super password')
180     ])
181
182     const res = await getConfig(server.url)
183     const data: ServerConfig = res.body
184
185     expect(data.signup.allowed).to.be.false
186   })
187
188   it('Should have the correct video allowed extensions', async function () {
189     const res = await getConfig(server.url)
190     const data: ServerConfig = res.body
191
192     expect(data.video.file.extensions).to.have.lengthOf(3)
193     expect(data.video.file.extensions).to.contain('.mp4')
194     expect(data.video.file.extensions).to.contain('.webm')
195     expect(data.video.file.extensions).to.contain('.ogv')
196
197     await uploadVideo(server.url, server.accessToken, { fixture: 'video_short.mkv' }, 400)
198     await uploadVideo(server.url, server.accessToken, { fixture: 'sample.ogg' }, 400)
199
200     expect(data.contactForm.enabled).to.be.true
201   })
202
203   it('Should get the customized configuration', async function () {
204     const res = await getCustomConfig(server.url, server.accessToken)
205     const data = res.body as CustomConfig
206
207     checkInitialConfig(server, data)
208   })
209
210   it('Should update the customized configuration', async function () {
211     const newCustomConfig: CustomConfig = {
212       instance: {
213         name: 'PeerTube updated',
214         shortDescription: 'my short description',
215         description: 'my super description',
216         terms: 'my super terms',
217         codeOfConduct: 'my super coc',
218
219         creationReason: 'my super creation reason',
220         moderationInformation: 'my super moderation information',
221         administrator: 'Kuja',
222         maintenanceLifetime: 'forever',
223         businessModel: 'my super business model',
224         hardwareInformation: '2vCore 3GB RAM',
225
226         languages: [ 'en', 'es' ],
227         categories: [ 1, 2 ],
228
229         defaultClientRoute: '/videos/recently-added',
230         isNSFW: true,
231         defaultNSFWPolicy: 'blur' as 'blur',
232         customizations: {
233           javascript: 'alert("coucou")',
234           css: 'body { background-color: red; }'
235         }
236       },
237       theme: {
238         default: 'default'
239       },
240       services: {
241         twitter: {
242           username: '@Kuja',
243           whitelisted: true
244         }
245       },
246       cache: {
247         previews: {
248           size: 2
249         },
250         captions: {
251           size: 3
252         }
253       },
254       signup: {
255         enabled: false,
256         limit: 5,
257         requiresEmailVerification: false
258       },
259       admin: {
260         email: 'superadmin1@example.com'
261       },
262       contactForm: {
263         enabled: false
264       },
265       user: {
266         videoQuota: 5242881,
267         videoQuotaDaily: 318742
268       },
269       transcoding: {
270         enabled: true,
271         allowAdditionalExtensions: true,
272         allowAudioFiles: true,
273         threads: 1,
274         resolutions: {
275           '240p': false,
276           '360p': true,
277           '480p': true,
278           '720p': false,
279           '1080p': false,
280           '2160p': false
281         },
282         hls: {
283           enabled: false
284         }
285       },
286       import: {
287         videos: {
288           http: {
289             enabled: false
290           },
291           torrent: {
292             enabled: false
293           }
294         }
295       },
296       autoBlacklist: {
297         videos: {
298           ofUsers: {
299             enabled: true
300           }
301         }
302       },
303       followers: {
304         instance: {
305           enabled: false,
306           manualApproval: true
307         }
308       },
309       followings: {
310         instance: {
311           autoFollowBack: {
312             enabled: true
313           },
314           autoFollowIndex: {
315             enabled: true,
316             indexUrl: 'https://updated.example.com'
317           }
318         }
319       }
320     }
321     await updateCustomConfig(server.url, server.accessToken, newCustomConfig)
322
323     const res = await getCustomConfig(server.url, server.accessToken)
324     const data = res.body
325
326     checkUpdatedConfig(data)
327   })
328
329   it('Should have the correct updated video allowed extensions', async function () {
330     const res = await getConfig(server.url)
331     const data: ServerConfig = res.body
332
333     expect(data.video.file.extensions).to.have.length.above(3)
334     expect(data.video.file.extensions).to.contain('.mp4')
335     expect(data.video.file.extensions).to.contain('.webm')
336     expect(data.video.file.extensions).to.contain('.ogv')
337     expect(data.video.file.extensions).to.contain('.flv')
338     expect(data.video.file.extensions).to.contain('.mkv')
339     expect(data.video.file.extensions).to.contain('.mp3')
340     expect(data.video.file.extensions).to.contain('.ogg')
341     expect(data.video.file.extensions).to.contain('.flac')
342
343     await uploadVideo(server.url, server.accessToken, { fixture: 'video_short.mkv' }, 200)
344     await uploadVideo(server.url, server.accessToken, { fixture: 'sample.ogg' }, 200)
345   })
346
347   it('Should have the configuration updated after a restart', async function () {
348     this.timeout(10000)
349
350     killallServers([ server ])
351
352     await reRunServer(server)
353
354     const res = await getCustomConfig(server.url, server.accessToken)
355     const data = res.body
356
357     checkUpdatedConfig(data)
358   })
359
360   it('Should fetch the about information', async function () {
361     const res = await getAbout(server.url)
362     const data: About = res.body
363
364     expect(data.instance.name).to.equal('PeerTube updated')
365     expect(data.instance.shortDescription).to.equal('my short description')
366     expect(data.instance.description).to.equal('my super description')
367     expect(data.instance.terms).to.equal('my super terms')
368     expect(data.instance.codeOfConduct).to.equal('my super coc')
369
370     expect(data.instance.creationReason).to.equal('my super creation reason')
371     expect(data.instance.moderationInformation).to.equal('my super moderation information')
372     expect(data.instance.administrator).to.equal('Kuja')
373     expect(data.instance.maintenanceLifetime).to.equal('forever')
374     expect(data.instance.businessModel).to.equal('my super business model')
375     expect(data.instance.hardwareInformation).to.equal('2vCore 3GB RAM')
376
377     expect(data.instance.languages).to.deep.equal([ 'en', 'es' ])
378     expect(data.instance.categories).to.deep.equal([ 1, 2 ])
379   })
380
381   it('Should remove the custom configuration', async function () {
382     this.timeout(10000)
383
384     await deleteCustomConfig(server.url, server.accessToken)
385
386     const res = await getCustomConfig(server.url, server.accessToken)
387     const data = res.body
388
389     checkInitialConfig(server, data)
390   })
391
392   after(async function () {
393     await cleanupTests([ server ])
394   })
395 })