From be587647f98a4b83ca06a61fe55c7ac5d60927c6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 6 Jun 2016 14:15:03 +0200 Subject: [PATCH] Add tags support to server --- package.json | 1 - server/controllers/api/v1/videos.js | 8 +- server/helpers/customValidators.js | 101 +++++++++++++++----- server/initializers/constants.js | 23 +++-- server/lib/videos.js | 3 +- server/middlewares/reqValidators/remote.js | 4 +- server/middlewares/reqValidators/videos.js | 14 +-- server/models/videos.js | 1 + server/tests/api/checkParams.js | 104 +++++++++++++++++++-- server/tests/api/friendsAdvanced.js | 3 +- server/tests/api/multiplePods.js | 28 +++++- server/tests/api/singlePod.js | 16 +++- server/tests/api/users.js | 19 +++- server/tests/api/utils.js | 25 +++-- 14 files changed, 277 insertions(+), 73 deletions(-) diff --git a/package.json b/package.json index 5e437b982..579ef3d2e 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,6 @@ "scripty": "^1.5.0", "segfault-handler": "^1.0.0", "ursa": "^0.9.1", - "validator": "^5.0.0", "webtorrent": "^0.93.2", "winston": "^2.1.1", "ws": "^1.0.1" diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js index 7f59dd232..5449cbcfa 100644 --- a/server/controllers/api/v1/videos.js +++ b/server/controllers/api/v1/videos.js @@ -115,7 +115,8 @@ function addVideo (req, res, next) { magnetUri: torrent.magnetURI, author: res.locals.oauth.token.user.username, duration: videoFile.duration, - thumbnail: thumbnailName + thumbnail: thumbnailName, + tags: videoInfos.tags } Videos.add(videoData, function (err, insertedVideo) { @@ -156,7 +157,7 @@ function addVideo (req, res, next) { return callback(null) } - ], function (err) { + ], function andFinally (err) { if (err) { logger.error('Cannot insert the video.') return next(err) @@ -228,7 +229,7 @@ function removeVideo (req, res, next) { return callback(null) } - ], function (err) { + ], function andFinally (err) { if (err) { logger.error('Errors when removed the video.', { error: err }) return next(err) @@ -259,6 +260,7 @@ function getFormatedVideo (videoObj) { magnetUri: videoObj.magnetUri, author: videoObj.author, duration: videoObj.duration, + tags: videoObj.tags, thumbnailPath: constants.THUMBNAILS_STATIC_PATH + '/' + videoObj.thumbnail, createdDate: videoObj.createdDate } diff --git a/server/helpers/customValidators.js b/server/helpers/customValidators.js index 5a0e70ffc..9c3ff38ef 100644 --- a/server/helpers/customValidators.js +++ b/server/helpers/customValidators.js @@ -1,34 +1,47 @@ 'use strict' -const validator = require('validator') +const validator = require('express-validator').validator const constants = require('../initializers/constants') +const VIDEOS_CONSTRAINTS_FIELDS = constants.VIDEOS_CONSTRAINTS_FIELDS const customValidators = { - eachIsRemoteVideosAddValid: eachIsRemoteVideosAddValid, - eachIsRemoteVideosRemoveValid: eachIsRemoteVideosRemoveValid, - isArray: isArray -} - -function eachIsRemoteVideosAddValid (values) { - return values.every(function (val) { - return validator.isLength(val.name, 1, 50) && - validator.isLength(val.description, 1, 50) && - validator.isLength(val.magnetUri, 10) && - validator.isURL(val.podUrl) && - !isNaN(val.duration) && - val.duration >= 0 && - val.duration < constants.MAXIMUM_VIDEO_DURATION && - validator.isLength(val.author, 1, constants.MAXIMUM_AUTHOR_LENGTH) && - validator.isBase64(val.thumbnailBase64) && - validator.isByteLength(val.thumbnailBase64, { min: 0, max: 20000 }) && - validator.isDate(val.createdDate) + exists: exists, + isEachAddRemoteVideosValid: isEachAddRemoteVideosValid, + isEachRemoveRemoteVideosValid: isEachRemoveRemoteVideosValid, + isArray: isArray, + isVideoAuthorValid: isVideoAuthorValid, + isVideoDateValid: isVideoDateValid, + isVideoDescriptionValid: isVideoDescriptionValid, + isVideoDurationValid: isVideoDurationValid, + isVideoMagnetUriValid: isVideoMagnetUriValid, + isVideoNameValid: isVideoNameValid, + isVideoPodUrlValid: isVideoPodUrlValid, + isVideoTagsValid: isVideoTagsValid, + isVideoThumbnailValid: isVideoThumbnailValid +} + +function exists (value) { + return value !== undefined && value !== null +} + +function isEachAddRemoteVideosValid (videos) { + return videos.every(function (video) { + return isVideoAuthorValid(video.author) && + isVideoDateValid(video.createdDate) && + isVideoDescriptionValid(video.description) && + isVideoDurationValid(video.duration) && + isVideoMagnetUriValid(video.magnetUri) && + isVideoNameValid(video.name) && + isVideoPodUrlValid(video.podUrl) && + isVideoTagsValid(video.tags) && + isVideoThumbnailValid(video.thumbnailBase64) }) } -function eachIsRemoteVideosRemoveValid (values) { - return values.every(function (val) { - return validator.isLength(val.magnetUri, 10) +function isEachRemoveRemoteVideosValid (videos) { + return videos.every(function (video) { + return isVideoMagnetUriValid(video.magnetUri) }) } @@ -36,6 +49,50 @@ function isArray (value) { return Array.isArray(value) } +function isVideoAuthorValid (value) { + return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR) +} + +function isVideoDateValid (value) { + return validator.isDate(value) +} + +function isVideoDescriptionValid (value) { + return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) +} + +function isVideoDurationValid (value) { + return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) +} + +function isVideoMagnetUriValid (value) { + return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.MAGNET_URI) +} + +function isVideoNameValid (value) { + return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) +} + +function isVideoPodUrlValid (value) { + return validator.isURL(value) +} + +function isVideoTagsValid (tags) { + return isArray(tags) && + validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && + tags.every(function (tag) { + return validator.isAlphanumeric(tag) && + validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) + }) +} + +function isVideoThumbnailValid (value) { + return validator.isBase64(value) && + validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) +} + // --------------------------------------------------------------------------- module.exports = customValidators + +// --------------------------------------------------------------------------- diff --git a/server/initializers/constants.js b/server/initializers/constants.js index 63ac863c1..97d22abdb 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -9,11 +9,6 @@ let FRIEND_BASE_SCORE = 100 // Time to wait between requests to the friends (10 min) let INTERVAL = 600000 -// Max length of the author username -const MAXIMUM_AUTHOR_LENGTH = 20 -// 2 hours maximum for the duration of a video (in seconds) -let MAXIMUM_VIDEO_DURATION = 7200 - // Number of results by default for the pagination const PAGINATION_COUNT_DEFAULT = 15 @@ -42,11 +37,22 @@ const THUMBNAILS_SIZE = '200x110' // Path for access to thumbnails with express router const THUMBNAILS_STATIC_PATH = '/static/thumbnails' +const VIDEOS_CONSTRAINTS_FIELDS = { + NAME: { min: 1, max: 50 }, // Length + DESCRIPTION: { min: 1, max: 250 }, // Length + MAGNET_URI: { min: 10 }, // Length + DURATION: { min: 1, max: 7200 }, // Number + AUTHOR: { min: 3, max: 20 }, // Length + TAGS: { min: 1, max: 3 }, // Number of total tags + TAG: { min: 2, max: 10 }, // Length + THUMBNAIL: { min: 0, max: 20000 } // Bytes +} + // Special constants for a test instance if (isTestInstance() === true) { FRIEND_BASE_SCORE = 20 INTERVAL = 10000 - MAXIMUM_VIDEO_DURATION = 14 + VIDEOS_CONSTRAINTS_FIELDS.DURATION.max = 14 REQUEST_RETRIES = 2 } @@ -56,15 +62,14 @@ module.exports = { API_VERSION: API_VERSION, FRIEND_BASE_SCORE: FRIEND_BASE_SCORE, INTERVAL: INTERVAL, - MAXIMUM_AUTHOR_LENGTH: MAXIMUM_AUTHOR_LENGTH, - MAXIMUM_VIDEO_DURATION: MAXIMUM_VIDEO_DURATION, PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT, PODS_SCORE: PODS_SCORE, REQUEST_RETRIES: REQUEST_RETRIES, SEARCHABLE_COLUMNS: SEARCHABLE_COLUMNS, SORTABLE_COLUMNS: SORTABLE_COLUMNS, THUMBNAILS_SIZE: THUMBNAILS_SIZE, - THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH + THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH, + VIDEOS_CONSTRAINTS_FIELDS: VIDEOS_CONSTRAINTS_FIELDS } // --------------------------------------------------------------------------- diff --git a/server/lib/videos.js b/server/lib/videos.js index 154c31ef9..e0db0e1d5 100644 --- a/server/lib/videos.js +++ b/server/lib/videos.js @@ -153,7 +153,8 @@ function createRemoteVideoObjects (videos, callback) { magnetUri: video.magnetUri, podUrl: video.podUrl, duration: video.duration, - thumbnail: thumbnailName + thumbnail: thumbnailName, + tags: video.tags } remoteVideos.push(params) diff --git a/server/middlewares/reqValidators/remote.js b/server/middlewares/reqValidators/remote.js index 3bc0e0f40..b5f3118b0 100644 --- a/server/middlewares/reqValidators/remote.js +++ b/server/middlewares/reqValidators/remote.js @@ -11,7 +11,7 @@ const reqValidatorsRemote = { function remoteVideosAdd (req, res, next) { req.checkBody('data').isArray() - req.checkBody('data').eachIsRemoteVideosAddValid() + req.checkBody('data').isEachAddRemoteVideosValid() logger.debug('Checking remoteVideosAdd parameters', { parameters: req.body }) @@ -20,7 +20,7 @@ function remoteVideosAdd (req, res, next) { function remoteVideosRemove (req, res, next) { req.checkBody('data').isArray() - req.checkBody('data').eachIsRemoteVideosRemoveValid() + req.checkBody('data').isEachRemoveRemoteVideosValid() logger.debug('Checking remoteVideosRemove parameters', { parameters: req.body }) diff --git a/server/middlewares/reqValidators/videos.js b/server/middlewares/reqValidators/videos.js index 10b6d39c6..3618e4716 100644 --- a/server/middlewares/reqValidators/videos.js +++ b/server/middlewares/reqValidators/videos.js @@ -2,6 +2,7 @@ const checkErrors = require('./utils').checkErrors const constants = require('../../initializers/constants') +const customValidators = require('../../helpers/customValidators') const logger = require('../../helpers/logger') const videos = require('../../lib/videos') const Videos = require('../../models/videos') @@ -16,8 +17,9 @@ const reqValidatorsVideos = { function videosAdd (req, res, next) { req.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty() req.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i) - req.checkBody('name', 'Should have a name').isLength(1, 50) - req.checkBody('description', 'Should have a description').isLength(1, 250) + req.checkBody('name', 'Should have a valid name').isVideoNameValid() + req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid() + req.checkBody('tags', 'Should have correct tags').isVideoTagsValid() logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) @@ -29,7 +31,7 @@ function videosAdd (req, res, next) { return res.status(400).send('Cannot retrieve metadata of the file.') } - if (duration > constants.MAXIMUM_VIDEO_DURATION) { + if (!customValidators.isVideoDurationValid(duration)) { return res.status(400).send('Duration of the video file is too big (max: ' + constants.MAXIMUM_VIDEO_DURATION + 's).') } @@ -48,7 +50,7 @@ function videosGet (req, res, next) { Videos.get(req.params.id, function (err, video) { if (err) { logger.error('Error in videosGet request validator.', { error: err }) - res.sendStatus(500) + return res.sendStatus(500) } const state = videos.getVideoState(video) @@ -68,7 +70,7 @@ function videosRemove (req, res, next) { Videos.get(req.params.id, function (err, video) { if (err) { logger.error('Error in videosRemove request validator.', { error: err }) - res.sendStatus(500) + return res.sendStatus(500) } const state = videos.getVideoState(video) @@ -82,7 +84,7 @@ function videosRemove (req, res, next) { function videosSearch (req, res, next) { const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS - req.checkParams('value', 'Should have a name').notEmpty() + req.checkParams('value', 'Should have a valid search').notEmpty() req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns) logger.debug('Checking videosSearch parameters', { parameters: req.params }) diff --git a/server/models/videos.js b/server/models/videos.js index 7bd41f7ee..f4658c371 100644 --- a/server/models/videos.js +++ b/server/models/videos.js @@ -21,6 +21,7 @@ const videosSchema = mongoose.Schema({ author: String, duration: Number, thumbnail: String, + tags: [ String ], createdDate: { type: Date, default: Date.now diff --git a/server/tests/api/checkParams.js b/server/tests/api/checkParams.js index e02fd0da5..95a7738f8 100644 --- a/server/tests/api/checkParams.js +++ b/server/tests/api/checkParams.js @@ -23,7 +23,14 @@ describe('Test parameters validator', function () { Object.keys(fields).forEach(function (field) { const value = fields[field] - req.field(field, value) + + if (Array.isArray(value)) { + for (let i = 0; i < value.length; i++) { + req.field(field + '[' + i + ']', value[i]) + } + } else { + req.field(field, value) + } }) Object.keys(attaches).forEach(function (attach) { @@ -198,7 +205,8 @@ describe('Test parameters validator', function () { it('Should fail without name', function (done) { const data = { - description: 'my super description' + description: 'my super description', + tags: [ 'tag1', 'tag2' ] } const attach = { 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') @@ -209,7 +217,8 @@ describe('Test parameters validator', function () { it('Should fail with a long name', function (done) { const data = { name: 'My very very very very very very very very very very very very very very very very long name', - description: 'my super description' + description: 'my super description', + tags: [ 'tag1', 'tag2' ] } const attach = { 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') @@ -219,7 +228,8 @@ describe('Test parameters validator', function () { it('Should fail without description', function (done) { const data = { - name: 'my super name' + name: 'my super name', + tags: [ 'tag1', 'tag2' ] } const attach = { 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') @@ -232,7 +242,8 @@ describe('Test parameters validator', function () { name: 'my super name', description: 'my super description which is very very very very very very very very very very very very very very' + 'very very very very very very very very very very very very very very very very very very very very very' + - 'very very very very very very very very very very very very very very very long' + 'very very very very very very very very very very very very very very very long', + tags: [ 'tag1', 'tag2' ] } const attach = { 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') @@ -240,11 +251,83 @@ describe('Test parameters validator', function () { makePostRequest(path, server.accessToken, data, attach, done) }) - it('Should fail without an input file', function (done) { + it('Should fail without tags', function (done) { const data = { name: 'my super name', description: 'my super description' } + const attach = { + 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') + } + makePostRequest(path, server.accessToken, data, attach, done) + }) + + it('Should fail with too many tags', function (done) { + const data = { + name: 'my super name', + description: 'my super description', + tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ] + } + const attach = { + 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') + } + makePostRequest(path, server.accessToken, data, attach, done) + }) + + it('Should fail with not enough tags', function (done) { + const data = { + name: 'my super name', + description: 'my super description', + tags: [ ] + } + const attach = { + 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') + } + makePostRequest(path, server.accessToken, data, attach, done) + }) + + it('Should fail with a tag length too low', function (done) { + const data = { + name: 'my super name', + description: 'my super description', + tags: [ 'tag1', 't' ] + } + const attach = { + 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') + } + makePostRequest(path, server.accessToken, data, attach, done) + }) + + it('Should fail with a tag length too big', function (done) { + const data = { + name: 'my super name', + description: 'my super description', + tags: [ 'mysupertagtoolong', 'tag1' ] + } + const attach = { + 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') + } + makePostRequest(path, server.accessToken, data, attach, done) + }) + + it('Should fail with malformed tags', function (done) { + const data = { + name: 'my super name', + description: 'my super description', + tags: [ 'my tag' ] + } + const attach = { + 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') + } + makePostRequest(path, server.accessToken, data, attach, done) + }) + + it('Should fail without an input file', function (done) { + const data = { + name: 'my super name', + description: 'my super description', + tags: [ 'tag1', 'tag2' ] + } const attach = {} makePostRequest(path, server.accessToken, data, attach, done) }) @@ -252,7 +335,8 @@ describe('Test parameters validator', function () { it('Should fail without an incorrect input file', function (done) { const data = { name: 'my super name', - description: 'my super description' + description: 'my super description', + tags: [ 'tag1', 'tag2' ] } const attach = { 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short_fake.webm') @@ -263,7 +347,8 @@ describe('Test parameters validator', function () { it('Should fail with a too big duration', function (done) { const data = { name: 'my super name', - description: 'my super description' + description: 'my super description', + tags: [ 'tag1', 'tag2' ] } const attach = { 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_too_long.webm') @@ -274,7 +359,8 @@ describe('Test parameters validator', function () { it('Should succeed with the correct parameters', function (done) { const data = { name: 'my super name', - description: 'my super description' + description: 'my super description', + tags: [ 'tag1', 'tag2' ] } const attach = { 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') diff --git a/server/tests/api/friendsAdvanced.js b/server/tests/api/friendsAdvanced.js index 7b895b6b5..86620254e 100644 --- a/server/tests/api/friendsAdvanced.js +++ b/server/tests/api/friendsAdvanced.js @@ -27,10 +27,11 @@ describe('Test advanced friends', function () { function uploadVideo (podNumber, callback) { const name = 'my super video' const description = 'my super description' + const tags = [ 'tag1', 'tag2' ] const fixture = 'video_short.webm' const server = servers[podNumber - 1] - return utils.uploadVideo(server.url, server.accessToken, name, description, fixture, callback) + return utils.uploadVideo(server.url, server.accessToken, name, description, tags, fixture, callback) } function getVideos (podNumber, callback) { diff --git a/server/tests/api/multiplePods.js b/server/tests/api/multiplePods.js index dac6dd410..40326c260 100644 --- a/server/tests/api/multiplePods.js +++ b/server/tests/api/multiplePods.js @@ -75,7 +75,11 @@ describe('Test multiple pods', function () { async.series([ function (next) { - utils.uploadVideo(servers[0].url, servers[0].accessToken, 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', next) + const name = 'my super name for pod 1' + const description = 'my super description for pod 1' + const tags = [ 'tag1p1', 'tag2p1' ] + const file = 'video_short1.webm' + utils.uploadVideo(servers[0].url, servers[0].accessToken, name, description, tags, file, next) }, function (next) { setTimeout(next, 11000) @@ -99,6 +103,7 @@ describe('Test multiple pods', function () { expect(video.podUrl).to.equal('localhost:9001') expect(video.magnetUri).to.exist expect(video.duration).to.equal(10) + expect(video.tags).to.deep.equal([ 'tag1p1', 'tag2p1' ]) expect(utils.dateIsValid(video.createdDate)).to.be.true if (server.url !== 'http://localhost:9001') { @@ -131,7 +136,11 @@ describe('Test multiple pods', function () { async.series([ function (next) { - utils.uploadVideo(servers[1].url, servers[1].accessToken, 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', next) + const name = 'my super name for pod 2' + const description = 'my super description for pod 2' + const tags = [ 'tag1p2', 'tag2p2', 'tag3p2' ] + const file = 'video_short2.webm' + utils.uploadVideo(servers[1].url, servers[1].accessToken, name, description, tags, file, next) }, function (next) { setTimeout(next, 11000) @@ -155,6 +164,7 @@ describe('Test multiple pods', function () { expect(video.podUrl).to.equal('localhost:9002') expect(video.magnetUri).to.exist expect(video.duration).to.equal(5) + expect(video.tags).to.deep.equal([ 'tag1p2', 'tag2p2', 'tag3p2' ]) expect(utils.dateIsValid(video.createdDate)).to.be.true if (server.url !== 'http://localhost:9002') { @@ -187,10 +197,18 @@ describe('Test multiple pods', function () { async.series([ function (next) { - utils.uploadVideo(servers[2].url, servers[2].accessToken, 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', next) + const name = 'my super name for pod 3' + const description = 'my super description for pod 3' + const tags = [ 'tag1p3' ] + const file = 'video_short3.webm' + utils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next) }, function (next) { - utils.uploadVideo(servers[2].url, servers[2].accessToken, 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', next) + const name = 'my super name for pod 3-2' + const description = 'my super description for pod 3-2' + const tags = [ 'tag2p3', 'tag3p3', 'tag4p3' ] + const file = 'video_short.webm' + utils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next) }, function (next) { setTimeout(next, 22000) @@ -224,6 +242,7 @@ describe('Test multiple pods', function () { expect(video1.podUrl).to.equal('localhost:9003') expect(video1.magnetUri).to.exist expect(video1.duration).to.equal(5) + expect(video1.tags).to.deep.equal([ 'tag1p3' ]) expect(utils.dateIsValid(video1.createdDate)).to.be.true expect(video2.name).to.equal('my super name for pod 3-2') @@ -231,6 +250,7 @@ describe('Test multiple pods', function () { expect(video2.podUrl).to.equal('localhost:9003') expect(video2.magnetUri).to.exist expect(video2.duration).to.equal(5) + expect(video2.tags).to.deep.equal([ 'tag2p3', 'tag3p3', 'tag4p3' ]) expect(utils.dateIsValid(video2.createdDate)).to.be.true if (server.url !== 'http://localhost:9003') { diff --git a/server/tests/api/singlePod.js b/server/tests/api/singlePod.js index 296dd0aa4..ef882b080 100644 --- a/server/tests/api/singlePod.js +++ b/server/tests/api/singlePod.js @@ -57,7 +57,11 @@ describe('Test a single pod', function () { it('Should upload the video', function (done) { this.timeout(5000) - utils.uploadVideo(server.url, server.accessToken, 'my super name', 'my super description', 'video_short.webm', done) + const name = 'my super name' + const description = 'my super description' + const tags = [ 'tag1', 'tag2', 'tag3' ] + const file = 'video_short.webm' + utils.uploadVideo(server.url, server.accessToken, name, description, tags, file, done) }) it('Should seed the uploaded video', function (done) { @@ -78,6 +82,7 @@ describe('Test a single pod', function () { expect(video.magnetUri).to.exist expect(video.author).to.equal('root') expect(video.isLocal).to.be.true + expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ]) expect(utils.dateIsValid(video.createdDate)).to.be.true utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { @@ -112,6 +117,7 @@ describe('Test a single pod', function () { expect(video.magnetUri).to.exist expect(video.author).to.equal('root') expect(video.isLocal).to.be.true + expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ]) expect(utils.dateIsValid(video.createdDate)).to.be.true utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { @@ -143,6 +149,7 @@ describe('Test a single pod', function () { expect(video.podUrl).to.equal('localhost:9001') expect(video.author).to.equal('root') expect(video.isLocal).to.be.true + expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ]) expect(utils.dateIsValid(video.createdDate)).to.be.true utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { @@ -168,6 +175,7 @@ describe('Test a single pod', function () { expect(video.podUrl).to.equal('localhost:9001') expect(video.author).to.equal('root') expect(video.isLocal).to.be.true + expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ]) expect(utils.dateIsValid(video.createdDate)).to.be.true utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) { @@ -235,7 +243,11 @@ describe('Test a single pod', function () { 'video_short1.webm', 'video_short2.webm', 'video_short3.webm' ] async.each(videos, function (video, callbackEach) { - utils.uploadVideo(server.url, server.accessToken, video + ' name', video + ' description', video, callbackEach) + const name = video + ' name' + const description = video + ' description' + const tags = [ 'tag1', 'tag2', 'tag3' ] + + utils.uploadVideo(server.url, server.accessToken, name, description, tags, video, callbackEach) }, done) }) diff --git a/server/tests/api/users.js b/server/tests/api/users.js index 9ab5083a0..7ab426d85 100644 --- a/server/tests/api/users.js +++ b/server/tests/api/users.js @@ -79,7 +79,12 @@ describe('Test users', function () { it('Should not be able to upload a video', function (done) { accessToken = 'mysupertoken' - utils.uploadVideo(server.url, accessToken, 'my super name', 'my super description', 'video_short.webm', 401, done) + + const name = 'my super name' + const description = 'my super description' + const tags = [ 'tag1', 'tag2' ] + const video = 'video_short.webm' + utils.uploadVideo(server.url, accessToken, name, description, tags, video, 401, done) }) it('Should not be able to make friends', function (done) { @@ -102,7 +107,11 @@ describe('Test users', function () { }) it('Should upload the video with the correct token', function (done) { - utils.uploadVideo(server.url, accessToken, 'my super name', 'my super description', 'video_short.webm', 204, function (err, res) { + const name = 'my super name' + const description = 'my super description' + const tags = [ 'tag1', 'tag2' ] + const video = 'video_short.webm' + utils.uploadVideo(server.url, accessToken, name, description, tags, video, 204, function (err, res) { if (err) throw err utils.getVideosList(server.url, function (err, res) { @@ -118,7 +127,11 @@ describe('Test users', function () { }) it('Should upload the video again with the correct token', function (done) { - utils.uploadVideo(server.url, accessToken, 'my super name 2', 'my super description 2', 'video_short.webm', 204, done) + const name = 'my super name 2' + const description = 'my super description 2' + const tags = [ 'tag1' ] + const video = 'video_short.webm' + utils.uploadVideo(server.url, accessToken, name, description, tags, video, 204, done) }) it('Should not be able to remove the video with an incorrect token', function (done) { diff --git a/server/tests/api/utils.js b/server/tests/api/utils.js index c6430c930..e0068eada 100644 --- a/server/tests/api/utils.js +++ b/server/tests/api/utils.js @@ -349,7 +349,7 @@ function testImage (url, videoName, imagePath, callback) { }) } -function uploadVideo (url, accessToken, name, description, fixture, specialStatus, end) { +function uploadVideo (url, accessToken, name, description, tags, fixture, specialStatus, end) { if (!end) { end = specialStatus specialStatus = 204 @@ -357,15 +357,20 @@ function uploadVideo (url, accessToken, name, description, fixture, specialStatu const path = '/api/v1/videos' - request(url) - .post(path) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + accessToken) - .field('name', name) - .field('description', description) - .attach('videofile', pathUtils.join(__dirname, 'fixtures', fixture)) - .expect(specialStatus) - .end(end) + const req = request(url) + .post(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .field('name', name) + .field('description', description) + + for (let i = 0; i < tags.length; i++) { + req.field('tags[' + i + ']', tags[i]) + } + + req.attach('videofile', pathUtils.join(__dirname, 'fixtures', fixture)) + .expect(specialStatus) + .end(end) } // --------------------------------------------------------------------------- -- 2.41.0