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 fail with a bad videoChannelSyncId param', async function () {
63 await makeGetRequest({
66 query: { videoChannelSyncId: 'toto' },
67 token: server.accessToken
71 it('Should success with the correct parameters', async function () {
72 await makeGetRequest({ url: server.url, path: myPath, expectedStatus: HttpStatusCode.OK_200, token: server.accessToken })
76 describe('When adding a video import', function () {
81 targetUrl: FIXTURE_URLS.goodVideo,
82 name: 'my super name',
87 commentsEnabled: true,
88 downloadEnabled: true,
89 waitTranscoding: true,
90 description: 'my super description',
91 support: 'my super support text',
92 tags: [ 'tag1', 'tag2' ],
93 privacy: VideoPrivacy.PUBLIC,
98 it('Should fail with nothing', async function () {
100 await makePostBodyRequest({
103 token: server.accessToken,
105 expectedStatus: HttpStatusCode.BAD_REQUEST_400
109 it('Should fail without a target url', async function () {
110 const fields = omit(baseCorrectParams, 'targetUrl')
111 await makePostBodyRequest({
114 token: server.accessToken,
116 expectedStatus: HttpStatusCode.BAD_REQUEST_400
120 it('Should fail with a bad target url', async function () {
121 const fields = { ...baseCorrectParams, targetUrl: 'htt://hello' }
123 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
126 it('Should fail with localhost', async function () {
127 const fields = { ...baseCorrectParams, targetUrl: 'http://localhost:8000' }
129 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
132 it('Should fail with a private IP target urls', async function () {
134 'http://127.0.0.1:8000',
136 'http://127.0.0.1/hello',
137 'https://192.168.1.42',
138 'http://192.168.1.42',
139 'http://127.0.0.1.cpy.re'
142 for (const targetUrl of targetUrls) {
143 const fields = { ...baseCorrectParams, targetUrl }
145 await makePostBodyRequest({
148 token: server.accessToken,
150 expectedStatus: HttpStatusCode.FORBIDDEN_403
155 it('Should fail with a long name', async function () {
156 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
158 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
161 it('Should fail with a bad category', async function () {
162 const fields = { ...baseCorrectParams, category: 125 }
164 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
167 it('Should fail with a bad licence', async function () {
168 const fields = { ...baseCorrectParams, licence: 125 }
170 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
173 it('Should fail with a bad language', async function () {
174 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
176 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
179 it('Should fail with a long description', async function () {
180 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
182 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
185 it('Should fail with a long support text', async function () {
186 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
188 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
191 it('Should fail without a channel', async function () {
192 const fields = omit(baseCorrectParams, 'channelId')
194 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
197 it('Should fail with a bad channel', async function () {
198 const fields = { ...baseCorrectParams, channelId: 545454 }
200 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
203 it('Should fail with another user channel', async function () {
206 password: 'fake_password'
208 await server.users.create({ username: user.username, password: user.password })
210 const accessTokenUser = await server.login.getAccessToken(user)
211 const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
212 const customChannelId = videoChannels[0].id
214 const fields = { ...baseCorrectParams, channelId: customChannelId }
216 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
219 it('Should fail with too many tags', async function () {
220 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
222 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
225 it('Should fail with a tag length too low', async function () {
226 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
228 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
231 it('Should fail with a tag length too big', async function () {
232 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
234 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
237 it('Should fail with an incorrect thumbnail file', async function () {
238 const fields = baseCorrectParams
240 thumbnailfile: buildAbsoluteFixturePath('video_short.mp4')
243 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
246 it('Should fail with a big thumbnail file', async function () {
247 const fields = baseCorrectParams
249 thumbnailfile: buildAbsoluteFixturePath('preview-big.png')
252 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
255 it('Should fail with an incorrect preview file', async function () {
256 const fields = baseCorrectParams
258 previewfile: buildAbsoluteFixturePath('video_short.mp4')
261 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
264 it('Should fail with a big preview file', async function () {
265 const fields = baseCorrectParams
267 previewfile: buildAbsoluteFixturePath('preview-big.png')
270 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
273 it('Should fail with an invalid torrent file', async function () {
274 const fields = omit(baseCorrectParams, 'targetUrl')
276 torrentfile: buildAbsoluteFixturePath('avatar-big.png')
279 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
282 it('Should fail with an invalid magnet URI', async function () {
283 let fields = omit(baseCorrectParams, 'targetUrl')
284 fields = { ...fields, magnetUri: 'blabla' }
286 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
289 it('Should succeed with the correct parameters', async function () {
292 await makePostBodyRequest({
295 token: server.accessToken,
296 fields: baseCorrectParams,
297 expectedStatus: HttpStatusCode.OK_200
301 it('Should forbid to import http videos', async function () {
302 await server.config.updateCustomSubConfig({
317 await makePostBodyRequest({
320 token: server.accessToken,
321 fields: baseCorrectParams,
322 expectedStatus: HttpStatusCode.CONFLICT_409
326 it('Should forbid to import torrent videos', async function () {
327 await server.config.updateCustomSubConfig({
342 let fields = omit(baseCorrectParams, 'targetUrl')
343 fields = { ...fields, magnetUri: FIXTURE_URLS.magnet }
345 await makePostBodyRequest({
348 token: server.accessToken,
350 expectedStatus: HttpStatusCode.CONFLICT_409
353 fields = omit(fields, 'magnetUri')
355 torrentfile: buildAbsoluteFixturePath('video-720p.torrent')
358 await makeUploadRequest({
361 token: server.accessToken,
364 expectedStatus: HttpStatusCode.CONFLICT_409
369 describe('Deleting/cancelling a video import', function () {
372 async function importVideo () {
373 const attributes = { channelId: server.store.channel.id, targetUrl: FIXTURE_URLS.goodVideo }
374 const res = await server.imports.importVideo({ attributes })
379 before(async function () {
380 importId = await importVideo()
383 it('Should fail with an invalid import id', async function () {
384 await server.imports.cancel({ importId: 'artyom' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
385 await server.imports.delete({ importId: 'artyom' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
388 it('Should fail with an unknown import id', async function () {
389 await server.imports.cancel({ importId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
390 await server.imports.delete({ importId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
393 it('Should fail without token', async function () {
394 await server.imports.cancel({ importId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
395 await server.imports.delete({ importId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
398 it('Should fail with another user token', async function () {
399 await server.imports.cancel({ importId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
400 await server.imports.delete({ importId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
403 it('Should fail to cancel non pending import', async function () {
406 await waitJobs([ server ])
408 await server.imports.cancel({ importId, expectedStatus: HttpStatusCode.CONFLICT_409 })
411 it('Should succeed to delete an import', async function () {
412 await server.imports.delete({ importId })
415 it('Should fail to delete a pending import', async function () {
416 await server.jobs.pauseJobQueue()
418 importId = await importVideo()
420 await server.imports.delete({ importId, expectedStatus: HttpStatusCode.CONFLICT_409 })
423 it('Should succeed to cancel an import', async function () {
424 importId = await importVideo()
426 await server.imports.cancel({ importId })
430 after(async function () {
431 await cleanupTests([ server ])