From 07313784afc86deb406b7400ee39a0dc136ee62b Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 9 Jun 2019 14:45:28 -0700 Subject: [PATCH] Added unit and integration tests for balance endpoints with blockbook --- src/routes/v3/bitcore.js | 3 +++ src/routes/v3/blockbook.js | 35 ++++++++++++++++++----------------- test/v3/blockbook.js | 38 ++++++++++++++++++++++++-------------- 3 files changed, 45 insertions(+), 31 deletions(-) diff --git a/src/routes/v3/bitcore.js b/src/routes/v3/bitcore.js index 873f399..54965f3 100644 --- a/src/routes/v3/bitcore.js +++ b/src/routes/v3/bitcore.js @@ -24,6 +24,9 @@ const BITBOX = new BITBOXJS() // Connect the route endpoints to their handler functions. router.get("/", root) router.get("/balance/:address", balanceSingle) +router.post("/balance", balanceBulk) +router.get("/utxos/:address", utxosSingle) +router.post("/utxos", utxosBulk) // Root API endpoint. Simply acknowledges that it exists. function root(req, res, next) { diff --git a/src/routes/v3/blockbook.js b/src/routes/v3/blockbook.js index a046a27..059685b 100644 --- a/src/routes/v3/blockbook.js +++ b/src/routes/v3/blockbook.js @@ -19,11 +19,14 @@ util.inspect.defaultOptions = { depth: 1 } const BITBOXJS = require("@chris.troutner/bitbox-js") const BITBOX = new BITBOXJS() -//const BITCORE_URL = process.env.BITCORE_URL +//const BLOCKBOOK_URL = process.env.BLOCKBOOK_URL // Connect the route endpoints to their handler functions. router.get("/", root) router.get("/balance/:address", balanceSingle) +router.post("/balance", balanceBulk) +//router.get("/utxos/:address", utxosSingle) +//router.post("/utxos", utxosBulk) // Root API endpoint. Simply acknowledges that it exists. function root(req, res, next) { @@ -34,7 +37,7 @@ function root(req, res, next) { // Returns a Promise. async function balanceFromBlockbook(thisAddress) { try { - //console.log(`BITCORE_URL: ${BITCORE_URL}`) + //console.log(`BLOCKBOOK_URL: ${BLOCKBOOK_URL}`) // Convert the address to a cashaddr without a prefix. const addr = BITBOX.Address.toCashAddress(thisAddress) @@ -107,8 +110,6 @@ async function balanceSingle(req, res, next) { res.status(200) return res.json(retData) } catch (err) { - console.log(`err: ${JSON.stringify(err, null, 2)}`) - // Attempt to decode the error message. const { msg, status } = routeUtils.decodeError(err) if (msg) { @@ -123,7 +124,7 @@ async function balanceSingle(req, res, next) { return res.json({ error: util.inspect(err) }) } } -/* + // POST handler for bulk queries on address details async function balanceBulk(req, res, next) { try { @@ -147,7 +148,7 @@ async function balanceBulk(req, res, next) { } wlogger.debug( - `Executing bitcore.js/balanceBulk with these addresses: `, + `Executing blockbook.js/balanceBulk with these addresses: `, addresses ) @@ -178,7 +179,7 @@ async function balanceBulk(req, res, next) { // Loops through each address and creates an array of Promises, querying // Insight API in parallel. addresses = addresses.map(async (address, index) => - balanceFromBitcore(address) + balanceFromBlockbook(address) ) // Wait for all parallel Insight requests to return. @@ -195,18 +196,18 @@ async function balanceBulk(req, res, next) { return res.json({ error: msg }) } - wlogger.error(`Error in bitcore.js/balanceBulk().`, err) + wlogger.error(`Error in blockbook.js/balanceBulk().`, err) res.status(500) return res.json({ error: util.inspect(err) }) } } - -// Query the Bitcore Node API for utxos associated with a BCH address. +/* +// Query the Blockbook API for utxos associated with a BCH address. // Returns a Promise. -async function utxosFromBitcore(thisAddress) { +async function utxosFromBlockbook(thisAddress) { try { - //console.log(`BITCORE_URL: ${BITCORE_URL}`) + //console.log(`BLOCKBOOK_URL: ${BLOCKBOOK_URL}`) // Convert the address to a cashaddr without a prefix. const addr = BITBOX.Address.toCashAddress(thisAddress, false) @@ -215,9 +216,9 @@ async function utxosFromBitcore(thisAddress) { let network = "mainnet" if (process.env.NETWORK === "testnet") network = "testnet" - const path = `${process.env.BITCORE_URL}api/BCH/${network}/address/${addr}/?unspent=true` + const path = `${process.env.BLOCKBOOK_URL}api/BCH/${network}/address/${addr}/?unspent=true` - // Query the Bitcore Node API. + // Query the Blockbook API. const axiosResponse = await axios.get(path) const retData = axiosResponse.data //console.log(`retData: ${util.inspect(retData)}`) @@ -249,7 +250,7 @@ async function utxosSingle(req, res, next) { } wlogger.debug( - `Executing bitcore/balanceSingle with this address: `, + `Executing blockbook/balanceSingle with this address: `, address ) @@ -376,8 +377,8 @@ module.exports = { router, testableComponents: { root, - balanceSingle - //balanceBulk, + balanceSingle, + balanceBulk //utxosSingle, //utxosBulk } diff --git a/test/v3/blockbook.js b/test/v3/blockbook.js index 91d89a4..4cabf5c 100644 --- a/test/v3/blockbook.js +++ b/test/v3/blockbook.js @@ -175,7 +175,7 @@ describe("#Blockbook Router", () => { assert.isArray(result.txids) }) }) - /* + describe("#Balance Bulk", () => { // details route handler. const balanceBulk = blockbookRoute.testableComponents.balanceBulk @@ -250,7 +250,7 @@ describe("#Blockbook Router", () => { }) it("should throw 500 when network issues", async () => { - const savedUrl = process.env.BITCOINCOM_BASEURL + const savedUrl = process.env.BLOCKBOOK_URL try { req.body = { @@ -258,13 +258,13 @@ describe("#Blockbook Router", () => { } // Switch the Insight URL to something that will error out. - process.env.BITCOINCOM_BASEURL = "http://fakeurl/api/" + process.env.BLOCKBOOK_URL = "http://fakeurl/api/" const result = await balanceBulk(req, res) //console.log(`network issue result: ${util.inspect(result)}`) // Restore the saved URL. - process.env.BITCOINCOM_BASEURL = savedUrl + process.env.BLOCKBOOK_URL = savedUrl assert.isAbove(res.statusCode, 499, "HTTP status code 500 expected.") //assert.include(result.error, "ENOTFOUND", "Error message expected") @@ -275,7 +275,7 @@ describe("#Blockbook Router", () => { ) } catch (err) { // Restore the saved URL. - process.env.BITCOINCOM_BASEURL = savedUrl + process.env.BLOCKBOOK_URL = savedUrl } }) @@ -296,10 +296,20 @@ describe("#Blockbook Router", () => { // console.log(`result: ${util.inspect(result)}`) assert.isArray(result) - assert.hasAllKeys(result[0], ["confirmed", "unconfirmed", "balance"]) - assert.isNumber(result[0].confirmed) - assert.isNumber(result[0].unconfirmed) - assert.isNumber(result[0].balance) + assert.hasAnyKeys(result[0], [ + "page", + "totalPages", + "itemsOnPage", + "address", + "balance", + "totalReceived", + "totalSent", + "unconfirmedBalance", + "unconfirmedTxs", + "txs", + "txids" + ]) + assert.isArray(result[0].txids) }) it("should get details for multiple addresses", async () => { @@ -326,7 +336,7 @@ describe("#Blockbook Router", () => { assert.equal(result.length, 2, "2 outputs for 2 inputs") }) }) - + /* describe("#UTXOs Single", () => { // details route handler. const utxosSingle = blockbookRoute.testableComponents.utxosSingle @@ -503,7 +513,7 @@ describe("#Blockbook Router", () => { }) it("should throw 500 when network issues", async () => { - const savedUrl = process.env.BITCOINCOM_BASEURL + const savedUrl = process.env.BLOCKBOOK_URL try { req.body = { @@ -511,13 +521,13 @@ describe("#Blockbook Router", () => { } // Switch the Insight URL to something that will error out. - process.env.BITCOINCOM_BASEURL = "http://fakeurl/api/" + process.env.BLOCKBOOK_URL = "http://fakeurl/api/" const result = await utxosBulk(req, res) //console.log(`network issue result: ${util.inspect(result)}`) // Restore the saved URL. - process.env.BITCOINCOM_BASEURL = savedUrl + process.env.BLOCKBOOK_URL = savedUrl assert.isAbove(res.statusCode, 499, "HTTP status code 500 expected.") //assert.include(result.error, "ENOTFOUND", "Error message expected") @@ -528,7 +538,7 @@ describe("#Blockbook Router", () => { ) } catch (err) { // Restore the saved URL. - process.env.BITCOINCOM_BASEURL = savedUrl + process.env.BLOCKBOOK_URL = savedUrl } })