Skip to content

Commit beec742

Browse files
committed
test(mbe): add mocha test integration
Ticket: WP-4695
1 parent bf8f0d4 commit beec742

File tree

5 files changed

+524
-16
lines changed

5 files changed

+524
-16
lines changed

.mocharc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
require: ['ts-node/register'],
3+
extension: ['ts'],
4+
timeout: 60000,
5+
reporter: 'min',
6+
'reporter-option': ['cdn=true', 'json=false', 'consoleReporter=spec'],
7+
exit: true,
8+
};

package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,34 @@
1818
},
1919
"dependencies": {
2020
"@api-ts/io-ts-http": "^3.2.1",
21-
"@api-ts/response": "^2.1.0",
2221
"@api-ts/openapi-generator": "^5.7.0",
23-
"@api-ts/typed-express-router": "^1.1.13",
22+
"@api-ts/response": "^2.1.0",
23+
"@api-ts/typed-express-router": "^1.1.13",
2424
"@bitgo/sdk-core": "^35.2.0",
2525
"bitgo": "^48.0.0",
2626
"body-parser": "^1.20.3",
2727
"connect-timeout": "^1.9.0",
2828
"debug": "^3.1.0",
29-
"io-ts": "2.1.3",
30-
"winston": "^3.11.0",
3129
"express": "4.17.3",
30+
"io-ts": "2.1.3",
3231
"lodash": "^4.17.20",
3332
"morgan": "^1.9.1",
3433
"proxy-agent": "6.4.0",
3534
"proxyquire": "^2.1.3",
3635
"superagent": "^8.0.9",
36+
"winston": "^3.11.0",
3737
"zod": "^3.25.48"
3838
},
3939
"devDependencies": {
4040
"@api-ts/openapi-generator": "^5.7.0",
41-
"nodemon": "^3.1.10",
4241
"@types/body-parser": "^1.17.0",
4342
"@types/connect-timeout": "^1.9.0",
4443
"@types/debug": "^4.1.12",
4544
"@types/express": "4.17.13",
45+
"@types/jasmine": "^5.1.8",
4646
"@types/jest": "^29.5.12",
4747
"@types/lodash": "^4.14.121",
48+
"@types/mocha": "^10.0.10",
4849
"@types/morgan": "^1.7.35",
4950
"@types/node": "^16.18.46",
5051
"@types/sinon": "^10.0.11",
@@ -55,8 +56,11 @@
5556
"eslint": "^8.0.0",
5657
"eslint-config-prettier": "^8.0.0",
5758
"eslint-plugin-prettier": "^4.0.0",
59+
"jasmine": "^5.8.0",
5860
"jest": "^29.7.0",
61+
"mocha": "^11.6.0",
5962
"nock": "^13.3.1",
63+
"nodemon": "^3.1.10",
6064
"nyc": "^15.0.0",
6165
"prettier": "^2.0.0",
6266
"should": "^13.2.3",
@@ -65,6 +69,7 @@
6569
"sinon": "^13.0.1",
6670
"supertest": "^4.0.2",
6771
"ts-jest": "^29.1.2",
72+
"ts-node": "^10.9.2",
6873
"typescript": "^4.2.4"
6974
},
7075
"engines": {
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import 'should';
2+
3+
import * as request from 'supertest';
4+
import nock from 'nock';
5+
import { app as expressApp } from '../../masterExpressApp';
6+
import { AppMode, MasterExpressConfig, TlsMode } from '../../types';
7+
import { Environments } from '@bitgo/sdk-core';
8+
import { before, after } from 'mocha';
9+
10+
describe('POST /api/:coin/wallet/generate', () => {
11+
let agent: request.SuperAgentTest;
12+
const enclavedExpressUrl = 'http://enclaved.invalid';
13+
const bitgoApiUrl = Environments.test.uri;
14+
const coin = 'tbtc';
15+
const accessToken = 'test-token';
16+
17+
before(() => {
18+
nock.disableNetConnect();
19+
nock.enableNetConnect('127.0.0.1');
20+
21+
const config: MasterExpressConfig = {
22+
appMode: AppMode.MASTER_EXPRESS,
23+
port: 0, // Let OS assign a free port
24+
bind: 'localhost',
25+
timeout: 60000,
26+
logFile: '',
27+
env: 'test',
28+
disableEnvCheck: true,
29+
authVersion: 2,
30+
enclavedExpressUrl: enclavedExpressUrl,
31+
enclavedExpressCert: 'dummy-cert',
32+
tlsMode: TlsMode.DISABLED,
33+
mtlsRequestCert: false,
34+
allowSelfSigned: true,
35+
};
36+
37+
const app = expressApp(config);
38+
agent = request.agent(app);
39+
});
40+
41+
afterEach(() => {
42+
nock.cleanAll();
43+
});
44+
45+
after(() => {
46+
nock.restore();
47+
});
48+
49+
it('should generate a wallet by calling the enclaved express service', async () => {
50+
const userKeychainNock = nock(enclavedExpressUrl)
51+
.post(`/api/${coin}/key/independent`)
52+
.reply(200, {
53+
pub: 'xpub_user',
54+
source: 'user',
55+
type: 'independent',
56+
});
57+
58+
const backupKeychainNock = nock(enclavedExpressUrl)
59+
.post(`/api/${coin}/key/independent`)
60+
.reply(200, {
61+
pub: 'xpub_backup',
62+
source: 'backup',
63+
type: 'independent',
64+
});
65+
66+
const bitgoAddUserKeyNock = nock(bitgoApiUrl)
67+
.post(`/api/v2/${coin}/key`, {
68+
pub: 'xpub_user',
69+
keyType: 'independent',
70+
source: 'user',
71+
})
72+
.matchHeader('any', () => true)
73+
.reply(200, { id: 'user-key-id', pub: 'xpub_user' });
74+
75+
const bitgoAddBackupKeyNock = nock(bitgoApiUrl)
76+
.post(`/api/v2/${coin}/key`, {
77+
pub: 'xpub_backup',
78+
keyType: 'independent',
79+
source: 'backup',
80+
})
81+
.matchHeader('any', () => true)
82+
.reply(200, { id: 'backup-key-id', pub: 'xpub_backup' });
83+
84+
const bitgoAddBitGoKeyNock = nock(bitgoApiUrl)
85+
.post(`/api/v2/${coin}/key`, {
86+
source: 'bitgo',
87+
keyType: 'independent',
88+
enterprise: 'test_enterprise',
89+
})
90+
.reply(200, { id: 'bitgo-key-id', pub: 'xpub_bitgo' });
91+
92+
const bitgoAddWalletNock = nock(bitgoApiUrl)
93+
.post(`/api/v2/${coin}/wallet/add`, {
94+
label: 'test_wallet',
95+
m: 2,
96+
n: 3,
97+
keys: ['user-key-id', 'backup-key-id', 'bitgo-key-id'],
98+
type: 'cold',
99+
subType: 'onPrem',
100+
multisigType: 'onchain',
101+
enterprise: 'test_enterprise',
102+
})
103+
.matchHeader('any', () => true)
104+
.reply(200, {
105+
id: 'new-wallet-id',
106+
multisigType: 'onchain',
107+
type: 'cold',
108+
subType: 'onPrem',
109+
});
110+
111+
const response = await agent
112+
.post(`/api/${coin}/wallet/generate`)
113+
.set('Authorization', `Bearer ${accessToken}`)
114+
.send({
115+
label: 'test_wallet',
116+
enterprise: 'test_enterprise',
117+
});
118+
119+
response.status.should.equal(200);
120+
response.body.should.have.property('wallet');
121+
response.body.wallet.should.have.properties({
122+
id: 'new-wallet-id',
123+
multisigType: 'onchain',
124+
type: 'cold',
125+
subType: 'onPrem',
126+
});
127+
response.body.should.have.propertyByPath('userKeychain', 'pub').eql('xpub_user');
128+
response.body.should.have.propertyByPath('backupKeychain', 'pub').eql('xpub_backup');
129+
response.body.should.have.propertyByPath('bitgoKeychain', 'pub').eql('xpub_bitgo');
130+
131+
userKeychainNock.done();
132+
backupKeychainNock.done();
133+
bitgoAddUserKeyNock.done();
134+
bitgoAddBackupKeyNock.done();
135+
bitgoAddBitGoKeyNock.done();
136+
bitgoAddWalletNock.done();
137+
});
138+
});

src/masterBitgoExpress/generateWallet.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ export async function handleGenerateWalletOnPrem(
3030

3131
const reqId = new RequestTracer();
3232

33-
// Assign the default multiSig type value based on the coin
34-
if (!req.decoded.multisigType) {
35-
req.decoded.multisigType = baseCoin.getDefaultMultisigType();
36-
}
37-
3833
const { label, enterprise } = req.decoded;
3934

4035
// Create wallet parameters with type assertion to allow 'onprem' subtype
@@ -94,6 +89,7 @@ export async function handleGenerateWalletOnPrem(
9489
backupKeychain: backupKeychainPromise(),
9590
bitgoKeychain: baseCoin.keychains().createBitGo({
9691
enterprise: req.decoded.enterprise,
92+
keyType: 'independent',
9793
reqId,
9894
isDistributedCustody: req.decoded.isDistributedCustody,
9995
}),

0 commit comments

Comments
 (0)