]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/video-channels.ts
0dd4364262c26fcd98a675f34e94f815189b8fbc
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / video-channels.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
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 import { VideoChannelUpdate } from '../../../../shared/models/videos'
28 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
29
30 const expect = chai.expect
31
32 describe('Test video channels API validator', function () {
33 const videoChannelPath = '/api/v1/video-channels'
34 let server: ServerInfo
35 let accessTokenUser: string
36
37 // ---------------------------------------------------------------
38
39 before(async function () {
40 this.timeout(30000)
41
42 server = await flushAndRunServer(1)
43
44 await setAccessTokensToServers([ server ])
45
46 const user = {
47 username: 'fake',
48 password: 'fake_password'
49 }
50
51 {
52 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
53 accessTokenUser = await userLogin(server, user)
54 }
55 })
56
57 describe('When listing a video channels', function () {
58 it('Should fail with a bad start pagination', async function () {
59 await checkBadStartPagination(server.url, videoChannelPath, server.accessToken)
60 })
61
62 it('Should fail with a bad count pagination', async function () {
63 await checkBadCountPagination(server.url, videoChannelPath, server.accessToken)
64 })
65
66 it('Should fail with an incorrect sort', async function () {
67 await checkBadSortPagination(server.url, videoChannelPath, server.accessToken)
68 })
69 })
70
71 describe('When listing account video channels', function () {
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
86 it('Should fail with a unknown account', async function () {
87 await getAccountVideoChannelsList({ url: server.url, accountName: 'unknown', specialStatus: HttpStatusCode.NOT_FOUND_404 })
88 })
89
90 it('Should succeed with the correct parameters', async function () {
91 await makeGetRequest({
92 url: server.url,
93 path: accountChannelPath,
94 statusCodeExpected: HttpStatusCode.OK_200
95 })
96 })
97 })
98
99 describe('When adding a video channel', function () {
100 const baseCorrectParams = {
101 name: 'super_channel',
102 displayName: 'hello',
103 description: 'super description',
104 support: 'super support text'
105 }
106
107 it('Should fail with a non authenticated user', async function () {
108 await makePostBodyRequest({
109 url: server.url,
110 path: videoChannelPath,
111 token: 'none',
112 fields: baseCorrectParams,
113 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
114 })
115 })
116
117 it('Should fail with nothing', async function () {
118 const fields = {}
119 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
120 })
121
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
132 it('Should fail without a name', async function () {
133 const fields = omit(baseCorrectParams, 'displayName')
134 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
135 })
136
137 it('Should fail with a long name', async function () {
138 const fields = immutableAssign(baseCorrectParams, { displayName: 'super'.repeat(25) })
139 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
140 })
141
142 it('Should fail with a long description', async function () {
143 const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(201) })
144 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
145 })
146
147 it('Should fail with a long support text', async function () {
148 const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) })
149 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
150 })
151
152 it('Should succeed with the correct parameters', async function () {
153 await makePostBodyRequest({
154 url: server.url,
155 path: videoChannelPath,
156 token: server.accessToken,
157 fields: baseCorrectParams,
158 statusCodeExpected: HttpStatusCode.OK_200
159 })
160 })
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,
168 statusCodeExpected: HttpStatusCode.CONFLICT_409
169 })
170 })
171 })
172
173 describe('When updating a video channel', function () {
174 const baseCorrectParams: VideoChannelUpdate = {
175 displayName: 'hello',
176 description: 'super description',
177 support: 'toto',
178 bulkVideosSupportUpdate: false
179 }
180 let path: string
181
182 before(async function () {
183 path = videoChannelPath + '/super_channel'
184 })
185
186 it('Should fail with a non authenticated user', async function () {
187 await makePutBodyRequest({
188 url: server.url,
189 path,
190 token: 'hi',
191 fields: baseCorrectParams,
192 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
193 })
194 })
195
196 it('Should fail with another authenticated user', async function () {
197 await makePutBodyRequest({
198 url: server.url,
199 path,
200 token: accessTokenUser,
201 fields: baseCorrectParams,
202 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
203 })
204 })
205
206 it('Should fail with a long name', async function () {
207 const fields = immutableAssign(baseCorrectParams, { displayName: 'super'.repeat(25) })
208 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
209 })
210
211 it('Should fail with a long description', async function () {
212 const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(201) })
213 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
214 })
215
216 it('Should fail with a long support text', async function () {
217 const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) })
218 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
219 })
220
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
226 it('Should succeed with the correct parameters', async function () {
227 await makePutBodyRequest({
228 url: server.url,
229 path,
230 token: server.accessToken,
231 fields: baseCorrectParams,
232 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
233 })
234 })
235 })
236
237 describe('When updating video channel avatar', function () {
238 let path: string
239
240 before(async function () {
241 path = videoChannelPath + '/super_channel'
242 })
243
244 it('Should fail with an incorrect input file', async function () {
245 const fields = {}
246 const attaches = {
247 avatarfile: join(__dirname, '..', '..', 'fixtures', 'video_short.mp4')
248 }
249 await makeUploadRequest({ url: server.url, path: path + '/avatar/pick', token: server.accessToken, fields, attaches })
250 })
251
252 it('Should fail with a big file', async function () {
253 const fields = {}
254 const attaches = {
255 avatarfile: join(__dirname, '..', '..', 'fixtures', 'avatar-big.png')
256 }
257 await makeUploadRequest({ url: server.url, path: path + '/avatar/pick', token: server.accessToken, fields, attaches })
258 })
259
260 it('Should fail with an unauthenticated user', async function () {
261 const fields = {}
262 const attaches = {
263 avatarfile: join(__dirname, '..', '..', 'fixtures', 'avatar.png')
264 }
265 await makeUploadRequest({
266 url: server.url,
267 path: path + '/avatar/pick',
268 fields,
269 attaches,
270 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
271 })
272 })
273
274 it('Should succeed with the correct params', async function () {
275 const fields = {}
276 const attaches = {
277 avatarfile: join(__dirname, '..', '..', 'fixtures', 'avatar.png')
278 }
279 await makeUploadRequest({
280 url: server.url,
281 path: path + '/avatar/pick',
282 token: server.accessToken,
283 fields,
284 attaches,
285 statusCodeExpected: HttpStatusCode.OK_200
286 })
287 })
288 })
289
290 describe('When getting a video channel', function () {
291 it('Should return the list of the video channels with nothing', async function () {
292 const res = await makeGetRequest({
293 url: server.url,
294 path: videoChannelPath,
295 statusCodeExpected: HttpStatusCode.OK_200
296 })
297
298 expect(res.body.data).to.be.an('array')
299 })
300
301 it('Should return 404 with an incorrect video channel', async function () {
302 await makeGetRequest({
303 url: server.url,
304 path: videoChannelPath + '/super_channel2',
305 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
306 })
307 })
308
309 it('Should succeed with the correct parameters', async function () {
310 await makeGetRequest({
311 url: server.url,
312 path: videoChannelPath + '/super_channel',
313 statusCodeExpected: HttpStatusCode.OK_200
314 })
315 })
316 })
317
318 describe('When deleting a video channel', function () {
319 it('Should fail with a non authenticated user', async function () {
320 await deleteVideoChannel(server.url, 'coucou', 'super_channel', HttpStatusCode.UNAUTHORIZED_401)
321 })
322
323 it('Should fail with another authenticated user', async function () {
324 await deleteVideoChannel(server.url, accessTokenUser, 'super_channel', HttpStatusCode.FORBIDDEN_403)
325 })
326
327 it('Should fail with an unknown video channel id', async function () {
328 await deleteVideoChannel(server.url, server.accessToken, 'super_channel2', HttpStatusCode.NOT_FOUND_404)
329 })
330
331 it('Should succeed with the correct parameters', async function () {
332 await deleteVideoChannel(server.url, server.accessToken, 'super_channel')
333 })
334
335 it('Should fail to delete the last user video channel', async function () {
336 await deleteVideoChannel(server.url, server.accessToken, 'root_channel', HttpStatusCode.CONFLICT_409)
337 })
338 })
339
340 after(async function () {
341 await cleanupTests([ server ])
342 })
343 })