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