mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-21 16:52:04 -07:00
fix(electrumx): Fixing unit tests for v5 Electrumx route
This commit is contained in:
@@ -34,6 +34,7 @@ class Electrum {
|
||||
|
||||
this.fulcrumApi = process.env.FULCRUM_API
|
||||
if (!this.fulcrumApi) {
|
||||
// console.warn('FULCRUM_API env var not set. Can not connect to Fulcrum indexer.')
|
||||
throw new Error(
|
||||
'FULCRUM_API env var not set. Can not connect to Fulcrum indexer.'
|
||||
)
|
||||
@@ -451,11 +452,13 @@ class Electrum {
|
||||
const response = await _this.axios.get(
|
||||
`${_this.fulcrumApi}electrumx/tx/data/${txid}`
|
||||
)
|
||||
// console.log(`_transactionDetailsFromElectrum(): ${JSON.stringify(electrumResponse, null, 2)}`)
|
||||
// console.log(`response.data: ${JSON.stringify(response.data, null, 2)}`)
|
||||
|
||||
res.status(200)
|
||||
return res.json(response.data)
|
||||
} catch (err) {
|
||||
console.log('err: ', err)
|
||||
|
||||
// Write out error to error log.
|
||||
wlogger.error('Error in elecrumx.js/getTransactionDetails().', err)
|
||||
|
||||
@@ -1085,6 +1088,9 @@ class Electrum {
|
||||
errorHandler (err, res) {
|
||||
// Attempt to decode the error message.
|
||||
const { msg, status } = _this.routeUtils.decodeError(err)
|
||||
// console.log('errorHandler msg: ', msg)
|
||||
// console.log('errorHandler status: ', status)
|
||||
|
||||
if (msg) {
|
||||
res.status(status)
|
||||
return res.json({ success: false, error: msg })
|
||||
|
||||
+38
-12
@@ -30,25 +30,18 @@ const mockData = require('./mocks/electrumx-mock')
|
||||
const util = require('util')
|
||||
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)
|
||||
// }
|
||||
if (!process.env.FULCRUM_API) process.env.FULCRUM_API = 'http://localhost'
|
||||
|
||||
describe('#Electrumx', () => {
|
||||
let req, res
|
||||
let sandbox
|
||||
const electrumxRoute = new ElecrumxRoute()
|
||||
// let electrumxRoute
|
||||
|
||||
before(async () => {
|
||||
if (!process.env.TEST) process.env.TEST = 'unit'
|
||||
if (!process.env.TEST) {
|
||||
process.env.TEST = 'unit'
|
||||
}
|
||||
console.log(`Testing type is: ${process.env.TEST}`)
|
||||
|
||||
if (!process.env.NETWORK) process.env.NETWORK = 'testnet'
|
||||
@@ -641,9 +634,25 @@ describe('#Electrumx', () => {
|
||||
})
|
||||
|
||||
it('should pass errors from electrum-cash to user', async () => {
|
||||
if (process.env.TEST === 'unit') {
|
||||
sandbox.stub(electrumxRoute.axios, 'get').rejects({
|
||||
response: {
|
||||
data: {
|
||||
error: {
|
||||
message: {
|
||||
success: false,
|
||||
error: 'Invalid tx hash'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
req.params.txid = '02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c'
|
||||
|
||||
const result = await electrumxRoute.getTransactionDetails(req, res)
|
||||
// console.log('result: ', result)
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error.error, 'Invalid tx hash')
|
||||
@@ -823,11 +832,28 @@ describe('#Electrumx', () => {
|
||||
})
|
||||
|
||||
it('should pass errors from electrum-cash to user', async () => {
|
||||
if (process.env.TEST === 'unit') {
|
||||
sandbox.stub(electrumxRoute.axios, 'post').rejects({
|
||||
response: {
|
||||
data: {
|
||||
error: {
|
||||
message: {
|
||||
success: false,
|
||||
error:
|
||||
'the transaction was rejected by network rules.\n\nTX decode failed\n'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
req.body.txHex = mockData.txDetails.hex.substring(10)
|
||||
const result = await electrumxRoute.broadcastTransaction(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
// assert.equal(res.statusCode, 503, 'Expect 503 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error.error, 'the transaction was rejected')
|
||||
|
||||
Reference in New Issue
Block a user