Fixed tests.

This commit is contained in:
Chris Kleeschulte 2017-08-21 17:10:25 -04:00
parent 8f4029b3e5
commit a1ca054fb8
9 changed files with 1271 additions and 68 deletions

View File

@ -11,6 +11,7 @@ function AddressController(node) {
this._block = this.node.services.block; this._block = this.node.services.block;
this.txController = new TxController(node); this.txController = new TxController(node);
this.common = new Common({log: this.node.log}); this.common = new Common({log: this.node.log});
this._block = this.node.services.block;
} }
AddressController.prototype.show = function(req, res) { AddressController.prototype.show = function(req, res) {
@ -176,8 +177,7 @@ AddressController.prototype.transformUtxo = function(utxoArg) {
}; };
if (utxoArg.height && utxoArg.height > 0) { if (utxoArg.height && utxoArg.height > 0) {
utxo.height = utxoArg.height; utxo.height = utxoArg.height;
var height = this._block.getTip().height; utxo.confirmations = this._block.getTip().height - utxoArg.height + 1;
utxo.confirmations = height - utxoArg.height + 1;
} else { } else {
utxo.confirmations = 0; utxo.confirmations = 0;
} }

View File

@ -5,8 +5,8 @@ var Common = require('./common');
function StatusController(node) { function StatusController(node) {
this.node = node; this.node = node;
this.common = new Common({log: this.node.log}); this.common = new Common({log: this.node.log});
this._header = this.node.services.header;
this._block = this.node.services.block; this._block = this.node.services.block;
this._header = this.node.services.header;
} }
StatusController.prototype.show = function(req, res) { StatusController.prototype.show = function(req, res) {
@ -88,7 +88,7 @@ StatusController.prototype.getBestBlockHash = function(callback) {
}; };
StatusController.prototype.getDifficulty = function(callback) { StatusController.prototype.getDifficulty = function(callback) {
this._p2p.getInfo(function(err, info) { this.getInfo(function(err, info) {
if (err) { if (err) {
return callback(err); return callback(err);
} }

View File

@ -57,10 +57,8 @@ TxController.prototype.transformTransaction = function(transaction, options, cal
$.checkArgument(_.isFunction(callback)); $.checkArgument(_.isFunction(callback));
var confirmations = 0; var confirmations = 0;
if(transaction.height >= 0) {
if(transaction.__height >= 0) { confirmations = this._block.getTip().height - transaction.height + 1;
var height = this._block.getTip().height;
confirmations = height - transaction.__height + 1;
} }
var transformed = { var transformed = {

1207
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -28,8 +28,7 @@
"dependencies": { "dependencies": {
"JSONStream": "^1.3.1", "JSONStream": "^1.3.1",
"async": "*", "async": "*",
"bcoin": "bcoin-org/bcoin#886008a1822ce1da7fa8395ee7db4bcc1750a28a", "bitcore-lib": "5.0.0-beta.1",
"bitcore-lib": "bitpay/bitcore-lib#transitional",
"bitcore-message": "^1.0.1", "bitcore-message": "^1.0.1",
"body-parser": "^1.13.3", "body-parser": "^1.13.3",
"compression": "^1.6.1", "compression": "^1.6.1",

View File

@ -191,6 +191,7 @@ describe('Addresses', function() {
}; };
describe('/addr/:addr', function() { describe('/addr/:addr', function() {
var node = { var node = {
services: { address: {} },
getAddressSummary: sinon.stub().callsArgWith(2, null, summary) getAddressSummary: sinon.stub().callsArgWith(2, null, summary)
}; };
@ -229,7 +230,7 @@ describe('Addresses', function() {
}); });
it('handle error', function() { it('handle error', function() {
var testnode = {}; var testnode = { services: { address: { getTip: sinon.stub().returns({ height: 123 }) } } };
testnode.log = {}; testnode.log = {};
testnode.log.error = sinon.stub(); testnode.log.error = sinon.stub();
var controller = new AddressController(testnode); var controller = new AddressController(testnode);
@ -328,11 +329,7 @@ describe('Addresses', function() {
]; ];
var node = { var node = {
services: { services: { address: {}, block: { getTip: sinon.stub().returns({ height: 534230 }) } },
bitcoind: {
height: 534230
}
},
getAddressUnspentOutputs: sinon.stub().callsArgWith(2, null, utxos.slice(0, 1)) getAddressUnspentOutputs: sinon.stub().callsArgWith(2, null, utxos.slice(0, 1))
}; };
@ -392,11 +389,7 @@ describe('Addresses', function() {
]; ];
var node = { var node = {
services: { services: { address: {}, block: { getTip: sinon.stub().returns({ height: 534230 }) } },
bitcoind: {
height: 534230
}
},
getAddressUnspentOutputs: sinon.stub().callsArgWith(2, null, utxos) getAddressUnspentOutputs: sinon.stub().callsArgWith(2, null, utxos)
}; };
@ -550,11 +543,7 @@ describe('Addresses', function() {
var node = { var node = {
getAddressHistory: sinon.stub().callsArgWith(2, null, txinfos2), getAddressHistory: sinon.stub().callsArgWith(2, null, txinfos2),
services: { services: { address: {}, block: { getTip: sinon.stub().returns({ height: 534232 }) } },
bitcoind: {
height: 534232
}
},
network: 'testnet' network: 'testnet'
}; };
@ -687,11 +676,7 @@ describe('Addresses', function() {
var node = { var node = {
getAddressHistory: sinon.stub().callsArgWith(2, null, txinfos2), getAddressHistory: sinon.stub().callsArgWith(2, null, txinfos2),
services: { services: { block: { getTip: sinon.stub().returns({ height: 534232 }) }, address: {} },
bitcoind: {
height: 534232
}
},
network: 'testnet' network: 'testnet'
}; };
@ -716,7 +701,7 @@ describe('Addresses', function() {
}); });
describe('#_getTransformOptions', function() { describe('#_getTransformOptions', function() {
it('will return false with value of string "0"', function() { it('will return false with value of string "0"', function() {
var node = {}; var node = { services: { address: {}, block: {} } };
var addresses = new AddressController(node); var addresses = new AddressController(node);
var req = { var req = {
query: { query: {
@ -733,7 +718,7 @@ describe('Addresses', function() {
}); });
}); });
it('will return true with value of string "1"', function() { it('will return true with value of string "1"', function() {
var node = {}; var node = { services: { address: {}, block: {} } };
var addresses = new AddressController(node); var addresses = new AddressController(node);
var req = { var req = {
query: { query: {
@ -750,7 +735,7 @@ describe('Addresses', function() {
}); });
}); });
it('will return true with value of number "1"', function() { it('will return true with value of number "1"', function() {
var node = {}; var node = { services: { address: {}, block: {} } };
var addresses = new AddressController(node); var addresses = new AddressController(node);
var req = { var req = {
query: { query: {

View File

@ -8,7 +8,9 @@ describe('Index', function() {
describe('@constructor', function() { describe('@constructor', function() {
it('will set rate limiter options', function() { it('will set rate limiter options', function() {
var options = {}; var options = {};
var node = {}; var node = { services: { block: {} },
log: sinon.stub()
};
var index = new InsightAPI({ var index = new InsightAPI({
rateLimiterOptions: options, rateLimiterOptions: options,
node: node node: node
@ -16,7 +18,9 @@ describe('Index', function() {
index.rateLimiterOptions.should.equal(options); index.rateLimiterOptions.should.equal(options);
}); });
it('will set disable rate limiter option', function() { it('will set disable rate limiter option', function() {
var node = {}; var node = { services: { block: {} },
log: sinon.stub()
};
var index = new InsightAPI({ var index = new InsightAPI({
disableRateLimiter: true, disableRateLimiter: true,
node: node node: node
@ -29,7 +33,9 @@ describe('Index', function() {
var options = { var options = {
whitelist: ['127.0.0.1'] whitelist: ['127.0.0.1']
}; };
var node = {}; var node = { services: { block: {} },
log: sinon.stub()
};
var index = new InsightAPI({ var index = new InsightAPI({
rateLimiterOptions: options, rateLimiterOptions: options,
node: node node: node
@ -40,7 +46,7 @@ describe('Index', function() {
}); });
describe('#cache', function() { describe('#cache', function() {
it('will set cache control header', function(done) { it('will set cache control header', function(done) {
var node = { var node = { services: { block: {} },
log: sinon.stub() log: sinon.stub()
}; };
var index = new InsightAPI({ var index = new InsightAPI({
@ -60,9 +66,11 @@ describe('Index', function() {
}); });
}); });
it('will NOT set cache control header', function(done) { it('will NOT set cache control header', function(done) {
var node = {
var node = { services: { block: {} },
log: sinon.stub() log: sinon.stub()
}; };
var index = new InsightAPI({ var index = new InsightAPI({
enableCache: false, enableCache: false,
node: node node: node
@ -80,7 +88,7 @@ describe('Index', function() {
}); });
describe('#cacheShort', function() { describe('#cacheShort', function() {
it('will set SHORT cache control header', function(done) { it('will set SHORT cache control header', function(done) {
var node = { var node = { services: { block: {} },
log: sinon.stub() log: sinon.stub()
}; };
var index = new InsightAPI({ var index = new InsightAPI({
@ -101,7 +109,7 @@ describe('Index', function() {
}); });
}); });
it('will set SHORT DEFAULT cache control header', function(done) { it('will set SHORT DEFAULT cache control header', function(done) {
var node = { var node = { services: { block: {} },
log: sinon.stub() log: sinon.stub()
}; };
var index = new InsightAPI({ var index = new InsightAPI({
@ -123,7 +131,7 @@ describe('Index', function() {
}); });
describe('#cacheLong', function() { describe('#cacheLong', function() {
it('will set LONG cache control header', function(done) { it('will set LONG cache control header', function(done) {
var node = { var node = { services: { block: {} },
log: sinon.stub() log: sinon.stub()
}; };
var index = new InsightAPI({ var index = new InsightAPI({
@ -144,7 +152,8 @@ describe('Index', function() {
}); });
}); });
it('will set LONG DEFAULT cache control header', function(done) { it('will set LONG DEFAULT cache control header', function(done) {
var node = {
var node = { services: { block: {} },
log: sinon.stub() log: sinon.stub()
}; };
var index = new InsightAPI({ var index = new InsightAPI({
@ -166,7 +175,7 @@ describe('Index', function() {
}); });
describe('#setupRoutes', function() { describe('#setupRoutes', function() {
it('will use rate limiter by default', function() { it('will use rate limiter by default', function() {
var node = {}; var node = { services: { block: {} } };
var index = new InsightAPI({ var index = new InsightAPI({
node: node node: node
}); });
@ -189,7 +198,7 @@ describe('Index', function() {
middleware.callCount.should.equal(1); middleware.callCount.should.equal(1);
}); });
it('will NOT use rate limiter if disabled', function() { it('will NOT use rate limiter if disabled', function() {
var node = {}; var node = { services: { block: {} } };
var index = new InsightAPI({ var index = new InsightAPI({
node: node, node: node,
disableRateLimiter: true disableRateLimiter: true

View File

@ -30,10 +30,10 @@ describe('Status', function() {
var node = { var node = {
services: { services: {
bitcoind: { header: { getInfo: sinon.stub().callsArgWith(0, null, info) },
getInfo: sinon.stub().callsArgWith(0, null, info), block: {
getTip: sinon.stub().returns({ hash: outSetInfo.bestblock }),
getBestBlockHash: sinon.stub().callsArgWith(0, null, outSetInfo.bestblock), getBestBlockHash: sinon.stub().callsArgWith(0, null, outSetInfo.bestblock),
tiphash: outSetInfo.bestblock
} }
} }
}; };
@ -54,6 +54,7 @@ describe('Status', function() {
should.exist(data.info.difficulty); should.exist(data.info.difficulty);
should.exist(data.info.testnet); should.exist(data.info.testnet);
should.exist(data.info.relayfee); should.exist(data.info.relayfee);
done(); done();
} }
}; };
@ -114,10 +115,11 @@ describe('Status', function() {
it('should have correct data', function(done) { it('should have correct data', function(done) {
var node = { var node = {
services: { services: {
bitcoind: { header: { getInfo: sinon.stub() },
height: 500000, block: {
isSynced: sinon.stub().callsArgWith(0, null, true), isSynced: sinon.stub().callsArgWith(0, null, true),
syncPercentage: sinon.stub().callsArgWith(0, null, 99.99) syncPercentage: sinon.stub().callsArgWith(0, null, 100),
getTip: sinon.stub().returns({ height: 500000, hash: 'aa' })
} }
} }
}; };
@ -146,7 +148,11 @@ describe('Status', function() {
describe('/peer', function() { describe('/peer', function() {
it('should have correct data', function(done) { it('should have correct data', function(done) {
var node = {}; var node = {
services: {
block: { getTip: sinon.stub().returns({ height: 123 }) }
}
};
var expected = { var expected = {
connected: true, connected: true,
@ -170,7 +176,14 @@ describe('Status', function() {
describe('/version', function() { describe('/version', function() {
it('should have correct data', function(done) { it('should have correct data', function(done) {
var node = {}; var node = {
services: {
block: {
getTip: sinon.stub().returns({ height: 123 })
}
}
};
var expected = { var expected = {
version: require('../package.json').version version: require('../package.json').version
}; };

View File

@ -175,12 +175,8 @@ describe('Transactions', function() {
}; };
var node = { var node = {
services: { block: { getTip: sinon.stub().returns({ height: 534203 }) } },
getDetailedTransaction: sinon.stub().callsArgWith(1, null, detailedTransaction), getDetailedTransaction: sinon.stub().callsArgWith(1, null, detailedTransaction),
services: {
bitcoind: {
height: 534203
},
},
network: 'testnet' network: 'testnet'
}; };
@ -352,15 +348,11 @@ describe('Transactions', function() {
}; };
var node = { var node = {
services: { block: { getTip: sinon.stub().returns({ height: 534209 }) } },
getBlockOverview: sinon.stub().callsArgWith(1, null, blockOverview), getBlockOverview: sinon.stub().callsArgWith(1, null, blockOverview),
getDetailedTransaction: function(txid, callback) { getDetailedTransaction: function(txid, callback) {
callback(null, transactionDetails[txid]); callback(null, transactionDetails[txid]);
}, },
services: {
bitcoind: {
height: 534209
}
},
network: 'testnet' network: 'testnet'
}; };
@ -738,12 +730,8 @@ describe('Transactions', function() {
txinfos[1].tx.outputs[1].spentHeight = 112; txinfos[1].tx.outputs[1].spentHeight = 112;
var node = { var node = {
services: { block: { getTip: sinon.stub().returns({ height: 534223 }) } },
getAddressHistory: sinon.stub().callsArgWith(2, null, historyResult), getAddressHistory: sinon.stub().callsArgWith(2, null, historyResult),
services: {
bitcoind: {
height: 534223
}
},
network: 'testnet' network: 'testnet'
}; };
@ -948,6 +936,7 @@ describe('Transactions', function() {
var hex = '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff2303d6250800feb0aae355fe263600000963676d696e6572343208ae5800000000000000ffffffff01c018824a000000001976a91468bedce8982d25c3b6b03f6238cbad00378b8ead88ac00000000'; var hex = '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff2303d6250800feb0aae355fe263600000963676d696e6572343208ae5800000000000000ffffffff01c018824a000000001976a91468bedce8982d25c3b6b03f6238cbad00378b8ead88ac00000000';
var node = { var node = {
services: { block: { getTip: sinon.stub().returns({ height: 534233 }) } },
getTransaction: sinon.stub().callsArgWith(1, null, bitcore.Transaction().fromBuffer(new Buffer(hex, 'hex'))) getTransaction: sinon.stub().callsArgWith(1, null, bitcore.Transaction().fromBuffer(new Buffer(hex, 'hex')))
}; };
@ -988,6 +977,7 @@ describe('Transactions', function() {
var tx = bitcore.Transaction().fromBuffer(new Buffer(rawTx, 'hex')); var tx = bitcore.Transaction().fromBuffer(new Buffer(rawTx, 'hex'));
var node = { var node = {
services: { block: { getTip: sinon.stub().returns({ height: 534233 }) } },
network: bitcore.Networks.livenet network: bitcore.Networks.livenet
}; };
@ -1012,6 +1002,7 @@ describe('Transactions', function() {
var tx = bitcore.Transaction().fromBuffer(new Buffer(rawTx, 'hex')); var tx = bitcore.Transaction().fromBuffer(new Buffer(rawTx, 'hex'));
var node = { var node = {
services: { block: { getTip: sinon.stub().returns({ height: 534233 }) } },
network: bitcore.Networks.testnet network: bitcore.Networks.testnet
}; };
@ -1032,6 +1023,7 @@ describe('Transactions', function() {
]; ];
var node = { var node = {
services: { block: { getTip: sinon.stub().returns({ height: 534233 }) } },
network: bitcore.Networks.livenet network: bitcore.Networks.livenet
}; };