]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/video-channels.ts
ff04f6b03f1727d9e1fc25298974480ecc044105
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / video-channels.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import { omit } from 'lodash'
5 import 'mocha'
6 import {
7 cleanupTests,
8 createUser,
9 deleteVideoChannel,
10 flushAndRunServer,
11 getAccountVideoChannelsList,
12 immutableAssign,
13 makeGetRequest,
14 makePostBodyRequest,
15 makePutBodyRequest,
16 makeUploadRequest,
17 ServerInfo,
18 setAccessTokensToServers,
19 userLogin
20 } from '../../../../shared/extra-utils'
21 import {
22 checkBadCountPagination,
23 checkBadSortPagination,
24 checkBadStartPagination
25 } from '../../../../shared/extra-utils/requests/check-api-params'
26 import { join } from 'path'
27
28 const expect = chai.expect
29
30 describe('Test video channels API validator', function () {
31 const videoChannelPath = '/api/v1/video-channels'
32 let server: ServerInfo
33 let accessTokenUser: string
34
35 // ---------------------------------------------------------------
36
37 before(async function () {
38 this.timeout(30000)
39
40 server = await flushAndRunServer(1)
41
42 await setAccessTokensToServers([ server ])
43
44 const user = {
45 username: 'fake',
46 password: 'fake_password'
47 }
48
49 {
50 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
51 accessTokenUser = await userLogin(server, user)
52 }
53 })
54
55 describe('When listing a video channels', function () {
56 it('Should fail with a bad start pagination', async function () {
57 await checkBadStartPagination(server.url, videoChannelPath, server.accessToken)
58 })
59
60 it('Should fail with a bad count pagination', async function () {
61 await checkBadCountPagination(server.url, videoChannelPath, server.accessToken)
62 })
63
64 it('Should fail with an incorrect sort', async function () {
65 await checkBadSortPagination(server.url, videoChannelPath, server.accessToken)
66 })
67 })
68
69 describe('When listing account video channels', function () {
70 const accountChannelPath = '/api/v1/accounts/fake/video-channels'
71
72 it('Should fail with a bad start pagination', async function () {
73 await checkBadStartPagination(server.url, accountChannelPath, server.accessToken)
74 })
75
76 it('Should fail with a bad count pagination', async function () {
77 await checkBadCountPagination(server.url, accountChannelPath, server.accessToken)
78 })
79
80 it('Should fail with an incorrect sort', async function () {
81 await checkBadSortPagination(server.url, accountChannelPath, server.accessToken)
82 })
83
84 it('Should fail with a unknown account', async function () {
85 await getAccountVideoChannelsList({ url: server.url, accountName: 'unknown', specialStatus: 404 })
86 })
87
88 it('Should succeed with the correct parameters', async function () {
89 await makeGetRequest({
90 url: server.url,
91 path: accountChannelPath,
92 statusCodeExpected: 200
93 })
94 })
95 })
96
97 describe('When adding a video channel', function () {
98 const baseCorrectParams = {
99 name: 'super_channel',
100 displayName: 'hello',
101 description: 'super description',
102 support: 'super support text'
103 }
104
105 it('Should fail with a non authenticated user', async function () {
106 await makePostBodyRequest({
107 url: server.url,
108 path: videoChannelPath,
109 token: 'none',
110 fields: baseCorrectParams,
111 statusCodeExpected: 401
112 })
113 })
114
115 it('Should fail with nothing', async function () {
116 const fields = {}
117 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
118 })
119
120 it('Should fail without a name', async function () {
121 const fields = omit(baseCorrectParams, 'name')
122 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
123 })
124
125 it('Should fail with a bad name', async function () {
126 const fields = immutableAssign(baseCorrectParams, { name: 'super name' })
127 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
128 })
129
130 it('Should fail without a name', async function () {
131 const fields = omit(baseCorrectParams, 'displayName')
132 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
133 })
134
135 it('Should fail with a long name', async function () {
136 const fields = immutableAssign(baseCorrectParams, { displayName: 'super'.repeat(25) })
137 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
138 })
139
140 it('Should fail with a long description', async function () {
141 const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(201) })
142 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
143 })
144
145 it('Should fail with a long support text', async function () {
146 const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) })
147 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
148 })
149
150 it('Should succeed with the correct parameters', async function () {
151 await makePostBodyRequest({
152 url: server.url,
153 path: videoChannelPath,
154 token: server.accessToken,
155 fields: baseCorrectParams,
156 statusCodeExpected: 200
157 })
158 })
159
160 it('Should fail when adding a channel with the same username', async function () {
161 await makePostBodyRequest({
162 url: server.url,
163 path: videoChannelPath,
164 token: server.accessToken,
165 fields: baseCorrectParams,
166 statusCodeExpected: 409
167 })
168 })
169 })
170
171 describe('When updating a video channel', function () {
172 const baseCorrectParams = {
173 displayName: 'hello',
174 description: 'super description'
175 }
176 let path: string
177
178 before(async function () {
179 path = videoChannelPath + '/super_channel'
180 })
181
182 it('Should fail with a non authenticated user', async function () {
183 await makePutBodyRequest({
184 url: server.url,
185 path,
186 token: 'hi',
187 fields: baseCorrectParams,
188 statusCodeExpected: 401
189 })
190 })
191
192 it('Should fail with another authenticated user', async function () {
193 await makePutBodyRequest({
194 url: server.url,
195 path,
196 token: accessTokenUser,
197 fields: baseCorrectParams,
198 statusCodeExpected: 403
199 })
200 })
201
202 it('Should fail with a long name', async function () {
203 const fields = immutableAssign(baseCorrectParams, { displayName: 'super'.repeat(25) })
204 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
205 })
206
207 it('Should fail with a long description', async function () {
208 const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(201) })
209 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
210 })
211
212 it('Should fail with a long support text', async function () {
213 const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) })
214 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
215 })
216
217 it('Should succeed with the correct parameters', async function () {
218 await makePutBodyRequest({
219 url: server.url,
220 path,
221 token: server.accessToken,
222 fields: baseCorrectParams,
223 statusCodeExpected: 204
224 })
225 })
226 })
227
228 describe('When updating video channel avatar', function () {
229 let path: string
230
231 before(async function () {
232 path = videoChannelPath + '/super_channel'
233 })
234
235 it('Should fail with an incorrect input file', async function () {
236 const fields = {}
237 const attaches = {
238 'avatarfile': join(__dirname, '..', '..', 'fixtures', 'video_short.mp4')
239 }
240 await makeUploadRequest({ url: server.url, path: path + '/avatar/pick', token: server.accessToken, fields, attaches })
241 })
242
243 it('Should fail with a big file', async function () {
244 const fields = {}
245 const attaches = {
246 'avatarfile': join(__dirname, '..', '..', 'fixtures', 'avatar-big.png')
247 }
248 await makeUploadRequest({ url: server.url, path: path + '/avatar/pick', token: server.accessToken, fields, attaches })
249 })
250
251 it('Should fail with an unauthenticated user', async function () {
252 const fields = {}
253 const attaches = {
254 'avatarfile': join(__dirname, '..', '..', 'fixtures', 'avatar.png')
255 }
256 await makeUploadRequest({
257 url: server.url,
258 path: path + '/avatar/pick',
259 fields,
260 attaches,
261 statusCodeExpected: 401
262 })
263 })
264
265 it('Should succeed with the correct params', async function () {
266 const fields = {}
267 const attaches = {
268 'avatarfile': join(__dirname, '..', '..', 'fixtures', 'avatar.png')
269 }
270 await makeUploadRequest({
271 url: server.url,
272 path: path + '/avatar/pick',
273 token: server.accessToken,
274 fields,
275 attaches,
276 statusCodeExpected: 200
277 })
278 })
279 })
280
281 describe('When getting a video channel', function () {
282 it('Should return the list of the video channels with nothing', async function () {
283 const res = await makeGetRequest({
284 url: server.url,
285 path: videoChannelPath,
286 statusCodeExpected: 200
287 })
288
289 expect(res.body.data).to.be.an('array')
290 })
291
292 it('Should return 404 with an incorrect video channel', async function () {
293 await makeGetRequest({
294 url: server.url,
295 path: videoChannelPath + '/super_channel2',
296 statusCodeExpected: 404
297 })
298 })
299
300 it('Should succeed with the correct parameters', async function () {
301 await makeGetRequest({
302 url: server.url,
303 path: videoChannelPath + '/super_channel',
304 statusCodeExpected: 200
305 })
306 })
307 })
308
309 describe('When deleting a video channel', function () {
310 it('Should fail with a non authenticated user', async function () {
311 await deleteVideoChannel(server.url, 'coucou', 'super_channel', 401)
312 })
313
314 it('Should fail with another authenticated user', async function () {
315 await deleteVideoChannel(server.url, accessTokenUser, 'super_channel', 403)
316 })
317
318 it('Should fail with an unknown video channel id', async function () {
319 await deleteVideoChannel(server.url, server.accessToken,'super_channel2', 404)
320 })
321
322 it('Should succeed with the correct parameters', async function () {
323 await deleteVideoChannel(server.url, server.accessToken, 'super_channel')
324 })
325
326 it('Should fail to delete the last user video channel', async function () {
327 await deleteVideoChannel(server.url, server.accessToken, 'root_channel', 409)
328 })
329 })
330
331 after(async function () {
332 await cleanupTests([ server ])
333 })
334 })