1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination, FIXTURE_URLS } from '@server/tests/shared'
4 import { buildAbsoluteFixturePath, omit } from '@shared/core-utils'
5 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
13 setAccessTokensToServers,
14 setDefaultVideoChannel,
16 } from '@shared/server-commands'
18 describe('Test video imports API validator', function () {
19 const path = '/api/v1/videos/imports'
20 let server: PeerTubeServer
21 let userAccessToken = ''
24 // ---------------------------------------------------------------
26 before(async function () {
29 server = await createSingleServer(1)
31 await setAccessTokensToServers([ server ])
32 await setDefaultVideoChannel([ server ])
34 const username = 'user1'
35 const password = 'my super password'
36 await server.users.create({ username, password })
37 userAccessToken = await server.login.getAccessToken({ username, password })
40 const { videoChannels } = await server.users.getMyInfo()
41 channelId = videoChannels[0].id
45 describe('When listing my video imports', function () {
46 const myPath = '/api/v1/users/me/videos/imports'
48 it('Should fail with a bad start pagination', async function () {
49 await checkBadStartPagination(server.url, myPath, server.accessToken)
52 it('Should fail with a bad count pagination', async function () {
53 await checkBadCountPagination(server.url, myPath, server.accessToken)
56 it('Should fail with an incorrect sort', async function () {
57 await checkBadSortPagination(server.url, myPath, server.accessToken)
60 it('Should fail with a bad videoChannelSyncId param', async function () {
61 await makeGetRequest({
64 query: { videoChannelSyncId: 'toto' },
65 token: server.accessToken
69 it('Should success with the correct parameters', async function () {
70 await makeGetRequest({ url: server.url, path: myPath, expectedStatus: HttpStatusCode.OK_200, token: server.accessToken })
74 describe('When adding a video import', function () {
79 targetUrl: FIXTURE_URLS.goodVideo,
80 name: 'my super name',
85 commentsEnabled: true,
86 downloadEnabled: true,
87 waitTranscoding: true,
88 description: 'my super description',
89 support: 'my super support text',
90 tags: [ 'tag1', 'tag2' ],
91 privacy: VideoPrivacy.PUBLIC,
96 it('Should fail with nothing', async function () {
98 await makePostBodyRequest({
101 token: server.accessToken,
103 expectedStatus: HttpStatusCode.BAD_REQUEST_400
107 it('Should fail without a target url', async function () {
108 const fields = omit(baseCorrectParams, [ 'targetUrl' ])
109 await makePostBodyRequest({
112 token: server.accessToken,
114 expectedStatus: HttpStatusCode.BAD_REQUEST_400
118 it('Should fail with a bad target url', async function () {
119 const fields = { ...baseCorrectParams, targetUrl: 'htt://hello' }
121 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
124 it('Should fail with localhost', async function () {
125 const fields = { ...baseCorrectParams, targetUrl: 'http://localhost:8000' }
127 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
130 it('Should fail with a private IP target urls', async function () {
132 'http://127.0.0.1:8000',
134 'http://127.0.0.1/hello',
135 'https://192.168.1.42',
136 'http://192.168.1.42',
137 'http://127.0.0.1.cpy.re'
140 for (const targetUrl of targetUrls) {
141 const fields = { ...baseCorrectParams, targetUrl }
143 await makePostBodyRequest({
146 token: server.accessToken,
148 expectedStatus: HttpStatusCode.FORBIDDEN_403
153 it('Should fail with a long name', async function () {
154 const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
156 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
159 it('Should fail with a bad category', async function () {
160 const fields = { ...baseCorrectParams, category: 125 }
162 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
165 it('Should fail with a bad licence', async function () {
166 const fields = { ...baseCorrectParams, licence: 125 }
168 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
171 it('Should fail with a bad language', async function () {
172 const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
174 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
177 it('Should fail with a long description', async function () {
178 const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
180 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
183 it('Should fail with a long support text', async function () {
184 const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
186 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
189 it('Should fail without a channel', async function () {
190 const fields = omit(baseCorrectParams, [ 'channelId' ])
192 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
195 it('Should fail with a bad channel', async function () {
196 const fields = { ...baseCorrectParams, channelId: 545454 }
198 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
201 it('Should fail with another user channel', async function () {
204 password: 'fake_password'
206 await server.users.create({ username: user.username, password: user.password })
208 const accessTokenUser = await server.login.getAccessToken(user)
209 const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
210 const customChannelId = videoChannels[0].id
212 const fields = { ...baseCorrectParams, channelId: customChannelId }
214 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
217 it('Should fail with too many tags', async function () {
218 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
220 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
223 it('Should fail with a tag length too low', async function () {
224 const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
226 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
229 it('Should fail with a tag length too big', async function () {
230 const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
232 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
235 it('Should fail with an incorrect thumbnail file', async function () {
236 const fields = baseCorrectParams
238 thumbnailfile: buildAbsoluteFixturePath('video_short.mp4')
241 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
244 it('Should fail with a big thumbnail file', async function () {
245 const fields = baseCorrectParams
247 thumbnailfile: buildAbsoluteFixturePath('preview-big.png')
250 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
253 it('Should fail with an incorrect preview file', async function () {
254 const fields = baseCorrectParams
256 previewfile: buildAbsoluteFixturePath('video_short.mp4')
259 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
262 it('Should fail with a big preview file', async function () {
263 const fields = baseCorrectParams
265 previewfile: buildAbsoluteFixturePath('preview-big.png')
268 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
271 it('Should fail with an invalid torrent file', async function () {
272 const fields = omit(baseCorrectParams, [ 'targetUrl' ])
274 torrentfile: buildAbsoluteFixturePath('avatar-big.png')
277 await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
280 it('Should fail with an invalid magnet URI', async function () {
281 let fields = omit(baseCorrectParams, [ 'targetUrl' ])
282 fields = { ...fields, magnetUri: 'blabla' }
284 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
287 it('Should succeed with the correct parameters', async function () {
290 await makePostBodyRequest({
293 token: server.accessToken,
294 fields: baseCorrectParams,
295 expectedStatus: HttpStatusCode.OK_200
299 it('Should forbid to import http videos', async function () {
300 await server.config.updateCustomSubConfig({
315 await makePostBodyRequest({
318 token: server.accessToken,
319 fields: baseCorrectParams,
320 expectedStatus: HttpStatusCode.CONFLICT_409
324 it('Should forbid to import torrent videos', async function () {
325 await server.config.updateCustomSubConfig({
340 let fields = omit(baseCorrectParams, [ 'targetUrl' ])
341 fields = { ...fields, magnetUri: FIXTURE_URLS.magnet }
343 await makePostBodyRequest({
346 token: server.accessToken,
348 expectedStatus: HttpStatusCode.CONFLICT_409
351 fields = omit(fields, [ 'magnetUri' ])
353 torrentfile: buildAbsoluteFixturePath('video-720p.torrent')
356 await makeUploadRequest({
359 token: server.accessToken,
362 expectedStatus: HttpStatusCode.CONFLICT_409
367 describe('Deleting/cancelling a video import', function () {
370 async function importVideo () {
371 const attributes = { channelId: server.store.channel.id, targetUrl: FIXTURE_URLS.goodVideo }
372 const res = await server.imports.importVideo({ attributes })
377 before(async function () {
378 importId = await importVideo()
381 it('Should fail with an invalid import id', async function () {
382 await server.imports.cancel({ importId: 'artyom' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
383 await server.imports.delete({ importId: 'artyom' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
386 it('Should fail with an unknown import id', async function () {
387 await server.imports.cancel({ importId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
388 await server.imports.delete({ importId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
391 it('Should fail without token', async function () {
392 await server.imports.cancel({ importId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
393 await server.imports.delete({ importId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
396 it('Should fail with another user token', async function () {
397 await server.imports.cancel({ importId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
398 await server.imports.delete({ importId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
401 it('Should fail to cancel non pending import', async function () {
404 await waitJobs([ server ])
406 await server.imports.cancel({ importId, expectedStatus: HttpStatusCode.CONFLICT_409 })
409 it('Should succeed to delete an import', async function () {
410 await server.imports.delete({ importId })
413 it('Should fail to delete a pending import', async function () {
414 await server.jobs.pauseJobQueue()
416 importId = await importVideo()
418 await server.imports.delete({ importId, expectedStatus: HttpStatusCode.CONFLICT_409 })
421 it('Should succeed to cancel an import', async function () {
422 importId = await importVideo()
424 await server.imports.cancel({ importId })
428 after(async function () {
429 await cleanupTests([ server ])