Skip to content

Commit eadac71

Browse files
committed
refactor: create testUtils and nock prebuildTransaction
Ticket: WAL-1489
1 parent 53279bc commit eadac71

5 files changed

Lines changed: 79 additions & 72 deletions

File tree

src/__tests__/api/master/ecdsa.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,10 @@ import {
1212
TxRequest,
1313
Wallet,
1414
} from '@bitgo-beta/sdk-core';
15+
import { BitGoAPI } from '@bitgo-beta/sdk-api';
1516
import { AdvancedWalletManagerClient } from '../../../masterBitgoExpress/clients/advancedWalletManagerClient';
1617
import { signAndSendEcdsaMPCv2FromTxRequest } from '../../../masterBitgoExpress/handlers/ecdsa';
17-
import { BitGoAPI } from '@bitgo-beta/sdk-api';
18-
19-
class BitGoAPITestHarness extends BitGoAPI {
20-
static clearConstantsCache(): void {
21-
BitGoAPI._constants = {};
22-
BitGoAPI._constantsExpire = {};
23-
}
24-
}
18+
import { BitGoAPITestHarness } from './testUtils';
2519

2620
describe('Ecdsa Signing Handler', () => {
2721
let bitgo: BitGoAPI;

src/__tests__/api/master/eddsa.test.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@ import {
1313
import { BitGoAPI } from '@bitgo-beta/sdk-api';
1414
import { AdvancedWalletManagerClient as AdvancedWalletManagerClient } from '../../../masterBitgoExpress/clients/advancedWalletManagerClient';
1515
import { handleEddsaSigning } from '../../../masterBitgoExpress/handlers/eddsa';
16-
17-
class BitGoAPITestHarness extends BitGoAPI {
18-
static clearConstantsCache(): void {
19-
BitGoAPI._constants = {};
20-
BitGoAPI._constantsExpire = {};
21-
}
22-
}
16+
import { BitGoAPITestHarness } from './testUtils';
2317

2418
describe('Eddsa Signing Handler', () => {
2519
let bitgo: BitGoBase;

src/__tests__/api/master/generateWallet.test.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@ import { Environments } from '@bitgo-beta/sdk-core';
1010
import { BitGoAPI } from '@bitgo-beta/sdk-api';
1111
import * as middleware from '../../../shared/middleware';
1212
import { BitGoRequest } from '../../../types/request';
13-
14-
class BitGoAPITestHarness extends BitGoAPI {
15-
static clearConstantsCache(): void {
16-
BitGoAPI._constants = {};
17-
BitGoAPI._constantsExpire = {};
18-
}
19-
}
13+
import { BitGoAPITestHarness } from './testUtils';
2014

2115
function mockWalletResponse(id: string, coinName: string, overrides: Record<string, unknown> = {}) {
2216
return {

src/__tests__/api/master/sendMany.test.ts

Lines changed: 67 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,20 @@ import {
1010
openpgpUtils,
1111
SignatureShareRecord,
1212
SignatureShareType,
13-
Wallet,
1413
} from '@bitgo-beta/sdk-core';
15-
import { BitGoAPI } from '@bitgo-beta/sdk-api';
14+
import * as utxolib from '@bitgo-beta/utxo-lib';
1615
import { Tbtc } from '@bitgo-beta/sdk-coin-btc';
1716
import { Tsol } from '@bitgo-beta/sdk-coin-sol';
1817
import assert from 'assert';
19-
20-
class BitGoAPITestHarness extends BitGoAPI {
21-
static clearConstantsCache(): void {
22-
BitGoAPI._constants = {};
23-
BitGoAPI._constantsExpire = {};
24-
}
25-
}
18+
import { BitGoAPITestHarness } from './testUtils';
2619

2720
const testWalletId = 'test-wallet-id';
2821
const testBitgoApiUrl = Environments.test.uri;
2922
const tssTxRequestId = 'test-tx-request-id';
3023

31-
function mockMultisigPrebuildResponse(walletIdParam: string) {
32-
return {
33-
txHex: 'prebuilt-tx-hex',
34-
txInfo: {
35-
nP2SHInputs: 1,
36-
nSegwitInputs: 0,
37-
nOutputs: 2,
38-
},
39-
walletId: walletIdParam,
40-
};
41-
}
24+
const TBTC_PREBUILD_PSBT_HEX = utxolib.bitgo
25+
.createPsbtForNetwork({ network: utxolib.networks.testnet })
26+
.toHex();
4227

4328
function buildPendingEdDsaTxRequest(walletIdParam: string) {
4429
return {
@@ -404,9 +389,13 @@ describe('POST /api/v1/:coin/advancedwallet/:walletId/sendMany', () => {
404389
.matchHeader('any', () => true)
405390
.reply(200, { id: 'bitgo-key-id', pub: 'xpub_bitgo' });
406391

407-
const prebuildStub = sinon
408-
.stub(Wallet.prototype, 'prebuildTransaction')
409-
.resolves(mockMultisigPrebuildResponse(walletId));
392+
const prebuildBuildNock = nock(bitgoApiUrl)
393+
.post(`/api/v2/${coin}/wallet/${walletId}/tx/build`)
394+
.reply(200, {
395+
txHex: TBTC_PREBUILD_PSBT_HEX,
396+
txInfo: { nP2SHInputs: 1, nSegwitInputs: 0, nOutputs: 2 },
397+
});
398+
nock(bitgoApiUrl).get(`/api/v2/${coin}/public/block/latest`).reply(200, { height: 800000 });
410399

411400
const verifyStub = sinon.stub(Tbtc.prototype, 'verifyTransaction').resolves(true);
412401

@@ -459,7 +448,7 @@ describe('POST /api/v1/:coin/advancedwallet/:walletId/sendMany', () => {
459448
response.body.should.have.property('status', 'signed');
460449

461450
walletGetNock.done();
462-
sinon.assert.calledOnce(prebuildStub);
451+
prebuildBuildNock.done();
463452
sinon.assert.calledOnce(verifyStub);
464453
keychainGetNock.done();
465454
signNock.done();
@@ -493,9 +482,13 @@ describe('POST /api/v1/:coin/advancedwallet/:walletId/sendMany', () => {
493482
.matchHeader('any', () => true)
494483
.reply(200, { id: 'bitgo-key-id', pub: 'xpub_bitgo' });
495484

496-
sinon
497-
.stub(Wallet.prototype, 'prebuildTransaction')
498-
.resolves(mockMultisigPrebuildResponse(walletId));
485+
nock(bitgoApiUrl)
486+
.post(`/api/v2/${coin}/wallet/${walletId}/tx/build`)
487+
.reply(200, {
488+
txHex: TBTC_PREBUILD_PSBT_HEX,
489+
txInfo: { nP2SHInputs: 1, nSegwitInputs: 0, nOutputs: 2 },
490+
});
491+
nock(bitgoApiUrl).get(`/api/v2/${coin}/public/block/latest`).reply(200, { height: 800000 });
499492

500493
sinon.stub(Tbtc.prototype, 'verifyTransaction').resolves(true);
501494

@@ -559,9 +552,13 @@ describe('POST /api/v1/:coin/advancedwallet/:walletId/sendMany', () => {
559552
.matchHeader('any', () => true)
560553
.reply(200, { id: 'bitgo-key-id', pub: 'xpub_bitgo' });
561554

562-
sinon
563-
.stub(Wallet.prototype, 'prebuildTransaction')
564-
.resolves(mockMultisigPrebuildResponse(walletId));
555+
nock(bitgoApiUrl)
556+
.post(`/api/v2/${coin}/wallet/${walletId}/tx/build`)
557+
.reply(200, {
558+
txHex: TBTC_PREBUILD_PSBT_HEX,
559+
txInfo: { nP2SHInputs: 1, nSegwitInputs: 0, nOutputs: 2 },
560+
});
561+
nock(bitgoApiUrl).get(`/api/v2/${coin}/public/block/latest`).reply(200, { height: 800000 });
565562

566563
sinon.stub(Tbtc.prototype, 'verifyTransaction').resolves(true);
567564

@@ -625,9 +622,13 @@ describe('POST /api/v1/:coin/advancedwallet/:walletId/sendMany', () => {
625622
.matchHeader('any', () => true)
626623
.reply(200, { id: 'bitgo-key-id', pub: 'xpub_bitgo' });
627624

628-
const prebuildStub = sinon
629-
.stub(Wallet.prototype, 'prebuildTransaction')
630-
.resolves(mockMultisigPrebuildResponse(walletId));
625+
const prebuildBuildNock = nock(bitgoApiUrl)
626+
.post(`/api/v2/${coin}/wallet/${walletId}/tx/build`)
627+
.reply(200, {
628+
txHex: TBTC_PREBUILD_PSBT_HEX,
629+
txInfo: { nP2SHInputs: 1, nSegwitInputs: 0, nOutputs: 2 },
630+
});
631+
nock(bitgoApiUrl).get(`/api/v2/${coin}/public/block/latest`).reply(200, { height: 800000 });
631632

632633
const verifyStub = sinon.stub(Tbtc.prototype, 'verifyTransaction').resolves(true);
633634

@@ -676,7 +677,7 @@ describe('POST /api/v1/:coin/advancedwallet/:walletId/sendMany', () => {
676677
response.body.should.have.property('status', 'signed');
677678

678679
walletGetNock.done();
679-
sinon.assert.calledOnce(prebuildStub);
680+
prebuildBuildNock.done();
680681
sinon.assert.calledOnce(verifyStub);
681682
keychainGetNock.done();
682683
signNock.done();
@@ -1061,9 +1062,13 @@ describe('POST /api/v1/:coin/advancedwallet/:walletId/sendMany', () => {
10611062
pub: 'xpub_user',
10621063
});
10631064

1064-
const prebuildStub = sinon
1065-
.stub(Wallet.prototype, 'prebuildTransaction')
1066-
.resolves(mockMultisigPrebuildResponse(walletId));
1065+
const prebuildBuildNock = nock(bitgoApiUrl)
1066+
.post(`/api/v2/${coin}/wallet/${walletId}/tx/build`)
1067+
.reply(200, {
1068+
txHex: TBTC_PREBUILD_PSBT_HEX,
1069+
txInfo: { nP2SHInputs: 1, nSegwitInputs: 0, nOutputs: 2 },
1070+
});
1071+
nock(bitgoApiUrl).get(`/api/v2/${coin}/public/block/latest`).reply(200, { height: 800000 });
10671072

10681073
// Mock verifyTransaction to return false
10691074
const verifyStub = sinon.stub(Tbtc.prototype, 'verifyTransaction').resolves(false);
@@ -1086,7 +1091,7 @@ describe('POST /api/v1/:coin/advancedwallet/:walletId/sendMany', () => {
10861091

10871092
walletGetNock.done();
10881093
keychainGetNock.done();
1089-
sinon.assert.calledOnce(prebuildStub);
1094+
prebuildBuildNock.done();
10901095
sinon.assert.calledOnce(verifyStub);
10911096
});
10921097

@@ -1110,9 +1115,13 @@ describe('POST /api/v1/:coin/advancedwallet/:walletId/sendMany', () => {
11101115
pub: 'xpub_user',
11111116
});
11121117

1113-
const prebuildStub = sinon
1114-
.stub(Wallet.prototype, 'prebuildTransaction')
1115-
.resolves(mockMultisigPrebuildResponse(walletId));
1118+
const prebuildBuildNock = nock(bitgoApiUrl)
1119+
.post(`/api/v2/${coin}/wallet/${walletId}/tx/build`)
1120+
.reply(200, {
1121+
txHex: TBTC_PREBUILD_PSBT_HEX,
1122+
txInfo: { nP2SHInputs: 1, nSegwitInputs: 0, nOutputs: 2 },
1123+
});
1124+
nock(bitgoApiUrl).get(`/api/v2/${coin}/public/block/latest`).reply(200, { height: 800000 });
11161125

11171126
// Mock verifyTransaction to throw an error
11181127
const verifyStub = sinon
@@ -1137,7 +1146,7 @@ describe('POST /api/v1/:coin/advancedwallet/:walletId/sendMany', () => {
11371146

11381147
walletGetNock.done();
11391148
keychainGetNock.done();
1140-
sinon.assert.calledOnce(prebuildStub);
1149+
prebuildBuildNock.done();
11411150
sinon.assert.calledOnce(verifyStub);
11421151
});
11431152

@@ -1172,9 +1181,13 @@ describe('POST /api/v1/:coin/advancedwallet/:walletId/sendMany', () => {
11721181
.matchHeader('any', () => true)
11731182
.reply(200, { id: 'bitgo-key-id', pub: 'xpub_bitgo' });
11741183

1175-
const prebuildStub = sinon
1176-
.stub(Wallet.prototype, 'prebuildTransaction')
1177-
.resolves(mockMultisigPrebuildResponse(walletId));
1184+
const prebuildBuildNock = nock(bitgoApiUrl)
1185+
.post(`/api/v2/${coin}/wallet/${walletId}/tx/build`)
1186+
.reply(200, {
1187+
txHex: TBTC_PREBUILD_PSBT_HEX,
1188+
txInfo: { nP2SHInputs: 1, nSegwitInputs: 0, nOutputs: 2 },
1189+
});
1190+
nock(bitgoApiUrl).get(`/api/v2/${coin}/public/block/latest`).reply(200, { height: 800000 });
11781191

11791192
const verifyStub = sinon.stub(Tbtc.prototype, 'verifyTransaction').resolves(true);
11801193

@@ -1207,7 +1220,7 @@ describe('POST /api/v1/:coin/advancedwallet/:walletId/sendMany', () => {
12071220

12081221
walletGetNock.done();
12091222
keychainGetNock.done();
1210-
sinon.assert.calledOnce(prebuildStub);
1223+
prebuildBuildNock.done();
12111224
sinon.assert.calledOnce(verifyStub);
12121225
signNock.done();
12131226
});
@@ -1321,9 +1334,13 @@ describe('POST /api/v1/:coin/advancedwallet/:walletId/sendMany', () => {
13211334
pub: 'xpub_bitgo',
13221335
});
13231336

1324-
const prebuildStub = sinon
1325-
.stub(Wallet.prototype, 'prebuildTransaction')
1326-
.resolves(mockMultisigPrebuildResponse(walletId));
1337+
const prebuildBuildNock = nock(bitgoApiUrl)
1338+
.post(`/api/v2/${coin}/wallet/${walletId}/tx/build`)
1339+
.reply(200, {
1340+
txHex: TBTC_PREBUILD_PSBT_HEX,
1341+
txInfo: { nP2SHInputs: 1, nSegwitInputs: 0, nOutputs: 2 },
1342+
});
1343+
nock(bitgoApiUrl).get(`/api/v2/${coin}/public/block/latest`).reply(200, { height: 800000 });
13271344

13281345
const verifyStub = sinon.stub(Tbtc.prototype, 'verifyTransaction').resolves(true);
13291346

@@ -1361,7 +1378,7 @@ describe('POST /api/v1/:coin/advancedwallet/:walletId/sendMany', () => {
13611378
keychainGetNock.done();
13621379
backupKeychainGetNock.done();
13631380
bitgoKeychainGetNock.done();
1364-
sinon.assert.calledOnce(prebuildStub);
1381+
prebuildBuildNock.done();
13651382
sinon.assert.calledOnce(verifyStub);
13661383
signNock.done();
13671384
submitNock.done();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { BitGoAPI } from '@bitgo-beta/sdk-api';
2+
3+
export class BitGoAPITestHarness extends BitGoAPI {
4+
static clearConstantsCache(): void {
5+
BitGoAPI._constants = {};
6+
BitGoAPI._constantsExpire = {};
7+
}
8+
}

0 commit comments

Comments
 (0)