Skip to content

Commit 799a3e6

Browse files
committed
Refactored rawtransaction unit tests
1 parent d98dbe1 commit 799a3e6

11 files changed

Lines changed: 50 additions & 505 deletions

lib/RawTransactions.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export class RawTransactions {
2222

2323
// Array of hexes
2424
} else if (Array.isArray(hex)) {
25-
2625
// Dev note: must use axios.post for unit test stubbing.
2726
const response: AxiosResponse = await axios.post(
2827
`${this.restURL}rawtransactions/decodeRawTransaction`,
@@ -52,14 +51,13 @@ export class RawTransactions {
5251

5352
return response.data
5453
} else if (Array.isArray(script)) {
55-
const options: AxiosRequestConfig = {
56-
method: "POST",
57-
url: `${this.restURL}rawtransactions/decodeScript`,
58-
data: {
54+
// Dev note: must use axios.post for unit test stubbing.
55+
const response: AxiosResponse = await axios.post(
56+
`${this.restURL}rawtransactions/decodeScript`,
57+
{
5958
hexes: script
6059
}
61-
}
62-
const response: AxiosResponse = await axios(options)
60+
)
6361

6462
return response.data
6563
}
@@ -90,7 +88,6 @@ export class RawTransactions {
9088

9189
return response.data
9290
} else if (Array.isArray(txid)) {
93-
9491
// Dev note: must use axios.post for unit test stubbing.
9592
const response: AxiosResponse = await axios.post(
9693
`${this.restURL}rawtransactions/getRawTransaction`,
@@ -133,14 +130,14 @@ export class RawTransactions {
133130

134131
// Array input
135132
} else if (Array.isArray(hex)) {
136-
const options: AxiosRequestConfig = {
137-
method: "POST",
138-
url: `${this.restURL}rawtransactions/sendRawTransaction`,
139-
data: {
133+
134+
// Dev note: must use axios.post for unit test stubbing.
135+
const response: AxiosResponse = await axios.post(
136+
`${this.restURL}rawtransactions/sendRawTransaction`,
137+
{
140138
hexes: hex
141139
}
142-
}
143-
const response: AxiosResponse = await axios(options)
140+
)
144141

145142
return response.data
146143
}

test/unit/Address.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const assert: Chai.AssertStatic = chai.assert
1818
const fixtures = require("./fixtures/Address.json")
1919
const Bitcoin = require("bitcoincashjs-lib")
2020
const sinon = require("sinon")
21-
const addressMock = require("./fixtures/address-mock.js")
21+
const addressMock = require("./mocks/address-mock.js")
2222

2323
function flatten(arrays: any) {
2424
return [].concat.apply([], arrays)

test/unit/Block.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { BlockDetailsResult } from "bitcoin-com-rest"
1111
// consts
1212
const bitbox: BITBOX = new BITBOX()
1313
const assert: Chai.AssertStatic = chai.assert
14-
const blockMock: any = require("./fixtures/block-mock")
14+
const blockMock: any = require("./mocks/block-mock")
1515

1616
util.inspect.defaultOptions = {
1717
showHidden: true,

test/unit/Blockchain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { BlockHeaderResult } from "bitcoin-com-rest"
1111
// consts
1212
const bitbox: BITBOX = new BITBOX()
1313
const assert: Chai.AssertStatic = chai.assert
14-
const blockchainMock = require("./fixtures/blockchain-mock")
14+
const blockchainMock = require("./mocks/blockchain-mock")
1515

1616
util.inspect.defaultOptions = {
1717
showHidden: true,

test/unit/CashAccounts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const assert: Chai.AssertStatic = chai.assert
1616

1717
// TODO: port from require to import syntax
1818
const sinon = require("sinon")
19-
const cashAccountsMock = require("./fixtures/cashaccounts-mock.js")
19+
const cashAccountsMock = require("./mocks/cashaccounts-mock.js")
2020

2121
util.inspect.defaultOptions = {
2222
showHidden: true,

test/unit/RawTransactions.ts

Lines changed: 35 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import * as sinon from "sinon"
1313
// consts
1414
const bitbox: BITBOX = new BITBOX()
1515
const assert: Chai.AssertStatic = chai.assert
16-
const rtMock = require("./fixtures/rawtransactions-mock")
16+
const rtMock = require("./mocks/rawtransactions-mock")
1717

1818
// Used for debugging
1919
util.inspect.defaultOptions = { depth: 1 }
@@ -277,138 +277,103 @@ describe("#RawTransactions", (): void => {
277277
)
278278
}
279279
})
280-
281-
it(`should throw error on array size rate limit`, async (): Promise<
282-
any
283-
> => {
284-
try {
285-
const dataMock: string =
286-
"23213453b4642a73b4fc30d3112d72549ca153a8707255b14373b59e43558de1"
287-
const data: string[] = []
288-
for (let i: number = 0; i < 25; i++) data.push(dataMock)
289-
290-
const result:
291-
| VerboseRawTransactionResult
292-
| VerboseRawTransactionResult[]
293-
| string
294-
| string[] = await bitbox.RawTransactions.getRawTransaction(data)
295-
296-
console.log(`result: ${util.inspect(result)}`)
297-
assert.equal(true, false, "Unexpected result!")
298-
} catch (err) {
299-
assert.hasAnyKeys(err, ["error"])
300-
assert.include(err.error, "Array too large")
301-
}
302-
})
303280
})
304281

305282
describe("#decodeScript", () => {
306283
it("should decode script for a single hex", async (): Promise<any> => {
284+
// Mock the call to rest to prevent live network calls.
285+
const resolved = new Promise(r => r({ data: rtMock.decodedScript }))
286+
sandbox.stub(axios, "get").returns(resolved)
287+
307288
const hex: string =
308289
"4830450221009a51e00ec3524a7389592bc27bea4af5104a59510f5f0cfafa64bbd5c164ca2e02206c2a8bbb47eabdeed52f17d7df668d521600286406930426e3a9415fe10ed592012102e6e1423f7abde8b70bca3e78a7d030e5efabd3eb35c19302542b5fe7879c1a16"
309290

310291
const result = await bitbox.RawTransactions.decodeScript(hex)
292+
//console.log(`result: ${JSON.stringify(result,null,2)}`)
311293

312294
assert.hasAllKeys(result, ["asm", "type", "p2sh"])
313295
})
314296

315-
// CT 2/20/19 - Waiting for this PR to be merged complete the test:
316-
// https://github.com/Bitcoin-com/rest.bitcoin.com/pull/312
317-
/*
318297
it("should decode an array of tx hexes", async () => {
298+
// Mock the call to rest to prevent live network calls.
299+
const testData = [rtMock.decodedScript, rtMock.decodedScript]
300+
const resolved = new Promise(r => r({ data: testData }))
301+
sandbox.stub(axios, "post").returns(resolved)
302+
319303
const hexes = [
320304
"4830450221009a51e00ec3524a7389592bc27bea4af5104a59510f5f0cfafa64bbd5c164ca2e02206c2a8bbb47eabdeed52f17d7df668d521600286406930426e3a9415fe10ed592012102e6e1423f7abde8b70bca3e78a7d030e5efabd3eb35c19302542b5fe7879c1a16",
321305
"4830450221009a51e00ec3524a7389592bc27bea4af5104a59510f5f0cfafa64bbd5c164ca2e02206c2a8bbb47eabdeed52f17d7df668d521600286406930426e3a9415fe10ed592012102e6e1423f7abde8b70bca3e78a7d030e5efabd3eb35c19302542b5fe7879c1a16"
322306
]
323307

324308
const result = await bitbox.RawTransactions.decodeScript(hexes)
325-
console.log(`result ${JSON.stringify(result, null, 2)}`)
309+
//console.log(`result ${JSON.stringify(result, null, 2)}`)
310+
311+
assert.isArray(result)
312+
assert.hasAnyKeys(result[0], ["asm", "type", "p2sh"])
326313
})
327-
*/
328-
/*
329-
it(`should throw an error for improper single input`, async () => {
314+
315+
it(`should pass error from server to user`, async (): Promise<any> => {
330316
try {
331-
const addr = 12345
317+
// Mock out data for unit test, to prevent live network call.
318+
sandbox
319+
.stub(axios, "get")
320+
.throws("error", "Input must be a string or array of strings")
332321

333-
await bitbox.RawTransactions.decodeRawTransaction(addr)
322+
const hexes: any = 12345
323+
324+
await bitbox.RawTransactions.decodeScript(hexes)
334325
assert.equal(true, false, "Unexpected result!")
335326
} catch (err) {
336-
//console.log(`err: `, err)
327+
//console.log(`err: ${util.inspect(err)}`)
337328
assert.include(
338329
err.message,
339-
`Input must be a string or array of strings.`
330+
`Input must be a string or array of strings`
340331
)
341332
}
342333
})
343-
*/
344334
})
345335

346336
/*
347-
Testing sentRawTransaction isn't really possible with an integration test,
337+
Testing sendRawTransaction isn't really possible with an integration test,
348338
as the endpoint really needs an e2e test to be properly tested. The tests
349339
below expect error messages returned from the server, but at least test
350340
that the server is responding on those endpoints, and responds consistently.
351341
*/
352342
describe("sendRawTransaction", () => {
353343
it("should send a single transaction hex", async (): Promise<any> => {
354344
try {
345+
// Mock out data for unit test, to prevent live network call.
346+
sandbox.stub(axios, "get").throws({ error: "Missing inputs" })
347+
355348
const hex: string =
356349
"01000000013ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000006a4730440220540986d1c58d6e76f8f05501c520c38ce55393d0ed7ed3c3a82c69af04221232022058ea43ed6c05fec0eccce749a63332ed4525460105346f11108b9c26df93cd72012103083dfc5a0254613941ddc91af39ff90cd711cdcde03a87b144b883b524660c39ffffffff01807c814a000000001976a914d7e7c4e0b70eaa67ceff9d2823d1bbb9f6df9a5188ac00000000"
357350

358351
await bitbox.RawTransactions.sendRawTransaction(hex)
359352

360353
assert.equal(true, false, "Unexpected result!")
361354
} catch (err) {
355+
//console.log(`err: ${util.inspect(err)}`)
362356
assert.hasAllKeys(err, ["error"])
363357
assert.include(err.error, "Missing inputs")
364358
}
365359
})
366360

367361
it("should send an array of tx hexes", async (): Promise<any> => {
368362
try {
363+
// Mock out data for unit test, to prevent live network call.
364+
sandbox.stub(axios, "post").throws({ error: "Missing inputs" })
365+
369366
const hexes: string[] = [
370367
"01000000013ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000006a4730440220540986d1c58d6e76f8f05501c520c38ce55393d0ed7ed3c3a82c69af04221232022058ea43ed6c05fec0eccce749a63332ed4525460105346f11108b9c26df93cd72012103083dfc5a0254613941ddc91af39ff90cd711cdcde03a87b144b883b524660c39ffffffff01807c814a000000001976a914d7e7c4e0b70eaa67ceff9d2823d1bbb9f6df9a5188ac00000000",
371368
"01000000013ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000006a4730440220540986d1c58d6e76f8f05501c520c38ce55393d0ed7ed3c3a82c69af04221232022058ea43ed6c05fec0eccce749a63332ed4525460105346f11108b9c26df93cd72012103083dfc5a0254613941ddc91af39ff90cd711cdcde03a87b144b883b524660c39ffffffff01807c814a000000001976a914d7e7c4e0b70eaa67ceff9d2823d1bbb9f6df9a5188ac00000000"
372369
]
373370

374371
const result = await bitbox.RawTransactions.sendRawTransaction(hexes)
375372
} catch (err) {
373+
// console.log(`err: ${util.inspect(err)}`)
376374
assert.hasAllKeys(err, ["error"])
377375
assert.include(err.error, "Missing inputs")
378376
}
379377
})
380-
381-
it(`should throw an error for improper single input`, async (): Promise<
382-
any
383-
> => {
384-
try {
385-
const addr: any = 12345
386-
387-
await bitbox.RawTransactions.sendRawTransaction(addr)
388-
assert.equal(true, false, "Unexpected result!")
389-
} catch (err) {
390-
assert.include(
391-
err.message,
392-
`Input hex must be a string or array of strings`
393-
)
394-
}
395-
})
396-
397-
it(`should throw error on array size rate limit`, async (): Promise<
398-
any
399-
> => {
400-
try {
401-
const dataMock: string =
402-
"01000000013ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000006a4730440220540986d1c58d6e76f8f05501c520c38ce55393d0ed7ed3c3a82c69af04221232022058ea43ed6c05fec0eccce749a63332ed4525460105346f11108b9c26df93cd72012103083dfc5a0254613941ddc91af39ff90cd711cdcde03a87b144b883b524660c39ffffffff01807c814a000000001976a914d7e7c4e0b70eaa67ceff9d2823d1bbb9f6df9a5188ac00000000"
403-
const data: string[] = []
404-
for (let i: number = 0; i < 25; i++) data.push(dataMock)
405-
406-
await bitbox.RawTransactions.sendRawTransaction(data)
407-
assert.equal(true, false, "Unexpected result!")
408-
} catch (err) {
409-
assert.hasAnyKeys(err, ["error"])
410-
assert.include(err.error, "Array too large")
411-
}
412-
})
413378
})
414379
})

0 commit comments

Comments
 (0)