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