-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyProviderClient.test.ts
More file actions
239 lines (202 loc) · 7.92 KB
/
keyProviderClient.test.ts
File metadata and controls
239 lines (202 loc) · 7.92 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
231
232
233
234
235
236
237
238
239
import { AppMode, AdvancedWalletManagerConfig, TlsMode, SigningMode } from '../../../initConfig';
import { app as expressApp } from '../../../advancedWalletManagerApp';
import { KeyProviderClient } from '../../../advancedWalletManager/keyProviderClient/keyProviderClient';
import express from 'express';
import nock from 'nock';
import 'should';
import * as request from 'supertest';
describe('postMpcV2Key', () => {
let cfg: AdvancedWalletManagerConfig;
let app: express.Application;
let agent: request.SuperAgentTest;
// test config
const keyProviderUrl = 'http://key-provider.invalid';
const coin = 'tsol';
const accessToken = 'test-token';
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 = expressApp(cfg);
agent = request.agent(app);
});
afterEach(() => {
nock.cleanAll();
});
it('should bubble up 400 key provider errors', async () => {
nock(keyProviderUrl).post(/.*/).reply(400, { message: 'This is an error message' }).persist();
const response = await agent
.post(`/api/${coin}/mpcv2/initialize`)
.set('Authorization', `Bearer ${accessToken}`)
.send({ source: 'user' });
response.status.should.equal(400);
response.body.should.have.property('error', 'BadRequestError');
response.body.should.have.property('details', 'This is an error message');
});
it('should bubble up 404 key provider errors', async () => {
nock(keyProviderUrl).post(/.*/).reply(404, { message: 'This is an error message' }).persist();
const response = await agent
.post(`/api/${coin}/mpcv2/initialize`)
.set('Authorization', `Bearer ${accessToken}`)
.send({ source: 'user' });
response.status.should.equal(404);
response.body.should.have.property('error', 'NotFoundError');
response.body.should.have.property('details', 'This is an error message');
});
it('should bubble up 409 key provider errors', async () => {
nock(keyProviderUrl).post(/.*/).reply(409, { message: 'This is an error message' }).persist();
const response = await agent
.post(`/api/${coin}/mpcv2/initialize`)
.set('Authorization', `Bearer ${accessToken}`)
.send({ source: 'user' });
response.status.should.equal(409);
response.body.should.have.property('error', 'ConflictError');
response.body.should.have.property('details', 'This is an error message');
});
it('should bubble up 500 key provider errors', async () => {
nock(keyProviderUrl).post(/.*/).reply(500, { message: 'This is an error message' }).persist();
const response = await agent
.post(`/api/${coin}/mpcv2/initialize`)
.set('Authorization', `Bearer ${accessToken}`)
.send({ source: 'user' });
response.status.should.equal(500);
response.body.should.have.property('error', 'Internal Server Error');
response.body.should.have.property('details', 'This is an error message');
});
it('should handle unexpected key provider errors', async () => {
nock(keyProviderUrl).post(/.*/).reply(502, { message: 'Unexpected error' }).persist();
const response = await agent
.post(`/api/${coin}/mpcv2/initialize`)
.set('Authorization', `Bearer ${accessToken}`)
.send({ source: 'user' });
response.status.should.equal(500);
response.body.should.have.property('error', 'Internal Server Error');
response.body.should.have.property(
'details',
'key provider returned unexpected response. 502: Unexpected error',
);
});
});
describe('KeyProviderClient.generateKey', () => {
const keyProviderUrl = 'http://key-provider.invalid';
const endPointPath = '/key/generate';
const params = { coin: 'hteth', source: 'user' as const, type: 'independent' as const };
const mockResponse = { pub: 'xpub661MyMwAq', coin: 'hteth', source: 'user', type: 'independent' };
let client: KeyProviderClient;
before(() => {
nock.disableNetConnect();
client = new KeyProviderClient({
appMode: AppMode.ADVANCED_WALLET_MANAGER,
signingMode: SigningMode.LOCAL,
port: 0,
bind: 'localhost',
timeout: 60000,
httpLoggerFile: '',
keyProviderUrl,
tlsMode: TlsMode.DISABLED,
clientCertAllowSelfSigned: true,
});
});
afterEach(() => nock.cleanAll());
it('should call POST /key/generate with correct params and return response', async () => {
const nockMocked = nock(keyProviderUrl).post(endPointPath, params).reply(200, mockResponse);
const result = await client.generateKey(params);
result.should.have.property('pub', mockResponse.pub);
result.should.have.property('coin', mockResponse.coin);
result.should.have.property('source', mockResponse.source);
result.should.have.property('type', mockResponse.type);
nockMocked.done();
});
[
{
url: endPointPath,
statusCode: 400,
mockedError: 'bad request',
expectedError: 'bad request',
},
{ url: endPointPath, statusCode: 404, mockedError: 'not found', expectedError: 'not found' },
{ url: endPointPath, statusCode: 409, mockedError: 'conflict', expectedError: 'conflict' },
{
url: endPointPath,
statusCode: 500,
mockedError: 'internal error',
expectedError: 'internal error',
},
].forEach(({ url, statusCode, mockedError, expectedError }) => {
it(`should bubble up ${statusCode} errors`, async () => {
const nockMocked = nock(keyProviderUrl)
.post(url)
.reply(statusCode, { message: mockedError })
.persist();
await client.generateKey(params).should.be.rejectedWith(expectedError);
nockMocked.done();
});
});
});
describe('KeyProviderClient.sign', () => {
const keyProviderUrl = 'http://key-provider.invalid';
const endPointPath = '/sign';
const params = {
pub: 'xpub661MyMwAq',
source: 'user' as const,
signablePayload: 'deadbeef',
algorithm: 'ecdsa',
};
const mockResponse = { signature: 'signedpsbt' };
let client: KeyProviderClient;
before(() => {
nock.disableNetConnect();
client = new KeyProviderClient({
appMode: AppMode.ADVANCED_WALLET_MANAGER,
signingMode: SigningMode.LOCAL,
port: 0,
bind: 'localhost',
timeout: 60000,
httpLoggerFile: '',
keyProviderUrl,
tlsMode: TlsMode.DISABLED,
clientCertAllowSelfSigned: true,
});
});
afterEach(() => nock.cleanAll());
it('should call POST /sign with correct params and return signature', async () => {
const n = nock(keyProviderUrl).post(endPointPath, params).reply(200, mockResponse);
const result = await client.sign(params);
result.should.have.property('signature', mockResponse.signature);
n.done();
});
it('should throw if response has no signature', async () => {
nock(keyProviderUrl).post(endPointPath).reply(200, {});
await client
.sign(params)
.should.be.rejectedWith(/key provider returned unexpected response when signing/);
});
[
{ statusCode: 400, mockedError: 'bad request', expectedError: 'bad request' },
{ statusCode: 404, mockedError: 'not found', expectedError: 'not found' },
{ statusCode: 409, mockedError: 'conflict', expectedError: 'conflict' },
{ statusCode: 500, mockedError: 'internal error', expectedError: 'internal error' },
].forEach(({ statusCode, mockedError, expectedError }) => {
it(`should bubble up ${statusCode} errors`, async () => {
const nockMocked = nock(keyProviderUrl)
.post(endPointPath)
.reply(statusCode, { message: mockedError })
.persist();
await client.sign(params).should.be.rejectedWith(expectedError);
nockMocked.done();
});
});
});