]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Server: make friends urls come from the request instead of the
authorChocobozzz <florian.bigard@gmail.com>
Sat, 20 Aug 2016 14:59:25 +0000 (16:59 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Sat, 20 Aug 2016 15:11:38 +0000 (17:11 +0200)
configuration file

14 files changed:
config/default.yaml
config/test-1.yaml
config/test-2.yaml
config/test-3.yaml
config/test-4.yaml
config/test-5.yaml
config/test-6.yaml
server/controllers/api/v1/pods.js
server/helpers/custom-validators/misc.js
server/initializers/checker.js
server/lib/friends.js
server/middlewares/validators/pods.js
server/tests/api/check-params.js
server/tests/utils/pods.js

index b5fd7b06d456adc0eb1c607abb9448c98e9b2729..d32d3e17b26ff464b2a4f6dcf9a35ea9b9916148 100644 (file)
@@ -18,8 +18,5 @@ storage:
   logs: 'logs/'
   thumbnails: 'thumbnails/'
 
-network:
-  friends: []
-
 electron:
   debug: false
index 3afcb1b043904d45cb754d4dff1ebef72ed27baa..0a8dd39379d05d5e8d0dd58ed75b82505efcec7e 100644 (file)
@@ -14,7 +14,3 @@ storage:
   uploads: 'test1/uploads/'
   logs: 'test1/logs/'
   thumbnails: 'test1/thumbnails/'
-
-network:
-  friends:
-    - 'http://localhost:9002'
index 51cc186eb422f1d7b525eb218ad34e1ce2cbc450..40f410559b7b2ea58d0f310e534b7e220e3bb12f 100644 (file)
@@ -14,7 +14,3 @@ storage:
   uploads: 'test2/uploads/'
   logs: 'test2/logs/'
   thumbnails: 'test2/thumbnails/'
-
-network:
-  friends:
-    - 'http://localhost:9003'
index 7ef01ba4da759d5b2f80ca3829284c64627e4ac2..87b33522895a519080384e2e666e9de6dba03363 100644 (file)
@@ -14,7 +14,3 @@ storage:
   uploads: 'test3/uploads/'
   logs: 'test3/logs/'
   thumbnails: 'test3/thumbnails/'
-
-network:
-  friends:
-    - 'http://localhost:9001'
index a4d3bb164f5ca946220918df3cea8cf2f9613e64..22abc0a583b7c0bab5dee382bd70ae6565b4bdb6 100644 (file)
@@ -14,7 +14,3 @@ storage:
   uploads: 'test4/uploads/'
   logs: 'test4/logs/'
   thumbnails: 'test4/thumbnails/'
-
-network:
-  friends:
-    - 'http://localhost:9002'
index 0435c17fee64af11a13300c85b92abdee9ff2ac4..af561999496866327e2a8b3ae9ad67342177fe16 100644 (file)
@@ -14,8 +14,3 @@ storage:
   uploads: 'test5/uploads/'
   logs: 'test5/logs/'
   thumbnails: 'test5/thumbnails/'
-
-network:
-  friends:
-    - 'http://localhost:9001'
-    - 'http://localhost:9004'
index b7dbd4bdd1cef9d51a32ce576f11a5d2960f52f6..5b8bf306b4a408d9d2dde6da59821f3061135ec0 100644 (file)
@@ -14,9 +14,3 @@ storage:
   uploads: 'test6/uploads/'
   logs: 'test6/logs/'
   thumbnails: 'test6/thumbnails/'
-
-network:
-  friends:
-    - 'http://localhost:9001'
-    - 'http://localhost:9002'
-    - 'http://localhost:9003'
index f61f2a4837451f1341225e73eef0f59dcdebee6b..982a1e3647066381ad7ffe519884adc41b4f2419 100644 (file)
@@ -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()
index 782ae3dee5f6e5af572370e6c8a6d0596a3cd1b0..13904ea1b1faa5d1c1998be0e539942bda5704cf 100644 (file)
@@ -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
index 4b6997547b31b62d957954757e4df8c1d18e9a53..2a33009b4ed7b65f26529364b24b0c0d0d5bf415 100644 (file)
@@ -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) {
index 6a2c37fd724b5f081ca43e9e8b81500e9eeacdee..667055d4c554f69cc5b8852d5b88ec92a0811323 100644 (file)
@@ -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) {
index fda2e865f58157ebc6a852aa8e130ce0fa8d5a9c..7c4d04affc8b497cab3d2a703ed879a86543a401 100644 (file)
@@ -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 })
index fc8b0a42a93e30130f774221c3e551cfe213fd92..ec666417c2a9c67bf29a59145d3dc4deb5b62945 100644 (file)
@@ -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)
index 3664921108cfb889bc3f27e6b35c06fac8e013a7..9a9148856bd4c0850854c6d3a417d0d046f5af4e 100644 (file)
@@ -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