1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { omit } from 'lodash'
5 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination, FIXTURE_URLS } from '@server/tests/shared'
6 import { buildAbsoluteFixturePath } from '@shared/core-utils'
7 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
15 setAccessTokensToServers,
16 setDefaultVideoChannel,
18 } from '@shared/server-commands'
20 describe('Test video imports API validator', function () {
21 const path = '/api/v1/videos/imports'
22 let server: PeerTubeServer
23 let userAccessToken = ''
26 // ---------------------------------------------------------------
28 before(async function () {
31 server = await createSingleServer(1)
33 await setAccessTokensToServers([ server ])
34 await setDefaultVideoChannel([ server ])
36 const username = 'user1'
37 const password = 'my super password'
38 await server.users.create({ username: username, password: password })
39 userAccessToken = await server.login.getAccessToken({ username, password })
42 const { videoChannels } = await server.users.getMyInfo()
43 channelId = videoChannels[0].id
47 describe('When listing my video imports', function () {
48 const myPath = '/api/v1/users/me/videos/imports'
50 it('Should fail with a bad start pagination', async function () {
51 await checkBadStartPagination(server.url, myPath, server.accessToken)
54 it('Should fail with a bad count pagination', async function () {
55 await checkBadCountPagination(server.url, myPath, server.accessToken)
58 it('Should fail with an incorrect sort', async function () {
59 await checkBadSortPagination(server.url, myPath, server.accessToken)
62 it('Should success with the correct parameters', async function () {
63 await makeGetRequest({ url: server.url, path: myPath, expectedStatus: HttpStatusCode.OK_200, token: server.accessToken })
67 describe('When adding a video import', function () {
72 targetUrl: FIXTURE_URLS.goodVideo,
73 name: 'my super name',
78 commentsEnabled: true,
79 downloadEnabled: true,
80 waitTranscoding: true,
81 description: 'my super description',
82 support: 'my super support text',
83 tags: [ 'tag1', 'tag2' ],
84 privacy: VideoPrivacy.PUBLIC,
89 it('Should fail with nothing', async function () {
91 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
94 it('Should fail without a target url', async function () {
95 const fields = omit(baseCorrectParams, 'targetUrl')
96 await makePostBodyRequest({
99 token: server.accessToken,
101 expectedStatus: HttpStatusCode.BAD_REQUEST_400
105 it('Should fail with a bad target url', async function () {
106 const fields = { ...baseCorrectParams, targetUrl: 'htt://hello' }
108 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
111 it('Should fail with localhost', async function () {
112 const fields = { ...baseCorrectParams, targetUrl: 'http://localhost:8000' }
114 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
117 it('Should fail with a private IP target urls', async function () {
119 'http://127.0.0.1:8000',
121 'http://127.0.0.1/hello',
122 'https://192.168.1.42',
123 'http://192.168.1.42'
126 for (const targetUrl of targetUrls) {
127 const fields = { ...baseCorrectParams, targetUrl }
129 await makePostBodyRequest({
132 token: server.accessToken,
134 expectedStatus: HttpStatusCode.FORBIDDEN_403
139 it('Should fail with a long name', async function () {
140 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
142 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
145 it('Should fail with a bad category', async function () {
146 const fields = { ...baseCorrectParams, category: 125 }
148 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
151 it('Should fail with a bad licence', async function () {
152 const fields = { ...baseCorrectParams, licence: 125 }
154 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
157 it('Should fail with a bad language', async function () {
158 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
160 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
163 it('Should fail with a long description', async function () {
164 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
166 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
169 it('Should fail with a long support text', async function () {
170 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
172 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
175 it('Should fail without a channel', async function () {
176 const fields = omit(baseCorrectParams, 'channelId')
178 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
181 it('Should fail with a bad channel', async function () {
182 const fields = { ...baseCorrectParams, channelId: 545454 }
184 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
187 it('Should fail with another user channel', async function () {
190 password: 'fake_password'
192 await server.users.create({ username: user.username, password: user.password })
194 const accessTokenUser = await server.login.getAccessToken(user)
195 const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
196 const customChannelId = videoChannels[0].id
198 const fields = { ...baseCorrectParams, channelId: customChannelId }
200 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
203 it('Should fail with too many tags', async function () {
204 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
206 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
209 it('Should fail with a tag length too low', async function () {
210 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
212 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
215 it('Should fail with a tag length too big', async function () {
216 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
218 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
221 it('Should fail with an incorrect thumbnail file', async function () {
222 const fields = baseCorrectParams
224 thumbnailfile: buildAbsoluteFixturePath('video_short.mp4')
227 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
230 it('Should fail with a big thumbnail file', async function () {
231 const fields = baseCorrectParams
233 thumbnailfile: buildAbsoluteFixturePath('preview-big.png')
236 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
239 it('Should fail with an incorrect preview file', async function () {
240 const fields = baseCorrectParams
242 previewfile: buildAbsoluteFixturePath('video_short.mp4')
245 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
248 it('Should fail with a big preview file', async function () {
249 const fields = baseCorrectParams
251 previewfile: buildAbsoluteFixturePath('preview-big.png')
254 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
257 it('Should fail with an invalid torrent file', async function () {
258 const fields = omit(baseCorrectParams, 'targetUrl')
260 torrentfile: buildAbsoluteFixturePath('avatar-big.png')
263 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
266 it('Should fail with an invalid magnet URI', async function () {
267 let fields = omit(baseCorrectParams, 'targetUrl')
268 fields = { ...fields, magnetUri: 'blabla' }
270 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
273 it('Should succeed with the correct parameters', async function () {
276 await makePostBodyRequest({
279 token: server.accessToken,
280 fields: baseCorrectParams,
281 expectedStatus: HttpStatusCode.OK_200
285 it('Should forbid to import http videos', async function () {
286 await server.config.updateCustomSubConfig({
301 await makePostBodyRequest({
304 token: server.accessToken,
305 fields: baseCorrectParams,
306 expectedStatus: HttpStatusCode.CONFLICT_409
310 it('Should forbid to import torrent videos', async function () {
311 await server.config.updateCustomSubConfig({
326 let fields = omit(baseCorrectParams, 'targetUrl')
327 fields = { ...fields, magnetUri: FIXTURE_URLS.magnet }
329 await makePostBodyRequest({
332 token: server.accessToken,
334 expectedStatus: HttpStatusCode.CONFLICT_409
337 fields = omit(fields, 'magnetUri')
339 torrentfile: buildAbsoluteFixturePath('video-720p.torrent')
342 await makeUploadRequest({
345 token: server.accessToken,
348 expectedStatus: HttpStatusCode.CONFLICT_409
353 describe('Deleting/cancelling a video import', function () {
356 async function importVideo () {
357 const attributes = { channelId: server.store.channel.id, targetUrl: FIXTURE_URLS.goodVideo }
358 const res = await server.imports.importVideo({ attributes })
363 before(async function () {
364 importId = await importVideo()
367 it('Should fail with an invalid import id', async function () {
368 await server.imports.cancel({ importId: 'artyom' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
369 await server.imports.delete({ importId: 'artyom' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
372 it('Should fail with an unknown import id', async function () {
373 await server.imports.cancel({ importId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
374 await server.imports.delete({ importId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
377 it('Should fail without token', async function () {
378 await server.imports.cancel({ importId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
379 await server.imports.delete({ importId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
382 it('Should fail with another user token', async function () {
383 await server.imports.cancel({ importId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
384 await server.imports.delete({ importId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
387 it('Should fail to cancel non pending import', async function () {
390 await waitJobs([ server ])
392 await server.imports.cancel({ importId, expectedStatus: HttpStatusCode.CONFLICT_409 })
395 it('Should succeed to delete an import', async function () {
396 await server.imports.delete({ importId })
399 it('Should fail to delete a pending import', async function () {
400 await server.jobs.pauseJobQueue()
402 importId = await importVideo()
404 await server.imports.delete({ importId, expectedStatus: HttpStatusCode.CONFLICT_409 })
407 it('Should succeed to cancel an import', async function () {
408 importId = await importVideo()
410 await server.imports.cancel({ importId })
414 after(async function () {
415 await cleanupTests([ server ])