]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/video-channels.ts
Update changelog
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / video-channels.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
5f04dd2f 2
3d470a53 3import 'mocha'
5f04dd2f 4import * as chai from 'chai'
11ba2ab3 5import { omit } from 'lodash'
c55e3d72
C
6import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
7import { buildAbsoluteFixturePath } from '@shared/core-utils'
8import { HttpStatusCode, VideoChannelUpdate } from '@shared/models'
5f04dd2f 9import {
a5461888 10 ChannelsCommand,
7c3b7976 11 cleanupTests,
254d3579 12 createSingleServer,
48dce1c9
C
13 makeGetRequest,
14 makePostBodyRequest,
52d9f792
C
15 makePutBodyRequest,
16 makeUploadRequest,
254d3579 17 PeerTubeServer,
41d1d075 18 setAccessTokensToServers
bf54587a 19} from '@shared/server-commands'
11ba2ab3
C
20
21const expect = chai.expect
5f04dd2f 22
08c1efbe 23describe('Test video channels API validator', function () {
48dce1c9 24 const videoChannelPath = '/api/v1/video-channels'
254d3579 25 let server: PeerTubeServer
5f04dd2f 26 let accessTokenUser: string
a5461888 27 let command: ChannelsCommand
5f04dd2f
C
28
29 // ---------------------------------------------------------------
30
31 before(async function () {
e212f887 32 this.timeout(30000)
5f04dd2f 33
254d3579 34 server = await createSingleServer(1)
5f04dd2f
C
35
36 await setAccessTokensToServers([ server ])
37
5f04dd2f
C
38 const user = {
39 username: 'fake',
40 password: 'fake_password'
41 }
6b738c7a
C
42
43 {
89d241a7
C
44 await server.users.create({ username: user.username, password: user.password })
45 accessTokenUser = await server.login.getAccessToken(user)
6b738c7a 46 }
a5461888 47
89d241a7 48 command = server.channels
5f04dd2f
C
49 })
50
51 describe('When listing a video channels', function () {
52 it('Should fail with a bad start pagination', async function () {
48dce1c9 53 await checkBadStartPagination(server.url, videoChannelPath, server.accessToken)
5f04dd2f
C
54 })
55
56 it('Should fail with a bad count pagination', async function () {
48dce1c9 57 await checkBadCountPagination(server.url, videoChannelPath, server.accessToken)
5f04dd2f
C
58 })
59
60 it('Should fail with an incorrect sort', async function () {
48dce1c9 61 await checkBadSortPagination(server.url, videoChannelPath, server.accessToken)
5f04dd2f
C
62 })
63 })
64
d50acfab 65 describe('When listing account video channels', function () {
91b66319
C
66 const accountChannelPath = '/api/v1/accounts/fake/video-channels'
67
68 it('Should fail with a bad start pagination', async function () {
69 await checkBadStartPagination(server.url, accountChannelPath, server.accessToken)
70 })
71
72 it('Should fail with a bad count pagination', async function () {
73 await checkBadCountPagination(server.url, accountChannelPath, server.accessToken)
74 })
75
76 it('Should fail with an incorrect sort', async function () {
77 await checkBadSortPagination(server.url, accountChannelPath, server.accessToken)
78 })
79
d50acfab 80 it('Should fail with a unknown account', async function () {
89d241a7 81 await server.channels.listByAccount({ accountName: 'unknown', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
91b66319
C
82 })
83
84 it('Should succeed with the correct parameters', async function () {
85 await makeGetRequest({
86 url: server.url,
87 path: accountChannelPath,
c0e8b12e 88 expectedStatus: HttpStatusCode.OK_200
91b66319 89 })
5f04dd2f
C
90 })
91 })
92
93 describe('When adding a video channel', function () {
11ba2ab3 94 const baseCorrectParams = {
8a19bee1 95 name: 'super_channel',
08c1efbe 96 displayName: 'hello',
2422c46b
C
97 description: 'super description',
98 support: 'super support text'
11ba2ab3 99 }
48dce1c9 100
5f04dd2f 101 it('Should fail with a non authenticated user', async function () {
cc918ac3
C
102 await makePostBodyRequest({
103 url: server.url,
104 path: videoChannelPath,
105 token: 'none',
106 fields: baseCorrectParams,
c0e8b12e 107 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
cc918ac3 108 })
5f04dd2f
C
109 })
110
111 it('Should fail with nothing', async function () {
112 const fields = {}
cc918ac3 113 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
5f04dd2f
C
114 })
115
8a19bee1
C
116 it('Should fail without a name', async function () {
117 const fields = omit(baseCorrectParams, 'name')
118 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
119 })
120
121 it('Should fail with a bad name', async function () {
6c5065a0 122 const fields = { ...baseCorrectParams, name: 'super name' }
8a19bee1
C
123 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
124 })
125
08c1efbe
C
126 it('Should fail without a name', async function () {
127 const fields = omit(baseCorrectParams, 'displayName')
cc918ac3 128 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
5f04dd2f
C
129 })
130
131 it('Should fail with a long name', async function () {
6c5065a0 132 const fields = { ...baseCorrectParams, displayName: 'super'.repeat(25) }
cc918ac3 133 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
5f04dd2f
C
134 })
135
136 it('Should fail with a long description', async function () {
6c5065a0 137 const fields = { ...baseCorrectParams, description: 'super'.repeat(201) }
cc918ac3 138 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
5f04dd2f
C
139 })
140
2422c46b 141 it('Should fail with a long support text', async function () {
6c5065a0 142 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
cc918ac3 143 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
2422c46b
C
144 })
145
5f04dd2f 146 it('Should succeed with the correct parameters', async function () {
11ba2ab3
C
147 await makePostBodyRequest({
148 url: server.url,
cc918ac3 149 path: videoChannelPath,
11ba2ab3
C
150 token: server.accessToken,
151 fields: baseCorrectParams,
c0e8b12e 152 expectedStatus: HttpStatusCode.OK_200
11ba2ab3 153 })
5f04dd2f 154 })
601527d7
C
155
156 it('Should fail when adding a channel with the same username', async function () {
157 await makePostBodyRequest({
158 url: server.url,
159 path: videoChannelPath,
160 token: server.accessToken,
161 fields: baseCorrectParams,
c0e8b12e 162 expectedStatus: HttpStatusCode.CONFLICT_409
601527d7
C
163 })
164 })
5f04dd2f
C
165 })
166
167 describe('When updating a video channel', function () {
7d14d4d2 168 const baseCorrectParams: VideoChannelUpdate = {
08c1efbe 169 displayName: 'hello',
7d14d4d2
C
170 description: 'super description',
171 support: 'toto',
172 bulkVideosSupportUpdate: false
11ba2ab3 173 }
6b738c7a 174 let path: string
11ba2ab3 175
5f04dd2f 176 before(async function () {
8a19bee1 177 path = videoChannelPath + '/super_channel'
5f04dd2f
C
178 })
179
180 it('Should fail with a non authenticated user', async function () {
11ba2ab3
C
181 await makePutBodyRequest({
182 url: server.url,
48dce1c9 183 path,
11ba2ab3
C
184 token: 'hi',
185 fields: baseCorrectParams,
c0e8b12e 186 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
11ba2ab3 187 })
5f04dd2f
C
188 })
189
190 it('Should fail with another authenticated user', async function () {
5f04dd2f
C
191 await makePutBodyRequest({
192 url: server.url,
48dce1c9 193 path,
5f04dd2f 194 token: accessTokenUser,
11ba2ab3 195 fields: baseCorrectParams,
c0e8b12e 196 expectedStatus: HttpStatusCode.FORBIDDEN_403
5f04dd2f
C
197 })
198 })
199
200 it('Should fail with a long name', async function () {
6c5065a0 201 const fields = { ...baseCorrectParams, displayName: 'super'.repeat(25) }
48dce1c9 202 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
5f04dd2f
C
203 })
204
205 it('Should fail with a long description', async function () {
6c5065a0 206 const fields = { ...baseCorrectParams, description: 'super'.repeat(201) }
48dce1c9 207 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
5f04dd2f
C
208 })
209
2422c46b 210 it('Should fail with a long support text', async function () {
6c5065a0 211 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
48dce1c9 212 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
2422c46b
C
213 })
214
7d14d4d2 215 it('Should fail with a bad bulkVideosSupportUpdate field', async function () {
6c5065a0 216 const fields = { ...baseCorrectParams, bulkVideosSupportUpdate: 'super' }
7d14d4d2
C
217 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
218 })
219
5f04dd2f 220 it('Should succeed with the correct parameters', async function () {
5f04dd2f
C
221 await makePutBodyRequest({
222 url: server.url,
48dce1c9 223 path,
5f04dd2f 224 token: server.accessToken,
11ba2ab3 225 fields: baseCorrectParams,
c0e8b12e 226 expectedStatus: HttpStatusCode.NO_CONTENT_204
5f04dd2f
C
227 })
228 })
229 })
230
213e30ef
C
231 describe('When updating video channel avatar/banner', function () {
232 const types = [ 'avatar', 'banner' ]
4bbfc6c6
C
233 let path: string
234
235 before(async function () {
8a19bee1 236 path = videoChannelPath + '/super_channel'
4bbfc6c6
C
237 })
238
239 it('Should fail with an incorrect input file', async function () {
213e30ef
C
240 for (const type of types) {
241 const fields = {}
242 const attaches = {
3d470a53 243 [type + 'file']: buildAbsoluteFixturePath('video_short.mp4')
213e30ef
C
244 }
245
246 await makeUploadRequest({ url: server.url, path: `${path}/${type}/pick`, token: server.accessToken, fields, attaches })
4bbfc6c6 247 }
4bbfc6c6
C
248 })
249
250 it('Should fail with a big file', async function () {
213e30ef
C
251 for (const type of types) {
252 const fields = {}
253 const attaches = {
3d470a53 254 [type + 'file']: buildAbsoluteFixturePath('avatar-big.png')
213e30ef
C
255 }
256 await makeUploadRequest({ url: server.url, path: `${path}/${type}/pick`, token: server.accessToken, fields, attaches })
4bbfc6c6 257 }
4bbfc6c6
C
258 })
259
260 it('Should fail with an unauthenticated user', async function () {
213e30ef
C
261 for (const type of types) {
262 const fields = {}
263 const attaches = {
3d470a53 264 [type + 'file']: buildAbsoluteFixturePath('avatar.png')
213e30ef
C
265 }
266 await makeUploadRequest({
267 url: server.url,
268 path: `${path}/${type}/pick`,
269 fields,
270 attaches,
c0e8b12e 271 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
213e30ef 272 })
4bbfc6c6 273 }
4bbfc6c6
C
274 })
275
276 it('Should succeed with the correct params', async function () {
213e30ef
C
277 for (const type of types) {
278 const fields = {}
279 const attaches = {
3d470a53 280 [type + 'file']: buildAbsoluteFixturePath('avatar.png')
213e30ef
C
281 }
282 await makeUploadRequest({
283 url: server.url,
284 path: `${path}/${type}/pick`,
285 token: server.accessToken,
286 fields,
287 attaches,
c0e8b12e 288 expectedStatus: HttpStatusCode.OK_200
213e30ef 289 })
4bbfc6c6 290 }
4bbfc6c6
C
291 })
292 })
293
5f04dd2f 294 describe('When getting a video channel', function () {
5f04dd2f 295 it('Should return the list of the video channels with nothing', async function () {
11ba2ab3
C
296 const res = await makeGetRequest({
297 url: server.url,
cc918ac3 298 path: videoChannelPath,
c0e8b12e 299 expectedStatus: HttpStatusCode.OK_200
11ba2ab3 300 })
5f04dd2f
C
301
302 expect(res.body.data).to.be.an('array')
303 })
304
5f04dd2f 305 it('Should return 404 with an incorrect video channel', async function () {
11ba2ab3
C
306 await makeGetRequest({
307 url: server.url,
8a19bee1 308 path: videoChannelPath + '/super_channel2',
c0e8b12e 309 expectedStatus: HttpStatusCode.NOT_FOUND_404
11ba2ab3 310 })
5f04dd2f
C
311 })
312
313 it('Should succeed with the correct parameters', async function () {
11ba2ab3
C
314 await makeGetRequest({
315 url: server.url,
8a19bee1 316 path: videoChannelPath + '/super_channel',
c0e8b12e 317 expectedStatus: HttpStatusCode.OK_200
11ba2ab3 318 })
5f04dd2f
C
319 })
320 })
321
4beda9e1
C
322 describe('When getting channel followers', function () {
323 const path = '/api/v1/video-channels/super_channel/followers'
324
325 it('Should fail with a bad start pagination', async function () {
326 await checkBadStartPagination(server.url, path, server.accessToken)
327 })
328
329 it('Should fail with a bad count pagination', async function () {
330 await checkBadCountPagination(server.url, path, server.accessToken)
331 })
332
333 it('Should fail with an incorrect sort', async function () {
334 await checkBadSortPagination(server.url, path, server.accessToken)
335 })
336
337 it('Should fail with a unauthenticated user', async function () {
338 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
339 })
340
341 it('Should fail with a another user', async function () {
342 await makeGetRequest({ url: server.url, path, token: accessTokenUser, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
343 })
344
345 it('Should succeed with the correct params', async function () {
346 await makeGetRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.OK_200 })
347 })
348 })
349
5f04dd2f 350 describe('When deleting a video channel', function () {
5f04dd2f 351 it('Should fail with a non authenticated user', async function () {
a5461888 352 await command.delete({ token: 'coucou', channelName: 'super_channel', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
5f04dd2f
C
353 })
354
355 it('Should fail with another authenticated user', async function () {
a5461888 356 await command.delete({ token: accessTokenUser, channelName: 'super_channel', expectedStatus: HttpStatusCode.FORBIDDEN_403 })
5f04dd2f
C
357 })
358
48dce1c9 359 it('Should fail with an unknown video channel id', async function () {
a5461888 360 await command.delete({ channelName: 'super_channel2', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
5f04dd2f
C
361 })
362
363 it('Should succeed with the correct parameters', async function () {
a5461888 364 await command.delete({ channelName: 'super_channel' })
5f04dd2f
C
365 })
366
367 it('Should fail to delete the last user video channel', async function () {
a5461888 368 await command.delete({ channelName: 'root_channel', expectedStatus: HttpStatusCode.CONFLICT_409 })
5f04dd2f
C
369 })
370 })
371
7c3b7976
C
372 after(async function () {
373 await cleanupTests([ server ])
5f04dd2f
C
374 })
375})