Skip to content

Commit 515807f

Browse files
committed
test: removed try-catch and split test into 4 seperate tests
ticket: WP-6196 TICKET: WP-5445
1 parent 2eb2b11 commit 515807f

File tree

1 file changed

+191
-63
lines changed

1 file changed

+191
-63
lines changed

modules/express/test/unit/clientRoutes/lightning/lightningSignerRoutes.ts

Lines changed: 191 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -99,70 +99,198 @@ describe('Lightning signer routes', () => {
9999
});
100100
}
101101

102-
for (const addIpCaveatToMacaroon of [true, false]) {
103-
for (const includeWatchOnlyIp of [true, false]) {
104-
it(`create signer macaroon ${addIpCaveatToMacaroon ? 'with' : 'without'} including IP caveat when it ${
105-
includeWatchOnlyIp ? 'does' : `doesn't`
106-
} exist`, async () => {
107-
const readFileStub = sinon.stub(fs.promises, 'readFile').resolves(JSON.stringify(lightningSignerConfigs));
108-
const wpWalletnock = nock(bgUrl)
109-
.get(`/api/v2/tlnbtc/wallet/${apiData.wallet.id}`)
110-
.query({ includeBalance: false })
111-
.reply(200, {
112-
...apiData.wallet,
113-
...(includeWatchOnlyIp ? {} : { watchOnlyExternalIp: null }),
114-
});
115-
116-
const wpKeychainNocks = [
117-
nock(bgUrl).get(`/api/v2/tlnbtc/key/${apiData.userAuthKey.id}`).reply(200, apiData.userAuthKey),
118-
nock(bgUrl).get(`/api/v2/tlnbtc/key/${apiData.nodeAuthKey.id}`).reply(200, apiData.nodeAuthKey),
119-
];
120-
121-
const signerMacaroon = nock(lightningSignerConfigs.fakeid.url)
122-
.post(`/v1/macaroon`)
123-
.reply(200, signerApiData.bakeMacaroon);
124-
125-
const wpWalletUpdateNock = nock(bgUrl).put(`/api/v2/tlnbtc/wallet/${apiData.wallet.id}`).reply(200);
126-
127-
const req = {
128-
bitgo: bitgo,
129-
body: { ...apiData.signerMacaroonRequestBody, addIpCaveatToMacaroon },
130-
params: {
131-
coin: 'tlnbtc',
132-
id: 'fakeid',
133-
walletId: 'fakeid',
134-
},
135-
decoded: {
136-
coin: 'tlnbtc',
137-
walletId: apiData.wallet.id,
138-
passphrase: apiData.signerMacaroonRequestBody.passphrase,
139-
addIpCaveatToMacaroon,
140-
},
141-
config: {
142-
lightningSignerFileSystemPath: 'lightningSignerFileSystemPath',
143-
},
144-
} as unknown as ExpressApiRouteRequest<'express.lightning.signerMacaroon', 'post'>;
145-
146-
try {
147-
const res = await handleCreateSignerMacaroon(req);
148-
decodeOrElse('SignerMacaroonResponse200', SignerMacaroonResponse[200], res, (_) => {
149-
throw new Error('Response did not match expected codec');
150-
});
151-
} catch (e) {
152-
if (!includeWatchOnlyIp || addIpCaveatToMacaroon) {
153-
throw e;
154-
}
155-
}
156-
157-
wpWalletUpdateNock.done();
158-
signerMacaroon.done();
159-
wpKeychainNocks.forEach((s) => s.done());
160-
wpWalletnock.done();
161-
readFileStub.calledOnceWith('lightningSignerFileSystemPath').should.be.true();
162-
readFileStub.restore();
102+
it('should create signer macaroon with IP caveat when watchOnlyExternalIp exists', async () => {
103+
const readFileStub = sinon.stub(fs.promises, 'readFile').resolves(JSON.stringify(lightningSignerConfigs));
104+
105+
const wpWalletnock = nock(bgUrl)
106+
.get(`/api/v2/tlnbtc/wallet/${apiData.wallet.id}`)
107+
.query({ includeBalance: false })
108+
.reply(200, apiData.wallet);
109+
110+
const wpKeychainNocks = [
111+
nock(bgUrl).get(`/api/v2/tlnbtc/key/${apiData.userAuthKey.id}`).reply(200, apiData.userAuthKey),
112+
nock(bgUrl).get(`/api/v2/tlnbtc/key/${apiData.nodeAuthKey.id}`).reply(200, apiData.nodeAuthKey),
113+
];
114+
115+
const signerMacaroon = nock(lightningSignerConfigs.fakeid.url)
116+
.post(`/v1/macaroon`)
117+
.reply(200, signerApiData.bakeMacaroon);
118+
119+
const wpWalletUpdateNock = nock(bgUrl).put(`/api/v2/tlnbtc/wallet/${apiData.wallet.id}`).reply(200);
120+
121+
const req = {
122+
bitgo: bitgo,
123+
body: { ...apiData.signerMacaroonRequestBody, addIpCaveatToMacaroon: true },
124+
params: {
125+
coin: 'tlnbtc',
126+
walletId: 'fakeid',
127+
},
128+
decoded: {
129+
coin: 'tlnbtc',
130+
walletId: 'fakeid',
131+
passphrase: apiData.signerMacaroonRequestBody.passphrase,
132+
addIpCaveatToMacaroon: true,
133+
},
134+
config: {
135+
lightningSignerFileSystemPath: 'lightningSignerFileSystemPath',
136+
},
137+
} as unknown as ExpressApiRouteRequest<'express.lightning.signerMacaroon', 'post'>;
138+
139+
const res = await handleCreateSignerMacaroon(req);
140+
decodeOrElse('SignerMacaroonResponse200', SignerMacaroonResponse[200], res, (_) => {
141+
throw new Error('Response did not match expected codec');
142+
});
143+
144+
wpWalletnock.done();
145+
wpKeychainNocks.forEach((s) => s.done());
146+
signerMacaroon.done();
147+
wpWalletUpdateNock.done();
148+
readFileStub.restore();
149+
});
150+
151+
it('should fail to create signer macaroon with IP caveat when watchOnlyExternalIp does not exist', async () => {
152+
const readFileStub = sinon.stub(fs.promises, 'readFile').resolves(JSON.stringify(lightningSignerConfigs));
153+
154+
const wpWalletnock = nock(bgUrl)
155+
.get(`/api/v2/tlnbtc/wallet/${apiData.wallet.id}`)
156+
.query({ includeBalance: false })
157+
.reply(200, {
158+
...apiData.wallet,
159+
coinSpecific: {
160+
...apiData.wallet.coinSpecific,
161+
watchOnlyExternalIp: null,
162+
},
163163
});
164-
}
165-
}
164+
165+
const req = {
166+
bitgo: bitgo,
167+
body: { ...apiData.signerMacaroonRequestBody, addIpCaveatToMacaroon: true },
168+
params: {
169+
coin: 'tlnbtc',
170+
walletId: 'fakeid',
171+
},
172+
decoded: {
173+
coin: 'tlnbtc',
174+
walletId: 'fakeid',
175+
passphrase: apiData.signerMacaroonRequestBody.passphrase,
176+
addIpCaveatToMacaroon: true,
177+
},
178+
config: {
179+
lightningSignerFileSystemPath: 'lightningSignerFileSystemPath',
180+
},
181+
} as unknown as ExpressApiRouteRequest<'express.lightning.signerMacaroon', 'post'>;
182+
183+
await handleCreateSignerMacaroon(req).should.be.rejectedWith(
184+
/Cannot create signer macaroon because the external IP is not set/
185+
);
186+
187+
wpWalletnock.done();
188+
readFileStub.restore();
189+
});
190+
191+
it('should create signer macaroon without IP caveat when watchOnlyExternalIp exists', async () => {
192+
const readFileStub = sinon.stub(fs.promises, 'readFile').resolves(JSON.stringify(lightningSignerConfigs));
193+
194+
const wpWalletnock = nock(bgUrl)
195+
.get(`/api/v2/tlnbtc/wallet/${apiData.wallet.id}`)
196+
.query({ includeBalance: false })
197+
.reply(200, apiData.wallet);
198+
199+
const wpKeychainNocks = [
200+
nock(bgUrl).get(`/api/v2/tlnbtc/key/${apiData.userAuthKey.id}`).reply(200, apiData.userAuthKey),
201+
nock(bgUrl).get(`/api/v2/tlnbtc/key/${apiData.nodeAuthKey.id}`).reply(200, apiData.nodeAuthKey),
202+
];
203+
204+
const signerMacaroon = nock(lightningSignerConfigs.fakeid.url)
205+
.post(`/v1/macaroon`)
206+
.reply(200, signerApiData.bakeMacaroon);
207+
208+
const wpWalletUpdateNock = nock(bgUrl).put(`/api/v2/tlnbtc/wallet/${apiData.wallet.id}`).reply(200);
209+
210+
const req = {
211+
bitgo: bitgo,
212+
body: { ...apiData.signerMacaroonRequestBody, addIpCaveatToMacaroon: false },
213+
params: {
214+
coin: 'tlnbtc',
215+
walletId: 'fakeid',
216+
},
217+
decoded: {
218+
coin: 'tlnbtc',
219+
walletId: 'fakeid',
220+
passphrase: apiData.signerMacaroonRequestBody.passphrase,
221+
addIpCaveatToMacaroon: false,
222+
},
223+
config: {
224+
lightningSignerFileSystemPath: 'lightningSignerFileSystemPath',
225+
},
226+
} as unknown as ExpressApiRouteRequest<'express.lightning.signerMacaroon', 'post'>;
227+
228+
const res = await handleCreateSignerMacaroon(req);
229+
decodeOrElse('SignerMacaroonResponse200', SignerMacaroonResponse[200], res, (_) => {
230+
throw new Error('Response did not match expected codec');
231+
});
232+
233+
wpWalletnock.done();
234+
wpKeychainNocks.forEach((s) => s.done());
235+
signerMacaroon.done();
236+
wpWalletUpdateNock.done();
237+
readFileStub.restore();
238+
});
239+
240+
it('should create signer macaroon without IP caveat when watchOnlyExternalIp does not exist', async () => {
241+
const readFileStub = sinon.stub(fs.promises, 'readFile').resolves(JSON.stringify(lightningSignerConfigs));
242+
243+
const wpWalletnock = nock(bgUrl)
244+
.get(`/api/v2/tlnbtc/wallet/${apiData.wallet.id}`)
245+
.query({ includeBalance: false })
246+
.reply(200, {
247+
...apiData.wallet,
248+
coinSpecific: {
249+
...apiData.wallet.coinSpecific,
250+
watchOnlyExternalIp: null,
251+
},
252+
});
253+
254+
const wpKeychainNocks = [
255+
nock(bgUrl).get(`/api/v2/tlnbtc/key/${apiData.userAuthKey.id}`).reply(200, apiData.userAuthKey),
256+
nock(bgUrl).get(`/api/v2/tlnbtc/key/${apiData.nodeAuthKey.id}`).reply(200, apiData.nodeAuthKey),
257+
];
258+
259+
const signerMacaroon = nock(lightningSignerConfigs.fakeid.url)
260+
.post(`/v1/macaroon`)
261+
.reply(200, signerApiData.bakeMacaroon);
262+
263+
const wpWalletUpdateNock = nock(bgUrl).put(`/api/v2/tlnbtc/wallet/${apiData.wallet.id}`).reply(200);
264+
265+
const req = {
266+
bitgo: bitgo,
267+
body: { ...apiData.signerMacaroonRequestBody, addIpCaveatToMacaroon: false },
268+
params: {
269+
coin: 'tlnbtc',
270+
walletId: 'fakeid',
271+
},
272+
decoded: {
273+
coin: 'tlnbtc',
274+
walletId: 'fakeid',
275+
passphrase: apiData.signerMacaroonRequestBody.passphrase,
276+
addIpCaveatToMacaroon: false,
277+
},
278+
config: {
279+
lightningSignerFileSystemPath: 'lightningSignerFileSystemPath',
280+
},
281+
} as unknown as ExpressApiRouteRequest<'express.lightning.signerMacaroon', 'post'>;
282+
283+
const res = await handleCreateSignerMacaroon(req);
284+
decodeOrElse('SignerMacaroonResponse200', SignerMacaroonResponse[200], res, (_) => {
285+
throw new Error('Response did not match expected codec');
286+
});
287+
288+
wpWalletnock.done();
289+
wpKeychainNocks.forEach((s) => s.done());
290+
signerMacaroon.done();
291+
wpWalletUpdateNock.done();
292+
readFileStub.restore();
293+
});
166294

167295
it('should get signer wallet state', async () => {
168296
const readFileStub = sinon.stub(fs.promises, 'readFile').resolves(JSON.stringify(lightningSignerConfigs));

0 commit comments

Comments
 (0)