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