bitcore-wallet-service/test/expressapp.js

122 lines
3.7 KiB
JavaScript
Raw Normal View History

2015-10-06 09:42:56 -07:00
'use strict';
var chai = require('chai');
var sinon = require('sinon');
var request = require('request');
var http = require('http');
var should = chai.should();
var proxyquire = require('proxyquire');
var config = require('../config.js');
describe('ExpressApp', function() {
describe('#constructor', function() {
it('will set an express app', function() {
var TestExpressApp = proxyquire('../lib/expressapp', {});
var express = new TestExpressApp();
should.exist(express.app);
should.exist(express.app.use);
should.exist(express.app.enable);
});
});
describe('#start', function() {
it('will listen at the specified port', function(done) {
var initialize = sinon.stub().callsArg(1);
var TestExpressApp = proxyquire('../lib/expressapp', {
'./server': {
initialize: initialize
}
});
var app = new TestExpressApp();
var options = {};
app.start(config, function(err) {
should.not.exist(err);
initialize.callCount.should.equal(1);
done();
});
});
describe('Routes', function() {
var testPort = 3239;
var testHost = 'http://127.0.0.1';
2015-10-15 12:30:14 -07:00
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();
});
2015-10-06 09:42:56 -07:00
it('/v2/wallets', function(done) {
var server = {
getStatus: sinon.stub().callsArgWith(1, null, {})
};
var TestExpressApp = proxyquire('../lib/expressapp', {
'./server': {
initialize: sinon.stub().callsArg(1),
getInstanceWithAuth: sinon.stub().callsArgWith(1, null, server),
}
});
2015-10-15 12:30:14 -07:00
start(TestExpressApp, function() {
var requestOptions = {
url: testHost + ':' + testPort + config.basePath + '/v2/wallets',
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('{}');
done();
});
});
});
2015-10-06 09:42:56 -07:00
2015-10-15 12:30:14 -07:00
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() {
2015-10-06 09:42:56 -07:00
var requestOptions = {
2015-10-15 12:30:14 -07:00
url: testHost + ':' + testPort + config.basePath + '/v1/notifications' + '?notificationId=123&minTs=0',
2015-10-06 09:42:56 -07:00
headers: {
'x-identity': 'identity',
'x-signature': 'signature'
}
};
2015-10-15 12:30:14 -07:00
request(requestOptions, function(err, response, body) {
2015-10-06 09:42:56 -07:00
should.not.exist(err);
response.statusCode.should.equal(200);
body.should.equal('{}');
2015-10-15 12:30:14 -07:00
server.getNotifications.calledWith({
notificationId: '123',
minTs: 1234000 - 60000, // override minTs argument with a hardcoded 60 seconds span
}).should.be.true;
clock.restore();
2015-10-06 09:42:56 -07:00
done();
});
});
});
});
});
});