]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/config.ts
Move utils to /shared
[github/Chocobozzz/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 user: {
52 videoQuota: 5242881,
53 videoQuotaDaily: 318742
54 },
55 transcoding: {
56 enabled: true,
57 threads: 1,
58 resolutions: {
59 '240p': false,
60 '360p': true,
61 '480p': true,
62 '720p': false,
63 '1080p': false
64 }
65 },
66 import: {
67 videos: {
68 http: {
69 enabled: false
70 },
71 torrent: {
72 enabled: false
73 }
74 }
75 }
76 }
77
78 // ---------------------------------------------------------------
79
80 before(async function () {
81 this.timeout(30000)
82
83 await flushTests()
84 server = await runServer(1)
85
86 await setAccessTokensToServers([ server ])
87
88 const user = {
89 username: 'user1',
90 password: 'password'
91 }
92 await createUser(server.url, server.accessToken, user.username, user.password)
93 userAccessToken = await userLogin(server, user)
94 })
95
96 describe('When getting the configuration', function () {
97 it('Should fail without token', async function () {
98 await makeGetRequest({
99 url: server.url,
100 path,
101 statusCodeExpected: 401
102 })
103 })
104
105 it('Should fail if the user is not an administrator', async function () {
106 await makeGetRequest({
107 url: server.url,
108 path,
109 token: userAccessToken,
110 statusCodeExpected: 403
111 })
112 })
113 })
114
115 describe('When updating the configuration', function () {
116 it('Should fail without token', async function () {
117 await makePutBodyRequest({
118 url: server.url,
119 path,
120 fields: updateParams,
121 statusCodeExpected: 401
122 })
123 })
124
125 it('Should fail if the user is not an administrator', async function () {
126 await makePutBodyRequest({
127 url: server.url,
128 path,
129 fields: updateParams,
130 token: userAccessToken,
131 statusCodeExpected: 403
132 })
133 })
134
135 it('Should fail if it misses a key', async function () {
136 const newUpdateParams = omit(updateParams, 'admin.email')
137
138 await makePutBodyRequest({
139 url: server.url,
140 path,
141 fields: newUpdateParams,
142 token: server.accessToken,
143 statusCodeExpected: 400
144 })
145 })
146
147 it('Should fail with a bad default NSFW policy', async function () {
148 const newUpdateParams = immutableAssign(updateParams, {
149 instance: {
150 defaultNSFWPolicy: 'hello'
151 }
152 })
153
154 await makePutBodyRequest({
155 url: server.url,
156 path,
157 fields: newUpdateParams,
158 token: server.accessToken,
159 statusCodeExpected: 400
160 })
161 })
162
163 it('Should success with the correct parameters', async function () {
164 await makePutBodyRequest({
165 url: server.url,
166 path,
167 fields: updateParams,
168 token: server.accessToken,
169 statusCodeExpected: 200
170 })
171 })
172 })
173
174 describe('When deleting the configuration', function () {
175 it('Should fail without token', async function () {
176 await makeDeleteRequest({
177 url: server.url,
178 path,
179 statusCodeExpected: 401
180 })
181 })
182
183 it('Should fail if the user is not an administrator', async function () {
184 await makeDeleteRequest({
185 url: server.url,
186 path,
187 token: userAccessToken,
188 statusCodeExpected: 403
189 })
190 })
191 })
192
193 after(async function () {
194 killallServers([ server ])
195
196 // Keep the logs if the test failed
197 if (this['ok']) {
198 await flushTests()
199 }
200 })
201 })