From 5d08a6a74e83f2e4dfe2f3ba7f5a39371e1bc89e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 3 Aug 2018 11:10:31 +0200 Subject: Add import http enabled configuration --- server/controllers/api/config.ts | 14 ++ server/controllers/api/users.ts | 1 - server/controllers/api/videos/import.ts | 4 +- server/helpers/audit-logger.ts | 6 +- server/initializers/checker.ts | 1 + server/initializers/constants.ts | 7 + server/middlewares/validators/config.ts | 1 + server/middlewares/validators/video-imports.ts | 9 + server/tests/api/check-params/config.ts | 7 + server/tests/api/check-params/index.ts | 1 + server/tests/api/check-params/video-imports.ts | 246 +++++++++++++++++++++++++ server/tests/api/server/config.ts | 9 + server/tests/client.ts | 7 + 13 files changed, 306 insertions(+), 7 deletions(-) create mode 100644 server/tests/api/check-params/video-imports.ts (limited to 'server') diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts index 411b13539..acbeab70b 100644 --- a/server/controllers/api/config.ts +++ b/server/controllers/api/config.ts @@ -65,6 +65,13 @@ async function getConfig (req: express.Request, res: express.Response, next: exp transcoding: { enabledResolutions }, + import: { + video: { + http: { + enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED + } + } + }, avatar: { file: { size: { @@ -225,6 +232,13 @@ function customConfig (): CustomConfig { '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ], '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ] } + }, + import: { + videos: { + http: { + enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED + } + } } } } diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index 6e5f9913e..879ba3f91 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts @@ -68,7 +68,6 @@ usersRouter.get('/me/video-quota-used', asyncMiddleware(getUserVideoQuotaUsed) ) - usersRouter.get('/me/videos/imports', authenticate, paginationValidator, diff --git a/server/controllers/api/videos/import.ts b/server/controllers/api/videos/import.ts index 33ac83cb9..ee11b0741 100644 --- a/server/controllers/api/videos/import.ts +++ b/server/controllers/api/videos/import.ts @@ -77,7 +77,7 @@ async function addVideoImport (req: express.Request, res: express.Response) { video.url = getVideoActivityPubUrl(video) // Process thumbnail file? - const thumbnailField = req.files['thumbnailfile'] + const thumbnailField = req.files ? req.files['thumbnailfile'] : undefined let downloadThumbnail = true if (thumbnailField) { const thumbnailPhysicalFile = thumbnailField[ 0 ] @@ -86,7 +86,7 @@ async function addVideoImport (req: express.Request, res: express.Response) { } // Process preview file? - const previewField = req.files['previewfile'] + const previewField = req.files ? req.files['previewfile'] : undefined let downloadPreview = true if (previewField) { const previewPhysicalFile = previewField[0] diff --git a/server/helpers/audit-logger.ts b/server/helpers/audit-logger.ts index 031b1bfbd..db20df20f 100644 --- a/server/helpers/audit-logger.ts +++ b/server/helpers/audit-logger.ts @@ -246,11 +246,9 @@ class CustomConfigAuditView extends EntityAuditView { const resolutionsDict = infos.transcoding.resolutions const resolutionsArray = [] Object.entries(resolutionsDict).forEach(([resolution, isEnabled]) => { - if (isEnabled) { - resolutionsArray.push(resolution) - } + if (isEnabled) resolutionsArray.push(resolution) }) - infos.transcoding.resolutions = resolutionsArray + Object.assign({}, infos, { transcoding: { resolutions: resolutionsArray } }) super(customConfigKeysToKeep, 'config', infos) } } diff --git a/server/initializers/checker.ts b/server/initializers/checker.ts index f1c2e80a9..608123607 100644 --- a/server/initializers/checker.ts +++ b/server/initializers/checker.ts @@ -51,6 +51,7 @@ function checkMissedConfig () { 'cache.previews.size', 'admin.email', 'signup.enabled', 'signup.limit', 'signup.filters.cidr.whitelist', 'signup.filters.cidr.blacklist', 'transcoding.enabled', 'transcoding.threads', + 'import.videos.http.enabled', 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route', 'instance.default_nsfw_policy', 'instance.robots', 'services.twitter.username', 'services.twitter.whitelisted' diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 5bfeb3746..069d9b2e8 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -206,6 +206,13 @@ const CONFIG = { get '1080p' () { return config.get('transcoding.resolutions.1080p') } } }, + IMPORT: { + VIDEOS: { + HTTP: { + get ENABLED () { return config.get('import.videos.http.enabled') } + } + } + }, CACHE: { PREVIEWS: { get SIZE () { return config.get('cache.previews.size') } diff --git a/server/middlewares/validators/config.ts b/server/middlewares/validators/config.ts index f58c0676c..9d303eee2 100644 --- a/server/middlewares/validators/config.ts +++ b/server/middlewares/validators/config.ts @@ -24,6 +24,7 @@ const customConfigUpdateValidator = [ body('transcoding.resolutions.480p').isBoolean().withMessage('Should have a valid transcoding 480p resolution enabled boolean'), body('transcoding.resolutions.720p').isBoolean().withMessage('Should have a valid transcoding 720p resolution enabled boolean'), body('transcoding.resolutions.1080p').isBoolean().withMessage('Should have a valid transcoding 1080p resolution enabled boolean'), + body('import.videos.http.enabled').isBoolean().withMessage('Should have a valid import video http enabled boolean'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking customConfigUpdateValidator parameters', { parameters: req.body }) diff --git a/server/middlewares/validators/video-imports.ts b/server/middlewares/validators/video-imports.ts index e0a552976..d806edfa3 100644 --- a/server/middlewares/validators/video-imports.ts +++ b/server/middlewares/validators/video-imports.ts @@ -7,6 +7,7 @@ import { getCommonVideoAttributes } from './videos' import { isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports' import { cleanUpReqFiles } from '../../helpers/utils' import { isVideoChannelOfAccountExist, isVideoNameValid } from '../../helpers/custom-validators/videos' +import { CONFIG } from '../../initializers/constants' const videoImportAddValidator = getCommonVideoAttributes().concat([ body('targetUrl').custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'), @@ -23,6 +24,14 @@ const videoImportAddValidator = getCommonVideoAttributes().concat([ const user = res.locals.oauth.token.User if (areValidationErrors(req, res)) return cleanUpReqFiles(req) + + if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true) { + cleanUpReqFiles(req) + return res.status(409) + .json({ error: 'Import is not enabled on this instance.' }) + .end() + } + if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) return next() diff --git a/server/tests/api/check-params/config.ts b/server/tests/api/check-params/config.ts index 03855237f..2742e26de 100644 --- a/server/tests/api/check-params/config.ts +++ b/server/tests/api/check-params/config.ts @@ -60,6 +60,13 @@ describe('Test config API validators', function () { '720p': false, '1080p': false } + }, + import: { + videos: { + http: { + enabled: false + } + } } } diff --git a/server/tests/api/check-params/index.ts b/server/tests/api/check-params/index.ts index 820dde889..03fdd5c4e 100644 --- a/server/tests/api/check-params/index.ts +++ b/server/tests/api/check-params/index.ts @@ -10,4 +10,5 @@ import './video-captions' import './video-channels' import './video-comments' import './videos' +import './video-imports' import './search' diff --git a/server/tests/api/check-params/video-imports.ts b/server/tests/api/check-params/video-imports.ts new file mode 100644 index 000000000..7f58efb74 --- /dev/null +++ b/server/tests/api/check-params/video-imports.ts @@ -0,0 +1,246 @@ +/* tslint:disable:no-unused-expression */ + +import { omit } from 'lodash' +import 'mocha' +import { join } from 'path' +import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum' +import { + createUser, + flushTests, + getMyUserInformation, + immutableAssign, + killallServers, + makeGetRequest, + makePostBodyRequest, + makeUploadRequest, + runServer, + ServerInfo, + setAccessTokensToServers, + userLogin +} from '../../utils' +import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params' + +describe('Test video imports API validator', function () { + const path = '/api/v1/videos/imports' + let server: ServerInfo + let userAccessToken = '' + let accountName: string + let channelId: number + let channelUUID: string + + // --------------------------------------------------------------- + + before(async function () { + this.timeout(30000) + + await flushTests() + + server = await runServer(1) + + await setAccessTokensToServers([ server ]) + + const username = 'user1' + const password = 'my super password' + await createUser(server.url, server.accessToken, username, password) + userAccessToken = await userLogin(server, { username, password }) + + { + const res = await getMyUserInformation(server.url, server.accessToken) + channelId = res.body.videoChannels[ 0 ].id + channelUUID = res.body.videoChannels[ 0 ].uuid + accountName = res.body.account.name + '@' + res.body.account.host + } + }) + + describe('When listing my video imports', function () { + const myPath = '/api/v1/users/me/videos/imports' + + it('Should fail with a bad start pagination', async function () { + await checkBadStartPagination(server.url, myPath, server.accessToken) + }) + + it('Should fail with a bad count pagination', async function () { + await checkBadCountPagination(server.url, myPath, server.accessToken) + }) + + it('Should fail with an incorrect sort', async function () { + await checkBadSortPagination(server.url, myPath, server.accessToken) + }) + + it('Should success with the correct parameters', async function () { + await makeGetRequest({ url: server.url, path: myPath, statusCodeExpected: 200, token: server.accessToken }) + }) + }) + + describe('When adding a video import', function () { + let baseCorrectParams + + before(function () { + baseCorrectParams = { + targetUrl: 'https://youtu.be/msX3jv1XdvM', + name: 'my super name', + category: 5, + licence: 1, + language: 'pt', + nsfw: false, + commentsEnabled: true, + waitTranscoding: true, + description: 'my super description', + support: 'my super support text', + tags: [ 'tag1', 'tag2' ], + privacy: VideoPrivacy.PUBLIC, + channelId: channelId + } + }) + + it('Should fail with nothing', async function () { + const fields = {} + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + + it('Should fail with a long name', async function () { + const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(65) }) + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + + it('Should fail with a bad category', async function () { + const fields = immutableAssign(baseCorrectParams, { category: 125 }) + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + + it('Should fail with a bad licence', async function () { + const fields = immutableAssign(baseCorrectParams, { licence: 125 }) + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + + it('Should fail with a bad language', async function () { + const fields = immutableAssign(baseCorrectParams, { language: 'a'.repeat(15) }) + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + + it('Should fail with a long description', async function () { + const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(2500) }) + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + + it('Should fail with a long support text', async function () { + const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(150) }) + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + + it('Should fail without a channel', async function () { + const fields = omit(baseCorrectParams, 'channelId') + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + + it('Should fail with a bad channel', async function () { + const fields = immutableAssign(baseCorrectParams, { channelId: 545454 }) + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + + it('Should fail with another user channel', async function () { + const user = { + username: 'fake', + password: 'fake_password' + } + await createUser(server.url, server.accessToken, user.username, user.password) + + const accessTokenUser = await userLogin(server, user) + const res = await getMyUserInformation(server.url, accessTokenUser) + const customChannelId = res.body.videoChannels[0].id + + const fields = immutableAssign(baseCorrectParams, { channelId: customChannelId }) + + await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields }) + }) + + it('Should fail with too many tags', async function () { + const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }) + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + + it('Should fail with a tag length too low', async function () { + const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 't' ] }) + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + + it('Should fail with a tag length too big', async function () { + const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }) + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + + it('Should fail with an incorrect thumbnail file', async function () { + const fields = baseCorrectParams + const attaches = { + 'thumbnailfile': join(__dirname, '..', '..', 'fixtures', 'avatar.png') + } + + await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches }) + }) + + it('Should fail with a big thumbnail file', async function () { + const fields = baseCorrectParams + const attaches = { + 'thumbnailfile': join(__dirname, '..', '..', 'fixtures', 'avatar-big.png') + } + + await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches }) + }) + + it('Should fail with an incorrect preview file', async function () { + const fields = baseCorrectParams + const attaches = { + 'previewfile': join(__dirname, '..', '..', 'fixtures', 'avatar.png') + } + + await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches }) + }) + + it('Should fail with a big preview file', async function () { + const fields = baseCorrectParams + const attaches = { + 'previewfile': join(__dirname, '..', '..', 'fixtures', 'avatar-big.png') + } + + await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches }) + }) + + it('Should succeed with the correct parameters', async function () { + this.timeout(10000) + + const fields = baseCorrectParams + + { + await makePostBodyRequest({ + url: server.url, + path, + token: server.accessToken, + fields, + statusCodeExpected: 200 + }) + } + }) + + it('Should forbid importing') + }) + + after(async function () { + killallServers([ server ]) + + // Keep the logs if the test failed + if (this['ok']) { + await flushTests() + } + }) +}) diff --git a/server/tests/api/server/config.ts b/server/tests/api/server/config.ts index 1782a8623..b65061a5d 100644 --- a/server/tests/api/server/config.ts +++ b/server/tests/api/server/config.ts @@ -44,6 +44,7 @@ function checkInitialConfig (data: CustomConfig) { expect(data.transcoding.resolutions['480p']).to.be.true expect(data.transcoding.resolutions['720p']).to.be.true expect(data.transcoding.resolutions['1080p']).to.be.true + expect(data.import.videos.http.enabled).to.be.true } function checkUpdatedConfig (data: CustomConfig) { @@ -70,6 +71,7 @@ function checkUpdatedConfig (data: CustomConfig) { expect(data.transcoding.resolutions['480p']).to.be.true expect(data.transcoding.resolutions['720p']).to.be.false expect(data.transcoding.resolutions['1080p']).to.be.false + expect(data.import.videos.http.enabled).to.be.false } describe('Test config', function () { @@ -160,6 +162,13 @@ describe('Test config', function () { '720p': false, '1080p': false } + }, + import: { + videos: { + http: { + enabled: false + } + } } } await updateCustomConfig(server.url, server.accessToken, newCustomConfig) diff --git a/server/tests/client.ts b/server/tests/client.ts index bcbac86e9..129b40cdf 100644 --- a/server/tests/client.ts +++ b/server/tests/client.ts @@ -164,6 +164,13 @@ describe('Test a client controllers', function () { '720p': false, '1080p': false } + }, + import: { + videos: { + http: { + enabled: false + } + } } } await updateCustomConfig(server.url, server.accessToken, newCustomConfig) -- cgit v1.2.3