fix(tests): Fixed unit and integration tests

This commit is contained in:
Chris Troutner
2019-10-31 07:52:30 -07:00
23 changed files with 2705 additions and 2645 deletions
+254 -8
View File
@@ -12,6 +12,7 @@ const chai = require("chai")
const assert = chai.assert
const utilRoute = require("../../src/routes/v3/util")
const nock = require("nock") // HTTP mocking
const sinon = require("sinon")
let originalEnvVars // Used during transition from integration to unit tests.
@@ -19,12 +20,15 @@ let originalEnvVars // Used during transition from integration to unit tests.
const { mockReq, mockRes } = require("./mocks/express-mocks")
const mockData = require("./mocks/util-mocks")
// Used for debugging.
const util = require("util")
util.inspect.defaultOptions = { depth: 1 }
const UtilRoute = utilRoute.UtilRoute
const utilRouteInst = new utilRoute.UtilRoute()
describe("#Util", () => {
let req, res
let sandbox
before(() => {
// Save existing environment variables.
@@ -58,12 +62,16 @@ describe("#Util", () => {
// Activate nock if it's inactive.
if (!nock.isActive()) nock.activate()
sandbox = sinon.createSandbox()
})
afterEach(() => {
// Clean up HTTP mocks.
nock.cleanAll() // clear interceptor list.
nock.restore()
sandbox.restore()
})
after(() => {
@@ -76,7 +84,7 @@ describe("#Util", () => {
describe("#root", async () => {
// root route handler.
const root = utilRoute.testableComponents.root
const root = utilRouteInst.root
it("should respond to GET for base route", async () => {
const result = root(req, res)
@@ -87,7 +95,7 @@ describe("#Util", () => {
})
describe("#validateAddressSingle", async () => {
const validateAddress = utilRoute.testableComponents.validateAddressSingle
const validateAddress = utilRouteInst.validateAddressSingle
it("should throw an error for an empty address", async () => {
const result = await validateAddress(req, res)
@@ -127,14 +135,14 @@ describe("#Util", () => {
// Mock the RPC call for unit tests.
if (process.env.TEST === "unit") {
nock(`${process.env.RPC_BASEURL}`)
.post(``)
.post(uri => uri.includes("/"))
.reply(200, { result: mockData.mockAddress })
}
req.params.address = `bitcoincash:qpujxqra3jmdlzzapwmmt7uspr7q0c9ff5hzljcrnd`
const result = await validateAddress(req, res)
//console.log(`result: ${util.inspect(result)}`)
// console.log(`result: ${util.inspect(result)}`)
assert.hasAnyKeys(result, [
"isvalid",
@@ -148,7 +156,7 @@ describe("#Util", () => {
})
describe("#validateAddressBulk", async () => {
const validateAddressBulk = utilRoute.testableComponents.validateAddressBulk
const validateAddressBulk = utilRouteInst.validateAddressBulk
it("should throw an error for an empty body", async () => {
const result = await validateAddressBulk(req, res)
@@ -247,7 +255,7 @@ describe("#Util", () => {
// Mock the RPC call for unit tests.
if (process.env.TEST === "unit") {
nock(`${process.env.RPC_BASEURL}`)
.post(``)
.post(uri => uri.includes("/"))
.reply(200, { result: mockData.mockAddress })
}
@@ -273,7 +281,7 @@ describe("#Util", () => {
// Mock the RPC call for unit tests.
if (process.env.TEST === "unit") {
nock(`${process.env.RPC_BASEURL}`)
.post(``)
.post(uri => uri.includes("/"))
.times(2)
.reply(200, { result: mockData.mockAddress })
}
@@ -297,4 +305,242 @@ describe("#Util", () => {
])
})
})
describe("#sweepWif", () => {
it("should throw 400 if WIF is not included", async () => {
req.body = {}
const result = await utilRouteInst.sweepWif(req, res)
assert.equal(res.statusCode, 400, "HTTP status code 400 expected.")
assert.include(
result.error,
"WIF needs to a proper compressed WIF starting with K or L",
"Proper error message"
)
})
it("should throw 400 if WIF is malformed", async () => {
req.body = {
wif: `abc123`
}
const result = await utilRouteInst.sweepWif(req, res)
assert.equal(res.statusCode, 400, "HTTP status code 400 expected.")
assert.include(
result.error,
"WIF needs to a proper compressed WIF starting with K or L",
"Proper error message"
)
})
it("should throw 400 if destination address is not included", async () => {
req.body = {
wif: "L5GEFg1tETLWBugmhSo9Zc4ms968qVmfmTroDxsJ982AiudAQGyt"
}
const result = await utilRouteInst.sweepWif(req, res)
assert.equal(res.statusCode, 400, "HTTP status code 400 expected.")
assert.include(
result.error,
"address can not be empty",
"Proper error message"
)
})
// Unit test only.
if (process.env.TEST === "unit") {
it("should generate transaction for valid token sweep", async () => {
// Mock the RPC call for unit tests.
sandbox
.stub(
utilRouteInst.blockbook.testableComponents,
"balanceFromBlockbook"
)
.resolves(mockData.mockBalance)
sandbox
.stub(
utilRouteInst.blockbook.testableComponents,
"utxosFromBlockbook"
)
.resolves(mockData.mockUtxos)
sandbox
.stub(utilRouteInst.bchjs.SLP.Utils, "tokenUtxoDetails")
.resolves(mockData.mockIsTokenUtxos)
// Mock sendRawTransaction() so that the hex does not actually get broadcast
// to the network.
sandbox
.stub(utilRouteInst.bchjs.RawTransactions, "sendRawTransaction")
.resolves("test-txid")
req.body = {
wif: "L5GEFg1tETLWBugmhSo9Zc4ms968qVmfmTroDxsJ982AiudAQGyt",
toAddr: "bitcoincash:qz2qn6zt4qmacf4r6c0e2pdcqsgnkxaa3ql2xpee6p"
}
const result = await utilRouteInst.sweepWif(req, res)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.equal(result, "test-txid")
})
}
it("should return balance if balance-only is true", async () => {
// Mock the RPC call for unit tests.
if (process.env.TEST === "unit") {
sandbox
.stub(
utilRouteInst.blockbook.testableComponents,
"balanceFromBlockbook"
)
.resolves(mockData.mockBalance)
}
// Mock sendRawTransaction() so that the hex does not actually get broadcast
// to the network.
sandbox
.stub(utilRouteInst.bchjs.RawTransactions, "sendRawTransaction")
.resolves("test-txid")
req.body = {
wif: "L5GEFg1tETLWBugmhSo9Zc4ms968qVmfmTroDxsJ982AiudAQGyt",
balanceOnly: true
}
const result = await utilRouteInst.sweepWif(req, res)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.isNumber(result)
})
// Unit tests only
if (process.env.TEST === "unit") {
it("should generate transaction for valid BCH-only sweep", async () => {
sandbox
.stub(
utilRouteInst.blockbook.testableComponents,
"balanceFromBlockbook"
)
.resolves(mockData.mockBalance)
sandbox
.stub(
utilRouteInst.blockbook.testableComponents,
"utxosFromBlockbook"
)
.resolves(mockData.mockUtxos)
// Force token utxo to appear as regular BCH utxo.
sandbox
.stub(utilRouteInst.bchjs.SLP.Utils, "tokenUtxoDetails")
.resolves([false, false])
// Mock sendRawTransaction() so that the hex does not actually get broadcast
// to the network.
sandbox
.stub(utilRouteInst.bchjs.RawTransactions, "sendRawTransaction")
.resolves("test-txid")
req.body = {
wif: "L5GEFg1tETLWBugmhSo9Zc4ms968qVmfmTroDxsJ982AiudAQGyt",
toAddr: "bitcoincash:qz2qn6zt4qmacf4r6c0e2pdcqsgnkxaa3ql2xpee6p"
}
const result = await utilRouteInst.sweepWif(req, res)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.equal(result, "test-txid")
})
it("should throw 422 error if no non-token UTXOs", async () => {
sandbox
.stub(
utilRouteInst.blockbook.testableComponents,
"balanceFromBlockbook"
)
.resolves(mockData.mockBalance)
sandbox
.stub(
utilRouteInst.blockbook.testableComponents,
"utxosFromBlockbook"
)
.resolves(mockData.mockUtxos)
// Force token utxo to appear as regular BCH utxo.
sandbox
.stub(utilRouteInst.bchjs.SLP.Utils, "tokenUtxoDetails")
.resolves(mockData.tokensOnly)
// Mock sendRawTransaction() so that the hex does not actually get broadcast
// to the network.
sandbox
.stub(utilRouteInst.bchjs.RawTransactions, "sendRawTransaction")
.resolves("test-txid")
req.body = {
wif: "L5GEFg1tETLWBugmhSo9Zc4ms968qVmfmTroDxsJ982AiudAQGyt",
toAddr: "bitcoincash:qz2qn6zt4qmacf4r6c0e2pdcqsgnkxaa3ql2xpee6p"
}
const result = await utilRouteInst.sweepWif(req, res)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.equal(res.statusCode, 422)
assert.property(result, "error")
assert.include(
result.error,
"Tokens found, but no BCH UTXOs found. Add BCH to wallet to move tokens"
)
})
it("should detect and throw error for multiple token classes", async () => {
sandbox
.stub(
utilRouteInst.blockbook.testableComponents,
"balanceFromBlockbook"
)
.resolves(mockData.mockBalance)
sandbox
.stub(
utilRouteInst.blockbook.testableComponents,
"utxosFromBlockbook"
)
.resolves(mockData.mockThreeUtxos)
// Force token utxo to appear as regular BCH utxo.
sandbox
.stub(utilRouteInst.bchjs.SLP.Utils, "tokenUtxoDetails")
.resolves(mockData.multipleTokens)
// Mock sendRawTransaction() so that the hex does not actually get broadcast
// to the network.
sandbox
.stub(utilRouteInst.bchjs.RawTransactions, "sendRawTransaction")
.resolves("test-txid")
req.body = {
wif: "L5GEFg1tETLWBugmhSo9Zc4ms968qVmfmTroDxsJ982AiudAQGyt",
toAddr: "bitcoincash:qz2qn6zt4qmacf4r6c0e2pdcqsgnkxaa3ql2xpee6p"
}
const result = await utilRouteInst.sweepWif(req, res)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.equal(res.statusCode, 422)
assert.property(result, "error")
assert.include(
result.error,
"Multiple token classes detected. This function only supports a single class of token"
)
})
}
})
})