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
+6 -5
View File
@@ -385,8 +385,9 @@ describe("#Block", () => {
// Mock the RPC call for unit tests.
if (process.env.TEST === "unit") {
console.log(`process.env.RPC_BASEURL: ${process.env.RPC_BASEURL}`)
nock(`${process.env.RPC_BASEURL}`)
.post(``)
.post(uri => uri.includes("/"))
.reply(500, {
error: {
code: -1,
@@ -410,7 +411,7 @@ describe("#Block", () => {
// 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.mockBlockHash })
}
@@ -501,7 +502,7 @@ describe("#Block", () => {
// Mock the RPC call for unit tests.
if (process.env.TEST === "unit") {
nock(`${process.env.RPC_BASEURL}`)
.post(``)
.post(uri => uri.includes("/"))
.reply(500, {
error: {
code: -1,
@@ -546,7 +547,7 @@ describe("#Block", () => {
// Mock the Insight URL for unit tests.
if (process.env.TEST === "unit") {
nock(`${process.env.RPC_BASEURL}`)
.post(``)
.post(uri => uri.includes("/"))
.reply(200, { result: mockData.mockBlockHash })
nock(`${process.env.BITCOINCOM_BASEURL}`)
@@ -589,7 +590,7 @@ describe("#Block", () => {
// Mock the Insight URL for unit tests.
if (process.env.TEST === "unit") {
nock(`${process.env.RPC_BASEURL}`)
.post(``)
.post(uri => uri.includes("/"))
.times(2)
.reply(200, { result: mockData.mockBlockHash })
+20 -20
View File
@@ -119,7 +119,7 @@ describe("#BlockchainRouter", () => {
// 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.mockBlockHash })
}
@@ -161,7 +161,7 @@ describe("#BlockchainRouter", () => {
// 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.mockBlockchainInfo })
}
@@ -213,7 +213,7 @@ describe("#BlockchainRouter", () => {
// 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: 126769 })
}
@@ -264,7 +264,7 @@ describe("#BlockchainRouter", () => {
// 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:
"000000202ed2723e7590e2b937f6821a99d6764cb8799bf30f8e300000000000000000001d311c02df9a1e3f57b8dbdcf97ec8dbc3109a26779724c63e560b29ad9ea501e2af955d286403183049e39c"
@@ -288,7 +288,7 @@ describe("#BlockchainRouter", () => {
// 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.mockBlockHeader })
}
@@ -404,7 +404,7 @@ describe("#BlockchainRouter", () => {
// 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.mockBlockHeaderConcise })
}
@@ -431,7 +431,7 @@ describe("#BlockchainRouter", () => {
// 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.mockBlockHeader })
}
@@ -468,7 +468,7 @@ describe("#BlockchainRouter", () => {
// 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.mockBlockHeaderConcise })
}
@@ -511,7 +511,7 @@ describe("#BlockchainRouter", () => {
// 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.mockChainTips })
}
@@ -552,7 +552,7 @@ describe("#BlockchainRouter", () => {
// 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: 4049809.205246544 })
}
@@ -592,7 +592,7 @@ describe("#BlockchainRouter", () => {
// 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.mockMempoolInfo })
}
@@ -638,7 +638,7 @@ describe("#BlockchainRouter", () => {
// 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.mockRawMempool })
}
@@ -690,7 +690,7 @@ describe("#BlockchainRouter", () => {
// 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: { error: "Transaction not in mempool" } })
}
@@ -836,7 +836,7 @@ describe("#BlockchainRouter", () => {
// 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.mockTxOut })
}
@@ -902,7 +902,7 @@ describe("#BlockchainRouter", () => {
// 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.mockTxOutProof })
}
@@ -963,7 +963,7 @@ describe("#BlockchainRouter", () => {
// 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.mockTxOutProof })
}
@@ -982,7 +982,7 @@ describe("#BlockchainRouter", () => {
// 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.mockTxOutProof })
}
@@ -1042,7 +1042,7 @@ describe("#BlockchainRouter", () => {
// 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: [expected] })
}
@@ -1109,7 +1109,7 @@ describe("#BlockchainRouter", () => {
// 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: [expected] })
}
@@ -1132,7 +1132,7 @@ describe("#BlockchainRouter", () => {
// 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: [expected] })
}
+1 -1
View File
@@ -115,7 +115,7 @@ describe("#ControlRouter", () => {
// 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.mockGetNetworkInfo })
}
+2 -2
View File
@@ -114,7 +114,7 @@ describe("#Mining", () => {
// 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.mockMiningInfo })
}
@@ -163,7 +163,7 @@ describe("#Mining", () => {
// 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: 517604755.6648782 })
}
+62 -1
View File
@@ -313,6 +313,64 @@ const mockFoobar = {
u: []
}
const mockSingleValidTxid = {
c: [
{
_id: "5d965fc27f1cf2184ca2fe73",
tx: {
h: "77872738b6bddee6c0cbdb9509603de20b15d4f6b26602f629417aec2f5d5e8d"
},
slp: {
valid: true,
invalidReason: null
}
}
],
u: []
}
const mockTwoValidTxid = {
c: [
{
_id: "5d965fc27f1cf2184ca2fe73",
tx: {
h: "77872738b6bddee6c0cbdb9509603de20b15d4f6b26602f629417aec2f5d5e8d"
},
slp: {
valid: true,
invalidReason: null
}
},
{
_id: "5d71e69758380a002c492a90",
tx: {
h: "552112f9e458dc7d1d8b328b0a6685e8af74a64b60b6846e7c86407f27f47e42"
},
slp: {
valid: true,
invalidReason: null
}
}
],
u: []
}
const mockTwoRedundentTxid = {
c: [
{
_id: "5db99c72a391ae2afd604bde",
tx: {
h: "d56a2b446d8149c39ca7e06163fe8097168c3604915f631bc58777d669135a56"
},
slp: {
valid: true,
invalidReason: null
}
}
],
u: []
}
module.exports = {
mockList,
mockSingleToken,
@@ -324,5 +382,8 @@ module.exports = {
mockTransactions,
mockSingleTokenError,
mockSingleAddress,
mockFoobar
mockFoobar,
mockSingleValidTxid,
mockTwoValidTxid,
mockTwoRedundentTxid
}
+114 -2
View File
@@ -13,6 +13,118 @@ const mockAddress = {
isscript: false
}
module.exports = {
mockAddress
const mockBalance = {
page: 1,
totalPages: 1,
itemsOnPage: 1000,
address: "bitcoincash:qzp7gdl52edm24xlpkyqnza9rv33u3mdxyc77j3u6k",
balance: "2546",
totalReceived: "2546",
totalSent: "0",
unconfirmedBalance: "0",
unconfirmedTxs: 0,
txs: 2,
txids: [
"e190d13b88578132608ab912a4d2be3e55aa2792d6042d481ae21d700639de56",
"44e1f48c4093fc61db1a8fa206aa402fc34e482b3f788cb38c123ca0e1a35db6"
]
}
const mockUtxos = [
{
txid: "e190d13b88578132608ab912a4d2be3e55aa2792d6042d481ae21d700639de56",
vout: 0,
value: "2000",
height: 605873,
confirmations: 298,
satoshis: 2000
},
{
txid: "44e1f48c4093fc61db1a8fa206aa402fc34e482b3f788cb38c123ca0e1a35db6",
vout: 1,
value: "546",
height: 605873,
confirmations: 298,
satoshis: 546
}
]
const mockThreeUtxos = [
{
txid: "e190d13b88578132608ab912a4d2be3e55aa2792d6042d481ae21d700639de56",
vout: 0,
value: "2000",
height: 605873,
confirmations: 298,
satoshis: 2000
},
{
txid: "44e1f48c4093fc61db1a8fa206aa402fc34e482b3f788cb38c123ca0e1a35db6",
vout: 1,
value: "546",
height: 605873,
confirmations: 298,
satoshis: 546
},
{
txid: "44e1f48c4093fc61db1a8fa206aa402fc34e482b3f788cb38c123ca0e1a35db6",
vout: 1,
value: "546",
height: 605873,
confirmations: 298,
satoshis: 546
}
]
const mockIsTokenUtxos = [
false,
{
txid: "44e1f48c4093fc61db1a8fa206aa402fc34e482b3f788cb38c123ca0e1a35db6",
vout: 1,
value: "546",
height: 605873,
confirmations: 298,
satoshis: 546,
utxoType: "token",
transactionType: "send",
tokenId: "497291b8a1dfe69c8daea50677a3d31a5ef0e9484d8bebb610dac64bbc202fb7",
tokenTicker: "TOK-CH",
tokenName: "TokyoCash",
tokenDocumentUrl: "",
tokenDocumentHash: "",
decimals: 8,
tokenQty: 2
}
]
const tapUtxo = {
txid: "2e030df12390186baf817fa2760540b886511e04bc520e88f6b4c2124cc2a7d4",
vout: 1,
value: "546",
height: 606564,
confirmations: 3,
satoshis: 546,
utxoType: "token",
transactionType: "send",
tokenId: "dd84ca78db4d617221b58eabc6667af8fe2f7eadbfcc213d35be9f1b419beb8d",
tokenTicker: "TAP",
tokenName: "Thoughts and Prayers",
tokenDocumentUrl: "",
tokenDocumentHash: "",
decimals: 0,
tokenQty: 1
}
const tokensOnly = [mockIsTokenUtxos[1], tapUtxo]
const multipleTokens = [false, mockIsTokenUtxos[1], tapUtxo]
module.exports = {
mockAddress,
mockBalance,
mockUtxos,
mockThreeUtxos,
mockIsTokenUtxos,
tokensOnly,
multipleTokens
}
+3
View File
@@ -27,6 +27,9 @@ describe("#route-ratelimits", () => {
RPC_USERNAME: process.env.RPC_USERNAME,
RPC_PASSWORD: process.env.RPC_PASSWORD
}
if (!process.env.JWT_AUTH_SERVER)
process.env.JWT_AUTH_SERVER = `http://fakeurl.com/`
})
// Setup the mocks before each test.
+16 -16
View File
@@ -134,7 +134,7 @@ describe("#Raw-Transactions", () => {
// 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.mockDecodeRawTransaction })
}
@@ -211,7 +211,7 @@ describe("#Raw-Transactions", () => {
// 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.mockDecodeRawTransaction })
}
@@ -240,7 +240,7 @@ describe("#Raw-Transactions", () => {
// 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.mockDecodeRawTransaction })
}
@@ -309,7 +309,7 @@ describe("#Raw-Transactions", () => {
// 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.mockDecodeScript })
}
@@ -375,7 +375,7 @@ describe("#Raw-Transactions", () => {
// 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.mockDecodeScript })
}
@@ -394,7 +394,7 @@ describe("#Raw-Transactions", () => {
// 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.mockDecodeScript })
}
@@ -453,7 +453,7 @@ describe("#Raw-Transactions", () => {
// Mock the RPC call for unit tests.
if (process.env.TEST === "unit") {
nock(`${process.env.RPC_BASEURL}`)
.post(``)
.post(uri => uri.includes("/"))
.reply(500, {
error: { message: "parameter 1 must be of length 64 (not 6)" }
})
@@ -473,7 +473,7 @@ describe("#Raw-Transactions", () => {
// 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.mockRawTransactionConcise })
}
@@ -492,7 +492,7 @@ describe("#Raw-Transactions", () => {
// 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.mockRawTransactionVerbose })
}
@@ -541,7 +541,7 @@ describe("#Raw-Transactions", () => {
// Mock the RPC call for unit tests.
if (process.env.TEST === "unit") {
nock(`${process.env.RPC_BASEURL}`)
.post(``)
.post(uri => uri.includes("/"))
.reply(500, {
error: { message: "parameter 1 must be of length 64 (not 6)" }
})
@@ -561,7 +561,7 @@ describe("#Raw-Transactions", () => {
// 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.mockRawTransactionConcise })
}
@@ -578,7 +578,7 @@ describe("#Raw-Transactions", () => {
// 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.mockRawTransactionVerbose })
}
@@ -647,7 +647,7 @@ describe("#Raw-Transactions", () => {
// Mock the RPC call for unit tests.
if (process.env.TEST === "unit") {
nock(`${process.env.RPC_BASEURL}`)
.post(``)
.post(uri => uri.includes("/"))
.reply(500, {
error: { message: "TX decode failed" }
})
@@ -671,7 +671,7 @@ describe("#Raw-Transactions", () => {
// 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:
"aef8848396e67532b42008b9d75b5a5a3459a6717740f31f0553b74102b4b118"
@@ -759,7 +759,7 @@ describe("#Raw-Transactions", () => {
// Mock the RPC call for unit tests.
if (process.env.TEST === "unit") {
nock(`${process.env.RPC_BASEURL}`)
.post(``)
.post(uri => uri.includes("/"))
.reply(500, {
error: { message: "TX decode failed" }
})
@@ -781,7 +781,7 @@ describe("#Raw-Transactions", () => {
// 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:
"aef8848396e67532b42008b9d75b5a5a3459a6717740f31f0553b74102b4b118"
+51 -23
View File
@@ -102,7 +102,7 @@ describe("#SLP", () => {
assert.equal(result.status, "slp", "Returns static string")
})
})
/*
describe("list()", () => {
// list route handler
const list = slpRoute.testableComponents.list
@@ -717,7 +717,7 @@ describe("#SLP", () => {
// Mock the RPC call for unit tests.
if (process.env.TEST === "unit") {
nock(`${process.env.SLPDB_URL}`)
.post(``)
.post(uri => uri.includes("/"))
.reply(200, { result: mockData.mockConvert })
}
@@ -806,7 +806,7 @@ describe("#SLP", () => {
])
})
})
*/
describe("validateBulk()", () => {
const validateBulk = slpRoute.testableComponents.validateBulk
@@ -835,9 +835,9 @@ describe("#SLP", () => {
it("should validate array with single element", async () => {
// Mock the RPC call for unit tests.
if (process.env.TEST === "unit") {
sandbox
.stub(slpRoute.testableComponents, "isValidSlpTxid")
.resolves(true)
nock(`${process.env.SLPDB_URL}`)
.get(uri => uri.includes("/"))
.reply(200, mockData.mockSingleValidTxid)
}
req.body.txids = [
@@ -854,14 +854,37 @@ describe("#SLP", () => {
it("should validate array with two elements", async () => {
// Mock the RPC call for unit tests.
if (process.env.TEST === "unit") {
sandbox
.stub(slpRoute.testableComponents, "isValidSlpTxid")
.resolves(true)
nock(`${process.env.SLPDB_URL}`)
.get(uri => uri.includes("/"))
.reply(200, mockData.mockTwoValidTxid)
}
req.body.txids = [
"77872738b6bddee6c0cbdb9509603de20b15d4f6b26602f629417aec2f5d5e8d",
"77872738b6bddee6c0cbdb9509603de20b15d4f6b26602f629417aec2f5d5e8d"
"552112f9e458dc7d1d8b328b0a6685e8af74a64b60b6846e7c86407f27f47e42"
]
const result = await validateBulk(req, res)
// console.log(`result: ${util.inspect(result)}`)
assert.isArray(result)
assert.hasAllKeys(result[0], ["txid", "valid"])
assert.equal(result.length, 2)
})
// Captures a regression bug that went out to production, captured in this
// GitHub Issue: https://github.com/Bitcoin-com/rest.bitcoin.com/issues/518
it("should return two elements if given two elements", async () => {
// Mock the RPC call for unit tests.
if (process.env.TEST === "unit") {
nock(`${process.env.SLPDB_URL}`)
.get(uri => uri.includes("/"))
.reply(200, mockData.mockTwoRedundentTxid)
}
req.body.txids = [
"d56a2b446d8149c39ca7e06163fe8097168c3604915f631bc58777d669135a56",
"d56a2b446d8149c39ca7e06163fe8097168c3604915f631bc58777d669135a56"
]
const result = await validateBulk(req, res)
@@ -964,7 +987,7 @@ describe("#SLP", () => {
})
describe("txDetails()", () => {
let txDetails = slpRoute.testableComponents.txDetails
const txDetails = slpRoute.testableComponents.txDetails
it("should throw 400 if txid is empty", async () => {
const result = await txDetails(req, res)
@@ -999,21 +1022,26 @@ describe("#SLP", () => {
}
})
it("should get tx details with token info", async () => {
if (process.env.TEST === "unit") {
// Mock the slpjs library for unit tests.
pathStub.BitboxNetwork = slpjsMock.BitboxNetwork
txDetails = slpRouteStub.testableComponents.txDetails
}
if (process.env.TEST === "integration") {
it("should get tx details with token info", async () => {
// TODO: add mocking for unit testing. How do I mock reponse form SLPDB
// since it's not an object?
req.params.txid =
"497291b8a1dfe69c8daea50677a3d31a5ef0e9484d8bebb610dac64bbc202fb7"
// if (process.env.TEST === "unit") {
// // Mock the slpjs library for unit tests.
// pathStub.BitboxNetwork = slpjsMock.BitboxNetwork
// txDetails = slpRouteStub.testableComponents.txDetails
// }
const result = await txDetails(req, res)
//console.log(`result: ${JSON.stringify(result, null, 2)}`)
req.params.txid =
"497291b8a1dfe69c8daea50677a3d31a5ef0e9484d8bebb610dac64bbc202fb7"
assert.hasAnyKeys(result, ["tokenIsValid", "tokenInfo"])
})
const result = await txDetails(req, res)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.hasAnyKeys(result, ["tokenIsValid", "tokenInfo"])
})
}
})
describe("txsTokenIdAddressSingle()", () => {
+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"
)
})
}
})
})