Add language filters in user preferences
[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, flushAndRunServer, ServerInfo,
9   setAccessTokensToServers, userLogin, immutableAssign, cleanupTests
10 } from '../../../../shared/extra-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       isNSFW: true,
23       defaultClientRoute: '/videos/recently-added',
24       defaultNSFWPolicy: 'blur',
25       customizations: {
26         javascript: 'alert("coucou")',
27         css: 'body { background-color: red; }'
28       }
29     },
30     services: {
31       twitter: {
32         username: '@MySuperUsername',
33         whitelisted: true
34       }
35     },
36     cache: {
37       previews: {
38         size: 2
39       },
40       captions: {
41         size: 3
42       }
43     },
44     signup: {
45       enabled: false,
46       limit: 5,
47       requiresEmailVerification: false
48     },
49     admin: {
50       email: 'superadmin1@example.com'
51     },
52     contactForm: {
53       enabled: false
54     },
55     user: {
56       videoQuota: 5242881,
57       videoQuotaDaily: 318742
58     },
59     transcoding: {
60       enabled: true,
61       allowAdditionalExtensions: true,
62       allowAudioFiles: true,
63       threads: 1,
64       resolutions: {
65         '240p': false,
66         '360p': true,
67         '480p': true,
68         '720p': false,
69         '1080p': false,
70         '2160p': false
71       },
72       hls: {
73         enabled: false
74       }
75     },
76     import: {
77       videos: {
78         http: {
79           enabled: false
80         },
81         torrent: {
82           enabled: false
83         }
84       }
85     },
86     autoBlacklist: {
87       videos: {
88         ofUsers: {
89           enabled: false
90         }
91       }
92     },
93     followers: {
94       instance: {
95         enabled: false,
96         manualApproval: true
97       }
98     }
99   }
100
101   // ---------------------------------------------------------------
102
103   before(async function () {
104     this.timeout(30000)
105
106     server = await flushAndRunServer(1)
107
108     await setAccessTokensToServers([ server ])
109
110     const user = {
111       username: 'user1',
112       password: 'password'
113     }
114     await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
115     userAccessToken = await userLogin(server, user)
116   })
117
118   describe('When getting the configuration', function () {
119     it('Should fail without token', async function () {
120       await makeGetRequest({
121         url: server.url,
122         path,
123         statusCodeExpected: 401
124       })
125     })
126
127     it('Should fail if the user is not an administrator', async function () {
128       await makeGetRequest({
129         url: server.url,
130         path,
131         token: userAccessToken,
132         statusCodeExpected: 403
133       })
134     })
135   })
136
137   describe('When updating the configuration', function () {
138     it('Should fail without token', async function () {
139       await makePutBodyRequest({
140         url: server.url,
141         path,
142         fields: updateParams,
143         statusCodeExpected: 401
144       })
145     })
146
147     it('Should fail if the user is not an administrator', async function () {
148       await makePutBodyRequest({
149         url: server.url,
150         path,
151         fields: updateParams,
152         token: userAccessToken,
153         statusCodeExpected: 403
154       })
155     })
156
157     it('Should fail if it misses a key', async function () {
158       const newUpdateParams = omit(updateParams, 'admin.email')
159
160       await makePutBodyRequest({
161         url: server.url,
162         path,
163         fields: newUpdateParams,
164         token: server.accessToken,
165         statusCodeExpected: 400
166       })
167     })
168
169     it('Should fail with a bad default NSFW policy', async function () {
170       const newUpdateParams = immutableAssign(updateParams, {
171         instance: {
172           defaultNSFWPolicy: 'hello'
173         }
174       })
175
176       await makePutBodyRequest({
177         url: server.url,
178         path,
179         fields: newUpdateParams,
180         token: server.accessToken,
181         statusCodeExpected: 400
182       })
183     })
184
185     it('Should fail if email disabled and signup requires email verification', async function () {
186       // opposite scenario - success when enable enabled - covered via tests/api/users/user-verification.ts
187       const newUpdateParams = immutableAssign(updateParams, {
188         signup: {
189           enabled: true,
190           limit: 5,
191           requiresEmailVerification: true
192         }
193       })
194
195       await makePutBodyRequest({
196         url: server.url,
197         path,
198         fields: newUpdateParams,
199         token: server.accessToken,
200         statusCodeExpected: 400
201       })
202     })
203
204     it('Should success with the correct parameters', async function () {
205       await makePutBodyRequest({
206         url: server.url,
207         path,
208         fields: updateParams,
209         token: server.accessToken,
210         statusCodeExpected: 200
211       })
212     })
213   })
214
215   describe('When deleting the configuration', function () {
216     it('Should fail without token', async function () {
217       await makeDeleteRequest({
218         url: server.url,
219         path,
220         statusCodeExpected: 401
221       })
222     })
223
224     it('Should fail if the user is not an administrator', async function () {
225       await makeDeleteRequest({
226         url: server.url,
227         path,
228         token: userAccessToken,
229         statusCodeExpected: 403
230       })
231     })
232   })
233
234   after(async function () {
235     await cleanupTests([ server ])
236   })
237 })