1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
5 import { buildAbsoluteFixturePath, omit } from '@shared/core-utils'
6 import { HttpStatusCode, VideoChannelUpdate } from '@shared/models'
16 setAccessTokensToServers
17 } from '@shared/server-commands'
19 describe('Test video channels API validator', function () {
20 const videoChannelPath = '/api/v1/video-channels'
21 let server: PeerTubeServer
24 channelName: 'fake_channel',
29 let command: ChannelsCommand
31 // ---------------------------------------------------------------
33 before(async function () {
36 server = await createSingleServer(1)
38 await setAccessTokensToServers([ server ])
42 password: 'fake_password'
46 const user = await server.users.create({ username: userCreds.username, password: userCreds.password })
48 userInfo.accessToken = await server.login.getAccessToken(userCreds)
51 command = server.channels
54 describe('When listing a video channels', function () {
55 it('Should fail with a bad start pagination', async function () {
56 await checkBadStartPagination(server.url, videoChannelPath, server.accessToken)
59 it('Should fail with a bad count pagination', async function () {
60 await checkBadCountPagination(server.url, videoChannelPath, server.accessToken)
63 it('Should fail with an incorrect sort', async function () {
64 await checkBadSortPagination(server.url, videoChannelPath, server.accessToken)
68 describe('When listing account video channels', function () {
69 const accountChannelPath = '/api/v1/accounts/fake/video-channels'
71 it('Should fail with a bad start pagination', async function () {
72 await checkBadStartPagination(server.url, accountChannelPath, server.accessToken)
75 it('Should fail with a bad count pagination', async function () {
76 await checkBadCountPagination(server.url, accountChannelPath, server.accessToken)
79 it('Should fail with an incorrect sort', async function () {
80 await checkBadSortPagination(server.url, accountChannelPath, server.accessToken)
83 it('Should fail with a unknown account', async function () {
84 await server.channels.listByAccount({ accountName: 'unknown', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
87 it('Should succeed with the correct parameters', async function () {
88 await makeGetRequest({
90 path: accountChannelPath,
91 expectedStatus: HttpStatusCode.OK_200
96 describe('When adding a video channel', function () {
97 const baseCorrectParams = {
98 name: 'super_channel',
100 description: 'super description',
101 support: 'super support text'
104 it('Should fail with a non authenticated user', async function () {
105 await makePostBodyRequest({
107 path: videoChannelPath,
109 fields: baseCorrectParams,
110 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
114 it('Should fail with nothing', async function () {
116 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
119 it('Should fail without a name', async function () {
120 const fields = omit(baseCorrectParams, [ 'name' ])
121 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
124 it('Should fail with a bad name', async function () {
125 const fields = { ...baseCorrectParams, name: 'super name' }
126 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
129 it('Should fail without a name', async function () {
130 const fields = omit(baseCorrectParams, [ 'displayName' ])
131 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
134 it('Should fail with a long name', async function () {
135 const fields = { ...baseCorrectParams, displayName: 'super'.repeat(25) }
136 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
139 it('Should fail with a long description', async function () {
140 const fields = { ...baseCorrectParams, description: 'super'.repeat(201) }
141 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
144 it('Should fail with a long support text', async function () {
145 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
146 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
149 it('Should succeed with the correct parameters', async function () {
150 await makePostBodyRequest({
152 path: videoChannelPath,
153 token: server.accessToken,
154 fields: baseCorrectParams,
155 expectedStatus: HttpStatusCode.OK_200
159 it('Should fail when adding a channel with the same username', async function () {
160 await makePostBodyRequest({
162 path: videoChannelPath,
163 token: server.accessToken,
164 fields: baseCorrectParams,
165 expectedStatus: HttpStatusCode.CONFLICT_409
170 describe('When updating a video channel', function () {
171 const baseCorrectParams: VideoChannelUpdate = {
172 displayName: 'hello',
173 description: 'super description',
175 bulkVideosSupportUpdate: false
179 before(async function () {
180 path = videoChannelPath + '/super_channel'
183 it('Should fail with a non authenticated user', async function () {
184 await makePutBodyRequest({
188 fields: baseCorrectParams,
189 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
193 it('Should fail with another authenticated user', async function () {
194 await makePutBodyRequest({
197 token: userInfo.accessToken,
198 fields: baseCorrectParams,
199 expectedStatus: HttpStatusCode.FORBIDDEN_403
203 it('Should fail with a long name', async function () {
204 const fields = { ...baseCorrectParams, displayName: 'super'.repeat(25) }
205 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
208 it('Should fail with a long description', async function () {
209 const fields = { ...baseCorrectParams, description: 'super'.repeat(201) }
210 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
213 it('Should fail with a long support text', async function () {
214 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
215 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
218 it('Should fail with a bad bulkVideosSupportUpdate field', async function () {
219 const fields = { ...baseCorrectParams, bulkVideosSupportUpdate: 'super' }
220 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
223 it('Should succeed with the correct parameters', async function () {
224 await makePutBodyRequest({
227 token: server.accessToken,
228 fields: baseCorrectParams,
229 expectedStatus: HttpStatusCode.NO_CONTENT_204
234 describe('When updating video channel avatars/banners', function () {
235 const types = [ 'avatar', 'banner' ]
238 before(async function () {
239 path = videoChannelPath + '/super_channel'
242 it('Should fail with an incorrect input file', async function () {
243 for (const type of types) {
246 [type + 'file']: buildAbsoluteFixturePath('video_short.mp4')
249 await makeUploadRequest({ url: server.url, path: `${path}/${type}/pick`, token: server.accessToken, fields, attaches })
253 it('Should fail with a big file', async function () {
254 for (const type of types) {
257 [type + 'file']: buildAbsoluteFixturePath('avatar-big.png')
259 await makeUploadRequest({ url: server.url, path: `${path}/${type}/pick`, token: server.accessToken, fields, attaches })
263 it('Should fail with an unauthenticated user', async function () {
264 for (const type of types) {
267 [type + 'file']: buildAbsoluteFixturePath('avatar.png')
269 await makeUploadRequest({
271 path: `${path}/${type}/pick`,
274 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
279 it('Should succeed with the correct params', async function () {
280 for (const type of types) {
283 [type + 'file']: buildAbsoluteFixturePath('avatar.png')
285 await makeUploadRequest({
287 path: `${path}/${type}/pick`,
288 token: server.accessToken,
291 expectedStatus: HttpStatusCode.OK_200
297 describe('When getting a video channel', function () {
298 it('Should return the list of the video channels with nothing', async function () {
299 const res = await makeGetRequest({
301 path: videoChannelPath,
302 expectedStatus: HttpStatusCode.OK_200
305 expect(res.body.data).to.be.an('array')
308 it('Should return 404 with an incorrect video channel', async function () {
309 await makeGetRequest({
311 path: videoChannelPath + '/super_channel2',
312 expectedStatus: HttpStatusCode.NOT_FOUND_404
316 it('Should succeed with the correct parameters', async function () {
317 await makeGetRequest({
319 path: videoChannelPath + '/super_channel',
320 expectedStatus: HttpStatusCode.OK_200
325 describe('When getting channel followers', function () {
326 const path = '/api/v1/video-channels/super_channel/followers'
328 it('Should fail with a bad start pagination', async function () {
329 await checkBadStartPagination(server.url, path, server.accessToken)
332 it('Should fail with a bad count pagination', async function () {
333 await checkBadCountPagination(server.url, path, server.accessToken)
336 it('Should fail with an incorrect sort', async function () {
337 await checkBadSortPagination(server.url, path, server.accessToken)
340 it('Should fail with a unauthenticated user', async function () {
341 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
344 it('Should fail with a another user', async function () {
345 await makeGetRequest({ url: server.url, path, token: userInfo.accessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
348 it('Should succeed with the correct params', async function () {
349 await makeGetRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.OK_200 })
353 describe('When deleting a video channel', function () {
354 it('Should fail with a non authenticated user', async function () {
355 await command.delete({ token: 'coucou', channelName: 'super_channel', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
358 it('Should fail with another authenticated user', async function () {
359 await command.delete({ token: userInfo.accessToken, channelName: 'super_channel', expectedStatus: HttpStatusCode.FORBIDDEN_403 })
362 it('Should fail with an unknown video channel id', async function () {
363 await command.delete({ channelName: 'super_channel2', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
366 it('Should succeed with the correct parameters', async function () {
367 await command.delete({ channelName: 'super_channel' })
370 it('Should fail to delete the last user video channel', async function () {
371 await command.delete({ channelName: 'root_channel', expectedStatus: HttpStatusCode.CONFLICT_409 })
375 after(async function () {
376 await cleanupTests([ server ])