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, 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({
94 token: server.accessToken,
96 expectedStatus: HttpStatusCode.BAD_REQUEST_400
100 it('Should fail without a target url', async function () {
101 const fields = omit(baseCorrectParams, 'targetUrl')
102 await makePostBodyRequest({
105 token: server.accessToken,
107 expectedStatus: HttpStatusCode.BAD_REQUEST_400
111 it('Should fail with a bad target url', async function () {
112 const fields = { ...baseCorrectParams, targetUrl: 'htt://hello' }
114 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
117 it('Should fail with localhost', async function () {
118 const fields = { ...baseCorrectParams, targetUrl: 'http://localhost:8000' }
120 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
123 it('Should fail with a private IP target urls', async function () {
125 'http://127.0.0.1:8000',
127 'http://127.0.0.1/hello',
128 'https://192.168.1.42',
129 'http://192.168.1.42',
130 'http://127.0.0.1.cpy.re'
133 for (const targetUrl of targetUrls) {
134 const fields = { ...baseCorrectParams, targetUrl }
136 await makePostBodyRequest({
139 token: server.accessToken,
141 expectedStatus: HttpStatusCode.FORBIDDEN_403
146 it('Should fail with a long name', async function () {
147 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
149 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
152 it('Should fail with a bad category', async function () {
153 const fields = { ...baseCorrectParams, category: 125 }
155 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
158 it('Should fail with a bad licence', async function () {
159 const fields = { ...baseCorrectParams, licence: 125 }
161 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
164 it('Should fail with a bad language', async function () {
165 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
167 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
170 it('Should fail with a long description', async function () {
171 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
173 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
176 it('Should fail with a long support text', async function () {
177 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
179 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
182 it('Should fail without a channel', async function () {
183 const fields = omit(baseCorrectParams, 'channelId')
185 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
188 it('Should fail with a bad channel', async function () {
189 const fields = { ...baseCorrectParams, channelId: 545454 }
191 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
194 it('Should fail with another user channel', async function () {
197 password: 'fake_password'
199 await server.users.create({ username: user.username, password: user.password })
201 const accessTokenUser = await server.login.getAccessToken(user)
202 const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
203 const customChannelId = videoChannels[0].id
205 const fields = { ...baseCorrectParams, channelId: customChannelId }
207 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
210 it('Should fail with too many tags', async function () {
211 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
213 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
216 it('Should fail with a tag length too low', async function () {
217 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
219 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
222 it('Should fail with a tag length too big', async function () {
223 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
225 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
228 it('Should fail with an incorrect thumbnail file', async function () {
229 const fields = baseCorrectParams
231 thumbnailfile: buildAbsoluteFixturePath('video_short.mp4')
234 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
237 it('Should fail with a big thumbnail file', async function () {
238 const fields = baseCorrectParams
240 thumbnailfile: buildAbsoluteFixturePath('preview-big.png')
243 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
246 it('Should fail with an incorrect preview file', async function () {
247 const fields = baseCorrectParams
249 previewfile: buildAbsoluteFixturePath('video_short.mp4')
252 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
255 it('Should fail with a big preview file', async function () {
256 const fields = baseCorrectParams
258 previewfile: buildAbsoluteFixturePath('preview-big.png')
261 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
264 it('Should fail with an invalid torrent file', async function () {
265 const fields = omit(baseCorrectParams, 'targetUrl')
267 torrentfile: buildAbsoluteFixturePath('avatar-big.png')
270 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
273 it('Should fail with an invalid magnet URI', async function () {
274 let fields = omit(baseCorrectParams, 'targetUrl')
275 fields = { ...fields, magnetUri: 'blabla' }
277 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
280 it('Should succeed with the correct parameters', async function () {
283 await makePostBodyRequest({
286 token: server.accessToken,
287 fields: baseCorrectParams,
288 expectedStatus: HttpStatusCode.OK_200
292 it('Should forbid to import http videos', async function () {
293 await server.config.updateCustomSubConfig({
308 await makePostBodyRequest({
311 token: server.accessToken,
312 fields: baseCorrectParams,
313 expectedStatus: HttpStatusCode.CONFLICT_409
317 it('Should forbid to import torrent videos', async function () {
318 await server.config.updateCustomSubConfig({
333 let fields = omit(baseCorrectParams, 'targetUrl')
334 fields = { ...fields, magnetUri: FIXTURE_URLS.magnet }
336 await makePostBodyRequest({
339 token: server.accessToken,
341 expectedStatus: HttpStatusCode.CONFLICT_409
344 fields = omit(fields, 'magnetUri')
346 torrentfile: buildAbsoluteFixturePath('video-720p.torrent')
349 await makeUploadRequest({
352 token: server.accessToken,
355 expectedStatus: HttpStatusCode.CONFLICT_409
360 describe('Deleting/cancelling a video import', function () {
363 async function importVideo () {
364 const attributes = { channelId: server.store.channel.id, targetUrl: FIXTURE_URLS.goodVideo }
365 const res = await server.imports.importVideo({ attributes })
370 before(async function () {
371 importId = await importVideo()
374 it('Should fail with an invalid import id', async function () {
375 await server.imports.cancel({ importId: 'artyom' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
376 await server.imports.delete({ importId: 'artyom' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
379 it('Should fail with an unknown import id', async function () {
380 await server.imports.cancel({ importId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
381 await server.imports.delete({ importId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
384 it('Should fail without token', async function () {
385 await server.imports.cancel({ importId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
386 await server.imports.delete({ importId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
389 it('Should fail with another user token', async function () {
390 await server.imports.cancel({ importId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
391 await server.imports.delete({ importId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
394 it('Should fail to cancel non pending import', async function () {
397 await waitJobs([ server ])
399 await server.imports.cancel({ importId, expectedStatus: HttpStatusCode.CONFLICT_409 })
402 it('Should succeed to delete an import', async function () {
403 await server.imports.delete({ importId })
406 it('Should fail to delete a pending import', async function () {
407 await server.jobs.pauseJobQueue()
409 importId = await importVideo()
411 await server.imports.delete({ importId, expectedStatus: HttpStatusCode.CONFLICT_409 })
414 it('Should succeed to cancel an import', async function () {
415 importId = await importVideo()
417 await server.imports.cancel({ importId })
421 after(async function () {
422 await cleanupTests([ server ])