Files
bch-api/test/v3/mining.js
T

177 lines
5.2 KiB
JavaScript
Raw Normal View History

2019-06-08 19:13:19 -07:00
/*
TESTS FOR THE MINING.TS LIBRARY
This test file uses the environment variable TEST to switch between unit
and integration tests. By default, TEST is set to 'unit'. Set this variable
to 'integration' to run the tests against BCH mainnet.
*/
'use strict'
2019-06-08 19:13:19 -07:00
const chai = require('chai')
2019-06-08 19:13:19 -07:00
const assert = chai.assert
const miningRoute = require('../../src/routes/v3/full-node/mining')
const nock = require('nock') // HTTP mocking
2019-06-08 19:13:19 -07:00
let originalEnvVars // Used during transition from integration to unit tests.
// Mocking data.
const { mockReq, mockRes } = require('./mocks/express-mocks')
const mockData = require('./mocks/mining-mocks')
2019-06-08 19:13:19 -07:00
// Used for debugging.
const util = require('util')
2019-06-08 19:13:19 -07:00
util.inspect.defaultOptions = { depth: 1 }
describe('#Mining', () => {
2019-06-08 19:13:19 -07:00
let req, res
before(() => {
// Save existing environment variables.
originalEnvVars = {
BITCOINCOM_BASEURL: process.env.BITCOINCOM_BASEURL,
RPC_BASEURL: process.env.RPC_BASEURL,
RPC_USERNAME: process.env.RPC_USERNAME,
RPC_PASSWORD: process.env.RPC_PASSWORD
}
// Set default environment variables for unit tests.
if (!process.env.TEST) process.env.TEST = 'unit'
if (process.env.TEST === 'unit') {
process.env.BITCOINCOM_BASEURL = 'http://fakeurl/api/'
process.env.RPC_BASEURL = 'http://fakeurl/api'
process.env.RPC_USERNAME = 'fakeusername'
process.env.RPC_PASSWORD = 'fakepassword'
2019-06-08 19:13:19 -07:00
}
})
// Setup the mocks before each test.
beforeEach(() => {
// Mock the req and res objects used by Express routes.
req = mockReq
res = mockRes
// Explicitly reset the parmas and body.
req.params = {}
req.body = {}
req.query = {}
// Activate nock if it's inactive.
if (!nock.isActive()) nock.activate()
})
afterEach(() => {
// Clean up HTTP mocks.
nock.cleanAll() // clear interceptor list.
nock.restore()
})
after(() => {
// Restore any pre-existing environment variables.
process.env.BITCOINCOM_BASEURL = originalEnvVars.BITCOINCOM_BASEURL
process.env.RPC_BASEURL = originalEnvVars.RPC_BASEURL
process.env.RPC_USERNAME = originalEnvVars.RPC_USERNAME
process.env.RPC_PASSWORD = originalEnvVars.RPC_PASSWORD
})
describe('#root', async () => {
2019-06-08 19:13:19 -07:00
// root route handler.
const root = miningRoute.testableComponents.root
it('should respond to GET for base route', async () => {
2019-06-08 19:13:19 -07:00
const result = root(req, res)
// console.log(`result: ${util.inspect(result)}`)
2019-06-08 19:13:19 -07:00
assert.equal(result.status, 'mining', 'Returns static string')
2019-06-08 19:13:19 -07:00
})
})
describe('#getMiningInfo', async () => {
2019-06-08 19:13:19 -07:00
const getMiningInfo = miningRoute.testableComponents.getMiningInfo
it('should throw 503 when network issues', async () => {
2019-06-08 19:13:19 -07:00
// Save the existing RPC URL.
const savedUrl2 = process.env.RPC_BASEURL
// Manipulate the URL to cause a 500 network error.
process.env.RPC_BASEURL = 'http://fakeurl/api/'
2019-06-08 19:13:19 -07:00
await getMiningInfo(req, res)
// console.log(`result: ${util.inspect(result)}`)
2019-06-08 19:13:19 -07:00
// Restore the saved URL.
process.env.RPC_BASEURL = savedUrl2
assert.isAbove(
res.statusCode,
499,
'HTTP status code 500 or greater expected.'
2019-06-08 19:13:19 -07:00
)
// assert.include(result.error,"Network error: Could not communicate with full node","Error message expected")
2019-06-08 19:13:19 -07:00
})
it('should GET mining information', async () => {
2019-06-08 19:13:19 -07:00
// Mock the RPC call for unit tests.
if (process.env.TEST === 'unit') {
2019-06-08 19:13:19 -07:00
nock(`${process.env.RPC_BASEURL}`)
.post(uri => uri.includes('/'))
2019-06-08 19:13:19 -07:00
.reply(200, { result: mockData.mockMiningInfo })
}
const result = await getMiningInfo(req, res)
// console.log(`result: ${util.inspect(result)}`)
2019-06-08 19:13:19 -07:00
assert.hasAllKeys(result, [
'blocks',
'currentblocksize',
'currentblocktx',
'difficulty',
'blockprioritypercentage',
'warnings',
'networkhashps',
'pooledtx',
'chain'
2019-06-08 19:13:19 -07:00
])
})
})
describe('#getNetworkHashPS', async () => {
2019-06-08 19:13:19 -07:00
const getNetworkHashPS = miningRoute.testableComponents.getNetworkHashPS
it('should throw 503 when network issues', async () => {
2019-06-08 19:13:19 -07:00
// Save the existing RPC URL.
const savedUrl2 = process.env.RPC_BASEURL
// Manipulate the URL to cause a 500 network error.
process.env.RPC_BASEURL = 'http://fakeurl/api/'
2019-06-08 19:13:19 -07:00
await getNetworkHashPS(req, res)
// console.log(`result: ${util.inspect(result)}`)
2019-06-08 19:13:19 -07:00
// Restore the saved URL.
process.env.RPC_BASEURL = savedUrl2
assert.isAbove(
res.statusCode,
499,
'HTTP status code 500 or greater expected.'
2019-06-08 19:13:19 -07:00
)
// assert.include(result.error,"Network error: Could not communicate with full node","Error message expected")
2019-06-08 19:13:19 -07:00
})
it('should GET Network Hash per second', async () => {
2019-06-08 19:13:19 -07:00
// Mock the RPC call for unit tests.
if (process.env.TEST === 'unit') {
2019-06-08 19:13:19 -07:00
nock(`${process.env.RPC_BASEURL}`)
.post(uri => uri.includes('/'))
2019-06-08 19:13:19 -07:00
.reply(200, { result: 517604755.6648782 })
}
const result = await getNetworkHashPS(req, res)
// console.log(`result: ${util.inspect(result)}`)
2019-06-08 19:13:19 -07:00
assert.isNumber(result)
})
})
})