]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/video-channels.ts
Reorganize imports
[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 'mocha'
4 import * as chai from 'chai'
5 import { omit } from 'lodash'
6 import {
7 buildAbsoluteFixturePath,
8 ChannelsCommand,
9 checkBadCountPagination,
10 checkBadSortPagination,
11 checkBadStartPagination,
12 cleanupTests,
13 createSingleServer,
14 makeGetRequest,
15 makePostBodyRequest,
16 makePutBodyRequest,
17 makeUploadRequest,
18 PeerTubeServer,
19 setAccessTokensToServers
20 } from '@shared/extra-utils'
21 import { HttpStatusCode, VideoChannelUpdate } from '@shared/models'
22
23 const expect = chai.expect
24
25 describe('Test video channels API validator', function () {
26 const videoChannelPath = '/api/v1/video-channels'
27 let server: PeerTubeServer
28 let accessTokenUser: string
29 let command: ChannelsCommand
30
31 // ---------------------------------------------------------------
32
33 before(async function () {
34 this.timeout(30000)
35
36 server = await createSingleServer(1)
37
38 await setAccessTokensToServers([ server ])
39
40 const user = {
41 username: 'fake',
42 password: 'fake_password'
43 }
44
45 {
46 await server.users.create({ username: user.username, password: user.password })
47 accessTokenUser = await server.login.getAccessToken(user)
48 }
49
50 command = server.channels
51 })
52
53 describe('When listing a video channels', function () {
54 it('Should fail with a bad start pagination', async function () {
55 await checkBadStartPagination(server.url, videoChannelPath, server.accessToken)
56 })
57
58 it('Should fail with a bad count pagination', async function () {
59 await checkBadCountPagination(server.url, videoChannelPath, server.accessToken)
60 })
61
62 it('Should fail with an incorrect sort', async function () {
63 await checkBadSortPagination(server.url, videoChannelPath, server.accessToken)
64 })
65 })
66
67 describe('When listing account video channels', function () {
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
82 it('Should fail with a unknown account', async function () {
83 await server.channels.listByAccount({ accountName: 'unknown', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
84 })
85
86 it('Should succeed with the correct parameters', async function () {
87 await makeGetRequest({
88 url: server.url,
89 path: accountChannelPath,
90 expectedStatus: HttpStatusCode.OK_200
91 })
92 })
93 })
94
95 describe('When adding a video channel', function () {
96 const baseCorrectParams = {
97 name: 'super_channel',
98 displayName: 'hello',
99 description: 'super description',
100 support: 'super support text'
101 }
102
103 it('Should fail with a non authenticated user', async function () {
104 await makePostBodyRequest({
105 url: server.url,
106 path: videoChannelPath,
107 token: 'none',
108 fields: baseCorrectParams,
109 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
110 })
111 })
112
113 it('Should fail with nothing', async function () {
114 const fields = {}
115 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
116 })
117
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 () {
124 const fields = { ...baseCorrectParams, name: 'super name' }
125 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
126 })
127
128 it('Should fail without a name', async function () {
129 const fields = omit(baseCorrectParams, 'displayName')
130 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
131 })
132
133 it('Should fail with a long name', async function () {
134 const fields = { ...baseCorrectParams, displayName: 'super'.repeat(25) }
135 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
136 })
137
138 it('Should fail with a long description', async function () {
139 const fields = { ...baseCorrectParams, description: 'super'.repeat(201) }
140 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
141 })
142
143 it('Should fail with a long support text', async function () {
144 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
145 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
146 })
147
148 it('Should succeed with the correct parameters', async function () {
149 await makePostBodyRequest({
150 url: server.url,
151 path: videoChannelPath,
152 token: server.accessToken,
153 fields: baseCorrectParams,
154 expectedStatus: HttpStatusCode.OK_200
155 })
156 })
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,
164 expectedStatus: HttpStatusCode.CONFLICT_409
165 })
166 })
167 })
168
169 describe('When updating a video channel', function () {
170 const baseCorrectParams: VideoChannelUpdate = {
171 displayName: 'hello',
172 description: 'super description',
173 support: 'toto',
174 bulkVideosSupportUpdate: false
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 expectedStatus: HttpStatusCode.UNAUTHORIZED_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 expectedStatus: HttpStatusCode.FORBIDDEN_403
199 })
200 })
201
202 it('Should fail with a long name', async function () {
203 const fields = { ...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 = { ...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 = { ...baseCorrectParams, support: 'super'.repeat(201) }
214 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
215 })
216
217 it('Should fail with a bad bulkVideosSupportUpdate field', async function () {
218 const fields = { ...baseCorrectParams, bulkVideosSupportUpdate: 'super' }
219 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
220 })
221
222 it('Should succeed with the correct parameters', async function () {
223 await makePutBodyRequest({
224 url: server.url,
225 path,
226 token: server.accessToken,
227 fields: baseCorrectParams,
228 expectedStatus: HttpStatusCode.NO_CONTENT_204
229 })
230 })
231 })
232
233 describe('When updating video channel avatar/banner', function () {
234 const types = [ 'avatar', 'banner' ]
235 let path: string
236
237 before(async function () {
238 path = videoChannelPath + '/super_channel'
239 })
240
241 it('Should fail with an incorrect input file', async function () {
242 for (const type of types) {
243 const fields = {}
244 const attaches = {
245 [type + 'file']: buildAbsoluteFixturePath('video_short.mp4')
246 }
247
248 await makeUploadRequest({ url: server.url, path: `${path}/${type}/pick`, token: server.accessToken, fields, attaches })
249 }
250 })
251
252 it('Should fail with a big file', async function () {
253 for (const type of types) {
254 const fields = {}
255 const attaches = {
256 [type + 'file']: buildAbsoluteFixturePath('avatar-big.png')
257 }
258 await makeUploadRequest({ url: server.url, path: `${path}/${type}/pick`, token: server.accessToken, fields, attaches })
259 }
260 })
261
262 it('Should fail with an unauthenticated user', async function () {
263 for (const type of types) {
264 const fields = {}
265 const attaches = {
266 [type + 'file']: buildAbsoluteFixturePath('avatar.png')
267 }
268 await makeUploadRequest({
269 url: server.url,
270 path: `${path}/${type}/pick`,
271 fields,
272 attaches,
273 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
274 })
275 }
276 })
277
278 it('Should succeed with the correct params', async function () {
279 for (const type of types) {
280 const fields = {}
281 const attaches = {
282 [type + 'file']: buildAbsoluteFixturePath('avatar.png')
283 }
284 await makeUploadRequest({
285 url: server.url,
286 path: `${path}/${type}/pick`,
287 token: server.accessToken,
288 fields,
289 attaches,
290 expectedStatus: HttpStatusCode.OK_200
291 })
292 }
293 })
294 })
295
296 describe('When getting a video channel', function () {
297 it('Should return the list of the video channels with nothing', async function () {
298 const res = await makeGetRequest({
299 url: server.url,
300 path: videoChannelPath,
301 expectedStatus: HttpStatusCode.OK_200
302 })
303
304 expect(res.body.data).to.be.an('array')
305 })
306
307 it('Should return 404 with an incorrect video channel', async function () {
308 await makeGetRequest({
309 url: server.url,
310 path: videoChannelPath + '/super_channel2',
311 expectedStatus: HttpStatusCode.NOT_FOUND_404
312 })
313 })
314
315 it('Should succeed with the correct parameters', async function () {
316 await makeGetRequest({
317 url: server.url,
318 path: videoChannelPath + '/super_channel',
319 expectedStatus: HttpStatusCode.OK_200
320 })
321 })
322 })
323
324 describe('When deleting a video channel', function () {
325 it('Should fail with a non authenticated user', async function () {
326 await command.delete({ token: 'coucou', channelName: 'super_channel', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
327 })
328
329 it('Should fail with another authenticated user', async function () {
330 await command.delete({ token: accessTokenUser, channelName: 'super_channel', expectedStatus: HttpStatusCode.FORBIDDEN_403 })
331 })
332
333 it('Should fail with an unknown video channel id', async function () {
334 await command.delete({ channelName: 'super_channel2', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
335 })
336
337 it('Should succeed with the correct parameters', async function () {
338 await command.delete({ channelName: 'super_channel' })
339 })
340
341 it('Should fail to delete the last user video channel', async function () {
342 await command.delete({ channelName: 'root_channel', expectedStatus: HttpStatusCode.CONFLICT_409 })
343 })
344 })
345
346 after(async function () {
347 await cleanupTests([ server ])
348 })
349 })