Skip to content

Commit b1d5c26

Browse files
authored
test(express): added supertest for simpleCreate
2 parents 24d7778 + 7733506 commit b1d5c26

File tree

1 file changed

+323
-0
lines changed

1 file changed

+323
-0
lines changed
Lines changed: 323 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
1+
import * as assert from 'assert';
2+
import * as t from 'io-ts';
3+
import { SimpleCreateRequestBody, PostSimpleCreate } from '../../../src/typedRoutes/api/v1/simpleCreate';
4+
import { assertDecode } from './common';
5+
import 'should';
6+
import 'should-http';
7+
import 'should-sinon';
8+
import * as sinon from 'sinon';
9+
import { BitGo } from 'bitgo';
10+
import { setupAgent } from '../../lib/testutil';
11+
12+
describe('SimpleCreate codec tests', function () {
13+
describe('SimpleCreateRequestBody', function () {
14+
it('should validate body with required field (passphrase)', function () {
15+
const validBody = {
16+
passphrase: 'mySecurePassphrase123',
17+
};
18+
19+
const decoded = assertDecode(t.type(SimpleCreateRequestBody), validBody);
20+
assert.strictEqual(decoded.passphrase, validBody.passphrase);
21+
assert.strictEqual(decoded.label, undefined);
22+
assert.strictEqual(decoded.backupXpub, undefined);
23+
});
24+
25+
it('should validate body with all fields', function () {
26+
const validBody = {
27+
passphrase: 'mySecurePassphrase123',
28+
label: 'My Test Wallet',
29+
backupXpub:
30+
'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8',
31+
backupXpubProvider: 'keyternal',
32+
enterprise: 'enterprise123',
33+
passcodeEncryptionCode: 'encryptionCode123',
34+
disableTransactionNotifications: true,
35+
disableKRSEmail: false,
36+
};
37+
38+
const decoded = assertDecode(t.type(SimpleCreateRequestBody), validBody);
39+
assert.strictEqual(decoded.passphrase, validBody.passphrase);
40+
assert.strictEqual(decoded.label, validBody.label);
41+
assert.strictEqual(decoded.backupXpub, validBody.backupXpub);
42+
assert.strictEqual(decoded.backupXpubProvider, validBody.backupXpubProvider);
43+
assert.strictEqual(decoded.enterprise, validBody.enterprise);
44+
assert.strictEqual(decoded.passcodeEncryptionCode, validBody.passcodeEncryptionCode);
45+
assert.strictEqual(decoded.disableTransactionNotifications, validBody.disableTransactionNotifications);
46+
assert.strictEqual(decoded.disableKRSEmail, validBody.disableKRSEmail);
47+
});
48+
49+
it('should reject body with missing passphrase', function () {
50+
const invalidBody = {
51+
label: 'My Wallet',
52+
};
53+
54+
assert.throws(() => {
55+
assertDecode(t.type(SimpleCreateRequestBody), invalidBody);
56+
});
57+
});
58+
59+
it('should reject body with non-string passphrase', function () {
60+
const invalidBody = {
61+
passphrase: 12345,
62+
};
63+
64+
assert.throws(() => {
65+
assertDecode(t.type(SimpleCreateRequestBody), invalidBody);
66+
});
67+
});
68+
});
69+
70+
describe('SimpleCreateResponse', function () {
71+
const SimpleCreateResponse = PostSimpleCreate.response[200];
72+
73+
it('should validate response with all required fields', function () {
74+
const validResponse = {
75+
wallet: 'wallet_id_123',
76+
userKeychain: 'user_keychain_123',
77+
backupKeychain: 'backup_keychain_123',
78+
};
79+
80+
const decoded = assertDecode(SimpleCreateResponse, validResponse);
81+
assert.strictEqual(decoded.wallet, validResponse.wallet);
82+
assert.strictEqual(decoded.userKeychain, validResponse.userKeychain);
83+
assert.strictEqual(decoded.backupKeychain, validResponse.backupKeychain);
84+
});
85+
86+
it('should reject response with missing wallet field', function () {
87+
const invalidResponse = {
88+
userKeychain: 'user_keychain_123',
89+
backupKeychain: 'backup_keychain_123',
90+
};
91+
92+
assert.throws(() => {
93+
assertDecode(SimpleCreateResponse, invalidResponse);
94+
});
95+
});
96+
97+
it('should reject response with non-string fields', function () {
98+
const invalidResponse = {
99+
wallet: 123,
100+
userKeychain: 'user_keychain_123',
101+
backupKeychain: 'backup_keychain_123',
102+
};
103+
104+
assert.throws(() => {
105+
assertDecode(SimpleCreateResponse, invalidResponse);
106+
});
107+
});
108+
});
109+
110+
describe('PostSimpleCreate route definition', function () {
111+
it('should have the correct path', function () {
112+
assert.strictEqual(PostSimpleCreate.path, '/api/v1/wallets/simplecreate');
113+
});
114+
115+
it('should have the correct HTTP method', function () {
116+
assert.strictEqual(PostSimpleCreate.method, 'POST');
117+
});
118+
119+
it('should have the correct response types', function () {
120+
assert.ok(PostSimpleCreate.response[200]);
121+
assert.ok(PostSimpleCreate.response[400]);
122+
});
123+
});
124+
125+
// ==========================================
126+
// SUPERTEST INTEGRATION TESTS
127+
// ==========================================
128+
129+
describe('Supertest Integration Tests', function () {
130+
const agent = setupAgent();
131+
132+
const mockCreateWalletResponse = {
133+
wallet: 'wallet_id_123',
134+
userKeychain: 'user_keychain_123',
135+
backupKeychain: 'backup_keychain_123',
136+
};
137+
138+
afterEach(function () {
139+
sinon.restore();
140+
});
141+
142+
it('should successfully create wallet with passphrase only', async function () {
143+
const requestBody = {
144+
passphrase: 'mySecurePassphrase123',
145+
};
146+
147+
const createWalletStub = sinon.stub().resolves(mockCreateWalletResponse);
148+
const mockWallets = {
149+
createWalletWithKeychains: createWalletStub,
150+
};
151+
152+
sinon.stub(BitGo.prototype, 'wallets').returns(mockWallets as any);
153+
154+
const result = await agent
155+
.post('/api/v1/wallets/simplecreate')
156+
.set('Authorization', 'Bearer test_access_token_12345')
157+
.set('Content-Type', 'application/json')
158+
.send(requestBody);
159+
160+
assert.strictEqual(result.status, 200);
161+
result.body.should.have.property('wallet');
162+
result.body.should.have.property('userKeychain');
163+
result.body.should.have.property('backupKeychain');
164+
assert.strictEqual(result.body.wallet, mockCreateWalletResponse.wallet);
165+
166+
const decodedResponse = assertDecode(PostSimpleCreate.response[200], result.body);
167+
assert.strictEqual(decodedResponse.wallet, mockCreateWalletResponse.wallet);
168+
169+
sinon.assert.calledOnce(createWalletStub);
170+
sinon.assert.calledWith(createWalletStub, requestBody);
171+
});
172+
173+
it('should successfully create wallet with all optional parameters', async function () {
174+
const requestBody = {
175+
passphrase: 'mySecurePassphrase123',
176+
label: 'My Test Wallet',
177+
backupXpub:
178+
'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8',
179+
enterprise: 'enterprise123',
180+
disableTransactionNotifications: true,
181+
};
182+
183+
const createWalletStub = sinon.stub().resolves(mockCreateWalletResponse);
184+
const mockWallets = {
185+
createWalletWithKeychains: createWalletStub,
186+
};
187+
188+
sinon.stub(BitGo.prototype, 'wallets').returns(mockWallets as any);
189+
190+
const result = await agent
191+
.post('/api/v1/wallets/simplecreate')
192+
.set('Authorization', 'Bearer test_access_token_12345')
193+
.set('Content-Type', 'application/json')
194+
.send(requestBody);
195+
196+
assert.strictEqual(result.status, 200);
197+
assert.strictEqual(result.body.wallet, mockCreateWalletResponse.wallet);
198+
199+
const decodedResponse = assertDecode(PostSimpleCreate.response[200], result.body);
200+
assert.ok(decodedResponse);
201+
202+
sinon.assert.calledOnce(createWalletStub);
203+
sinon.assert.calledWith(createWalletStub, requestBody);
204+
});
205+
206+
it('should successfully create wallet with KRS backup provider', async function () {
207+
const requestBody = {
208+
passphrase: 'mySecurePassphrase123',
209+
label: 'KRS Wallet',
210+
backupXpubProvider: 'keyternal',
211+
};
212+
213+
const createWalletStub = sinon.stub().resolves(mockCreateWalletResponse);
214+
const mockWallets = {
215+
createWalletWithKeychains: createWalletStub,
216+
};
217+
218+
sinon.stub(BitGo.prototype, 'wallets').returns(mockWallets as any);
219+
220+
const result = await agent
221+
.post('/api/v1/wallets/simplecreate')
222+
.set('Authorization', 'Bearer test_access_token_12345')
223+
.set('Content-Type', 'application/json')
224+
.send(requestBody);
225+
226+
assert.strictEqual(result.status, 200);
227+
assert.strictEqual(result.body.wallet, mockCreateWalletResponse.wallet);
228+
229+
sinon.assert.calledOnce(createWalletStub);
230+
sinon.assert.calledWith(createWalletStub, requestBody);
231+
});
232+
});
233+
234+
// ==========================================
235+
// ERROR HANDLING TESTS
236+
// ==========================================
237+
238+
describe('Error Handling Tests', function () {
239+
const agent = setupAgent();
240+
241+
afterEach(function () {
242+
sinon.restore();
243+
});
244+
245+
it('should return 400 for missing passphrase', async function () {
246+
const requestBody = {
247+
label: 'My Wallet',
248+
};
249+
250+
const result = await agent
251+
.post('/api/v1/wallets/simplecreate')
252+
.set('Authorization', 'Bearer test_access_token_12345')
253+
.set('Content-Type', 'application/json')
254+
.send(requestBody);
255+
256+
assert.strictEqual(result.status, 400);
257+
assert.ok(Array.isArray(result.body));
258+
assert.ok(result.body.length > 0);
259+
});
260+
261+
it('should return 400 for non-string passphrase', async function () {
262+
const requestBody = {
263+
passphrase: 12345,
264+
};
265+
266+
const result = await agent
267+
.post('/api/v1/wallets/simplecreate')
268+
.set('Authorization', 'Bearer test_access_token_12345')
269+
.set('Content-Type', 'application/json')
270+
.send(requestBody);
271+
272+
assert.strictEqual(result.status, 400);
273+
assert.ok(Array.isArray(result.body));
274+
assert.ok(result.body.length > 0);
275+
});
276+
277+
it('should handle wallet creation failure', async function () {
278+
const requestBody = {
279+
passphrase: 'mySecurePassphrase123',
280+
label: 'Test Wallet',
281+
};
282+
283+
const createWalletStub = sinon.stub().rejects(new Error('Wallet creation failed'));
284+
const mockWallets = {
285+
createWalletWithKeychains: createWalletStub,
286+
};
287+
288+
sinon.stub(BitGo.prototype, 'wallets').returns(mockWallets as any);
289+
290+
const result = await agent
291+
.post('/api/v1/wallets/simplecreate')
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 invalid passphrase error', async function () {
301+
const requestBody = {
302+
passphrase: 'weak',
303+
label: 'Test Wallet',
304+
};
305+
306+
const createWalletStub = sinon.stub().rejects(new Error('Passphrase too weak'));
307+
const mockWallets = {
308+
createWalletWithKeychains: createWalletStub,
309+
};
310+
311+
sinon.stub(BitGo.prototype, 'wallets').returns(mockWallets as any);
312+
313+
const result = await agent
314+
.post('/api/v1/wallets/simplecreate')
315+
.set('Authorization', 'Bearer test_access_token_12345')
316+
.set('Content-Type', 'application/json')
317+
.send(requestBody);
318+
319+
assert.strictEqual(result.status, 500);
320+
result.body.should.have.property('error');
321+
});
322+
});
323+
});

0 commit comments

Comments
 (0)