logs: 'logs/'
thumbnails: 'thumbnails/'
-network:
- friends: []
-
electron:
debug: false
uploads: 'test1/uploads/'
logs: 'test1/logs/'
thumbnails: 'test1/thumbnails/'
-
-network:
- friends:
- - 'http://localhost:9002'
uploads: 'test2/uploads/'
logs: 'test2/logs/'
thumbnails: 'test2/thumbnails/'
-
-network:
- friends:
- - 'http://localhost:9003'
uploads: 'test3/uploads/'
logs: 'test3/logs/'
thumbnails: 'test3/thumbnails/'
-
-network:
- friends:
- - 'http://localhost:9001'
uploads: 'test4/uploads/'
logs: 'test4/logs/'
thumbnails: 'test4/thumbnails/'
-
-network:
- friends:
- - 'http://localhost:9002'
uploads: 'test5/uploads/'
logs: 'test5/logs/'
thumbnails: 'test5/thumbnails/'
-
-network:
- friends:
- - 'http://localhost:9001'
- - 'http://localhost:9004'
uploads: 'test6/uploads/'
logs: 'test6/logs/'
thumbnails: 'test6/thumbnails/'
-
-network:
- friends:
- - 'http://localhost:9001'
- - 'http://localhost:9002'
- - 'http://localhost:9003'
router.get('/', listPodsUrl)
router.post('/', validators.podsAdd, addPods)
-router.get('/makefriends',
+router.post('/makefriends',
oAuth.authenticate,
admin.ensureIsAdmin,
validators.makeFriends,
}
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()
'use strict'
+const validator = require('express-validator').validator
+
const miscValidators = {
exists: exists,
- isArray: isArray
+ isArray: isArray,
+ isEachUrl: isEachUrl
}
function exists (value) {
return Array.isArray(value)
}
+function isEachUrl (urls) {
+ return urls.every(function (url) {
+ return validator.isURL(url)
+ })
+}
+
// ---------------------------------------------------------------------------
module.exports = miscValidators
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) {
'use strict'
-const config = require('config')
const each = require('async/each')
const eachLimit = require('async/eachLimit')
const eachSeries = require('async/eachSeries')
fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback)
}
-function makeFriends (callback) {
+function makeFriends (urls, callback) {
const podsScore = {}
logger.info('Make friends!')
return callback(err)
}
- const urls = config.get('network.friends')
-
eachSeries(urls, function (url, callbackEach) {
computeForeignPodsList(url, podsScore, callbackEach)
}, function (err) {
}
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 })
})
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)
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)
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