-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathlightningInvoiceRoutes.test.ts
More file actions
199 lines (168 loc) · 6.36 KB
/
lightningInvoiceRoutes.test.ts
File metadata and controls
199 lines (168 loc) · 6.36 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
import * as sinon from 'sinon';
import should from 'should';
import * as express from 'express';
import { handlePayLightningInvoice, handleCreateLightningInvoice } from '../../../src/lightning/lightningInvoiceRoutes';
import { Invoice, PayInvoiceResponse } from '@bitgo/abstract-lightning';
import { BitGo } from 'bitgo';
import * as assert from 'node:assert';
describe('Lightning Invoice Routes', () => {
let bitgo;
const coin = 'tlnbtc';
const mockRequestObject = (params: { body?: any; params?: any; query?: any; bitgo?: any }) => {
const req: Partial<express.Request> = {};
req.body = params.body || {};
req.params = params.params || {};
req.query = params.query || {};
req.bitgo = params.bitgo;
// Add decoded property with both path params and body for typed routes
(req as any).decoded = {
coin: params.params?.coin,
id: params.params?.id,
...params.body,
};
return req as express.Request;
};
afterEach(() => {
sinon.restore();
});
describe('Create Lightning Invoice', () => {
it('should successfully create a lightning invoice', async () => {
const inputParams = {
valueMsat: '10000',
memo: 'test invoice',
expiry: 3600,
};
const expectedResponse = {
valueMsat: 10000n,
memo: 'test invoice',
paymentHash: 'abc123',
invoice: 'lntb100u1p3h2jk3pp5yndyvx4zmv...',
walletId: 'testWalletId',
status: 'open' as const,
expiresAt: new Date('2025-02-21T10:00:00.000Z'),
createdAt: new Date('2025-02-21T09:00:00.000Z'),
updatedAt: new Date('2025-02-21T09:00:00.000Z'),
};
const createInvoiceSpy = sinon.stub().resolves(expectedResponse);
const mockLightningWallet = {
createInvoice: createInvoiceSpy,
};
// Mock the module import
const proxyquire = require('proxyquire');
const lightningRoutes = proxyquire('../../../src/lightning/lightningInvoiceRoutes', {
'@bitgo/abstract-lightning': {
getLightningWallet: () => mockLightningWallet,
},
});
const walletStub = {};
const coinStub = {
wallets: () => ({ get: sinon.stub().resolves(walletStub) }),
};
const stubBitgo = sinon.createStubInstance(BitGo as any, { coin: coinStub });
const req = mockRequestObject({
params: { id: 'testWalletId', coin },
body: inputParams,
bitgo: stubBitgo,
});
const result = await lightningRoutes.handleCreateLightningInvoice(req);
should(result).deepEqual(Invoice.encode(expectedResponse));
const decodedResult = Invoice.decode(result);
assert.ok('right' in decodedResult);
should(decodedResult.right).deepEqual(expectedResponse);
should(createInvoiceSpy).be.calledOnce();
const [firstArg] = createInvoiceSpy.getCall(0).args;
should(firstArg).have.property('valueMsat', BigInt(10000));
should(firstArg).have.property('memo', 'test invoice');
should(firstArg).have.property('expiry', 3600);
});
it('should fail when valueMsat is missing from request', async () => {
const inputParams = {
memo: 'test invoice',
expiry: 3600,
};
const req = mockRequestObject({
params: { id: 'testWalletId', coin },
body: inputParams,
});
req.bitgo = bitgo;
await should(handleCreateLightningInvoice(req)).be.rejectedWith(
/^Invalid request body to create lightning invoice/
);
});
});
describe('Pay Lightning Invoice', () => {
it('should successfully pay a lightning invoice', async () => {
const inputParams = {
invoice: 'lntb100u1p3h2jk3pp5yndyvx4zmv...',
amountMsat: '10000',
passphrase: 'wallet-password-12345',
randomParamThatWontBreakDecoding: 'randomValue',
};
const expectedResponse: PayInvoiceResponse = {
paymentStatus: {
paymentHash: 'xyz789',
status: 'settled',
},
txRequestState: 'delivered',
txRequestId: '123',
};
const payInvoiceStub = sinon.stub().resolves(expectedResponse);
const mockLightningWallet = {
payInvoice: payInvoiceStub,
};
// Mock the module import
const proxyquire = require('proxyquire');
const lightningRoutes = proxyquire('../../../src/lightning/lightningInvoiceRoutes', {
'@bitgo/abstract-lightning': {
getLightningWallet: () => mockLightningWallet,
},
});
const walletStub = {};
const coinStub = {
wallets: () => ({ get: sinon.stub().resolves(walletStub) }),
};
const stubBitgo = sinon.createStubInstance(BitGo as any, { coin: coinStub });
const req = mockRequestObject({
params: { id: 'testWalletId', coin },
body: inputParams,
bitgo: stubBitgo,
});
const result = await lightningRoutes.handlePayLightningInvoice(req);
should(result).deepEqual(expectedResponse);
should(payInvoiceStub).be.calledOnce();
const [firstArg] = payInvoiceStub.getCall(0).args;
// we decode the amountMsat string to bigint, it should be in bigint format when passed to payInvoice
should(firstArg).have.property('amountMsat', BigInt(10000));
should(firstArg).have.property('invoice', inputParams.invoice);
should(firstArg).have.property('passphrase', 'wallet-password-12345');
});
it('should throw an error if the passphrase is missing in the request params', async () => {
const inputParams = {
invoice: 'lntb100u1p3h2jk3pp5yndyvx4zmv...',
amountMsat: '10000',
};
const req = mockRequestObject({
params: { id: 'testWalletId', coin },
body: inputParams,
});
req.bitgo = bitgo;
await should(handlePayLightningInvoice(req as any)).be.rejectedWith(
'Invalid request body to pay lightning invoice'
);
});
it('should throw an error if the invoice is missing in the request params', async () => {
const inputParams = {
amountMsat: '10000',
passphrase: 'wallet-password-12345',
};
const req = mockRequestObject({
params: { id: 'testWalletId', coin },
body: inputParams,
});
req.bitgo = bitgo;
await should(handlePayLightningInvoice(req as any)).be.rejectedWith(
/^Invalid request body to pay lightning invoice/
);
});
});
});