]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/config.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / config.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import { omit } from 'lodash'
4 import 'mocha'
5 import { CustomConfig } from '../../../../shared/models/server/custom-config.model'
6
7 import {
8 cleanupTests,
9 createUser,
10 flushAndRunServer,
11 immutableAssign,
12 makeDeleteRequest,
13 makeGetRequest,
14 makePutBodyRequest,
15 ServerInfo,
16 setAccessTokensToServers,
17 userLogin
18 } from '../../../../shared/extra-utils'
19 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
20
21 describe('Test config API validators', function () {
22 const path = '/api/v1/config/custom'
23 let server: ServerInfo
24 let userAccessToken: string
25 const updateParams: CustomConfig = {
26 instance: {
27 name: 'PeerTube updated',
28 shortDescription: 'my short description',
29 description: 'my super description',
30 terms: 'my super terms',
31 codeOfConduct: 'my super coc',
32
33 creationReason: 'my super reason',
34 moderationInformation: 'my super moderation information',
35 administrator: 'Kuja',
36 maintenanceLifetime: 'forever',
37 businessModel: 'my super business model',
38 hardwareInformation: '2vCore 3GB RAM',
39
40 languages: [ 'en', 'es' ],
41 categories: [ 1, 2 ],
42
43 isNSFW: true,
44 defaultClientRoute: '/videos/recently-added',
45 defaultNSFWPolicy: 'blur',
46 customizations: {
47 javascript: 'alert("coucou")',
48 css: 'body { background-color: red; }'
49 }
50 },
51 theme: {
52 default: 'default'
53 },
54 services: {
55 twitter: {
56 username: '@MySuperUsername',
57 whitelisted: true
58 }
59 },
60 cache: {
61 previews: {
62 size: 2
63 },
64 captions: {
65 size: 3
66 }
67 },
68 signup: {
69 enabled: false,
70 limit: 5,
71 requiresEmailVerification: false
72 },
73 admin: {
74 email: 'superadmin1@example.com'
75 },
76 contactForm: {
77 enabled: false
78 },
79 user: {
80 videoQuota: 5242881,
81 videoQuotaDaily: 318742
82 },
83 transcoding: {
84 enabled: true,
85 allowAdditionalExtensions: true,
86 allowAudioFiles: true,
87 threads: 1,
88 resolutions: {
89 '0p': false,
90 '240p': false,
91 '360p': true,
92 '480p': true,
93 '720p': false,
94 '1080p': false,
95 '2160p': false
96 },
97 webtorrent: {
98 enabled: true
99 },
100 hls: {
101 enabled: false
102 }
103 },
104 live: {
105 enabled: true,
106
107 allowReplay: false,
108 maxDuration: null,
109 maxInstanceLives: -1,
110 maxUserLives: 50,
111
112 transcoding: {
113 enabled: true,
114 threads: 4,
115 resolutions: {
116 '240p': true,
117 '360p': true,
118 '480p': true,
119 '720p': true,
120 '1080p': true,
121 '2160p': true
122 }
123 }
124 },
125 import: {
126 videos: {
127 http: {
128 enabled: false
129 },
130 torrent: {
131 enabled: false
132 }
133 }
134 },
135 autoBlacklist: {
136 videos: {
137 ofUsers: {
138 enabled: false
139 }
140 }
141 },
142 followers: {
143 instance: {
144 enabled: false,
145 manualApproval: true
146 }
147 },
148 followings: {
149 instance: {
150 autoFollowBack: {
151 enabled: true
152 },
153 autoFollowIndex: {
154 enabled: true,
155 indexUrl: 'https://index.example.com'
156 }
157 }
158 },
159 broadcastMessage: {
160 enabled: true,
161 dismissable: true,
162 message: 'super message',
163 level: 'warning'
164 },
165 search: {
166 remoteUri: {
167 users: true,
168 anonymous: true
169 },
170 searchIndex: {
171 enabled: true,
172 url: 'https://search.joinpeertube.org',
173 disableLocalSearch: true,
174 isDefaultSearch: true
175 }
176 }
177 }
178
179 // ---------------------------------------------------------------
180
181 before(async function () {
182 this.timeout(30000)
183
184 server = await flushAndRunServer(1)
185
186 await setAccessTokensToServers([ server ])
187
188 const user = {
189 username: 'user1',
190 password: 'password'
191 }
192 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
193 userAccessToken = await userLogin(server, user)
194 })
195
196 describe('When getting the configuration', function () {
197 it('Should fail without token', async function () {
198 await makeGetRequest({
199 url: server.url,
200 path,
201 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
202 })
203 })
204
205 it('Should fail if the user is not an administrator', async function () {
206 await makeGetRequest({
207 url: server.url,
208 path,
209 token: userAccessToken,
210 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
211 })
212 })
213 })
214
215 describe('When updating the configuration', function () {
216 it('Should fail without token', async function () {
217 await makePutBodyRequest({
218 url: server.url,
219 path,
220 fields: updateParams,
221 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
222 })
223 })
224
225 it('Should fail if the user is not an administrator', async function () {
226 await makePutBodyRequest({
227 url: server.url,
228 path,
229 fields: updateParams,
230 token: userAccessToken,
231 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
232 })
233 })
234
235 it('Should fail if it misses a key', async function () {
236 const newUpdateParams = omit(updateParams, 'admin.email')
237
238 await makePutBodyRequest({
239 url: server.url,
240 path,
241 fields: newUpdateParams,
242 token: server.accessToken,
243 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
244 })
245 })
246
247 it('Should fail with a bad default NSFW policy', async function () {
248 const newUpdateParams = immutableAssign(updateParams, {
249 instance: {
250 defaultNSFWPolicy: 'hello'
251 }
252 })
253
254 await makePutBodyRequest({
255 url: server.url,
256 path,
257 fields: newUpdateParams,
258 token: server.accessToken,
259 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
260 })
261 })
262
263 it('Should fail if email disabled and signup requires email verification', async function () {
264 // opposite scenario - success when enable enabled - covered via tests/api/users/user-verification.ts
265 const newUpdateParams = immutableAssign(updateParams, {
266 signup: {
267 enabled: true,
268 limit: 5,
269 requiresEmailVerification: true
270 }
271 })
272
273 await makePutBodyRequest({
274 url: server.url,
275 path,
276 fields: newUpdateParams,
277 token: server.accessToken,
278 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
279 })
280 })
281
282 it('Should fail with a disabled webtorrent & hls transcoding', async function () {
283 const newUpdateParams = immutableAssign(updateParams, {
284 transcoding: {
285 hls: {
286 enabled: false
287 },
288 webtorrent: {
289 enabled: false
290 }
291 }
292 })
293
294 await makePutBodyRequest({
295 url: server.url,
296 path,
297 fields: newUpdateParams,
298 token: server.accessToken,
299 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
300 })
301 })
302
303 it('Should success with the correct parameters', async function () {
304 await makePutBodyRequest({
305 url: server.url,
306 path,
307 fields: updateParams,
308 token: server.accessToken,
309 statusCodeExpected: HttpStatusCode.OK_200
310 })
311 })
312 })
313
314 describe('When deleting the configuration', function () {
315 it('Should fail without token', async function () {
316 await makeDeleteRequest({
317 url: server.url,
318 path,
319 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
320 })
321 })
322
323 it('Should fail if the user is not an administrator', async function () {
324 await makeDeleteRequest({
325 url: server.url,
326 path,
327 token: userAccessToken,
328 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
329 })
330 })
331 })
332
333 after(async function () {
334 await cleanupTests([ server ])
335 })
336 })