Skip to content

Commit a090e61

Browse files
authored
test(express): added supertest for acceptShare
2 parents b1d5c26 + d689f29 commit a090e61

File tree

1 file changed

+223
-12
lines changed

1 file changed

+223
-12
lines changed

modules/express/test/unit/typedRoutes/acceptShare.ts

Lines changed: 223 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@ import {
55
AcceptShareRequestBody,
66
PostAcceptShare,
77
} from '../../../src/typedRoutes/api/v1/acceptShare';
8-
9-
/**
10-
* Helper function to test io-ts codec decoding
11-
*/
12-
export function assertDecode<T>(codec: t.Type<T, unknown>, input: unknown): T {
13-
const result = codec.decode(input);
14-
if (result._tag === 'Left') {
15-
const errors = JSON.stringify(result.left, null, 2);
16-
assert.fail(`Decode failed with errors:\n${errors}`);
17-
}
18-
return result.right;
19-
}
8+
import { assertDecode } from './common';
9+
import 'should';
10+
import 'should-http';
11+
import 'should-sinon';
12+
import * as sinon from 'sinon';
13+
import { BitGo } from 'bitgo';
14+
import { setupAgent } from '../../lib/testutil';
2015

2116
describe('AcceptShare codec tests', function () {
2217
describe('AcceptShareRequestParams', function () {
@@ -130,4 +125,220 @@ describe('AcceptShare codec tests', function () {
130125
assert.strictEqual(decoded.unknownProperty, undefined);
131126
});
132127
});
128+
129+
describe('Supertest Integration Tests', function () {
130+
const agent = setupAgent();
131+
const shareId = 'share123456789abcdef';
132+
133+
const mockAcceptShareResponse = {
134+
state: 'accepted',
135+
changed: true,
136+
walletId: 'wallet123',
137+
};
138+
139+
afterEach(function () {
140+
sinon.restore();
141+
});
142+
143+
it('should successfully accept share with all optional fields', async function () {
144+
const requestBody = {
145+
userPassword: 'mySecurePassword',
146+
newWalletPassphrase: 'myNewPassphrase',
147+
overrideEncryptedXprv: 'encryptedXprvString',
148+
};
149+
150+
const acceptShareStub = sinon.stub().resolves(mockAcceptShareResponse);
151+
const mockWallets = {
152+
acceptShare: acceptShareStub,
153+
};
154+
155+
sinon.stub(BitGo.prototype, 'wallets').returns(mockWallets as any);
156+
157+
const result = await agent
158+
.post(`/api/v1/walletshare/${shareId}/acceptShare`)
159+
.set('Authorization', 'Bearer test_access_token_12345')
160+
.set('Content-Type', 'application/json')
161+
.send(requestBody);
162+
163+
assert.strictEqual(result.status, 200);
164+
assert.ok(result.body);
165+
166+
// Verify the method was called with correct params
167+
sinon.assert.calledOnce(acceptShareStub);
168+
const callArgs = acceptShareStub.firstCall.args[0];
169+
assert.strictEqual(callArgs.walletShareId, shareId);
170+
assert.strictEqual(callArgs.userPassword, requestBody.userPassword);
171+
assert.strictEqual(callArgs.newWalletPassphrase, requestBody.newWalletPassphrase);
172+
assert.strictEqual(callArgs.overrideEncryptedXprv, requestBody.overrideEncryptedXprv);
173+
});
174+
175+
it('should successfully accept share with empty body', async function () {
176+
const requestBody = {};
177+
178+
const acceptShareStub = sinon.stub().resolves(mockAcceptShareResponse);
179+
const mockWallets = {
180+
acceptShare: acceptShareStub,
181+
};
182+
183+
sinon.stub(BitGo.prototype, 'wallets').returns(mockWallets as any);
184+
185+
const result = await agent
186+
.post(`/api/v1/walletshare/${shareId}/acceptShare`)
187+
.set('Authorization', 'Bearer test_access_token_12345')
188+
.set('Content-Type', 'application/json')
189+
.send(requestBody);
190+
191+
assert.strictEqual(result.status, 200);
192+
assert.ok(result.body);
193+
194+
sinon.assert.calledOnce(acceptShareStub);
195+
const callArgs = acceptShareStub.firstCall.args[0];
196+
assert.strictEqual(callArgs.walletShareId, shareId);
197+
});
198+
199+
it('should successfully accept share with only userPassword', async function () {
200+
const requestBody = {
201+
userPassword: 'mySecurePassword',
202+
};
203+
204+
const acceptShareStub = sinon.stub().resolves(mockAcceptShareResponse);
205+
const mockWallets = {
206+
acceptShare: acceptShareStub,
207+
};
208+
209+
sinon.stub(BitGo.prototype, 'wallets').returns(mockWallets as any);
210+
211+
const result = await agent
212+
.post(`/api/v1/walletshare/${shareId}/acceptShare`)
213+
.set('Authorization', 'Bearer test_access_token_12345')
214+
.set('Content-Type', 'application/json')
215+
.send(requestBody);
216+
217+
assert.strictEqual(result.status, 200);
218+
assert.ok(result.body);
219+
220+
sinon.assert.calledOnce(acceptShareStub);
221+
const callArgs = acceptShareStub.firstCall.args[0];
222+
assert.strictEqual(callArgs.walletShareId, shareId);
223+
assert.strictEqual(callArgs.userPassword, requestBody.userPassword);
224+
});
225+
226+
it('should successfully accept share with only newWalletPassphrase', async function () {
227+
const requestBody = {
228+
newWalletPassphrase: 'myNewPassphrase',
229+
};
230+
231+
const acceptShareStub = sinon.stub().resolves(mockAcceptShareResponse);
232+
const mockWallets = {
233+
acceptShare: acceptShareStub,
234+
};
235+
236+
sinon.stub(BitGo.prototype, 'wallets').returns(mockWallets as any);
237+
238+
const result = await agent
239+
.post(`/api/v1/walletshare/${shareId}/acceptShare`)
240+
.set('Authorization', 'Bearer test_access_token_12345')
241+
.set('Content-Type', 'application/json')
242+
.send(requestBody);
243+
244+
assert.strictEqual(result.status, 200);
245+
assert.ok(result.body);
246+
247+
sinon.assert.calledOnce(acceptShareStub);
248+
const callArgs = acceptShareStub.firstCall.args[0];
249+
assert.strictEqual(callArgs.walletShareId, shareId);
250+
assert.strictEqual(callArgs.newWalletPassphrase, requestBody.newWalletPassphrase);
251+
});
252+
});
253+
254+
describe('Error Handling Tests', function () {
255+
const agent = setupAgent();
256+
const shareId = 'share123456789abcdef';
257+
258+
afterEach(function () {
259+
sinon.restore();
260+
});
261+
262+
it('should return 400 for non-string optional fields', async function () {
263+
const requestBody = {
264+
userPassword: 12345, // Invalid type
265+
};
266+
267+
const result = await agent
268+
.post(`/api/v1/walletshare/${shareId}/acceptShare`)
269+
.set('Authorization', 'Bearer test_access_token_12345')
270+
.set('Content-Type', 'application/json')
271+
.send(requestBody);
272+
273+
assert.strictEqual(result.status, 400);
274+
assert.ok(Array.isArray(result.body));
275+
assert.ok(result.body.length > 0);
276+
});
277+
278+
it('should handle acceptShare method throwing error', async function () {
279+
const requestBody = {
280+
userPassword: 'wrongPassword',
281+
};
282+
283+
const acceptShareStub = sinon.stub().rejects(new Error('Invalid password'));
284+
const mockWallets = {
285+
acceptShare: acceptShareStub,
286+
};
287+
288+
sinon.stub(BitGo.prototype, 'wallets').returns(mockWallets as any);
289+
290+
const result = await agent
291+
.post(`/api/v1/walletshare/${shareId}/acceptShare`)
292+
.set('Authorization', 'Bearer test_access_token_12345')
293+
.set('Content-Type', 'application/json')
294+
.send(requestBody);
295+
296+
assert.strictEqual(result.status, 500);
297+
result.body.should.have.property('error');
298+
});
299+
300+
it('should handle share not found error', async function () {
301+
const requestBody = {
302+
newWalletPassphrase: 'myNewPassphrase',
303+
};
304+
305+
const acceptShareStub = sinon.stub().rejects(new Error('Share not found'));
306+
const mockWallets = {
307+
acceptShare: acceptShareStub,
308+
};
309+
310+
sinon.stub(BitGo.prototype, 'wallets').returns(mockWallets as any);
311+
312+
const result = await agent
313+
.post(`/api/v1/walletshare/${shareId}/acceptShare`)
314+
.set('Authorization', 'Bearer test_access_token_12345')
315+
.set('Content-Type', 'application/json')
316+
.send(requestBody);
317+
318+
assert.strictEqual(result.status, 500);
319+
result.body.should.have.property('error');
320+
});
321+
322+
it('should handle share already accepted error', async function () {
323+
const requestBody = {
324+
userPassword: 'myPassword',
325+
};
326+
327+
const acceptShareStub = sinon.stub().rejects(new Error('Share already accepted'));
328+
const mockWallets = {
329+
acceptShare: acceptShareStub,
330+
};
331+
332+
sinon.stub(BitGo.prototype, 'wallets').returns(mockWallets as any);
333+
334+
const result = await agent
335+
.post(`/api/v1/walletshare/${shareId}/acceptShare`)
336+
.set('Authorization', 'Bearer test_access_token_12345')
337+
.set('Content-Type', 'application/json')
338+
.send(requestBody);
339+
340+
assert.strictEqual(result.status, 500);
341+
result.body.should.have.property('error');
342+
});
343+
});
133344
});

0 commit comments

Comments
 (0)