From 09c5af073dc30dfccdda352525fcea83b3d5fcdb Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 19 Oct 2015 14:58:38 -0300 Subject: [PATCH] accept minTs arg on v1/notifications but limit it to now - 60s --- lib/expressapp.js | 2 +- test/expressapp.js | 84 ++++++++++++++++++++++++++++++---------------- 2 files changed, 57 insertions(+), 29 deletions(-) diff --git a/lib/expressapp.js b/lib/expressapp.js index df8268d..f67109e 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -440,7 +440,7 @@ ExpressApp.prototype.start = function(opts, cb) { router.get('/v1/notifications/', function(req, res) { getServerWithAuth(req, res, function(server) { var opts = { - minTs: +Date.now() - (60 * 1000), + minTs: Math.max(+req.query.minTs, +Date.now() - (60 * 1000)), notificationId: req.query.notificationId, }; server.getNotifications(opts, function(err, notifications) { diff --git a/test/expressapp.js b/test/expressapp.js index cbf8671..02cb1ae 100644 --- a/test/expressapp.js +++ b/test/expressapp.js @@ -84,37 +84,65 @@ describe('ExpressApp', function() { }); }); - it('/v1/notifications', function(done) { - var clock = sinon.useFakeTimers(1234000, 'Date'); + describe('/v1/notifications', function(done) { + var server, TestExpressApp, clock; + beforeEach(function() { + clock = sinon.useFakeTimers(1234000, 'Date'); - var server = { - getNotifications: sinon.stub().callsArgWith(1, null, {}) - }; - var TestExpressApp = proxyquire('../lib/expressapp', { - './server': { - initialize: sinon.stub().callsArg(1), - getInstanceWithAuth: sinon.stub().callsArgWith(1, null, server), - } - }); - start(TestExpressApp, function() { - var requestOptions = { - url: testHost + ':' + testPort + config.basePath + '/v1/notifications' + '?notificationId=123&minTs=0', - headers: { - 'x-identity': 'identity', - 'x-signature': 'signature' - } + server = { + getNotifications: sinon.stub().callsArgWith(1, null, {}) }; - request(requestOptions, function(err, res, body) { - should.not.exist(err); - res.statusCode.should.equal(200); - body.should.equal('{}'); - server.getNotifications.calledWith({ - notificationId: '123', - minTs: 1234000 - 60000, // override minTs argument with a hardcoded 60 seconds span - }).should.be.true; + TestExpressApp = proxyquire('../lib/expressapp', { + './server': { + initialize: sinon.stub().callsArg(1), + getInstanceWithAuth: sinon.stub().callsArgWith(1, null, server), + } + }); + }); + afterEach(function() { + clock.restore(); + }); - clock.restore(); - done(); + it('should fetch notifications from a specific id', function(done) { + start(TestExpressApp, function() { + var requestOptions = { + url: testHost + ':' + testPort + config.basePath + '/v1/notifications' + '?notificationId=123&minTs=' + (Date.now() - 30000), + headers: { + 'x-identity': 'identity', + 'x-signature': 'signature' + } + }; + request(requestOptions, function(err, res, body) { + should.not.exist(err); + res.statusCode.should.equal(200); + body.should.equal('{}'); + server.getNotifications.calledWith({ + notificationId: '123', + minTs: +Date.now() - 30000, + }).should.be.true; + done(); + }); + }); + }); + it('should limit minTs to 60 seconds', function(done) { + start(TestExpressApp, function() { + var requestOptions = { + url: testHost + ':' + testPort + config.basePath + '/v1/notifications' + '?minTs=1', + headers: { + 'x-identity': 'identity', + 'x-signature': 'signature' + } + }; + request(requestOptions, function(err, res, body) { + 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 + }).should.be.true; + done(); + }); }); }); });