mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-21 16:52:04 -07:00
Fixed unit tests
This commit is contained in:
@@ -400,6 +400,8 @@ async function lookupToken(tokenId) {
|
|||||||
const b64 = Buffer.from(s).toString("base64")
|
const b64 = Buffer.from(s).toString("base64")
|
||||||
const url = `${process.env.SLPDB_URL}q/${b64}`
|
const url = `${process.env.SLPDB_URL}q/${b64}`
|
||||||
|
|
||||||
|
// console.log(`url: ${url}`)
|
||||||
|
|
||||||
const tokenRes = await axios.get(url)
|
const tokenRes = await axios.get(url)
|
||||||
//console.log(`tokenRes.data: ${util.inspect(tokenRes.data,null,2)}`)
|
//console.log(`tokenRes.data: ${util.inspect(tokenRes.data,null,2)}`)
|
||||||
//console.log(
|
//console.log(
|
||||||
|
|||||||
+15
-11
@@ -12,10 +12,20 @@
|
|||||||
|
|
||||||
const chai = require("chai")
|
const chai = require("chai")
|
||||||
const assert = chai.assert
|
const assert = chai.assert
|
||||||
const blockbookRoute = require("../../src/routes/v3/blockbook")
|
|
||||||
const nock = require("nock") // HTTP mocking
|
const nock = require("nock") // HTTP mocking
|
||||||
|
|
||||||
let originalUrl // Used during transition from integration to unit tests.
|
let originalUrl, mockServerUrl // Used during transition from integration to unit tests.
|
||||||
|
originalUrl = process.env.BLOCKBOOK_URL
|
||||||
|
|
||||||
|
// Set default environment variables for unit tests.
|
||||||
|
if (!process.env.TEST) process.env.TEST = "unit"
|
||||||
|
if (process.env.TEST === "unit") {
|
||||||
|
process.env.BLOCKBOOK_URL = "http://fakeurl/api/"
|
||||||
|
mockServerUrl = `http://fakeurl`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only load blockbook library after setting BLOCKBOOK_URL env var.
|
||||||
|
const blockbookRoute = require("../../src/routes/v3/blockbook")
|
||||||
|
|
||||||
// Mocking data.
|
// Mocking data.
|
||||||
const { mockReq, mockRes } = require("./mocks/express-mocks")
|
const { mockReq, mockRes } = require("./mocks/express-mocks")
|
||||||
@@ -26,17 +36,9 @@ const util = require("util")
|
|||||||
util.inspect.defaultOptions = { depth: 1 }
|
util.inspect.defaultOptions = { depth: 1 }
|
||||||
|
|
||||||
describe("#Blockbook Router", () => {
|
describe("#Blockbook Router", () => {
|
||||||
let req, res, mockServerUrl
|
let req, res
|
||||||
|
|
||||||
before(() => {
|
before(() => {
|
||||||
originalUrl = process.env.BLOCKBOOK_URL
|
|
||||||
|
|
||||||
// Set default environment variables for unit tests.
|
|
||||||
if (!process.env.TEST) process.env.TEST = "unit"
|
|
||||||
if (process.env.TEST === "unit") {
|
|
||||||
process.env.BLOCKBOOK_URL = "http://fakeurl/api/"
|
|
||||||
mockServerUrl = `http://fakeurl`
|
|
||||||
}
|
|
||||||
// console.log(`Testing type is: ${process.env.TEST}`)
|
// console.log(`Testing type is: ${process.env.TEST}`)
|
||||||
|
|
||||||
if (!process.env.NETWORK) process.env.NETWORK = "testnet"
|
if (!process.env.NETWORK) process.env.NETWORK = "testnet"
|
||||||
@@ -150,6 +152,8 @@ describe("#Blockbook Router", () => {
|
|||||||
it("should get balance for a single address", async () => {
|
it("should get balance for a single address", async () => {
|
||||||
req.params.address = `bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf`
|
req.params.address = `bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf`
|
||||||
|
|
||||||
|
// console.log(`process.env.BLOCKBOOK_URL: ${process.env.BLOCKBOOK_URL}`)
|
||||||
|
|
||||||
// Mock the Insight URL for unit tests.
|
// Mock the Insight URL for unit tests.
|
||||||
if (process.env.TEST === "unit") {
|
if (process.env.TEST === "unit") {
|
||||||
nock(`${process.env.BLOCKBOOK_URL}`)
|
nock(`${process.env.BLOCKBOOK_URL}`)
|
||||||
|
|||||||
@@ -267,7 +267,7 @@ describe("#BlockchainRouter", () => {
|
|||||||
.post(``)
|
.post(``)
|
||||||
.reply(200, {
|
.reply(200, {
|
||||||
result:
|
result:
|
||||||
"0000ff7f7d217c9b7845ea8b50d620c59a1bf7c276566406e9b7bc7e463e0000000000006d70322c0b697c1c81d2744f87f09f1e9780ba5d30338952e2cdc64e60456f8423bb0a5ceafa091a3e843526"
|
"000000202ed2723e7590e2b937f6821a99d6764cb8799bf30f8e300000000000000000001d311c02df9a1e3f57b8dbdcf97ec8dbc3109a26779724c63e560b29ad9ea501e2af955d286403183049e39c"
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ const mockBalance = {
|
|||||||
page: 1,
|
page: 1,
|
||||||
totalPages: 1,
|
totalPages: 1,
|
||||||
itemsOnPage: 1000,
|
itemsOnPage: 1000,
|
||||||
address: "bchtest:qq89kjkeqz9mngp8kl3dpmu43y2wztdjqu500gn4c4",
|
address: "bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf",
|
||||||
balance: "10000000",
|
balance: "10000000",
|
||||||
totalReceived: "10000000",
|
totalReceived: "10000000",
|
||||||
totalSent: "0",
|
totalSent: "0",
|
||||||
|
|||||||
@@ -218,7 +218,7 @@ const mockRawMempool = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
const mockBlockHeaderConcise =
|
const mockBlockHeaderConcise =
|
||||||
"0000ff7f7d217c9b7845ea8b50d620c59a1bf7c276566406e9b7bc7e463e0000000000006d70322c0b697c1c81d2744f87f09f1e9780ba5d30338952e2cdc64e60456f8423bb0a5ceafa091a3e843526"
|
"000000202ed2723e7590e2b937f6821a99d6764cb8799bf30f8e300000000000000000001d311c02df9a1e3f57b8dbdcf97ec8dbc3109a26779724c63e560b29ad9ea501e2af955d286403183049e39c"
|
||||||
|
|
||||||
const mockBlockHeader = {
|
const mockBlockHeader = {
|
||||||
hash: "00000000000008c3679777df34f1a09565f98b2400a05b7c8da72525fdca3900",
|
hash: "00000000000008c3679777df34f1a09565f98b2400a05b7c8da72525fdca3900",
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ const mockSingleToken = {
|
|||||||
tokenDetails: {
|
tokenDetails: {
|
||||||
decimals: 0,
|
decimals: 0,
|
||||||
tokenIdHex:
|
tokenIdHex:
|
||||||
"af8e10dd87e7092e5f0f3b9cf62e85e91f74395fbf22cd14f12bcdfbf1e8354f",
|
"38e97c5d7d3585a2cbf3f9580c82ca33985f9cb0845d4dcce220cb709f9538b0",
|
||||||
timestamp: "2019-06-14 06:31:37",
|
timestamp: "2019-06-14 06:31:37",
|
||||||
timestamp_unix: 1560493897,
|
timestamp_unix: 1560493897,
|
||||||
transactionType: "GENESIS",
|
transactionType: "GENESIS",
|
||||||
|
|||||||
+26
-22
@@ -16,14 +16,33 @@ const assert = chai.assert
|
|||||||
const nock = require("nock") // HTTP mocking
|
const nock = require("nock") // HTTP mocking
|
||||||
const sinon = require("sinon")
|
const sinon = require("sinon")
|
||||||
const proxyquire = require("proxyquire").noPreserveCache()
|
const proxyquire = require("proxyquire").noPreserveCache()
|
||||||
|
const axios = require("axios")
|
||||||
|
|
||||||
|
// Save existing environment variables.
|
||||||
|
// Used during transition from integration to unit tests.
|
||||||
|
let originalEnvVars, mockServerUrl
|
||||||
|
originalEnvVars = {
|
||||||
|
BITDB_URL: process.env.BITDB_URL,
|
||||||
|
BITCOINCOM_BASEURL: process.env.BITCOINCOM_BASEURL,
|
||||||
|
SLPDB_URL: process.env.SLPDB_URL
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set default environment variables for unit tests.
|
||||||
|
if (!process.env.TEST) process.env.TEST = "unit"
|
||||||
|
|
||||||
|
// Block network connections for unit tests.
|
||||||
|
if (process.env.TEST === "unit") {
|
||||||
|
process.env.BITDB_URL = "http://fakeurl/"
|
||||||
|
process.env.BITCOINCOM_BASEURL = "http://fakeurl/"
|
||||||
|
process.env.SLPDB_URL = "http://fakeurl/"
|
||||||
|
mockServerUrl = `http://fakeurl`
|
||||||
|
}
|
||||||
|
|
||||||
// Prepare the slpRoute for stubbing dependcies on slpjs.
|
// Prepare the slpRoute for stubbing dependcies on slpjs.
|
||||||
const slpRoute = require("../../src/routes/v3/slp")
|
const slpRoute = require("../../src/routes/v3/slp")
|
||||||
const pathStub = {} // Used to stub methods within slpjs.
|
const pathStub = {} // Used to stub methods within slpjs.
|
||||||
const slpRouteStub = proxyquire("../../src/routes/v3/slp", { slpjs: pathStub })
|
const slpRouteStub = proxyquire("../../src/routes/v3/slp", { slpjs: pathStub })
|
||||||
|
|
||||||
let originalEnvVars // Used during transition from integration to unit tests.
|
|
||||||
|
|
||||||
// Mocking data.
|
// Mocking data.
|
||||||
const { mockReq, mockRes } = require("./mocks/express-mocks")
|
const { mockReq, mockRes } = require("./mocks/express-mocks")
|
||||||
const mockData = require("./mocks/slp-mocks")
|
const mockData = require("./mocks/slp-mocks")
|
||||||
@@ -34,28 +53,10 @@ const util = require("util")
|
|||||||
util.inspect.defaultOptions = { depth: 1 }
|
util.inspect.defaultOptions = { depth: 1 }
|
||||||
|
|
||||||
describe("#SLP", () => {
|
describe("#SLP", () => {
|
||||||
let req, res, mockServerUrl
|
let req, res
|
||||||
let sandbox
|
let sandbox
|
||||||
|
|
||||||
before(() => {
|
before(() => {})
|
||||||
// Save existing environment variables.
|
|
||||||
originalEnvVars = {
|
|
||||||
BITDB_URL: process.env.BITDB_URL,
|
|
||||||
BITCOINCOM_BASEURL: process.env.BITCOINCOM_BASEURL,
|
|
||||||
SLPDB_URL: process.env.SLPDB_URL
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set default environment variables for unit tests.
|
|
||||||
if (!process.env.TEST) process.env.TEST = "unit"
|
|
||||||
|
|
||||||
// Block network connections for unit tests.
|
|
||||||
if (process.env.TEST === "unit") {
|
|
||||||
process.env.BITDB_URL = "http://fakeurl/"
|
|
||||||
process.env.BITCOINCOM_BASEURL = "http://fakeurl/"
|
|
||||||
process.env.SLPDB_URL = "http://fakeurl/"
|
|
||||||
mockServerUrl = `http://fakeurl`
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// Setup the mocks before each test.
|
// Setup the mocks before each test.
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -215,11 +216,14 @@ describe("#SLP", () => {
|
|||||||
const tokenIdToTest =
|
const tokenIdToTest =
|
||||||
"38e97c5d7d3585a2cbf3f9580c82ca33985f9cb0845d4dcce220cb709f9538b0"
|
"38e97c5d7d3585a2cbf3f9580c82ca33985f9cb0845d4dcce220cb709f9538b0"
|
||||||
|
|
||||||
|
// console.log(`mockServerUrl: ${mockServerUrl}`)
|
||||||
|
|
||||||
// Mock the RPC call for unit tests.
|
// Mock the RPC call for unit tests.
|
||||||
if (process.env.TEST === "unit") {
|
if (process.env.TEST === "unit") {
|
||||||
nock(mockServerUrl)
|
nock(mockServerUrl)
|
||||||
.get(uri => uri.includes("/"))
|
.get(uri => uri.includes("/"))
|
||||||
.reply(200, mockData.mockSingleToken)
|
.reply(200, mockData.mockSingleToken)
|
||||||
|
// sandbox.stub(axios, "get").resolves(mockData.mockSingleToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.params.tokenId = tokenIdToTest
|
req.params.tokenId = tokenIdToTest
|
||||||
|
|||||||
Reference in New Issue
Block a user