Add ability to add custom css/javascript
[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/config/about.model'
6 import { CustomConfig } from '../../../../shared/models/config/custom-config.model'
7 import { deleteCustomConfig, getAbout, killallServers, reRunServer } from '../../utils'
8 const expect = chai.expect
9
10 import {
11   getConfig,
12   flushTests,
13   runServer,
14   registerUser, getCustomConfig, setAccessTokensToServers, updateCustomConfig
15 } from '../../utils/index'
16
17 describe('Test config', function () {
18   let server = null
19
20   before(async function () {
21     this.timeout(30000)
22
23     await flushTests()
24     server = await runServer(1)
25     await setAccessTokensToServers([ server ])
26   })
27
28   it('Should have a correct config on a server with registration enabled', async function () {
29     const res = await getConfig(server.url)
30     const data = res.body
31
32     expect(data.signup.allowed).to.be.true
33   })
34
35   it('Should have a correct config on a server with registration enabled and a users limit', async function () {
36     this.timeout(5000)
37
38     await Promise.all([
39       registerUser(server.url, 'user1', 'super password'),
40       registerUser(server.url, 'user2', 'super password'),
41       registerUser(server.url, 'user3', 'super password')
42     ])
43
44     const res = await getConfig(server.url)
45     const data = res.body
46
47     expect(data.signup.allowed).to.be.false
48   })
49
50   it('Should get the customized configuration', async function () {
51     const res = await getCustomConfig(server.url, server.accessToken)
52     const data = res.body as CustomConfig
53
54     expect(data.instance.name).to.equal('PeerTube')
55     expect(data.instance.description).to.equal('Welcome to this PeerTube instance!')
56     expect(data.instance.terms).to.equal('No terms for now.')
57     expect(data.instance.customizations.css).to.be.empty
58     expect(data.instance.customizations.javascript).to.be.empty
59     expect(data.cache.previews.size).to.equal(1)
60     expect(data.signup.enabled).to.be.true
61     expect(data.signup.limit).to.equal(4)
62     expect(data.admin.email).to.equal('admin1@example.com')
63     expect(data.user.videoQuota).to.equal(5242880)
64     expect(data.transcoding.enabled).to.be.false
65     expect(data.transcoding.threads).to.equal(2)
66     expect(data.transcoding.resolutions['240p']).to.be.true
67     expect(data.transcoding.resolutions['360p']).to.be.true
68     expect(data.transcoding.resolutions['480p']).to.be.true
69     expect(data.transcoding.resolutions['720p']).to.be.true
70     expect(data.transcoding.resolutions['1080p']).to.be.true
71   })
72
73   it('Should update the customized configuration', async function () {
74     const newCustomConfig = {
75       instance: {
76         name: 'PeerTube updated',
77         description: 'my super description',
78         terms: 'my super terms',
79         customizations: {
80           javascript: 'alert("coucou")',
81           css: 'body { background-color: red; }'
82         }
83       },
84       cache: {
85         previews: {
86           size: 2
87         }
88       },
89       signup: {
90         enabled: false,
91         limit: 5
92       },
93       admin: {
94         email: 'superadmin1@example.com'
95       },
96       user: {
97         videoQuota: 5242881
98       },
99       transcoding: {
100         enabled: true,
101         threads: 1,
102         resolutions: {
103           '240p': false,
104           '360p': true,
105           '480p': true,
106           '720p': false,
107           '1080p': false
108         }
109       }
110     }
111     await updateCustomConfig(server.url, server.accessToken, newCustomConfig)
112
113     const res = await getCustomConfig(server.url, server.accessToken)
114     const data = res.body
115
116     expect(data.instance.name).to.equal('PeerTube updated')
117     expect(data.instance.description).to.equal('my super description')
118     expect(data.instance.terms).to.equal('my super terms')
119     expect(data.instance.customizations.javascript).to.equal('alert("coucou")')
120     expect(data.instance.customizations.css).to.equal('body { background-color: red; }')
121     expect(data.cache.previews.size).to.equal(2)
122     expect(data.signup.enabled).to.be.false
123     expect(data.signup.limit).to.equal(5)
124     expect(data.admin.email).to.equal('superadmin1@example.com')
125     expect(data.user.videoQuota).to.equal(5242881)
126     expect(data.transcoding.enabled).to.be.true
127     expect(data.transcoding.threads).to.equal(1)
128     expect(data.transcoding.resolutions['240p']).to.be.false
129     expect(data.transcoding.resolutions['360p']).to.be.true
130     expect(data.transcoding.resolutions['480p']).to.be.true
131     expect(data.transcoding.resolutions['720p']).to.be.false
132     expect(data.transcoding.resolutions['1080p']).to.be.false
133   })
134
135   it('Should have the configuration updated after a restart', async function () {
136     this.timeout(10000)
137
138     killallServers([ server ])
139
140     await reRunServer(server)
141
142     const res = await getCustomConfig(server.url, server.accessToken)
143     const data = res.body
144
145     expect(data.instance.name).to.equal('PeerTube updated')
146     expect(data.instance.description).to.equal('my super description')
147     expect(data.instance.terms).to.equal('my super terms')
148     expect(data.instance.customizations.javascript).to.equal('alert("coucou")')
149     expect(data.instance.customizations.css).to.equal('body { background-color: red; }')
150     expect(data.cache.previews.size).to.equal(2)
151     expect(data.signup.enabled).to.be.false
152     expect(data.signup.limit).to.equal(5)
153     expect(data.admin.email).to.equal('superadmin1@example.com')
154     expect(data.user.videoQuota).to.equal(5242881)
155     expect(data.transcoding.enabled).to.be.true
156     expect(data.transcoding.threads).to.equal(1)
157     expect(data.transcoding.resolutions['240p']).to.be.false
158     expect(data.transcoding.resolutions['360p']).to.be.true
159     expect(data.transcoding.resolutions['480p']).to.be.true
160     expect(data.transcoding.resolutions['720p']).to.be.false
161     expect(data.transcoding.resolutions['1080p']).to.be.false
162   })
163
164   it('Should fetch the about information', async function () {
165     const res = await getAbout(server.url)
166     const data: About = res.body
167
168     expect(data.instance.name).to.equal('PeerTube updated')
169     expect(data.instance.description).to.equal('my super description')
170     expect(data.instance.terms).to.equal('my super terms')
171   })
172
173   it('Should remove the custom configuration', async function () {
174     this.timeout(10000)
175
176     await deleteCustomConfig(server.url, server.accessToken)
177
178     const res = await getCustomConfig(server.url, server.accessToken)
179     const data = res.body
180
181     expect(data.instance.name).to.equal('PeerTube')
182     expect(data.instance.description).to.equal('Welcome to this PeerTube instance!')
183     expect(data.instance.terms).to.equal('No terms for now.')
184     expect(data.instance.customizations.css).to.be.empty
185     expect(data.instance.customizations.javascript).to.be.empty
186     expect(data.cache.previews.size).to.equal(1)
187     expect(data.signup.enabled).to.be.true
188     expect(data.signup.limit).to.equal(4)
189     expect(data.admin.email).to.equal('admin1@example.com')
190     expect(data.user.videoQuota).to.equal(5242880)
191     expect(data.transcoding.enabled).to.be.false
192     expect(data.transcoding.threads).to.equal(2)
193     expect(data.transcoding.resolutions['240p']).to.be.true
194     expect(data.transcoding.resolutions['360p']).to.be.true
195     expect(data.transcoding.resolutions['480p']).to.be.true
196     expect(data.transcoding.resolutions['720p']).to.be.true
197     expect(data.transcoding.resolutions['1080p']).to.be.true
198   })
199
200   after(async function () {
201     process.kill(-server.app.pid)
202
203     // Keep the logs if the test failed
204     if (this['ok']) {
205       await flushTests()
206     }
207   })
208 })