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