Merge branch 'develop' into release/v1.2.0
[oweals/peertube.git] / server / tests / api / check-params / config.ts
1 /* tslint:disable:no-unused-expression */
2
3 import { omit } from 'lodash'
4 import 'mocha'
5 import { CustomConfig } from '../../../../shared/models/server/custom-config.model'
6
7 import {
8   createUser, flushTests, killallServers, makeDeleteRequest, makeGetRequest, makePutBodyRequest, runServer, ServerInfo,
9   setAccessTokensToServers, userLogin, immutableAssign
10 } from '../../../../shared/utils'
11
12 describe('Test config API validators', function () {
13   const path = '/api/v1/config/custom'
14   let server: ServerInfo
15   let userAccessToken: string
16   const updateParams: CustomConfig = {
17     instance: {
18       name: 'PeerTube updated',
19       shortDescription: 'my short description',
20       description: 'my super description',
21       terms: 'my super terms',
22       defaultClientRoute: '/videos/recently-added',
23       defaultNSFWPolicy: 'blur',
24       customizations: {
25         javascript: 'alert("coucou")',
26         css: 'body { background-color: red; }'
27       }
28     },
29     services: {
30       twitter: {
31         username: '@MySuperUsername',
32         whitelisted: true
33       }
34     },
35     cache: {
36       previews: {
37         size: 2
38       },
39       captions: {
40         size: 3
41       }
42     },
43     signup: {
44       enabled: false,
45       limit: 5,
46       requiresEmailVerification: false
47     },
48     admin: {
49       email: 'superadmin1@example.com'
50     },
51     contactForm: {
52       enabled: false
53     },
54     user: {
55       videoQuota: 5242881,
56       videoQuotaDaily: 318742
57     },
58     transcoding: {
59       enabled: true,
60       allowAdditionalExtensions: true,
61       threads: 1,
62       resolutions: {
63         '240p': false,
64         '360p': true,
65         '480p': true,
66         '720p': false,
67         '1080p': false
68       }
69     },
70     import: {
71       videos: {
72         http: {
73           enabled: false
74         },
75         torrent: {
76           enabled: false
77         }
78       }
79     }
80   }
81
82   // ---------------------------------------------------------------
83
84   before(async function () {
85     this.timeout(30000)
86
87     await flushTests()
88     server = await runServer(1)
89
90     await setAccessTokensToServers([ server ])
91
92     const user = {
93       username: 'user1',
94       password: 'password'
95     }
96     await createUser(server.url, server.accessToken, user.username, user.password)
97     userAccessToken = await userLogin(server, user)
98   })
99
100   describe('When getting the configuration', function () {
101     it('Should fail without token', async function () {
102       await makeGetRequest({
103         url: server.url,
104         path,
105         statusCodeExpected: 401
106       })
107     })
108
109     it('Should fail if the user is not an administrator', async function () {
110       await makeGetRequest({
111         url: server.url,
112         path,
113         token: userAccessToken,
114         statusCodeExpected: 403
115       })
116     })
117   })
118
119   describe('When updating the configuration', function () {
120     it('Should fail without token', async function () {
121       await makePutBodyRequest({
122         url: server.url,
123         path,
124         fields: updateParams,
125         statusCodeExpected: 401
126       })
127     })
128
129     it('Should fail if the user is not an administrator', async function () {
130       await makePutBodyRequest({
131         url: server.url,
132         path,
133         fields: updateParams,
134         token: userAccessToken,
135         statusCodeExpected: 403
136       })
137     })
138
139     it('Should fail if it misses a key', async function () {
140       const newUpdateParams = omit(updateParams, 'admin.email')
141
142       await makePutBodyRequest({
143         url: server.url,
144         path,
145         fields: newUpdateParams,
146         token: server.accessToken,
147         statusCodeExpected: 400
148       })
149     })
150
151     it('Should fail with a bad default NSFW policy', async function () {
152       const newUpdateParams = immutableAssign(updateParams, {
153         instance: {
154           defaultNSFWPolicy: 'hello'
155         }
156       })
157
158       await makePutBodyRequest({
159         url: server.url,
160         path,
161         fields: newUpdateParams,
162         token: server.accessToken,
163         statusCodeExpected: 400
164       })
165     })
166
167     it('Should success with the correct parameters', async function () {
168       await makePutBodyRequest({
169         url: server.url,
170         path,
171         fields: updateParams,
172         token: server.accessToken,
173         statusCodeExpected: 200
174       })
175     })
176   })
177
178   describe('When deleting the configuration', function () {
179     it('Should fail without token', async function () {
180       await makeDeleteRequest({
181         url: server.url,
182         path,
183         statusCodeExpected: 401
184       })
185     })
186
187     it('Should fail if the user is not an administrator', async function () {
188       await makeDeleteRequest({
189         url: server.url,
190         path,
191         token: userAccessToken,
192         statusCodeExpected: 403
193       })
194     })
195   })
196
197   after(async function () {
198     killallServers([ server ])
199
200     // Keep the logs if the test failed
201     if (this['ok']) {
202       await flushTests()
203     }
204   })
205 })