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