express endpoint + tests

This commit is contained in:
Ivan Socolsky 2015-10-15 16:30:14 -03:00
parent bf40b5a000
commit 685df45e36
2 changed files with 73 additions and 11 deletions

View File

@ -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);

View File

@ -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();
});
});
});
});
});
});