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, FIXTURE_URLS } from '@server/tests/shared'
7 import { areHttpImportTestsDisabled, 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
28 channelName: 'fake_channel',
33 let command: ChannelsCommand
35 // ---------------------------------------------------------------
37 before(async function () {
40 server = await createSingleServer(1)
42 await setAccessTokensToServers([ server ])
46 password: 'fake_password'
50 const user = await server.users.create({ username: userCreds.username, password: userCreds.password })
52 userInfo.accessToken = await server.login.getAccessToken(userCreds)
55 command = server.channels
58 describe('When listing a video channels', function () {
59 it('Should fail with a bad start pagination', async function () {
60 await checkBadStartPagination(server.url, videoChannelPath, server.accessToken)
63 it('Should fail with a bad count pagination', async function () {
64 await checkBadCountPagination(server.url, videoChannelPath, server.accessToken)
67 it('Should fail with an incorrect sort', async function () {
68 await checkBadSortPagination(server.url, videoChannelPath, server.accessToken)
72 describe('When listing account video channels', function () {
73 const accountChannelPath = '/api/v1/accounts/fake/video-channels'
75 it('Should fail with a bad start pagination', async function () {
76 await checkBadStartPagination(server.url, accountChannelPath, server.accessToken)
79 it('Should fail with a bad count pagination', async function () {
80 await checkBadCountPagination(server.url, accountChannelPath, server.accessToken)
83 it('Should fail with an incorrect sort', async function () {
84 await checkBadSortPagination(server.url, accountChannelPath, server.accessToken)
87 it('Should fail with a unknown account', async function () {
88 await server.channels.listByAccount({ accountName: 'unknown', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
91 it('Should succeed with the correct parameters', async function () {
92 await makeGetRequest({
94 path: accountChannelPath,
95 expectedStatus: HttpStatusCode.OK_200
100 describe('When adding a video channel', function () {
101 const baseCorrectParams = {
102 name: 'super_channel',
103 displayName: 'hello',
104 description: 'super description',
105 support: 'super support text'
108 it('Should fail with a non authenticated user', async function () {
109 await makePostBodyRequest({
111 path: videoChannelPath,
113 fields: baseCorrectParams,
114 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
118 it('Should fail with nothing', async function () {
120 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
123 it('Should fail without a name', async function () {
124 const fields = omit(baseCorrectParams, 'name')
125 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
128 it('Should fail with a bad name', async function () {
129 const fields = { ...baseCorrectParams, name: 'super name' }
130 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
133 it('Should fail without a name', async function () {
134 const fields = omit(baseCorrectParams, 'displayName')
135 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
138 it('Should fail with a long name', async function () {
139 const fields = { ...baseCorrectParams, displayName: 'super'.repeat(25) }
140 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
143 it('Should fail with a long description', async function () {
144 const fields = { ...baseCorrectParams, description: 'super'.repeat(201) }
145 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
148 it('Should fail with a long support text', async function () {
149 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
150 await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
153 it('Should succeed with the correct parameters', async function () {
154 await makePostBodyRequest({
156 path: videoChannelPath,
157 token: server.accessToken,
158 fields: baseCorrectParams,
159 expectedStatus: HttpStatusCode.OK_200
163 it('Should fail when adding a channel with the same username', async function () {
164 await makePostBodyRequest({
166 path: videoChannelPath,
167 token: server.accessToken,
168 fields: baseCorrectParams,
169 expectedStatus: HttpStatusCode.CONFLICT_409
174 describe('When updating a video channel', function () {
175 const baseCorrectParams: VideoChannelUpdate = {
176 displayName: 'hello',
177 description: 'super description',
179 bulkVideosSupportUpdate: false
183 before(async function () {
184 path = videoChannelPath + '/super_channel'
187 it('Should fail with a non authenticated user', async function () {
188 await makePutBodyRequest({
192 fields: baseCorrectParams,
193 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
197 it('Should fail with another authenticated user', async function () {
198 await makePutBodyRequest({
201 token: userInfo.accessToken,
202 fields: baseCorrectParams,
203 expectedStatus: HttpStatusCode.FORBIDDEN_403
207 it('Should fail with a long name', async function () {
208 const fields = { ...baseCorrectParams, displayName: 'super'.repeat(25) }
209 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
212 it('Should fail with a long description', async function () {
213 const fields = { ...baseCorrectParams, description: 'super'.repeat(201) }
214 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
217 it('Should fail with a long support text', async function () {
218 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
219 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
222 it('Should fail with a bad bulkVideosSupportUpdate field', async function () {
223 const fields = { ...baseCorrectParams, bulkVideosSupportUpdate: 'super' }
224 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
227 it('Should succeed with the correct parameters', async function () {
228 await makePutBodyRequest({
231 token: server.accessToken,
232 fields: baseCorrectParams,
233 expectedStatus: HttpStatusCode.NO_CONTENT_204
238 describe('When updating video channel avatars/banners', function () {
239 const types = [ 'avatar', 'banner' ]
242 before(async function () {
243 path = videoChannelPath + '/super_channel'
246 it('Should fail with an incorrect input file', async function () {
247 for (const type of types) {
250 [type + 'file']: buildAbsoluteFixturePath('video_short.mp4')
253 await makeUploadRequest({ url: server.url, path: `${path}/${type}/pick`, token: server.accessToken, fields, attaches })
257 it('Should fail with a big file', async function () {
258 for (const type of types) {
261 [type + 'file']: buildAbsoluteFixturePath('avatar-big.png')
263 await makeUploadRequest({ url: server.url, path: `${path}/${type}/pick`, token: server.accessToken, fields, attaches })
267 it('Should fail with an unauthenticated user', async function () {
268 for (const type of types) {
271 [type + 'file']: buildAbsoluteFixturePath('avatar.png')
273 await makeUploadRequest({
275 path: `${path}/${type}/pick`,
278 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
283 it('Should succeed with the correct params', async function () {
284 for (const type of types) {
287 [type + 'file']: buildAbsoluteFixturePath('avatar.png')
289 await makeUploadRequest({
291 path: `${path}/${type}/pick`,
292 token: server.accessToken,
295 expectedStatus: HttpStatusCode.OK_200
301 describe('When getting a video channel', function () {
302 it('Should return the list of the video channels with nothing', async function () {
303 const res = await makeGetRequest({
305 path: videoChannelPath,
306 expectedStatus: HttpStatusCode.OK_200
309 expect(res.body.data).to.be.an('array')
312 it('Should return 404 with an incorrect video channel', async function () {
313 await makeGetRequest({
315 path: videoChannelPath + '/super_channel2',
316 expectedStatus: HttpStatusCode.NOT_FOUND_404
320 it('Should succeed with the correct parameters', async function () {
321 await makeGetRequest({
323 path: videoChannelPath + '/super_channel',
324 expectedStatus: HttpStatusCode.OK_200
329 describe('When getting channel followers', function () {
330 const path = '/api/v1/video-channels/super_channel/followers'
332 it('Should fail with a bad start pagination', async function () {
333 await checkBadStartPagination(server.url, path, server.accessToken)
336 it('Should fail with a bad count pagination', async function () {
337 await checkBadCountPagination(server.url, path, server.accessToken)
340 it('Should fail with an incorrect sort', async function () {
341 await checkBadSortPagination(server.url, path, server.accessToken)
344 it('Should fail with a unauthenticated user', async function () {
345 await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
348 it('Should fail with a another user', async function () {
349 await makeGetRequest({ url: server.url, path, token: userInfo.accessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
352 it('Should succeed with the correct params', async function () {
353 await makeGetRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.OK_200 })
357 describe('When triggering full synchronization', function () {
359 it('Should fail when HTTP upload is disabled', async function () {
360 await server.config.disableImports()
362 await command.importVideos({
363 channelName: 'super_channel',
364 externalChannelUrl: FIXTURE_URLS.youtubeChannel,
365 token: server.accessToken,
366 expectedStatus: HttpStatusCode.FORBIDDEN_403
369 await server.config.enableImports()
372 it('Should fail when externalChannelUrl is not provided', async function () {
373 await command.importVideos({
374 channelName: 'super_channel',
375 externalChannelUrl: null,
376 token: server.accessToken,
377 expectedStatus: HttpStatusCode.BAD_REQUEST_400
381 it('Should fail when externalChannelUrl is malformed', async function () {
382 await command.importVideos({
383 channelName: 'super_channel',
384 externalChannelUrl: 'not-a-url',
385 token: server.accessToken,
386 expectedStatus: HttpStatusCode.BAD_REQUEST_400
390 it('Should fail with no authentication', async function () {
391 await command.importVideos({
392 channelName: 'super_channel',
393 externalChannelUrl: FIXTURE_URLS.youtubeChannel,
395 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
399 it('Should fail when sync is not owned by the user', async function () {
400 await command.importVideos({
401 channelName: 'super_channel',
402 externalChannelUrl: FIXTURE_URLS.youtubeChannel,
403 token: userInfo.accessToken,
404 expectedStatus: HttpStatusCode.FORBIDDEN_403
408 it('Should fail when the user has no quota', async function () {
409 await server.users.update({
414 await command.importVideos({
415 channelName: 'fake_channel',
416 externalChannelUrl: FIXTURE_URLS.youtubeChannel,
417 token: userInfo.accessToken,
418 expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413
421 await server.users.update({
423 videoQuota: userInfo.videoQuota
427 it('Should fail when the user has no daily quota', async function () {
428 await server.users.update({
433 await command.importVideos({
434 channelName: 'fake_channel',
435 externalChannelUrl: FIXTURE_URLS.youtubeChannel,
436 token: userInfo.accessToken,
437 expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413
440 await server.users.update({
442 videoQuotaDaily: userInfo.videoQuotaDaily
446 it('Should succeed when sync is run by its owner', async function () {
447 if (!areHttpImportTestsDisabled()) return
449 await command.importVideos({
450 channelName: 'fake_channel',
451 externalChannelUrl: FIXTURE_URLS.youtubeChannel,
452 token: userInfo.accessToken
456 it('Should succeed when sync is run with root and for another user\'s channel', async function () {
457 if (!areHttpImportTestsDisabled()) return
459 await command.importVideos({
460 channelName: 'fake_channel',
461 externalChannelUrl: FIXTURE_URLS.youtubeChannel
466 describe('When deleting a video channel', function () {
467 it('Should fail with a non authenticated user', async function () {
468 await command.delete({ token: 'coucou', channelName: 'super_channel', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
471 it('Should fail with another authenticated user', async function () {
472 await command.delete({ token: userInfo.accessToken, channelName: 'super_channel', expectedStatus: HttpStatusCode.FORBIDDEN_403 })
475 it('Should fail with an unknown video channel id', async function () {
476 await command.delete({ channelName: 'super_channel2', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
479 it('Should succeed with the correct parameters', async function () {
480 await command.delete({ channelName: 'super_channel' })
483 it('Should fail to delete the last user video channel', async function () {
484 await command.delete({ channelName: 'root_channel', expectedStatus: HttpStatusCode.CONFLICT_409 })
488 after(async function () {
489 await cleanupTests([ server ])