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