From a088a74171badeeda1caf921d2af7449d8ea260b Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Wed, 25 Sep 2019 13:33:35 -0700 Subject: [PATCH] Added unit tests for Blockbook txBulk route handler. --- src/routes/v3/blockbook.js | 2 +- test/v3/blockbook.js | 175 +++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+), 1 deletion(-) diff --git a/src/routes/v3/blockbook.js b/src/routes/v3/blockbook.js index 7ae57f8..1b6d0a4 100644 --- a/src/routes/v3/blockbook.js +++ b/src/routes/v3/blockbook.js @@ -450,7 +450,7 @@ async function txBulk(req, res, next) { if (!Array.isArray(txids)) { res.status(400) return res.json({ - error: "txids needs to be an array. Use GET for single address." + error: "txids need to be an array. Use GET for single address." }) } diff --git a/test/v3/blockbook.js b/test/v3/blockbook.js index a0d4cfe..b05407c 100644 --- a/test/v3/blockbook.js +++ b/test/v3/blockbook.js @@ -702,4 +702,179 @@ describe("#Blockbook Router", () => { ]) }) }) + + describe("#txBulk", () => { + // route handler + const txBulk = blockbookRoute.testableComponents.txBulk + + it("should throw an error for an empty body", async () => { + req.body = {} + + const result = await txBulk(req, res) + //console.log(`result: ${util.inspect(result)}`) + + assert.equal(res.statusCode, 400, "HTTP status code 400 expected.") + assert.include( + result.error, + "txids need to be an array", + "Proper error message" + ) + }) + + it("should error on non-array single address", async () => { + req.body.txids = `5fe9b74056319a8c87f45cc745030715a6180758b94938dbf90d639d55652392` + + const result = await txBulk(req, res) + + assert.equal(res.statusCode, 400, "HTTP status code 400 expected.") + assert.include( + result.error, + "txids need to be an array", + "Proper error message" + ) + }) + + it("should throw 400 error if addresses array is too large", async () => { + const testArray = [] + for (var i = 0; i < 25; i++) testArray.push("") + + req.body.txids = testArray + + const result = await txBulk(req, res) + //console.log(`result: ${util.inspect(result)}`) + + assert.hasAllKeys(result, ["error"]) + assert.include(result.error, "Array too large") + }) + + it("should throw an error for an invalid address", async () => { + req.body = { + txids: [ + `5fe9b74056319a8c87f45cc745030715a6180758b94938dbf90d639d55652392`, + `abc` + ] + } + + const result = await txBulk(req, res) + + assert.equal(res.statusCode, 400, "HTTP status code 400 expected.") + assert.include( + result.error, + "txid must be of length 64", + "Proper error message" + ) + }) + + it("should throw 500 when network issues", async () => { + const savedUrl = process.env.BLOCKBOOK_URL + + try { + req.body = { + txids: [ + `5fe9b74056319a8c87f45cc745030715a6180758b94938dbf90d639d55652392` + ] + } + + // Switch the Insight URL to something that will error out. + process.env.BLOCKBOOK_URL = "http://fakeurl/api/" + + const result = await txBulk(req, res) + //console.log(`network issue result: ${util.inspect(result)}`) + + // Restore the saved URL. + process.env.BLOCKBOOK_URL = savedUrl + + assert.isAbove(res.statusCode, 499, "HTTP status code 500 expected.") + //assert.include(result.error, "ENOTFOUND", "Error message expected") + assert.include( + result.error, + "Network error: Could not communicate", + "Error message expected" + ) + } catch (err) { + // Restore the saved URL. + process.env.BLOCKBOOK_URL = savedUrl + } + }) + + it("should get details for a single address", async () => { + req.body = { + txids: [ + `5fe9b74056319a8c87f45cc745030715a6180758b94938dbf90d639d55652392` + ] + } + + // Mock the Insight URL for unit tests. + if (process.env.TEST === "unit") { + nock(mockServerUrl) + .get(uri => uri.includes("/")) + .reply(200, mockData.mockTx) + } + + // Call the details API. + const result = await txBulk(req, res) + //console.log(`result: ${util.inspect(result)}`) + + assert.isArray(result) + assert.hasAnyKeys(result[0], [ + "txid", + "version", + "vin", + "vout", + "blockHash", + "blockHeight", + "confirmations", + "blockTime", + "value", + "valueIn", + "fees", + "hex" + ]) + + // Vin + assert.isArray(result[0].vin) + assert.hasAnyKeys(result[0].vin[0], [ + "txid", + "sequence", + "n", + "addresses", + "value", + "hex" + ]) + + // Vout + assert.isArray(result[0].vout) + assert.hasAnyKeys(result[0].vout[0], [ + "value", + "n", + "spent", + "hex", + "addresses" + ]) + }) + + it("should get details for multiple txid", async () => { + req.body = { + txids: [ + `5fe9b74056319a8c87f45cc745030715a6180758b94938dbf90d639d55652392`, + `5fe9b74056319a8c87f45cc745030715a6180758b94938dbf90d639d55652392` + ] + } + + // Mock the Insight URL for unit tests. + if (process.env.TEST === "unit") { + nock(mockServerUrl) + .get(uri => uri.includes("/")) + .times(2) + .reply(200, mockData.mockTx) + } + + // Call the details API. + const result = await txBulk(req, res) + // console.log(`result: ${util.inspect(result)}`) + + assert.isArray(result) + assert.equal(result.length, 2, "2 outputs for 2 inputs") + }) + }) })