mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-21 16:52:04 -07:00
fix(v5 electrumx): Added first unit and integration tests for new electrumx route
This commit is contained in:
+126
-151
@@ -32,15 +32,15 @@ util.inspect.defaultOptions = { depth: 1 }
|
||||
|
||||
// A wrapper for asserting that the correct response is returned when an error
|
||||
// is expected.
|
||||
function expectRouteError (res, result, expectedError, code = 400) {
|
||||
assert.equal(res.statusCode, code, `HTTP status code ${code} expected.`)
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, expectedError)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
}
|
||||
// function expectRouteError (res, result, expectedError, code = 400) {
|
||||
// assert.equal(res.statusCode, code, `HTTP status code ${code} expected.`)
|
||||
//
|
||||
// assert.property(result, 'error')
|
||||
// assert.include(result.error, expectedError)
|
||||
//
|
||||
// assert.property(result, 'success')
|
||||
// assert.equal(result.success, false)
|
||||
// }
|
||||
|
||||
describe('#Electrumx', () => {
|
||||
let req, res
|
||||
@@ -54,20 +54,19 @@ describe('#Electrumx', () => {
|
||||
if (!process.env.NETWORK) process.env.NETWORK = 'testnet'
|
||||
|
||||
// Connect to electrumx servers if this is an integration test.
|
||||
if (process.env.TEST === 'integration') {
|
||||
await electrumxRoute.connect()
|
||||
console.log('Connected to ElectrumX server')
|
||||
}
|
||||
// if (process.env.TEST === 'integration') {
|
||||
// await electrumxRoute.connect()
|
||||
// console.log('Connected to ElectrumX server')
|
||||
// }
|
||||
})
|
||||
|
||||
after(async () => {
|
||||
// console.log(`electrumxRoute.electrumx: `, electrumxRoute.electrumx)
|
||||
|
||||
// Disconnect from the electrumx server if this is an integration test.
|
||||
if (process.env.TEST === 'integration') {
|
||||
await electrumxRoute.disconnect()
|
||||
console.log('Disconnected from ElectrumX server')
|
||||
}
|
||||
// if (process.env.TEST === 'integration') {
|
||||
// await electrumxRoute.disconnect()
|
||||
// console.log('Disconnected from ElectrumX server')
|
||||
// }
|
||||
})
|
||||
|
||||
// Setup the mocks before each test.
|
||||
@@ -95,15 +94,15 @@ describe('#Electrumx', () => {
|
||||
})
|
||||
|
||||
// A wrapper for stubbing with the Sinon sandbox.
|
||||
function stubMethodForUnitTests (obj, method, value) {
|
||||
if (process.env.TEST !== 'unit') return false
|
||||
|
||||
electrumxRoute.isReady = true // Force flag.
|
||||
|
||||
sandbox.stub(obj, method).resolves(value)
|
||||
|
||||
return true
|
||||
}
|
||||
// function stubMethodForUnitTests (obj, method, value) {
|
||||
// if (process.env.TEST !== 'unit') return false
|
||||
//
|
||||
// electrumxRoute.isReady = true // Force flag.
|
||||
//
|
||||
// sandbox.stub(obj, method).resolves(value)
|
||||
//
|
||||
// return true
|
||||
// }
|
||||
|
||||
describe('#root', () => {
|
||||
// root route handler.
|
||||
@@ -116,29 +115,106 @@ describe('#Electrumx', () => {
|
||||
})
|
||||
})
|
||||
|
||||
// describe('#addressToScripthash', () => {
|
||||
// it('should accurately return a scripthash for a P2PKH address', () => {
|
||||
// const addr = 'bitcoincash:qpr270a5sxphltdmggtj07v4nskn9gmg9yx4m5h7s4'
|
||||
//
|
||||
// const scripthash = electrumxRoute.addressToScripthash(addr)
|
||||
//
|
||||
// const expectedOutput =
|
||||
// 'bce4d5f2803bd1ed7c1ba00dcb3edffcbba50524af7c879d6bb918d04f138965'
|
||||
//
|
||||
// assert.equal(scripthash, expectedOutput)
|
||||
// })
|
||||
//
|
||||
// it('should accurately return a scripthash for a P2SH address', () => {
|
||||
// const addr = 'bitcoincash:pz0z7u9p96h2p6hfychxdrmwgdlzpk5luc5yks2wxq'
|
||||
//
|
||||
// const scripthash = electrumxRoute.addressToScripthash(addr)
|
||||
//
|
||||
// const expectedOutput =
|
||||
// '8bc2235c8e7d5634d9ec429fc0171f2c58e728d4f1e2fb7e440e313133cfa4f0'
|
||||
//
|
||||
// assert.equal(scripthash, expectedOutput)
|
||||
// })
|
||||
// })
|
||||
describe('#getBalance', () => {
|
||||
it('should throw 400 if address is empty', async () => {
|
||||
const result = await electrumxRoute.getBalance(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 422, 'Expect 422 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Unsupported address format')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should throw 400 on array input', async () => {
|
||||
req.params.address = ['qzs02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c']
|
||||
|
||||
const result = await electrumxRoute.getBalance(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'address can not be an array')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should throw an error for an invalid address', async () => {
|
||||
req.params.address = '02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c'
|
||||
|
||||
const result = await electrumxRoute.getBalance(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 422, 'Expect 422 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Unsupported address format')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should detect a network mismatch', async () => {
|
||||
req.params.address = 'bchtest:qq89kjkeqz9mngp8kl3dpmu43y2wztdjqu500gn4c4'
|
||||
|
||||
const result = await electrumxRoute.getBalance(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Invalid network', 'Proper error message')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should pass errors from electrum-cash to user', async () => {
|
||||
// Address has invalid checksum.
|
||||
req.params.address =
|
||||
'bitcoincash:qr69kyzha07dcecrsvjwsj4s6slnlq4r8c30lxnur2'
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.getBalance(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Unsupported address format')
|
||||
})
|
||||
|
||||
it('should get balance for a single address', async () => {
|
||||
req.params.address =
|
||||
'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
|
||||
|
||||
// Mock unit tests to prevent live network calls.
|
||||
if (process.env.TEST === 'unit') {
|
||||
electrumxRoute.isReady = true // Force flag.
|
||||
|
||||
sandbox
|
||||
.stub(electrumxRoute.axios, 'get')
|
||||
.resolves({ data: mockData.balance })
|
||||
}
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.getBalance(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, true)
|
||||
|
||||
assert.property(result, 'balance')
|
||||
assert.property(result.balance, 'confirmed')
|
||||
assert.property(result.balance, 'unconfirmed')
|
||||
})
|
||||
})
|
||||
|
||||
// describe('#_utxosFromElectrumx', () => {
|
||||
// it('should throw error for invalid address', async () => {
|
||||
@@ -1058,107 +1134,6 @@ describe('#Electrumx', () => {
|
||||
// })
|
||||
// })
|
||||
|
||||
// describe('#getBalance', () => {
|
||||
// it('should throw 400 if address is empty', async () => {
|
||||
// const result = await electrumxRoute.getBalance(req, res)
|
||||
// // console.log(`result: ${util.inspect(result)}`)
|
||||
//
|
||||
// assert.equal(res.statusCode, 422, 'Expect 422 status code')
|
||||
//
|
||||
// assert.property(result, 'error')
|
||||
// assert.include(result.error, 'Unsupported address format')
|
||||
//
|
||||
// assert.property(result, 'success')
|
||||
// assert.equal(result.success, false)
|
||||
// })
|
||||
//
|
||||
// it('should throw 400 on array input', async () => {
|
||||
// req.params.address = ['qzs02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c']
|
||||
//
|
||||
// const result = await electrumxRoute.getBalance(req, res)
|
||||
// // console.log(`result: ${util.inspect(result)}`)
|
||||
//
|
||||
// assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
//
|
||||
// assert.property(result, 'error')
|
||||
// assert.include(result.error, 'address can not be an array')
|
||||
//
|
||||
// assert.property(result, 'success')
|
||||
// assert.equal(result.success, false)
|
||||
// })
|
||||
//
|
||||
// it('should throw an error for an invalid address', async () => {
|
||||
// req.params.address = '02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c'
|
||||
//
|
||||
// const result = await electrumxRoute.getBalance(req, res)
|
||||
// // console.log(`result: ${util.inspect(result)}`)
|
||||
//
|
||||
// assert.equal(res.statusCode, 422, 'Expect 422 status code')
|
||||
//
|
||||
// assert.property(result, 'error')
|
||||
// assert.include(result.error, 'Unsupported address format')
|
||||
//
|
||||
// assert.property(result, 'success')
|
||||
// assert.equal(result.success, false)
|
||||
// })
|
||||
//
|
||||
// it('should detect a network mismatch', async () => {
|
||||
// req.params.address = 'bchtest:qq89kjkeqz9mngp8kl3dpmu43y2wztdjqu500gn4c4'
|
||||
//
|
||||
// const result = await electrumxRoute.getBalance(req, res)
|
||||
// // console.log(`result: ${util.inspect(result)}`)
|
||||
//
|
||||
// assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
//
|
||||
// assert.property(result, 'error')
|
||||
// assert.include(result.error, 'Invalid network', 'Proper error message')
|
||||
//
|
||||
// assert.property(result, 'success')
|
||||
// assert.equal(result.success, false)
|
||||
// })
|
||||
//
|
||||
// it('should pass errors from electrum-cash to user', async () => {
|
||||
// // Address has invalid checksum.
|
||||
// req.params.address =
|
||||
// 'bitcoincash:qr69kyzha07dcecrsvjwsj4s6slnlq4r8c30lxnur2'
|
||||
//
|
||||
// // Call the details API.
|
||||
// const result = await electrumxRoute.getBalance(req, res)
|
||||
// // console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
//
|
||||
// assert.property(result, 'success')
|
||||
// assert.equal(result.success, false)
|
||||
//
|
||||
// assert.property(result, 'error')
|
||||
// assert.include(result.error, 'Unsupported address format')
|
||||
// })
|
||||
//
|
||||
// it('should get balance for a single address', async () => {
|
||||
// req.params.address =
|
||||
// 'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
|
||||
//
|
||||
// // Mock unit tests to prevent live network calls.
|
||||
// if (process.env.TEST === 'unit') {
|
||||
// electrumxRoute.isReady = true // Force flag.
|
||||
//
|
||||
// sandbox
|
||||
// .stub(electrumxRoute, '_balanceFromElectrumx')
|
||||
// .resolves(mockData.balance)
|
||||
// }
|
||||
//
|
||||
// // Call the details API.
|
||||
// const result = await electrumxRoute.getBalance(req, res)
|
||||
// // console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
//
|
||||
// assert.property(result, 'success')
|
||||
// assert.equal(result.success, true)
|
||||
//
|
||||
// assert.property(result, 'balance')
|
||||
// assert.property(result.balance, 'confirmed')
|
||||
// assert.property(result.balance, 'unconfirmed')
|
||||
// })
|
||||
// })
|
||||
|
||||
// describe('#balanceBulk', () => {
|
||||
// it('should throw an error for an empty body', async () => {
|
||||
// req.body = {}
|
||||
|
||||
@@ -20,8 +20,11 @@ const utxos = [
|
||||
]
|
||||
|
||||
const balance = {
|
||||
confirmed: 7000,
|
||||
unconfirmed: 0
|
||||
success: true,
|
||||
balance: {
|
||||
confirmed: 7000,
|
||||
unconfirmed: 0
|
||||
}
|
||||
}
|
||||
|
||||
const txHistory = [
|
||||
@@ -44,7 +47,8 @@ const txDetails = {
|
||||
blocktime: 1578327094,
|
||||
confirmations: 31861,
|
||||
hash: '4db095f34d632a4daf942142c291f1f2abb5ba2e1ccac919d85bdc2f671fb251',
|
||||
hex: '020000000265d13ef402840c8a51f39779afb7ae4d49e4b0a3c24a3d0e7742038f2c679667010000006441dd1dd72770cadede1a7fd0363574846c48468a398ddfa41a9677c74cac8d2652b682743725a3b08c6c2021a629011e11a264d9036e9d5311e35b5f4937ca7b4e4121020797d8fd4d2fa6fd7cdeabe2526bfea2b90525d6e8ad506ec4ee3c53885aa309ffffffff65d13ef402840c8a51f39779afb7ae4d49e4b0a3c24a3d0e7742038f2c679667000000006441347d7f218c11c04487c1ad8baac28928fb10e5054cd4494b94d078cfa04ccf68e064fb188127ff656c0b98e9ce87f036d183925d0d0860605877d61e90375f774121028a53f95eb631b460854fc836b2e5d31cad16364b4dc3d970babfbdcc3f2e4954ffffffff035ac355000000000017a914189ce02e332548f4804bac65cba68202c9dbf822878dfd0800000000001976a914285bb350881b21ac89724c6fb6dc914d096cd53b88acf9ef3100000000001976a91445f1f1c4a9b9419a5088a3e9c24a293d7a150e6488ac00000000',
|
||||
hex:
|
||||
'020000000265d13ef402840c8a51f39779afb7ae4d49e4b0a3c24a3d0e7742038f2c679667010000006441dd1dd72770cadede1a7fd0363574846c48468a398ddfa41a9677c74cac8d2652b682743725a3b08c6c2021a629011e11a264d9036e9d5311e35b5f4937ca7b4e4121020797d8fd4d2fa6fd7cdeabe2526bfea2b90525d6e8ad506ec4ee3c53885aa309ffffffff65d13ef402840c8a51f39779afb7ae4d49e4b0a3c24a3d0e7742038f2c679667000000006441347d7f218c11c04487c1ad8baac28928fb10e5054cd4494b94d078cfa04ccf68e064fb188127ff656c0b98e9ce87f036d183925d0d0860605877d61e90375f774121028a53f95eb631b460854fc836b2e5d31cad16364b4dc3d970babfbdcc3f2e4954ffffffff035ac355000000000017a914189ce02e332548f4804bac65cba68202c9dbf822878dfd0800000000001976a914285bb350881b21ac89724c6fb6dc914d096cd53b88acf9ef3100000000001976a91445f1f1c4a9b9419a5088a3e9c24a293d7a150e6488ac00000000',
|
||||
locktime: 0,
|
||||
size: 392,
|
||||
time: 1578327094,
|
||||
@@ -53,8 +57,10 @@ const txDetails = {
|
||||
vin: [
|
||||
{
|
||||
scriptSig: {
|
||||
asm: 'dd1dd72770cadede1a7fd0363574846c48468a398ddfa41a9677c74cac8d2652b682743725a3b08c6c2021a629011e11a264d9036e9d5311e35b5f4937ca7b4e[ALL|FORKID] 020797d8fd4d2fa6fd7cdeabe2526bfea2b90525d6e8ad506ec4ee3c53885aa309',
|
||||
hex: '41dd1dd72770cadede1a7fd0363574846c48468a398ddfa41a9677c74cac8d2652b682743725a3b08c6c2021a629011e11a264d9036e9d5311e35b5f4937ca7b4e4121020797d8fd4d2fa6fd7cdeabe2526bfea2b90525d6e8ad506ec4ee3c53885aa309'
|
||||
asm:
|
||||
'dd1dd72770cadede1a7fd0363574846c48468a398ddfa41a9677c74cac8d2652b682743725a3b08c6c2021a629011e11a264d9036e9d5311e35b5f4937ca7b4e[ALL|FORKID] 020797d8fd4d2fa6fd7cdeabe2526bfea2b90525d6e8ad506ec4ee3c53885aa309',
|
||||
hex:
|
||||
'41dd1dd72770cadede1a7fd0363574846c48468a398ddfa41a9677c74cac8d2652b682743725a3b08c6c2021a629011e11a264d9036e9d5311e35b5f4937ca7b4e4121020797d8fd4d2fa6fd7cdeabe2526bfea2b90525d6e8ad506ec4ee3c53885aa309'
|
||||
},
|
||||
sequence: 4294967295,
|
||||
txid: '6796672c8f0342770e3d4ac2a3b0e4494daeb7af7997f3518a0c8402f43ed165',
|
||||
@@ -62,8 +68,10 @@ const txDetails = {
|
||||
},
|
||||
{
|
||||
scriptSig: {
|
||||
asm: '347d7f218c11c04487c1ad8baac28928fb10e5054cd4494b94d078cfa04ccf68e064fb188127ff656c0b98e9ce87f036d183925d0d0860605877d61e90375f77[ALL|FORKID] 028a53f95eb631b460854fc836b2e5d31cad16364b4dc3d970babfbdcc3f2e4954',
|
||||
hex: '41347d7f218c11c04487c1ad8baac28928fb10e5054cd4494b94d078cfa04ccf68e064fb188127ff656c0b98e9ce87f036d183925d0d0860605877d61e90375f774121028a53f95eb631b460854fc836b2e5d31cad16364b4dc3d970babfbdcc3f2e4954'
|
||||
asm:
|
||||
'347d7f218c11c04487c1ad8baac28928fb10e5054cd4494b94d078cfa04ccf68e064fb188127ff656c0b98e9ce87f036d183925d0d0860605877d61e90375f77[ALL|FORKID] 028a53f95eb631b460854fc836b2e5d31cad16364b4dc3d970babfbdcc3f2e4954',
|
||||
hex:
|
||||
'41347d7f218c11c04487c1ad8baac28928fb10e5054cd4494b94d078cfa04ccf68e064fb188127ff656c0b98e9ce87f036d183925d0d0860605877d61e90375f774121028a53f95eb631b460854fc836b2e5d31cad16364b4dc3d970babfbdcc3f2e4954'
|
||||
},
|
||||
sequence: 4294967295,
|
||||
txid: '6796672c8f0342770e3d4ac2a3b0e4494daeb7af7997f3518a0c8402f43ed165',
|
||||
@@ -86,7 +94,8 @@ const txDetails = {
|
||||
n: 1,
|
||||
scriptPubKey: {
|
||||
addresses: ['bitcoincash: qq59hv6s3qdjrtyfwfxxldkuj9xsjmx48vrz882knz'],
|
||||
asm: 'OP_DUP OP_HASH160 285bb350881b21ac89724c6fb6dc914d096cd53b OP_EQUALVERIFY OP_CHECKSIG',
|
||||
asm:
|
||||
'OP_DUP OP_HASH160 285bb350881b21ac89724c6fb6dc914d096cd53b OP_EQUALVERIFY OP_CHECKSIG',
|
||||
hex: '76a914285bb350881b21ac89724c6fb6dc914d096cd53b88ac',
|
||||
reqSigs: 1,
|
||||
type: 'pubkeyhash'
|
||||
@@ -97,7 +106,8 @@ const txDetails = {
|
||||
n: 2,
|
||||
scriptPubKey: {
|
||||
addresses: ['bitcoincash: qpzlruwy4xu5rxjs3z37nsj29y7h59gwvsu4ddp0u4'],
|
||||
asm: 'OP_DUP OP_HASH160 45f1f1c4a9b9419a5088a3e9c24a293d7a150e64 OP_EQUALVERIFY OP_CHECKSIG',
|
||||
asm:
|
||||
'OP_DUP OP_HASH160 45f1f1c4a9b9419a5088a3e9c24a293d7a150e64 OP_EQUALVERIFY OP_CHECKSIG',
|
||||
hex: '76a91445f1f1c4a9b9419a5088a3e9c24a293d7a150e6488ac',
|
||||
reqSigs: 1,
|
||||
type: 'pubkeyhash'
|
||||
|
||||
Reference in New Issue
Block a user