@@ -4,8 +4,8 @@ const axios = require('axios');
4
4
const jwt = require ( 'jsonwebtoken' ) ;
5
5
const { jwkToBuffer } = require ( 'jwk-to-pem' ) ;
6
6
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' ) ;
9
9
10
10
describe ( 'getJwks' , ( ) => {
11
11
it ( 'should fetch JWKS keys from authority' , async ( ) => {
@@ -73,3 +73,41 @@ describe('validateJwt', () => {
73
73
} ) ;
74
74
} ) ;
75
75
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