From 187501f8b8e334f043ae1e9af3c9d4f45ea0e6c1 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 7 Aug 2018 11:56:18 +0200 Subject: [PATCH] Add check param tests regarding video imports --- server/tests/api/check-params/users.ts | 44 ++++++++++++++-- .../tests/api/check-params/video-imports.ts | 50 ++++++++++++++++++- .../tests/fixtures/60fps_small-240p.torrent | 1 + server/tests/utils/videos/video-imports.ts | 14 +++++- 4 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 server/tests/fixtures/60fps_small-240p.torrent diff --git a/server/tests/api/check-params/users.ts b/server/tests/api/check-params/users.ts index e1954c64f..e1655e85b 100644 --- a/server/tests/api/check-params/users.ts +++ b/server/tests/api/check-params/users.ts @@ -3,7 +3,7 @@ import { omit } from 'lodash' import 'mocha' import { join } from 'path' -import { UserRole } from '../../../../shared' +import { UserRole, VideoImport, VideoImportState } from '../../../../shared' import { createUser, flushTests, getMyUserInformation, getMyUserVideoRating, getUsersList, immutableAssign, killallServers, makeGetRequest, @@ -11,6 +11,10 @@ import { updateUser, uploadVideo, userLogin } from '../../utils' import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params' +import { getMagnetURI, getMyVideoImports, getYoutubeVideoUrl, importVideo } from '../../utils/videos/video-imports' +import { VideoPrivacy } from '../../../../shared/models/videos' +import { waitJobs } from '../../utils/server/jobs' +import { expect } from 'chai' describe('Test users API validators', function () { const path = '/api/v1/users/' @@ -20,6 +24,7 @@ describe('Test users API validators', function () { let server: ServerInfo let serverWithRegistrationDisabled: ServerInfo let userAccessToken = '' + let channelId: number const user = { username: 'user1', password: 'my super password' @@ -41,8 +46,15 @@ describe('Test users API validators', function () { await createUser(server.url, server.accessToken, user.username, user.password, videoQuota) userAccessToken = await userLogin(server, user) - const res = await uploadVideo(server.url, server.accessToken, {}) - videoId = res.body.video.id + { + const res = await getMyUserInformation(server.url, server.accessToken) + channelId = res.body.videoChannels[ 0 ].id + } + + { + const res = await uploadVideo(server.url, server.accessToken, {}) + videoId = res.body.video.id + } }) describe('When listing users', function () { @@ -605,6 +617,32 @@ describe('Test users API validators', function () { await uploadVideo(server.url, userAccessToken, videoAttributes) await uploadVideo(server.url, userAccessToken, videoAttributes, 403) }) + + it('Should fail to import with HTTP/Torrent/magnet', async function () { + this.timeout(30000) + + const baseAttributes = { + channelId: 1, + privacy: VideoPrivacy.PUBLIC + } + await importVideo(server.url, server.accessToken, immutableAssign(baseAttributes, { targetUrl: getYoutubeVideoUrl() })) + await importVideo(server.url, server.accessToken, immutableAssign(baseAttributes, { magnetUri: getMagnetURI() })) + await importVideo(server.url, server.accessToken, immutableAssign(baseAttributes, { torrentfile: '60fps_small-240p.torrent' })) + + await waitJobs([ server ]) + + const res = await getMyVideoImports(server.url, server.accessToken) + + expect(res.body.total).to.equal(3) + const videoImports: VideoImport[] = res.body.data + expect(videoImports).to.have.lengthOf(3) + + for (const videoImport of videoImports) { + expect(videoImport.state.id).to.equal(VideoImportState.FAILED) + expect(videoImport.error).not.to.be.undefined + expect(videoImport.error).to.contain('user video quota is exceeded') + } + }) }) describe('When asking a password reset', function () { diff --git a/server/tests/api/check-params/video-imports.ts b/server/tests/api/check-params/video-imports.ts index 0ead34a47..e62f0918e 100644 --- a/server/tests/api/check-params/video-imports.ts +++ b/server/tests/api/check-params/video-imports.ts @@ -20,7 +20,7 @@ import { userLogin } from '../../utils' import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params' -import { getYoutubeVideoUrl } from '../../utils/videos/video-imports' +import { getMagnetURI, getYoutubeVideoUrl } from '../../utils/videos/video-imports' describe('Test video imports API validator', function () { const path = '/api/v1/videos/imports' @@ -229,6 +229,22 @@ describe('Test video imports API validator', function () { await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches }) }) + it('Should fail with an invalid torrent file', async function () { + const fields = omit(baseCorrectParams, 'targetUrl') + const attaches = { + 'torrentfile': join(__dirname, '..', '..', 'fixtures', 'avatar-big.png') + } + + await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches }) + }) + + it('Should fail with an invalid magnet URI', async function () { + let fields = omit(baseCorrectParams, 'targetUrl') + fields = immutableAssign(fields, { magnetUri: 'blabla' }) + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + it('Should succeed with the correct parameters', async function () { this.timeout(10000) @@ -243,12 +259,15 @@ describe('Test video imports API validator', function () { } }) - it('Should forbid to import videos', async function () { + it('Should forbid to import http videos', async function () { await updateCustomSubConfig(server.url, server.accessToken, { import: { videos: { http: { enabled: false + }, + torrent: { + enabled: true } } } @@ -262,6 +281,33 @@ describe('Test video imports API validator', function () { statusCodeExpected: 409 }) }) + + it('Should forbid to import torrent videos', async function () { + await updateCustomSubConfig(server.url, server.accessToken, { + import: { + videos: { + http: { + enabled: true + }, + torrent: { + enabled: false + } + } + } + }) + + let fields = omit(baseCorrectParams, 'targetUrl') + fields = immutableAssign(fields, { magnetUri: getMagnetURI() }) + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 409 }) + + fields = omit(fields, 'magnetUri') + const attaches = { + 'torrentfile': join(__dirname, '..', '..', 'fixtures', '60fps_small-240p.torrent') + } + + await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches, statusCodeExpected: 409 }) + }) }) after(async function () { diff --git a/server/tests/fixtures/60fps_small-240p.torrent b/server/tests/fixtures/60fps_small-240p.torrent new file mode 100644 index 000000000..ec4c0babe --- /dev/null +++ b/server/tests/fixtures/60fps_small-240p.torrent @@ -0,0 +1 @@ +d8:announce41:wss://peertube2.cpy.re:443/tracker/socket13:announce-listll41:wss://peertube2.cpy.re:443/tracker/socketel41:https://peertube2.cpy.re/tracker/announceee10:created by8:PeerTube13:creation datei1529593069e8:encoding5:UTF-84:infod6:lengthi30921e4:name20:60fps_small 240p.mp412:piece lengthi16384e6:pieces40:Ė…+çéCFm7çc0ÏÅT-@2Ç6©0áMür|Rv›$˜h%e8:url-listl84:https://peertube2.cpy.re/static/webseed/2b8dbe74-9548-4f6f-a8da-986aed9e5e45-240.mp4ee \ No newline at end of file diff --git a/server/tests/utils/videos/video-imports.ts b/server/tests/utils/videos/video-imports.ts index e0f916990..fa2f13b5e 100644 --- a/server/tests/utils/videos/video-imports.ts +++ b/server/tests/utils/videos/video-imports.ts @@ -1,17 +1,26 @@ import { VideoImportCreate } from '../../../../shared/models/videos' -import { makeGetRequest, makePostBodyRequest } from '..' +import { makeGetRequest, makePostBodyRequest, makeUploadRequest } from '..' function getYoutubeVideoUrl () { return 'https://youtu.be/msX3jv1XdvM' } +function getMagnetURI () { + // tslint:disable:max-line-length + return 'magnet:?xs=https%3A%2F%2Fpeertube2.cpy.re%2Fstatic%2Ftorrents%2F2b8dbe74-9548-4f6f-a8da-986aed9e5e45-240.torrent&xt=urn:btih:52bf3729e5859390a8751495196b5674a55c99f3&dn=60fps_small&tr=wss%3A%2F%2Fpeertube2.cpy.re%3A443%2Ftracker%2Fsocket&tr=https%3A%2F%2Fpeertube2.cpy.re%2Ftracker%2Fannounce&ws=https%3A%2F%2Fpeertube2.cpy.re%2Fstatic%2Fwebseed%2F2b8dbe74-9548-4f6f-a8da-986aed9e5e45-240.mp4' +} + function importVideo (url: string, token: string, attributes: VideoImportCreate) { const path = '/api/v1/videos/imports' - return makePostBodyRequest({ + let attaches: any = {} + if (attributes.torrentfile) attaches = { torrentfile: attributes.torrentfile } + + return makeUploadRequest({ url, path, token, + attaches, fields: attributes, statusCodeExpected: 200 }) @@ -33,5 +42,6 @@ function getMyVideoImports (url: string, token: string) { export { getYoutubeVideoUrl, importVideo, + getMagnetURI, getMyVideoImports } -- 2.25.1