@@ -111,3 +111,76 @@ describe('assignRoles', () => {
111
111
} ) ;
112
112
} ) ;
113
113
114
+ describe ( 'jwtAuthHandler' , ( ) => {
115
+ let req , res , next , jwtConfig , validVerifyResponse ;
116
+
117
+ beforeEach ( ( ) => {
118
+ req = { header : sinon . stub ( ) , isAuthenticated : sinon . stub ( ) , user : { } } ;
119
+ res = { status : sinon . stub ( ) . returnsThis ( ) , send : sinon . stub ( ) } ;
120
+ next = sinon . stub ( ) ;
121
+
122
+ jwtConfig = {
123
+ clientID : 'client-id' ,
124
+ authorityURL : 'https://accounts.google.com' ,
125
+ expectedAudience : 'expected-audience' ,
126
+ roleMapping : { 'admin' : { 'admin' : 'admin' } }
127
+ } ;
128
+
129
+ validVerifyResponse = {
130
+ header : { kid : '123' } ,
131
+ azp : 'client-id' ,
132
+ sub : 'user123' ,
133
+ admin : 'admin'
134
+ } ;
135
+ } ) ;
136
+
137
+ afterEach ( ( ) => {
138
+ sinon . restore ( ) ;
139
+ } ) ;
140
+
141
+ it ( 'should call next if user is authenticated' , async ( ) => {
142
+ req . isAuthenticated . returns ( true ) ;
143
+ await jwtAuthHandler ( ) ( req , res , next ) ;
144
+ expect ( next . calledOnce ) . to . be . true ;
145
+ } ) ;
146
+
147
+ it ( 'should return 401 if no token provided' , async ( ) => {
148
+ req . header . returns ( null ) ;
149
+ await jwtAuthHandler ( ) ( req , res , next ) ;
150
+
151
+ expect ( res . status . calledWith ( 401 ) ) . to . be . true ;
152
+ expect ( res . send . calledWith ( 'No token provided\n' ) ) . to . be . true ;
153
+ } ) ;
154
+
155
+ it ( 'should return 500 if authorityURL not configured' , async ( ) => {
156
+ req . header . returns ( 'Bearer fake-token' ) ;
157
+ jwtConfig . authorityURL = null ;
158
+ sinon . stub ( jwt , 'verify' ) . returns ( validVerifyResponse ) ;
159
+
160
+ await jwtAuthHandler ( jwtConfig ) ( req , res , next ) ;
161
+
162
+ expect ( res . status . calledWith ( 500 ) ) . to . be . true ;
163
+ expect ( res . send . calledWith ( 'OIDC authority URL is not configured\n' ) ) . to . be . true ;
164
+ } ) ;
165
+
166
+ it ( 'should return 500 if clientID not configured' , async ( ) => {
167
+ req . header . returns ( 'Bearer fake-token' ) ;
168
+ jwtConfig . clientID = null ;
169
+ sinon . stub ( jwt , 'verify' ) . returns ( validVerifyResponse ) ;
170
+
171
+ await jwtAuthHandler ( jwtConfig ) ( req , res , next ) ;
172
+
173
+ expect ( res . status . calledWith ( 500 ) ) . to . be . true ;
174
+ expect ( res . send . calledWith ( 'OIDC client ID is not configured\n' ) ) . to . be . true ;
175
+ } ) ;
176
+
177
+ it ( 'should return 401 if JWT validation fails' , async ( ) => {
178
+ req . header . returns ( 'Bearer fake-token' ) ;
179
+ sinon . stub ( jwt , 'verify' ) . throws ( new Error ( 'Invalid token' ) ) ;
180
+
181
+ await jwtAuthHandler ( jwtConfig ) ( req , res , next ) ;
182
+
183
+ expect ( res . status . calledWith ( 401 ) ) . to . be . true ;
184
+ expect ( res . send . calledWithMatch ( / J W T v a l i d a t i o n f a i l e d : / ) ) . to . be . true ;
185
+ } ) ;
186
+ } ) ;
0 commit comments