Merge branch 'release/v1.3.0' into develop
[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       },
71       hls: {
72         enabled: false
73       }
74     },
75     import: {
76       videos: {
77         http: {
78           enabled: false
79         },
80         torrent: {
81           enabled: false
82         }
83       }
84     },
85     autoBlacklist: {
86       videos: {
87         ofUsers: {
88           enabled: false
89         }
90       }
91     },
92     followers: {
93       instance: {
94         enabled: false,
95         manualApproval: true
96       }
97     }
98   }
99
100   // ---------------------------------------------------------------
101
102   before(async function () {
103     this.timeout(30000)
104
105     server = await flushAndRunServer(1)
106
107     await setAccessTokensToServers([ server ])
108
109     const user = {
110       username: 'user1',
111       password: 'password'
112     }
113     await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
114     userAccessToken = await userLogin(server, user)
115   })
116
117   describe('When getting the configuration', function () {
118     it('Should fail without token', async function () {
119       await makeGetRequest({
120         url: server.url,
121         path,
122         statusCodeExpected: 401
123       })
124     })
125
126     it('Should fail if the user is not an administrator', async function () {
127       await makeGetRequest({
128         url: server.url,
129         path,
130         token: userAccessToken,
131         statusCodeExpected: 403
132       })
133     })
134   })
135
136   describe('When updating the configuration', function () {
137     it('Should fail without token', async function () {
138       await makePutBodyRequest({
139         url: server.url,
140         path,
141         fields: updateParams,
142         statusCodeExpected: 401
143       })
144     })
145
146     it('Should fail if the user is not an administrator', async function () {
147       await makePutBodyRequest({
148         url: server.url,
149         path,
150         fields: updateParams,
151         token: userAccessToken,
152         statusCodeExpected: 403
153       })
154     })
155
156     it('Should fail if it misses a key', async function () {
157       const newUpdateParams = omit(updateParams, 'admin.email')
158
159       await makePutBodyRequest({
160         url: server.url,
161         path,
162         fields: newUpdateParams,
163         token: server.accessToken,
164         statusCodeExpected: 400
165       })
166     })
167
168     it('Should fail with a bad default NSFW policy', async function () {
169       const newUpdateParams = immutableAssign(updateParams, {
170         instance: {
171           defaultNSFWPolicy: 'hello'
172         }
173       })
174
175       await makePutBodyRequest({
176         url: server.url,
177         path,
178         fields: newUpdateParams,
179         token: server.accessToken,
180         statusCodeExpected: 400
181       })
182     })
183
184     it('Should fail if email disabled and signup requires email verification', async function () {
185       // opposite scenario - success when enable enabled - covered via tests/api/users/user-verification.ts
186       const newUpdateParams = immutableAssign(updateParams, {
187         signup: {
188           enabled: true,
189           limit: 5,
190           requiresEmailVerification: true
191         }
192       })
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 success with the correct parameters', async function () {
204       await makePutBodyRequest({
205         url: server.url,
206         path,
207         fields: updateParams,
208         token: server.accessToken,
209         statusCodeExpected: 200
210       })
211     })
212   })
213
214   describe('When deleting the configuration', function () {
215     it('Should fail without token', async function () {
216       await makeDeleteRequest({
217         url: server.url,
218         path,
219         statusCodeExpected: 401
220       })
221     })
222
223     it('Should fail if the user is not an administrator', async function () {
224       await makeDeleteRequest({
225         url: server.url,
226         path,
227         token: userAccessToken,
228         statusCodeExpected: 403
229       })
230     })
231   })
232
233   after(async function () {
234     await cleanupTests([ server ])
235   })
236 })