fix a few typos (#2141)
[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   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         '240p': false,
89         '360p': true,
90         '480p': true,
91         '720p': false,
92         '1080p': false,
93         '2160p': false
94       },
95       hls: {
96         enabled: false
97       }
98     },
99     import: {
100       videos: {
101         http: {
102           enabled: false
103         },
104         torrent: {
105           enabled: false
106         }
107       }
108     },
109     autoBlacklist: {
110       videos: {
111         ofUsers: {
112           enabled: false
113         }
114       }
115     },
116     followers: {
117       instance: {
118         enabled: false,
119         manualApproval: true
120       }
121     },
122     followings: {
123       instance: {
124         autoFollowBack: {
125           enabled: true
126         },
127         autoFollowIndex: {
128           enabled: true,
129           indexUrl: 'https://index.example.com'
130         }
131       }
132     }
133   }
134
135   // ---------------------------------------------------------------
136
137   before(async function () {
138     this.timeout(30000)
139
140     server = await flushAndRunServer(1)
141
142     await setAccessTokensToServers([ server ])
143
144     const user = {
145       username: 'user1',
146       password: 'password'
147     }
148     await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
149     userAccessToken = await userLogin(server, user)
150   })
151
152   describe('When getting the configuration', function () {
153     it('Should fail without token', async function () {
154       await makeGetRequest({
155         url: server.url,
156         path,
157         statusCodeExpected: 401
158       })
159     })
160
161     it('Should fail if the user is not an administrator', async function () {
162       await makeGetRequest({
163         url: server.url,
164         path,
165         token: userAccessToken,
166         statusCodeExpected: 403
167       })
168     })
169   })
170
171   describe('When updating the configuration', function () {
172     it('Should fail without token', async function () {
173       await makePutBodyRequest({
174         url: server.url,
175         path,
176         fields: updateParams,
177         statusCodeExpected: 401
178       })
179     })
180
181     it('Should fail if the user is not an administrator', async function () {
182       await makePutBodyRequest({
183         url: server.url,
184         path,
185         fields: updateParams,
186         token: userAccessToken,
187         statusCodeExpected: 403
188       })
189     })
190
191     it('Should fail if it misses a key', async function () {
192       const newUpdateParams = omit(updateParams, 'admin.email')
193
194       await makePutBodyRequest({
195         url: server.url,
196         path,
197         fields: newUpdateParams,
198         token: server.accessToken,
199         statusCodeExpected: 400
200       })
201     })
202
203     it('Should fail with a bad default NSFW policy', async function () {
204       const newUpdateParams = immutableAssign(updateParams, {
205         instance: {
206           defaultNSFWPolicy: 'hello'
207         }
208       })
209
210       await makePutBodyRequest({
211         url: server.url,
212         path,
213         fields: newUpdateParams,
214         token: server.accessToken,
215         statusCodeExpected: 400
216       })
217     })
218
219     it('Should fail if email disabled and signup requires email verification', async function () {
220       // opposite scenario - success when enable enabled - covered via tests/api/users/user-verification.ts
221       const newUpdateParams = immutableAssign(updateParams, {
222         signup: {
223           enabled: true,
224           limit: 5,
225           requiresEmailVerification: true
226         }
227       })
228
229       await makePutBodyRequest({
230         url: server.url,
231         path,
232         fields: newUpdateParams,
233         token: server.accessToken,
234         statusCodeExpected: 400
235       })
236     })
237
238     it('Should success with the correct parameters', async function () {
239       await makePutBodyRequest({
240         url: server.url,
241         path,
242         fields: updateParams,
243         token: server.accessToken,
244         statusCodeExpected: 200
245       })
246     })
247   })
248
249   describe('When deleting the configuration', function () {
250     it('Should fail without token', async function () {
251       await makeDeleteRequest({
252         url: server.url,
253         path,
254         statusCodeExpected: 401
255       })
256     })
257
258     it('Should fail if the user is not an administrator', async function () {
259       await makeDeleteRequest({
260         url: server.url,
261         path,
262         token: userAccessToken,
263         statusCodeExpected: 403
264       })
265     })
266   })
267
268   after(async function () {
269     await cleanupTests([ server ])
270   })
271 })