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',
124 'http://127.0.0.1.cpy.re'
127 for (const targetUrl of targetUrls) {
128 const fields = { ...baseCorrectParams, targetUrl }
130 await makePostBodyRequest({
133 token: server.accessToken,
135 expectedStatus: HttpStatusCode.FORBIDDEN_403
140 it('Should fail with a long name', async function () {
141 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
143 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
146 it('Should fail with a bad category', async function () {
147 const fields = { ...baseCorrectParams, category: 125 }
149 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
152 it('Should fail with a bad licence', async function () {
153 const fields = { ...baseCorrectParams, licence: 125 }
155 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
158 it('Should fail with a bad language', async function () {
159 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
161 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
164 it('Should fail with a long description', async function () {
165 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
167 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
170 it('Should fail with a long support text', async function () {
171 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
173 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
176 it('Should fail without a channel', async function () {
177 const fields = omit(baseCorrectParams, 'channelId')
179 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
182 it('Should fail with a bad channel', async function () {
183 const fields = { ...baseCorrectParams, channelId: 545454 }
185 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
188 it('Should fail with another user channel', async function () {
191 password: 'fake_password'
193 await server.users.create({ username: user.username, password: user.password })
195 const accessTokenUser = await server.login.getAccessToken(user)
196 const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
197 const customChannelId = videoChannels[0].id
199 const fields = { ...baseCorrectParams, channelId: customChannelId }
201 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
204 it('Should fail with too many tags', async function () {
205 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
207 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
210 it('Should fail with a tag length too low', async function () {
211 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
213 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
216 it('Should fail with a tag length too big', async function () {
217 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
219 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
222 it('Should fail with an incorrect thumbnail file', async function () {
223 const fields = baseCorrectParams
225 thumbnailfile: buildAbsoluteFixturePath('video_short.mp4')
228 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
231 it('Should fail with a big thumbnail file', async function () {
232 const fields = baseCorrectParams
234 thumbnailfile: buildAbsoluteFixturePath('preview-big.png')
237 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
240 it('Should fail with an incorrect preview file', async function () {
241 const fields = baseCorrectParams
243 previewfile: buildAbsoluteFixturePath('video_short.mp4')
246 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
249 it('Should fail with a big preview file', async function () {
250 const fields = baseCorrectParams
252 previewfile: buildAbsoluteFixturePath('preview-big.png')
255 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
258 it('Should fail with an invalid torrent file', async function () {
259 const fields = omit(baseCorrectParams, 'targetUrl')
261 torrentfile: buildAbsoluteFixturePath('avatar-big.png')
264 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
267 it('Should fail with an invalid magnet URI', async function () {
268 let fields = omit(baseCorrectParams, 'targetUrl')
269 fields = { ...fields, magnetUri: 'blabla' }
271 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
274 it('Should succeed with the correct parameters', async function () {
277 await makePostBodyRequest({
280 token: server.accessToken,
281 fields: baseCorrectParams,
282 expectedStatus: HttpStatusCode.OK_200
286 it('Should forbid to import http videos', async function () {
287 await server.config.updateCustomSubConfig({
302 await makePostBodyRequest({
305 token: server.accessToken,
306 fields: baseCorrectParams,
307 expectedStatus: HttpStatusCode.CONFLICT_409
311 it('Should forbid to import torrent videos', async function () {
312 await server.config.updateCustomSubConfig({
327 let fields = omit(baseCorrectParams, 'targetUrl')
328 fields = { ...fields, magnetUri: FIXTURE_URLS.magnet }
330 await makePostBodyRequest({
333 token: server.accessToken,
335 expectedStatus: HttpStatusCode.CONFLICT_409
338 fields = omit(fields, 'magnetUri')
340 torrentfile: buildAbsoluteFixturePath('video-720p.torrent')
343 await makeUploadRequest({
346 token: server.accessToken,
349 expectedStatus: HttpStatusCode.CONFLICT_409
354 describe('Deleting/cancelling a video import', function () {
357 async function importVideo () {
358 const attributes = { channelId: server.store.channel.id, targetUrl: FIXTURE_URLS.goodVideo }
359 const res = await server.imports.importVideo({ attributes })
364 before(async function () {
365 importId = await importVideo()
368 it('Should fail with an invalid import id', async function () {
369 await server.imports.cancel({ importId: 'artyom' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
370 await server.imports.delete({ importId: 'artyom' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
373 it('Should fail with an unknown import id', async function () {
374 await server.imports.cancel({ importId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
375 await server.imports.delete({ importId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
378 it('Should fail without token', async function () {
379 await server.imports.cancel({ importId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
380 await server.imports.delete({ importId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
383 it('Should fail with another user token', async function () {
384 await server.imports.cancel({ importId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
385 await server.imports.delete({ importId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
388 it('Should fail to cancel non pending import', async function () {
391 await waitJobs([ server ])
393 await server.imports.cancel({ importId, expectedStatus: HttpStatusCode.CONFLICT_409 })
396 it('Should succeed to delete an import', async function () {
397 await server.imports.delete({ importId })
400 it('Should fail to delete a pending import', async function () {
401 await server.jobs.pauseJobQueue()
403 importId = await importVideo()
405 await server.imports.delete({ importId, expectedStatus: HttpStatusCode.CONFLICT_409 })
408 it('Should succeed to cancel an import', async function () {
409 importId = await importVideo()
411 await server.imports.cancel({ importId })
415 after(async function () {
416 await cleanupTests([ server ])