diff --git a/lib/common/defaults.js b/lib/common/defaults.js index eabd6bd..ba65a13 100644 --- a/lib/common/defaults.js +++ b/lib/common/defaults.js @@ -83,4 +83,9 @@ Defaults.HISTORY_CACHE_ADDRESS_THRESOLD = 100; // Cache time for blockchain height (in seconds) Defaults.BLOCKHEIGHT_CACHE_TIME = 10 * 60; + +// Max allowed timespan for notification queries in seconds +Defaults.MAX_NOTIFICATIONS_TIMESPAN = 60 * 60 * 24 * 14 ; // ~ 2 weeks +Defaults.NOTIFICATIONS_TIMESPAN = 60; + module.exports = Defaults; diff --git a/lib/expressapp.js b/lib/expressapp.js index d955af4..cb60767 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -8,6 +8,9 @@ var express = require('express'); var bodyParser = require('body-parser'); var compression = require('compression'); +var Common = require('./common'); +var Defaults = Common.Defaults; + var WalletService = require('./server'); var Stats = require('./stats'); @@ -551,11 +554,14 @@ ExpressApp.prototype.start = function(opts, cb) { router.get('/v1/notifications/', function(req, res) { getServerWithAuth(req, res, function(server) { - var timeSpan = req.query.timeSpan ? Math.min(+req.query.timeSpan || 0, 60) : 60; + var timeSpan = req.query.timeSpan ? Math.min(+req.query.timeSpan || 0, Defaults.MAX_NOTIFICATIONS_TIMESPAN) : Defaults.NOTIFICATIONS_TIMESPAN; + + var opts = { minTs: +Date.now() - (timeSpan * 1000), notificationId: req.query.notificationId, }; + server.getNotifications(opts, function(err, notifications) { if (err) return returnError(err, res, req); res.json(notifications); diff --git a/test/expressapp.js b/test/expressapp.js index e70b60b..d437297 100644 --- a/test/expressapp.js +++ b/test/expressapp.js @@ -8,6 +8,11 @@ var should = chai.should(); var proxyquire = require('proxyquire'); var config = require('../config.js'); +var Common = require('../lib/common'); +var Defaults = Common.Defaults; + + + describe('ExpressApp', function() { describe('#constructor', function() { it('will set an express app', function() { @@ -186,7 +191,7 @@ describe('ExpressApp', function() { describe('/v1/notifications', function(done) { var server, TestExpressApp, clock; beforeEach(function() { - clock = sinon.useFakeTimers(1234000, 'Date'); + clock = sinon.useFakeTimers(2000000000, 'Date'); server = { getNotifications: sinon.stub().callsArgWith(1, null, {}) @@ -217,7 +222,7 @@ describe('ExpressApp', function() { body.should.equal('{}'); server.getNotifications.calledWith({ notificationId: '123', - minTs: +Date.now() - 60000, + minTs: +Date.now() - Defaults.NOTIFICATIONS_TIMESPAN * 1000, }).should.be.true; done(); }); @@ -243,10 +248,11 @@ describe('ExpressApp', function() { }); }); }); - it('should limit minTs to 60 seconds', function(done) { + it('should limit minTs to Defaults.MAX_NOTIFICATIONS_TIMESPAN', function(done) { start(TestExpressApp, function() { + var overLimit = Defaults.MAX_NOTIFICATIONS_TIMESPAN * 2; var requestOptions = { - url: testHost + ':' + testPort + config.basePath + '/v1/notifications' + '?timeSpan=90', + url: testHost + ':' + testPort + config.basePath + '/v1/notifications' + '?timeSpan=' + overLimit , headers: { 'x-identity': 'identity', 'x-signature': 'signature' @@ -256,9 +262,10 @@ describe('ExpressApp', function() { should.not.exist(err); res.statusCode.should.equal(200); body.should.equal('{}'); + server.getNotifications.calledWith({ notificationId: undefined, - minTs: Date.now() - 60000, // override minTs argument with a hardcoded 60 seconds span + minTs: Date.now() - Defaults.MAX_NOTIFICATIONS_TIMESPAN * 1000, // override minTs argument }).should.be.true; done(); });