Skip to content

Commit 5e1440e

Browse files
committed
test(auth): add test for getJwks helper
1 parent fdaeb6b commit 5e1440e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

test/testJwtAuthHandler.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const { expect } = require('chai');
2+
const sinon = require('sinon');
3+
const axios = require('axios');
4+
const { getJwks } = require('../src/service/passport/jwtUtils');
5+
6+
describe('getJwks', () => {
7+
it('should fetch JWKS keys from authority', async () => {
8+
const jwksResponse = { keys: [{ kid: 'test-key', kty: 'RSA', n: 'abc', e: 'AQAB' }] };
9+
10+
const getStub = sinon.stub(axios, 'get');
11+
getStub.onFirstCall().resolves({ data: { jwks_uri: 'https://mock.com/jwks' } });
12+
getStub.onSecondCall().resolves({ data: jwksResponse });
13+
14+
const keys = await getJwks('https://mock.com');
15+
expect(keys).to.deep.equal(jwksResponse.keys);
16+
17+
getStub.restore();
18+
});
19+
20+
it('should throw error if fetch fails', async () => {
21+
const stub = sinon.stub(axios, 'get').rejects(new Error('Network fail'));
22+
try {
23+
await getJwks('https://fail.com');
24+
} catch (err) {
25+
expect(err.message).to.equal('Failed to fetch JWKS');
26+
}
27+
stub.restore();
28+
});
29+
});

0 commit comments

Comments
 (0)