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