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