From 1e2564d3927ce4ca4ca9a09930da6da7ebb4e9a1 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 20 Aug 2016 16:59:25 +0200 Subject: Server: make friends urls come from the request instead of the configuration file --- server/controllers/api/v1/pods.js | 6 +++-- server/helpers/custom-validators/misc.js | 11 ++++++++- server/initializers/checker.js | 4 ++-- server/lib/friends.js | 5 +---- server/middlewares/validators/pods.js | 5 +++++ server/tests/api/check-params.js | 38 ++++++++++++++++++++++++++++---- server/tests/utils/pods.js | 27 ++++++++++++++++++++++- 7 files changed, 82 insertions(+), 14 deletions(-) (limited to 'server') diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js index f61f2a483..982a1e364 100644 --- a/server/controllers/api/v1/pods.js +++ b/server/controllers/api/v1/pods.js @@ -19,7 +19,7 @@ const Video = mongoose.model('Video') router.get('/', listPodsUrl) router.post('/', validators.podsAdd, addPods) -router.get('/makefriends', +router.post('/makefriends', oAuth.authenticate, admin.ensureIsAdmin, validators.makeFriends, @@ -83,7 +83,9 @@ function listPodsUrl (req, res, next) { } function makeFriends (req, res, next) { - friends.makeFriends(function (err) { + const urls = req.body.urls + + friends.makeFriends(urls, function (err) { if (err) return next(err) res.type('json').status(204).end() diff --git a/server/helpers/custom-validators/misc.js b/server/helpers/custom-validators/misc.js index 782ae3dee..13904ea1b 100644 --- a/server/helpers/custom-validators/misc.js +++ b/server/helpers/custom-validators/misc.js @@ -1,8 +1,11 @@ 'use strict' +const validator = require('express-validator').validator + const miscValidators = { exists: exists, - isArray: isArray + isArray: isArray, + isEachUrl: isEachUrl } function exists (value) { @@ -13,6 +16,12 @@ function isArray (value) { return Array.isArray(value) } +function isEachUrl (urls) { + return urls.every(function (url) { + return validator.isURL(url) + }) +} + // --------------------------------------------------------------------------- module.exports = miscValidators diff --git a/server/initializers/checker.js b/server/initializers/checker.js index 4b6997547..2a33009b4 100644 --- a/server/initializers/checker.js +++ b/server/initializers/checker.js @@ -17,8 +17,8 @@ function checkConfig () { const required = [ 'listen.port', 'webserver.https', 'webserver.host', 'webserver.port', 'database.host', 'database.port', 'database.suffix', - 'storage.certs', 'storage.uploads', 'storage.logs', - 'network.friends', 'electron.debug' ] + 'storage.certs', 'storage.uploads', 'storage.logs', 'storage.thumbnails', + 'electron.debug' ] const miss = [] for (const key of required) { diff --git a/server/lib/friends.js b/server/lib/friends.js index 6a2c37fd7..667055d4c 100644 --- a/server/lib/friends.js +++ b/server/lib/friends.js @@ -1,6 +1,5 @@ 'use strict' -const config = require('config') const each = require('async/each') const eachLimit = require('async/eachLimit') const eachSeries = require('async/eachSeries') @@ -44,7 +43,7 @@ function getMyCertificate (callback) { fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback) } -function makeFriends (callback) { +function makeFriends (urls, callback) { const podsScore = {} logger.info('Make friends!') @@ -54,8 +53,6 @@ function makeFriends (callback) { return callback(err) } - const urls = config.get('network.friends') - eachSeries(urls, function (url, callbackEach) { computeForeignPodsList(url, podsScore, callbackEach) }, function (err) { diff --git a/server/middlewares/validators/pods.js b/server/middlewares/validators/pods.js index fda2e865f..7c4d04aff 100644 --- a/server/middlewares/validators/pods.js +++ b/server/middlewares/validators/pods.js @@ -10,6 +10,11 @@ const validatorsPod = { } function makeFriends (req, res, next) { + req.checkBody('urls', 'Should have an array of urls').isArray() + req.checkBody('urls', 'Should be an url').isEachUrl() + + logger.debug('Checking makeFriends parameters', { parameters: req.body }) + friends.hasFriends(function (err, hasFriends) { if (err) { logger.error('Cannot know if we have friends.', { error: err }) diff --git a/server/tests/api/check-params.js b/server/tests/api/check-params.js index fc8b0a42a..ec666417c 100644 --- a/server/tests/api/check-params.js +++ b/server/tests/api/check-params.js @@ -108,10 +108,40 @@ describe('Test parameters validator', function () { }) describe('When making friends', function () { + const body = { + urls: [ 'http://localhost:9002' ] + } + + it('Should fail without urls', function (done) { + request(server.url) + .post(path + '/makefriends') + .set('Authorization', 'Bearer faketoken') + .set('Accept', 'application/json') + .expect(401, done) + }) + + it('Should fail with urls is not an array', function (done) { + request(server.url) + .post(path + '/makefriends') + .send({ urls: 'http://localhost:9002' }) + .set('Authorization', 'Bearer faketoken') + .set('Accept', 'application/json') + .expect(401, done) + }) + + it('Should fail if the array is not composed by urls', function (done) { + request(server.url) + .post(path + '/makefriends') + .send({ urls: [ 'http://localhost:9002', 'localhost:coucou' ] }) + .set('Authorization', 'Bearer faketoken') + .set('Accept', 'application/json') + .expect(401, done) + }) + it('Should fail with a invalid token', function (done) { request(server.url) - .get(path + '/makefriends') - .query({ start: 'hello' }) + .post(path + '/makefriends') + .send(body) .set('Authorization', 'Bearer faketoken') .set('Accept', 'application/json') .expect(401, done) @@ -119,8 +149,8 @@ describe('Test parameters validator', function () { it('Should fail if the user is not an administrator', function (done) { request(server.url) - .get(path + '/makefriends') - .query({ start: 'hello' }) + .post(path + '/makefriends') + .send(body) .set('Authorization', 'Bearer ' + userAccessToken) .set('Accept', 'application/json') .expect(403, done) diff --git a/server/tests/utils/pods.js b/server/tests/utils/pods.js index 366492110..9a9148856 100644 --- a/server/tests/utils/pods.js +++ b/server/tests/utils/pods.js @@ -27,13 +27,38 @@ function makeFriends (url, accessToken, expectedStatus, end) { expectedStatus = 204 } + // Which pod makes friends with which pod + const friendsMatrix = { + 'http://localhost:9001': [ + 'http://localhost:9002' + ], + 'http://localhost:9002': [ + 'http://localhost:9003' + ], + 'http://localhost:9003': [ + 'http://localhost:9001' + ], + 'http://localhost:9004': [ + 'http://localhost:9002' + ], + 'http://localhost:9005': [ + 'http://localhost:9001', + 'http://localhost:9004' + ], + 'http://localhost:9006': [ + 'http://localhost:9001', + 'http://localhost:9002', + 'http://localhost:9003' + ] + } const path = '/api/v1/pods/makefriends' // The first pod make friend with the third request(url) - .get(path) + .post(path) .set('Accept', 'application/json') .set('Authorization', 'Bearer ' + accessToken) + .send({ 'urls': friendsMatrix[url] }) .expect(expectedStatus) .end(function (err, res) { if (err) throw err -- cgit v1.2.3