Skip to content

Commit 407cb85

Browse files
committed
test(auth): add test for assignRoles helper function
1 parent dad5beb commit 407cb85

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

test/testJwtAuthHandler.test.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const axios = require('axios');
44
const jwt = require('jsonwebtoken');
55
const { jwkToBuffer } = require('jwk-to-pem');
66

7-
const { getJwks, validateJwt } = require('../src/service/passport/jwtUtils');
8-
const { jwtAuthHandler } = require('../src/service/passport/jwtAuthHandler');
7+
const { assignRoles, getJwks, validateJwt } = require('../src/service/passport/jwtUtils');
8+
const jwtAuthHandler = require('../src/service/passport/jwtAuthHandler');
99

1010
describe('getJwks', () => {
1111
it('should fetch JWKS keys from authority', async () => {
@@ -73,3 +73,41 @@ describe('validateJwt', () => {
7373
});
7474
});
7575

76+
describe('assignRoles', () => {
77+
it('should assign admin role based on claim', () => {
78+
const user = { username: 'admin-user' };
79+
const payload = { admin: 'admin' };
80+
const mapping = { admin: { 'admin': 'admin' } };
81+
82+
assignRoles(mapping, payload, user);
83+
expect(user.admin).to.be.true;
84+
});
85+
86+
it('should assign multiple roles based on claims', () => {
87+
const user = { username: 'multi-role-user' };
88+
const payload = { 'custom-claim-admin': 'custom-value', 'editor': 'editor' };
89+
const mapping = { admin: { 'custom-claim-admin': 'custom-value' }, editor: { 'editor': 'editor' } };
90+
91+
assignRoles(mapping, payload, user);
92+
expect(user.admin).to.be.true;
93+
expect(user.editor).to.be.true;
94+
});
95+
96+
it('should not assign role if claim mismatch', () => {
97+
const user = { username: 'basic-user' };
98+
const payload = { admin: 'nope' };
99+
const mapping = { admin: { admin: 'admin' } };
100+
101+
assignRoles(mapping, payload, user);
102+
expect(user.admin).to.be.undefined;
103+
});
104+
105+
it('should not assign role if no mapping provided', () => {
106+
const user = { username: 'no-role-user' };
107+
const payload = { admin: 'admin' };
108+
109+
assignRoles(null, payload, user);
110+
expect(user.admin).to.be.undefined;
111+
});
112+
});
113+

0 commit comments

Comments
 (0)