-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostIndependentKey.test.ts
More file actions
230 lines (190 loc) · 7.55 KB
/
Copy pathpostIndependentKey.test.ts
File metadata and controls
230 lines (190 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import 'should';
import * as request from 'supertest';
import nock from 'nock';
import { app as advancedWalletManagerApp } from '../../../advancedWalletManagerApp';
import { AppMode, AdvancedWalletManagerConfig, TlsMode, SigningMode } from '../../../shared/types';
import express from 'express';
import * as sinon from 'sinon';
import coinFactory from '../../../shared/coinFactory';
import { BaseCoin } from '@bitgo-beta/sdk-core';
import { CoinFamily } from '@bitgo-beta/statics';
describe('postIndependentKey', () => {
let cfg: AdvancedWalletManagerConfig;
let app: express.Application;
let agent: request.SuperAgentTest;
// test cofig
const keyProviderUrl = 'http://key-provider.invalid';
const coin = 'hteth';
const accessToken = 'test-token';
// sinon stubs
before(() => {
// nock config
nock.disableNetConnect();
nock.enableNetConnect('127.0.0.1');
// app config
cfg = {
appMode: AppMode.ADVANCED_WALLET_MANAGER,
signingMode: SigningMode.LOCAL,
port: 0, // Let OS assign a free port
bind: 'localhost',
timeout: 60000,
httpLoggerFile: '',
keyProviderUrl: keyProviderUrl,
tlsMode: TlsMode.DISABLED,
clientCertAllowSelfSigned: true,
};
// app setup
app = advancedWalletManagerApp(cfg);
agent = request.agent(app);
});
afterEach(() => {
nock.cleanAll();
});
// test cases
it('should post an independent key successfully', async () => {
const mockKeyProviderResponse = {
coin: coin,
pub: 'xpub661MyMwAqRbcGAEfZmG74QD11P4dCKRkuwpsJG87QKVPcMdA1PLe76de1Ted54rZ2gyqLYhmdhBCFMrt7AoVwPZwXa3Na9aUnvndvXbvmwu',
source: 'user',
type: 'independent',
};
const keyProviderNock = nock(keyProviderUrl).post(`/key`).reply(200, mockKeyProviderResponse);
const response = await agent
.post(`/api/${coin}/key/independent`)
.set('Authorization', `Bearer ${accessToken}`)
.send({ source: 'user' });
response.status.should.equal(200);
response.body.should.have.property('pub', mockKeyProviderResponse.pub);
response.body.should.have.property('coin', mockKeyProviderResponse.coin);
response.body.should.have.property('source', mockKeyProviderResponse.source);
keyProviderNock.done();
});
it('should fail to post an independent key if source is not provided', async () => {
const response = await agent
.post(`/api/${coin}/key/independent`)
.set('Authorization', `Bearer ${accessToken}`)
.send({});
response.status.should.equal(400);
});
it('should fail if there is an error in creating the public and private key pairs', async () => {
const coinStub = sinon.stub(coinFactory, 'getCoin').returns(
Promise.resolve({
getFamily: () => CoinFamily.ETH,
keychains: () => ({
create: () => ({}),
}),
} as unknown as BaseCoin),
);
const response = await agent
.post(`/api/${coin}/key/independent`)
.set('Authorization', `Bearer ${accessToken}`)
.send({ source: 'user' });
response.status.should.equal(500);
response.body.should.have.property('details', 'BitGo SDK failed to create public key');
coinStub.restore();
});
});
describe('postIndependentKey — external signing mode', () => {
let app: express.Application;
let agent: request.SuperAgentTest;
let coinStub: sinon.SinonStub;
const keyProviderUrl = 'http://key-provider.invalid';
const coin = 'tbtc';
const accessToken = 'test-token';
const mockGenerateKeyResponse = {
pub: 'xpub661MyMwAq',
coin,
source: 'user',
type: 'independent',
};
const utxoCoinStub = {
getFamily: () => CoinFamily.BTC,
getFullName: () => 'Test Bitcoin',
keychains: () => ({ create: sinon.stub().returns({ pub: 'xpub...', prv: 'xprv...' }) }),
} as unknown as BaseCoin;
const nonUtxoCoinStub = {
getFamily: () => CoinFamily.ETH,
getFullName: () => 'Test Ethereum',
keychains: () => ({ create: sinon.stub().returns({ pub: 'xpub...', prv: 'xprv...' }) }),
} as unknown as BaseCoin;
const unsupportedExternalCoinStub = {
getFamily: () => CoinFamily.XRP,
getFullName: () => 'Test XRP',
keychains: () => ({ create: sinon.stub().returns({ pub: 'xpub...', prv: 'xprv...' }) }),
} as unknown as BaseCoin;
before(() => {
nock.disableNetConnect();
nock.enableNetConnect('127.0.0.1');
app = advancedWalletManagerApp({
appMode: AppMode.ADVANCED_WALLET_MANAGER,
signingMode: SigningMode.EXTERNAL,
port: 0,
bind: 'localhost',
timeout: 60000,
httpLoggerFile: '',
keyProviderUrl,
tlsMode: TlsMode.DISABLED,
clientCertAllowSelfSigned: true,
});
agent = request.agent(app);
});
afterEach(() => {
nock.cleanAll();
coinStub?.restore();
});
it('should call POST /key/generate for UTXO coin and not call POST /key', async () => {
coinStub = sinon.stub(coinFactory, 'getCoin').resolves(utxoCoinStub);
const externalKeyGeneratorNock = nock(keyProviderUrl)
.post('/key/generate', { coin, source: 'user', type: 'independent' })
.reply(200, mockGenerateKeyResponse);
const localKeyGeneratorNock = nock(keyProviderUrl).post('/key').reply(200, {});
const response = await agent
.post(`/api/${coin}/key/independent`)
.set('Authorization', `Bearer ${accessToken}`)
.send({ source: 'user' });
response.status.should.equal(200);
response.body.should.have.property('pub', mockGenerateKeyResponse.pub);
externalKeyGeneratorNock.done();
localKeyGeneratorNock.isDone().should.equal(false);
});
it('should not call coin.keychains().create() in external mode for UTXO coin', async () => {
const createSpy = sinon.spy();
const utxoWithSpy = {
...utxoCoinStub,
keychains: () => ({ create: createSpy }),
} as unknown as BaseCoin;
coinStub = sinon.stub(coinFactory, 'getCoin').resolves(utxoWithSpy);
nock(keyProviderUrl).post('/key/generate').reply(200, mockGenerateKeyResponse);
await agent
.post(`/api/${coin}/key/independent`)
.set('Authorization', `Bearer ${accessToken}`)
.send({ source: 'backup' });
createSpy.called.should.equal(false);
});
it('should call POST /key/generate for ETH coin and not call POST /key', async () => {
coinStub = sinon.stub(coinFactory, 'getCoin').resolves(nonUtxoCoinStub);
const externalKeyGeneratorNock = nock(keyProviderUrl)
.post('/key/generate', { coin: 'hteth', source: 'user', type: 'independent' })
.reply(200, { ...mockGenerateKeyResponse, coin: 'hteth' });
const localKeyGeneratorNock = nock(keyProviderUrl).post('/key').reply(200, {});
const response = await agent
.post(`/api/hteth/key/independent`)
.set('Authorization', `Bearer ${accessToken}`)
.send({ source: 'user' });
response.status.should.equal(200);
response.body.should.have.property('pub', mockGenerateKeyResponse.pub);
externalKeyGeneratorNock.done();
localKeyGeneratorNock.isDone().should.equal(false);
});
it('should fall through to local path for unsupported external coin in external mode', async () => {
coinStub = sinon.stub(coinFactory, 'getCoin').resolves(unsupportedExternalCoinStub);
const externalKeyGeneratorNock = nock(keyProviderUrl).post('/key/generate').reply(200, {});
nock(keyProviderUrl).post('/key').reply(200, mockGenerateKeyResponse);
const response = await agent
.post(`/api/${coin}/key/independent`)
.set('Authorization', `Bearer ${accessToken}`)
.send({ source: 'user' });
response.status.should.equal(200);
externalKeyGeneratorNock.isDone().should.equal(false);
});
});