From 685df45e36a9f30576982a5b22c69933ee524402 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Thu, 15 Oct 2015 16:30:14 -0300 Subject: [PATCH] express endpoint + tests --- lib/expressapp.js | 22 +++++++++++++--- test/expressapp.js | 62 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 73 insertions(+), 11 deletions(-) diff --git a/lib/expressapp.js b/lib/expressapp.js index ea7e860..3ad7b2d 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -13,7 +13,7 @@ var Stats = require('./stats'); log.disableColor(); log.debug = log.verbose; -log.level = 'debug'; +log.level = 'info'; var ExpressApp = function() { this.app = express(); @@ -85,7 +85,8 @@ ExpressApp.prototype.start = function(opts, cb) { message: err.message, }).end(); } else { - var code = 500, message; + var code = 500, + message; if (_.isObject(err)) { code = err.code || err.statusCode; message = err.message || err.body; @@ -430,10 +431,25 @@ ExpressApp.prototype.start = function(opts, cb) { router.get('/v1/version/', function(req, res) { var server = getServer(req, res); - res.json( { serviceVersion: server.serviceVersion } ); + res.json({ + serviceVersion: server.serviceVersion + }); res.end(); }); + router.get('/v1/notifications/', function(req, res) { + getServerWithAuth(req, res, function(server) { + var opts = { + minTs: +Date.now() - (60 * 1000), + notificationId: req.query.notificationId, + }; + server.getNotifications(opts, function(err, notifications) { + if (err) return returnError(err, res, req); + res.json(notifications); + }); + }); + }); + this.app.use(opts.basePath || '/bws/api', router); WalletService.initialize(opts, cb); diff --git a/test/expressapp.js b/test/expressapp.js index 3e2b84f..2a9e8b0 100644 --- a/test/expressapp.js +++ b/test/expressapp.js @@ -38,6 +38,22 @@ describe('ExpressApp', function() { describe('Routes', function() { var testPort = 3239; var testHost = 'http://127.0.0.1'; + var httpServer; + + function start(ExpressApp, done) { + var app = new ExpressApp(); + httpServer = http.Server(app.app); + + app.start(config, function(err) { + should.not.exist(err); + httpServer.listen(testPort); + done(); + }); + }; + + afterEach(function() { + httpServer.close(); + }); it('/v2/wallets', function(done) { var server = { @@ -49,20 +65,15 @@ describe('ExpressApp', function() { getInstanceWithAuth: sinon.stub().callsArgWith(1, null, server), } }); - var app = new TestExpressApp(); - var httpServer = http.Server(app.app); - - app.start(config, function(err) { - should.not.exist(err); - httpServer.listen(testPort); + start(TestExpressApp, function() { var requestOptions = { - url: testHost + ':' + testPort + config.basePath + '/v1/wallets', + url: testHost + ':' + testPort + config.basePath + '/v2/wallets', headers: { 'x-identity': 'identity', 'x-signature': 'signature' } }; - request(requestOptions, function(err, response, body){ + request(requestOptions, function(err, response, body) { should.not.exist(err); response.statusCode.should.equal(200); body.should.equal('{}'); @@ -70,6 +81,41 @@ describe('ExpressApp', function() { }); }); }); + + it('/v1/notifications', function(done) { + var 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' + } + }; + request(requestOptions, function(err, response, body) { + should.not.exist(err); + response.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; + + clock.restore(); + done(); + }); + }); + }); }); }); });