aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-08-20 16:59:25 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-08-20 17:11:38 +0200
commit1e2564d3927ce4ca4ca9a09930da6da7ebb4e9a1 (patch)
tree1a2601d63152da4906335c6116ba5f1e8992af33
parente861452fb26553177ad4e32bfb18b4fd8a5b1816 (diff)
downloadPeerTube-1e2564d3927ce4ca4ca9a09930da6da7ebb4e9a1.tar.gz
PeerTube-1e2564d3927ce4ca4ca9a09930da6da7ebb4e9a1.tar.zst
PeerTube-1e2564d3927ce4ca4ca9a09930da6da7ebb4e9a1.zip
Server: make friends urls come from the request instead of the
configuration file
-rw-r--r--config/default.yaml3
-rw-r--r--config/test-1.yaml4
-rw-r--r--config/test-2.yaml4
-rw-r--r--config/test-3.yaml4
-rw-r--r--config/test-4.yaml4
-rw-r--r--config/test-5.yaml5
-rw-r--r--config/test-6.yaml6
-rw-r--r--server/controllers/api/v1/pods.js6
-rw-r--r--server/helpers/custom-validators/misc.js11
-rw-r--r--server/initializers/checker.js4
-rw-r--r--server/lib/friends.js5
-rw-r--r--server/middlewares/validators/pods.js5
-rw-r--r--server/tests/api/check-params.js38
-rw-r--r--server/tests/utils/pods.js27
14 files changed, 82 insertions, 44 deletions
diff --git a/config/default.yaml b/config/default.yaml
index b5fd7b06d..d32d3e17b 100644
--- a/config/default.yaml
+++ b/config/default.yaml
@@ -18,8 +18,5 @@ storage:
18 logs: 'logs/' 18 logs: 'logs/'
19 thumbnails: 'thumbnails/' 19 thumbnails: 'thumbnails/'
20 20
21network:
22 friends: []
23
24electron: 21electron:
25 debug: false 22 debug: false
diff --git a/config/test-1.yaml b/config/test-1.yaml
index 3afcb1b04..0a8dd3937 100644
--- a/config/test-1.yaml
+++ b/config/test-1.yaml
@@ -14,7 +14,3 @@ storage:
14 uploads: 'test1/uploads/' 14 uploads: 'test1/uploads/'
15 logs: 'test1/logs/' 15 logs: 'test1/logs/'
16 thumbnails: 'test1/thumbnails/' 16 thumbnails: 'test1/thumbnails/'
17
18network:
19 friends:
20 - 'http://localhost:9002'
diff --git a/config/test-2.yaml b/config/test-2.yaml
index 51cc186eb..40f410559 100644
--- a/config/test-2.yaml
+++ b/config/test-2.yaml
@@ -14,7 +14,3 @@ storage:
14 uploads: 'test2/uploads/' 14 uploads: 'test2/uploads/'
15 logs: 'test2/logs/' 15 logs: 'test2/logs/'
16 thumbnails: 'test2/thumbnails/' 16 thumbnails: 'test2/thumbnails/'
17
18network:
19 friends:
20 - 'http://localhost:9003'
diff --git a/config/test-3.yaml b/config/test-3.yaml
index 7ef01ba4d..87b335228 100644
--- a/config/test-3.yaml
+++ b/config/test-3.yaml
@@ -14,7 +14,3 @@ storage:
14 uploads: 'test3/uploads/' 14 uploads: 'test3/uploads/'
15 logs: 'test3/logs/' 15 logs: 'test3/logs/'
16 thumbnails: 'test3/thumbnails/' 16 thumbnails: 'test3/thumbnails/'
17
18network:
19 friends:
20 - 'http://localhost:9001'
diff --git a/config/test-4.yaml b/config/test-4.yaml
index a4d3bb164..22abc0a58 100644
--- a/config/test-4.yaml
+++ b/config/test-4.yaml
@@ -14,7 +14,3 @@ storage:
14 uploads: 'test4/uploads/' 14 uploads: 'test4/uploads/'
15 logs: 'test4/logs/' 15 logs: 'test4/logs/'
16 thumbnails: 'test4/thumbnails/' 16 thumbnails: 'test4/thumbnails/'
17
18network:
19 friends:
20 - 'http://localhost:9002'
diff --git a/config/test-5.yaml b/config/test-5.yaml
index 0435c17fe..af5619994 100644
--- a/config/test-5.yaml
+++ b/config/test-5.yaml
@@ -14,8 +14,3 @@ storage:
14 uploads: 'test5/uploads/' 14 uploads: 'test5/uploads/'
15 logs: 'test5/logs/' 15 logs: 'test5/logs/'
16 thumbnails: 'test5/thumbnails/' 16 thumbnails: 'test5/thumbnails/'
17
18network:
19 friends:
20 - 'http://localhost:9001'
21 - 'http://localhost:9004'
diff --git a/config/test-6.yaml b/config/test-6.yaml
index b7dbd4bdd..5b8bf306b 100644
--- a/config/test-6.yaml
+++ b/config/test-6.yaml
@@ -14,9 +14,3 @@ storage:
14 uploads: 'test6/uploads/' 14 uploads: 'test6/uploads/'
15 logs: 'test6/logs/' 15 logs: 'test6/logs/'
16 thumbnails: 'test6/thumbnails/' 16 thumbnails: 'test6/thumbnails/'
17
18network:
19 friends:
20 - 'http://localhost:9001'
21 - 'http://localhost:9002'
22 - 'http://localhost:9003'
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')
19 19
20router.get('/', listPodsUrl) 20router.get('/', listPodsUrl)
21router.post('/', validators.podsAdd, addPods) 21router.post('/', validators.podsAdd, addPods)
22router.get('/makefriends', 22router.post('/makefriends',
23 oAuth.authenticate, 23 oAuth.authenticate,
24 admin.ensureIsAdmin, 24 admin.ensureIsAdmin,
25 validators.makeFriends, 25 validators.makeFriends,
@@ -83,7 +83,9 @@ function listPodsUrl (req, res, next) {
83} 83}
84 84
85function makeFriends (req, res, next) { 85function makeFriends (req, res, next) {
86 friends.makeFriends(function (err) { 86 const urls = req.body.urls
87
88 friends.makeFriends(urls, function (err) {
87 if (err) return next(err) 89 if (err) return next(err)
88 90
89 res.type('json').status(204).end() 91 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 @@
1'use strict' 1'use strict'
2 2
3const validator = require('express-validator').validator
4
3const miscValidators = { 5const miscValidators = {
4 exists: exists, 6 exists: exists,
5 isArray: isArray 7 isArray: isArray,
8 isEachUrl: isEachUrl
6} 9}
7 10
8function exists (value) { 11function exists (value) {
@@ -13,6 +16,12 @@ function isArray (value) {
13 return Array.isArray(value) 16 return Array.isArray(value)
14} 17}
15 18
19function isEachUrl (urls) {
20 return urls.every(function (url) {
21 return validator.isURL(url)
22 })
23}
24
16// --------------------------------------------------------------------------- 25// ---------------------------------------------------------------------------
17 26
18module.exports = miscValidators 27module.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 () {
17 const required = [ 'listen.port', 17 const required = [ 'listen.port',
18 'webserver.https', 'webserver.host', 'webserver.port', 18 'webserver.https', 'webserver.host', 'webserver.port',
19 'database.host', 'database.port', 'database.suffix', 19 'database.host', 'database.port', 'database.suffix',
20 'storage.certs', 'storage.uploads', 'storage.logs', 20 'storage.certs', 'storage.uploads', 'storage.logs', 'storage.thumbnails',
21 'network.friends', 'electron.debug' ] 21 'electron.debug' ]
22 const miss = [] 22 const miss = []
23 23
24 for (const key of required) { 24 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 @@
1'use strict' 1'use strict'
2 2
3const config = require('config')
4const each = require('async/each') 3const each = require('async/each')
5const eachLimit = require('async/eachLimit') 4const eachLimit = require('async/eachLimit')
6const eachSeries = require('async/eachSeries') 5const eachSeries = require('async/eachSeries')
@@ -44,7 +43,7 @@ function getMyCertificate (callback) {
44 fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback) 43 fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback)
45} 44}
46 45
47function makeFriends (callback) { 46function makeFriends (urls, callback) {
48 const podsScore = {} 47 const podsScore = {}
49 48
50 logger.info('Make friends!') 49 logger.info('Make friends!')
@@ -54,8 +53,6 @@ function makeFriends (callback) {
54 return callback(err) 53 return callback(err)
55 } 54 }
56 55
57 const urls = config.get('network.friends')
58
59 eachSeries(urls, function (url, callbackEach) { 56 eachSeries(urls, function (url, callbackEach) {
60 computeForeignPodsList(url, podsScore, callbackEach) 57 computeForeignPodsList(url, podsScore, callbackEach)
61 }, function (err) { 58 }, 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 = {
10} 10}
11 11
12function makeFriends (req, res, next) { 12function makeFriends (req, res, next) {
13 req.checkBody('urls', 'Should have an array of urls').isArray()
14 req.checkBody('urls', 'Should be an url').isEachUrl()
15
16 logger.debug('Checking makeFriends parameters', { parameters: req.body })
17
13 friends.hasFriends(function (err, hasFriends) { 18 friends.hasFriends(function (err, hasFriends) {
14 if (err) { 19 if (err) {
15 logger.error('Cannot know if we have friends.', { error: err }) 20 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 () {
108 }) 108 })
109 109
110 describe('When making friends', function () { 110 describe('When making friends', function () {
111 const body = {
112 urls: [ 'http://localhost:9002' ]
113 }
114
115 it('Should fail without urls', function (done) {
116 request(server.url)
117 .post(path + '/makefriends')
118 .set('Authorization', 'Bearer faketoken')
119 .set('Accept', 'application/json')
120 .expect(401, done)
121 })
122
123 it('Should fail with urls is not an array', function (done) {
124 request(server.url)
125 .post(path + '/makefriends')
126 .send({ urls: 'http://localhost:9002' })
127 .set('Authorization', 'Bearer faketoken')
128 .set('Accept', 'application/json')
129 .expect(401, done)
130 })
131
132 it('Should fail if the array is not composed by urls', function (done) {
133 request(server.url)
134 .post(path + '/makefriends')
135 .send({ urls: [ 'http://localhost:9002', 'localhost:coucou' ] })
136 .set('Authorization', 'Bearer faketoken')
137 .set('Accept', 'application/json')
138 .expect(401, done)
139 })
140
111 it('Should fail with a invalid token', function (done) { 141 it('Should fail with a invalid token', function (done) {
112 request(server.url) 142 request(server.url)
113 .get(path + '/makefriends') 143 .post(path + '/makefriends')
114 .query({ start: 'hello' }) 144 .send(body)
115 .set('Authorization', 'Bearer faketoken') 145 .set('Authorization', 'Bearer faketoken')
116 .set('Accept', 'application/json') 146 .set('Accept', 'application/json')
117 .expect(401, done) 147 .expect(401, done)
@@ -119,8 +149,8 @@ describe('Test parameters validator', function () {
119 149
120 it('Should fail if the user is not an administrator', function (done) { 150 it('Should fail if the user is not an administrator', function (done) {
121 request(server.url) 151 request(server.url)
122 .get(path + '/makefriends') 152 .post(path + '/makefriends')
123 .query({ start: 'hello' }) 153 .send(body)
124 .set('Authorization', 'Bearer ' + userAccessToken) 154 .set('Authorization', 'Bearer ' + userAccessToken)
125 .set('Accept', 'application/json') 155 .set('Accept', 'application/json')
126 .expect(403, done) 156 .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) {
27 expectedStatus = 204 27 expectedStatus = 204
28 } 28 }
29 29
30 // Which pod makes friends with which pod
31 const friendsMatrix = {
32 'http://localhost:9001': [
33 'http://localhost:9002'
34 ],
35 'http://localhost:9002': [
36 'http://localhost:9003'
37 ],
38 'http://localhost:9003': [
39 'http://localhost:9001'
40 ],
41 'http://localhost:9004': [
42 'http://localhost:9002'
43 ],
44 'http://localhost:9005': [
45 'http://localhost:9001',
46 'http://localhost:9004'
47 ],
48 'http://localhost:9006': [
49 'http://localhost:9001',
50 'http://localhost:9002',
51 'http://localhost:9003'
52 ]
53 }
30 const path = '/api/v1/pods/makefriends' 54 const path = '/api/v1/pods/makefriends'
31 55
32 // The first pod make friend with the third 56 // The first pod make friend with the third
33 request(url) 57 request(url)
34 .get(path) 58 .post(path)
35 .set('Accept', 'application/json') 59 .set('Accept', 'application/json')
36 .set('Authorization', 'Bearer ' + accessToken) 60 .set('Authorization', 'Bearer ' + accessToken)
61 .send({ 'urls': friendsMatrix[url] })
37 .expect(expectedStatus) 62 .expect(expectedStatus)
38 .end(function (err, res) { 63 .end(function (err, res) {
39 if (err) throw err 64 if (err) throw err