aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--server/controllers/api/v1/index.js2
-rw-r--r--server/controllers/api/v1/requests.js38
-rw-r--r--server/initializers/constants.js6
-rw-r--r--server/models/request.js11
-rw-r--r--server/tests/api/check-params.js21
-rw-r--r--server/tests/api/requests.js128
6 files changed, 199 insertions, 7 deletions
diff --git a/server/controllers/api/v1/index.js b/server/controllers/api/v1/index.js
index af41bc280..2e4fb2dab 100644
--- a/server/controllers/api/v1/index.js
+++ b/server/controllers/api/v1/index.js
@@ -7,12 +7,14 @@ const router = express.Router()
7const clientsController = require('./clients') 7const clientsController = require('./clients')
8const podsController = require('./pods') 8const podsController = require('./pods')
9const remoteController = require('./remote') 9const remoteController = require('./remote')
10const requestsController = require('./requests')
10const usersController = require('./users') 11const usersController = require('./users')
11const videosController = require('./videos') 12const videosController = require('./videos')
12 13
13router.use('/clients', clientsController) 14router.use('/clients', clientsController)
14router.use('/pods', podsController) 15router.use('/pods', podsController)
15router.use('/remote', remoteController) 16router.use('/remote', remoteController)
17router.use('/requests', requestsController)
16router.use('/users', usersController) 18router.use('/users', usersController)
17router.use('/videos', videosController) 19router.use('/videos', videosController)
18router.use('/*', badRequest) 20router.use('/*', badRequest)
diff --git a/server/controllers/api/v1/requests.js b/server/controllers/api/v1/requests.js
new file mode 100644
index 000000000..6018bb40c
--- /dev/null
+++ b/server/controllers/api/v1/requests.js
@@ -0,0 +1,38 @@
1'use strict'
2
3const express = require('express')
4const mongoose = require('mongoose')
5
6const constants = require('../../../initializers/constants')
7const middlewares = require('../../../middlewares')
8const admin = middlewares.admin
9const oAuth = middlewares.oauth
10
11const Request = mongoose.model('Request')
12
13const router = express.Router()
14
15router.get('/stats',
16 oAuth.authenticate,
17 admin.ensureIsAdmin,
18 getStatsRequests
19)
20
21// ---------------------------------------------------------------------------
22
23module.exports = router
24
25// ---------------------------------------------------------------------------
26
27function getStatsRequests (req, res, next) {
28 Request.list(function (err, requests) {
29 if (err) return next(err)
30
31 const remainingMilliSeconds = constants.REQUESTS_INTERVAL - (Date.now() % constants.REQUESTS_INTERVAL)
32
33 return res.json({
34 requests: requests,
35 remainingMilliSeconds: remainingMilliSeconds
36 })
37 })
38}
diff --git a/server/initializers/constants.js b/server/initializers/constants.js
index dd4eff493..76ebb8681 100644
--- a/server/initializers/constants.js
+++ b/server/initializers/constants.js
@@ -55,7 +55,7 @@ const FRIEND_SCORE = {
55} 55}
56 56
57// Time to wait between requests to the friends (10 min) 57// Time to wait between requests to the friends (10 min)
58let INTERVAL = 600000 58let REQUESTS_INTERVAL = 600000
59 59
60const OAUTH_LIFETIME = { 60const OAUTH_LIFETIME = {
61 ACCESS_TOKEN: 3600 * 4, // 4 hours 61 ACCESS_TOKEN: 3600 * 4, // 4 hours
@@ -109,7 +109,7 @@ const USER_ROLES = {
109// Special constants for a test instance 109// Special constants for a test instance
110if (isTestInstance() === true) { 110if (isTestInstance() === true) {
111 FRIEND_SCORE.BASE = 20 111 FRIEND_SCORE.BASE = 20
112 INTERVAL = 10000 112 REQUESTS_INTERVAL = 10000
113 CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14 113 CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14
114} 114}
115 115
@@ -121,7 +121,7 @@ module.exports = {
121 CONFIG: CONFIG, 121 CONFIG: CONFIG,
122 CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS, 122 CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS,
123 FRIEND_SCORE: FRIEND_SCORE, 123 FRIEND_SCORE: FRIEND_SCORE,
124 INTERVAL: INTERVAL, 124 REQUESTS_INTERVAL: REQUESTS_INTERVAL,
125 OAUTH_LIFETIME: OAUTH_LIFETIME, 125 OAUTH_LIFETIME: OAUTH_LIFETIME,
126 PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT, 126 PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT,
127 PODS_SCORE: PODS_SCORE, 127 PODS_SCORE: PODS_SCORE,
diff --git a/server/models/request.js b/server/models/request.js
index 4d521919a..4e510e18a 100644
--- a/server/models/request.js
+++ b/server/models/request.js
@@ -19,14 +19,15 @@ let timer = null
19 19
20const RequestSchema = mongoose.Schema({ 20const RequestSchema = mongoose.Schema({
21 request: mongoose.Schema.Types.Mixed, 21 request: mongoose.Schema.Types.Mixed,
22 to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'users' } ] 22 to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Pod' } ]
23}) 23})
24 24
25RequestSchema.statics = { 25RequestSchema.statics = {
26 activate, 26 activate,
27 deactivate, 27 deactivate,
28 flush, 28 flush,
29 forceSend 29 forceSend,
30 list
30} 31}
31 32
32RequestSchema.pre('save', function (next) { 33RequestSchema.pre('save', function (next) {
@@ -53,7 +54,7 @@ mongoose.model('Request', RequestSchema)
53 54
54function activate () { 55function activate () {
55 logger.info('Requests scheduler activated.') 56 logger.info('Requests scheduler activated.')
56 timer = setInterval(makeRequests.bind(this), constants.INTERVAL) 57 timer = setInterval(makeRequests.bind(this), constants.REQUESTS_INTERVAL)
57} 58}
58 59
59function deactivate () { 60function deactivate () {
@@ -72,6 +73,10 @@ function forceSend () {
72 makeRequests.call(this) 73 makeRequests.call(this)
73} 74}
74 75
76function list (callback) {
77 this.find({ }, callback)
78}
79
75// --------------------------------------------------------------------------- 80// ---------------------------------------------------------------------------
76 81
77// Make a requests to friends of a certain type 82// Make a requests to friends of a certain type
diff --git a/server/tests/api/check-params.js b/server/tests/api/check-params.js
index f060b2ad4..a696bc171 100644
--- a/server/tests/api/check-params.js
+++ b/server/tests/api/check-params.js
@@ -13,6 +13,7 @@ const usersUtils = require('../utils/users')
13 13
14describe('Test parameters validator', function () { 14describe('Test parameters validator', function () {
15 let server = null 15 let server = null
16 let userAccessToken = null
16 17
17 // --------------------------------------------------------------- 18 // ---------------------------------------------------------------
18 19
@@ -496,7 +497,6 @@ describe('Test parameters validator', function () {
496 describe('Of the users API', function () { 497 describe('Of the users API', function () {
497 const path = '/api/v1/users/' 498 const path = '/api/v1/users/'
498 let userId = null 499 let userId = null
499 let userAccessToken = null
500 500
501 describe('When listing users', function () { 501 describe('When listing users', function () {
502 it('Should fail with a bad start pagination', function (done) { 502 it('Should fail with a bad start pagination', function (done) {
@@ -721,6 +721,25 @@ describe('Test parameters validator', function () {
721 }) 721 })
722 }) 722 })
723 723
724 describe('Of the requests API', function () {
725 const path = '/api/v1/requests/stats'
726
727 it('Should fail with an non authenticated user', function (done) {
728 request(server.url)
729 .get(path)
730 .set('Accept', 'application/json')
731 .expect(401, done)
732 })
733
734 it('Should fail with a non admin user', function (done) {
735 request(server.url)
736 .get(path)
737 .set('Authorization', 'Bearer ' + userAccessToken)
738 .set('Accept', 'application/json')
739 .expect(403, done)
740 })
741 })
742
724 after(function (done) { 743 after(function (done) {
725 process.kill(-server.app.pid) 744 process.kill(-server.app.pid)
726 745
diff --git a/server/tests/api/requests.js b/server/tests/api/requests.js
new file mode 100644
index 000000000..af36f6e34
--- /dev/null
+++ b/server/tests/api/requests.js
@@ -0,0 +1,128 @@
1'use strict'
2
3const chai = require('chai')
4const each = require('async/each')
5const expect = chai.expect
6const request = require('supertest')
7
8const loginUtils = require('../utils/login')
9const podsUtils = require('../utils/pods')
10const serversUtils = require('../utils/servers')
11const videosUtils = require('../utils/videos')
12
13describe('Test requests stats', function () {
14 const path = '/api/v1/requests/stats'
15 let servers = []
16
17 function uploadVideo (server, callback) {
18 const name = 'my super video'
19 const description = 'my super description'
20 const tags = [ 'tag1', 'tag2' ]
21 const fixture = 'video_short.webm'
22
23 videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, fixture, callback)
24 }
25
26 function getRequestsStats (server, callback) {
27 request(server.url)
28 .get(path)
29 .set('Accept', 'application/json')
30 .set('Authorization', 'Bearer ' + server.accessToken)
31 .expect(200)
32 .end(callback)
33 }
34
35 // ---------------------------------------------------------------
36
37 before(function (done) {
38 this.timeout(20000)
39 serversUtils.flushAndRunMultipleServers(2, function (serversRun, urlsRun) {
40 servers = serversRun
41
42 each(servers, function (server, callbackEach) {
43 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
44 if (err) return callbackEach(err)
45
46 server.accessToken = accessToken
47 callbackEach()
48 })
49 }, function (err) {
50 if (err) throw err
51
52 const server1 = servers[0]
53 podsUtils.makeFriends(server1.url, server1.accessToken, done)
54 })
55 })
56 })
57
58 it('Should have a correct timer', function (done) {
59 const server = servers[0]
60
61 getRequestsStats(server, function (err, res) {
62 if (err) throw err
63
64 const body = res.body
65 expect(body.remainingMilliSeconds).to.be.at.least(0)
66 expect(body.remainingMilliSeconds).to.be.at.most(10000)
67
68 done()
69 })
70 })
71
72 it('Should have the correct request', function (done) {
73 this.timeout(15000)
74
75 const server = servers[0]
76 // Ensure the requests of pod 1 won't be made
77 servers[1].app.kill()
78
79 uploadVideo(server, function (err) {
80 if (err) throw err
81
82 getRequestsStats(server, function (err, res) {
83 if (err) throw err
84
85 const body = res.body
86 expect(body.requests).to.have.lengthOf(1)
87
88 const request = body.requests[0]
89 expect(request.to).to.have.lengthOf(1)
90 expect(request.request.type).to.equal('add')
91
92 // Wait one cycle
93 setTimeout(done, 10000)
94 })
95 })
96 })
97
98 it('Should have the correct requests', function (done) {
99 const server = servers[0]
100
101 uploadVideo(server, function (err) {
102 if (err) throw err
103
104 getRequestsStats(server, function (err, res) {
105 if (err) throw err
106
107 const body = res.body
108 expect(body.requests).to.have.lengthOf(2)
109
110 const request = body.requests[1]
111 expect(request.to).to.have.lengthOf(1)
112 expect(request.request.type).to.equal('add')
113
114 done()
115 })
116 })
117 })
118
119 after(function (done) {
120 process.kill(-servers[0].app.pid)
121
122 if (this.ok) {
123 serversUtils.flushTests(done)
124 } else {
125 done()
126 }
127 })
128})