1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { omit } from 'lodash'
6 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
7 import { buildAbsoluteFixturePath } from '@shared/core-utils'
8 import { HttpStatusCode, VideoChannelUpdate } from '@shared/models'
18 setAccessTokensToServers
19 } from '@shared/server-commands'
21 const expect = chai.expect
23 describe('Test video channels API validator', function () {
24 const videoChannelPath = '/api/v1/video-channels'
25 let server: PeerTubeServer
26 let accessTokenUser: string
27 let command: ChannelsCommand
29 // ---------------------------------------------------------------
31 before(async function () {
34 server = await createSingleServer(1)
36 await setAccessTokensToServers([ server ])
40 password: 'fake_password'
44 await server.users.create({ username: user.username, password: user.password })
45 accessTokenUser = await server.login.getAccessToken(user)
48 command = server.channels
51 describe('When listing a video channels', function () {
52 it('Should fail with a bad start pagination', async function () {
53 await checkBadStartPagination(server.url, videoChannelPath, server.accessToken)
56 it('Should fail with a bad count pagination', async function () {
57 await checkBadCountPagination(server.url, videoChannelPath, server.accessToken)
60 it('Should fail with an incorrect sort', async function () {
61 await checkBadSortPagination(server.url, videoChannelPath, server.accessToken)
65 describe('When listing account video channels', function () {
66 const accountChannelPath = '/api/v1/accounts/fake/video-channels'
68 it('Should fail with a bad start pagination', async function () {
69 await checkBadStartPagination(server.url, accountChannelPath, server.accessToken)
72 it('Should fail with a bad count pagination', async function () {
73 await checkBadCountPagination(server.url, accountChannelPath, server.accessToken)
76 it('Should fail with an incorrect sort', async function () {
77 await checkBadSortPagination(server.url, accountChannelPath, server.accessToken)
80 it('Should fail with a unknown account', async function () {
81 await server.channels.listByAccount({ accountName: 'unknown', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
84 it('Should succeed with the correct parameters', async function () {
85 await makeGetRequest({
87 path: accountChannelPath,
88 expectedStatus: HttpStatusCode.OK_200
93 describe('When adding a video channel', function () {
94 const baseCorrectParams = {
95 name: 'super_channel',
97 description: 'super description',
98 support: 'super support text'
101 it('Should fail with a non authenticated user', async function () {
102 await makePostBodyRequest({
104 path: videoChannelPath,
106 fields: baseCorrectParams,
107 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
111 it('Should fail with nothing', async function () {
113 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
116 it('Should fail without a name', async function () {
117 const fields = omit(baseCorrectParams, 'name')
118 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
121 it('Should fail with a bad name', async function () {
122 const fields = { ...baseCorrectParams, name: 'super name' }
123 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
126 it('Should fail without a name', async function () {
127 const fields = omit(baseCorrectParams, 'displayName')
128 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
131 it('Should fail with a long name', async function () {
132 const fields = { ...baseCorrectParams, displayName: 'super'.repeat(25) }
133 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
136 it('Should fail with a long description', async function () {
137 const fields = { ...baseCorrectParams, description: 'super'.repeat(201) }
138 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
141 it('Should fail with a long support text', async function () {
142 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
143 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
146 it('Should succeed with the correct parameters', async function () {
147 await makePostBodyRequest({
149 path: videoChannelPath,
150 token: server.accessToken,
151 fields: baseCorrectParams,
152 expectedStatus: HttpStatusCode.OK_200
156 it('Should fail when adding a channel with the same username', async function () {
157 await makePostBodyRequest({
159 path: videoChannelPath,
160 token: server.accessToken,
161 fields: baseCorrectParams,
162 expectedStatus: HttpStatusCode.CONFLICT_409
167 describe('When updating a video channel', function () {
168 const baseCorrectParams: VideoChannelUpdate = {
169 displayName: 'hello',
170 description: 'super description',
172 bulkVideosSupportUpdate: false
176 before(async function () {
177 path = videoChannelPath + '/super_channel'
180 it('Should fail with a non authenticated user', async function () {
181 await makePutBodyRequest({
185 fields: baseCorrectParams,
186 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
190 it('Should fail with another authenticated user', async function () {
191 await makePutBodyRequest({
194 token: accessTokenUser,
195 fields: baseCorrectParams,
196 expectedStatus: HttpStatusCode.FORBIDDEN_403
200 it('Should fail with a long name', async function () {
201 const fields = { ...baseCorrectParams, displayName: 'super'.repeat(25) }
202 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
205 it('Should fail with a long description', async function () {
206 const fields = { ...baseCorrectParams, description: 'super'.repeat(201) }
207 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
210 it('Should fail with a long support text', async function () {
211 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
212 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
215 it('Should fail with a bad bulkVideosSupportUpdate field', async function () {
216 const fields = { ...baseCorrectParams, bulkVideosSupportUpdate: 'super' }
217 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
220 it('Should succeed with the correct parameters', async function () {
221 await makePutBodyRequest({
224 token: server.accessToken,
225 fields: baseCorrectParams,
226 expectedStatus: HttpStatusCode.NO_CONTENT_204
231 describe('When updating video channel avatars/banners', function () {
232 const types = [ 'avatar', 'banner' ]
235 before(async function () {
236 path = videoChannelPath + '/super_channel'
239 it('Should fail with an incorrect input file', async function () {
240 for (const type of types) {
243 [type + 'file']: buildAbsoluteFixturePath('video_short.mp4')
246 await makeUploadRequest({ url: server.url, path: `${path}/${type}/pick`, token: server.accessToken, fields, attaches })
250 it('Should fail with a big file', async function () {
251 for (const type of types) {
254 [type + 'file']: buildAbsoluteFixturePath('avatar-big.png')
256 await makeUploadRequest({ url: server.url, path: `${path}/${type}/pick`, token: server.accessToken, fields, attaches })
260 it('Should fail with an unauthenticated user', async function () {
261 for (const type of types) {
264 [type + 'file']: buildAbsoluteFixturePath('avatar.png')
266 await makeUploadRequest({
268 path: `${path}/${type}/pick`,
271 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
276 it('Should succeed with the correct params', async function () {
277 for (const type of types) {
280 [type + 'file']: buildAbsoluteFixturePath('avatar.png')
282 await makeUploadRequest({
284 path: `${path}/${type}/pick`,
285 token: server.accessToken,
288 expectedStatus: HttpStatusCode.OK_200
294 describe('When getting a video channel', function () {
295 it('Should return the list of the video channels with nothing', async function () {
296 const res = await makeGetRequest({
298 path: videoChannelPath,
299 expectedStatus: HttpStatusCode.OK_200
302 expect(res.body.data).to.be.an('array')
305 it('Should return 404 with an incorrect video channel', async function () {
306 await makeGetRequest({
308 path: videoChannelPath + '/super_channel2',
309 expectedStatus: HttpStatusCode.NOT_FOUND_404
313 it('Should succeed with the correct parameters', async function () {
314 await makeGetRequest({
316 path: videoChannelPath + '/super_channel',
317 expectedStatus: HttpStatusCode.OK_200
322 describe('When getting channel followers', function () {
323 const path = '/api/v1/video-channels/super_channel/followers'
325 it('Should fail with a bad start pagination', async function () {
326 await checkBadStartPagination(server.url, path, server.accessToken)
329 it('Should fail with a bad count pagination', async function () {
330 await checkBadCountPagination(server.url, path, server.accessToken)
333 it('Should fail with an incorrect sort', async function () {
334 await checkBadSortPagination(server.url, path, server.accessToken)
337 it('Should fail with a unauthenticated user', async function () {
338 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
341 it('Should fail with a another user', async function () {
342 await makeGetRequest({ url: server.url, path, token: accessTokenUser, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
345 it('Should succeed with the correct params', async function () {
346 await makeGetRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.OK_200 })
350 describe('When deleting a video channel', function () {
351 it('Should fail with a non authenticated user', async function () {
352 await command.delete({ token: 'coucou', channelName: 'super_channel', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
355 it('Should fail with another authenticated user', async function () {
356 await command.delete({ token: accessTokenUser, channelName: 'super_channel', expectedStatus: HttpStatusCode.FORBIDDEN_403 })
359 it('Should fail with an unknown video channel id', async function () {
360 await command.delete({ channelName: 'super_channel2', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
363 it('Should succeed with the correct parameters', async function () {
364 await command.delete({ channelName: 'super_channel' })
367 it('Should fail to delete the last user video channel', async function () {
368 await command.delete({ channelName: 'root_channel', expectedStatus: HttpStatusCode.CONFLICT_409 })
372 after(async function () {
373 await cleanupTests([ server ])