99import io .vertx .core .Handler ;
1010import io .vertx .core .http .HttpServerRequest ;
1111import io .vertx .core .http .HttpServerResponse ;
12+ import io .vertx .core .json .JsonArray ;
1213import io .vertx .core .json .JsonObject ;
1314import io .vertx .ext .auth .User ;
1415import io .vertx .ext .web .RoutingContext ;
2223import org .mockito .Mock ;
2324import org .mockito .MockitoAnnotations ;
2425
25- import java .util .List ;
26- import java .util .Map ;
26+ import java .util .* ;
27+ import java .util .stream . Collectors ;
2728import java .util .stream .Stream ;
2829
30+ import static org .junit .jupiter .api .Assertions .assertEquals ;
2931import static org .junit .jupiter .api .Assertions .assertThrows ;
3032import static org .mockito .ArgumentMatchers .any ;
3133import static org .mockito .ArgumentMatchers .eq ;
@@ -67,6 +69,20 @@ public void setup() {
6769 when (rc .response ()).thenReturn (response );
6870 when (rc .session ()).thenReturn (session );
6971
72+ Map <String , Object > contextData = new HashMap <>();
73+
74+ when (rc .put (anyString (), any ())).thenAnswer (invocation -> {
75+ String key = invocation .getArgument (0 );
76+ Object value = invocation .getArgument (1 );
77+ contextData .put (key , value );
78+ return rc ; // Return rc for chaining
79+ });
80+
81+ when (rc .get (anyString ())).thenAnswer (invocation -> {
82+ String key = invocation .getArgument (0 );
83+ return contextData .get (key );
84+ });
85+
7086 when (response .setStatusCode (anyInt ())).thenReturn (response );
7187 when (response .putHeader (anyString (), anyString ())).thenReturn (response );
7288 }
@@ -151,7 +167,7 @@ public void testIdToken_GoodTokenUnauthorized() throws JwtVerificationException
151167 handler .handle (rc );
152168
153169 verify (idTokenVerifier ).decode (eq ("testIdToken" ), any ());
154- verify (jwt , times (6 )).getClaims ();
170+ verify (jwt , times (5 )).getClaims ();
155171 verifyUnauthorized (false );
156172 }
157173
@@ -173,7 +189,7 @@ public void testIdToken_GoodTokenRealRoleUnauthorized(List<String> userOktaGroup
173189 handler .handle (rc );
174190
175191 verify (idTokenVerifier ).decode (eq ("testIdToken" ), any ());
176- verify (jwt , times (6 )).getClaims ();
192+ verify (jwt , times (5 )).getClaims ();
177193 verifyUnauthorized (false );
178194 }
179195
@@ -197,9 +213,13 @@ public void testIdToken_GoodTokenAuthorized(List<String> userOktaGroups, Role...
197213
198214 Handler <RoutingContext > handler = adminAuthMiddleware .handle (innerHandler , endpointRoles );
199215 handler .handle (rc );
200-
216+ JsonObject userDetails = rc .get ("userDetails" );
217+ Set <String > groups = userDetails .getJsonArray ("groups" ).stream ()
218+ .map (Object ::toString )
219+ .collect (Collectors .toSet ());
220+ assertEquals (new HashSet <>(userOktaGroups ), groups );
201221 verify (idTokenVerifier ).decode (eq ("testIdToken" ), any ());
202- verify (jwt , times (6 )).getClaims ();
222+ verify (jwt , times (5 )).getClaims ();
203223 verify (innerHandler ).handle (eq (rc ));
204224 }
205225
@@ -251,7 +271,7 @@ public void testAccessToken_GoodTokenUnauthorized(String customOktaScope, Role..
251271 handler .handle (rc );
252272
253273 verify (accessTokenVerifier ).decode (eq ("testAccessToken" ));
254- verify (jwt , times (5 )).getClaims ();
274+ verify (jwt , times (4 )).getClaims ();
255275 verifyUnauthorized (false );
256276 }
257277
@@ -274,7 +294,7 @@ public void testAccessToken_GoodTokenAuthorized(OktaCustomScope scope, Role allo
274294 handler .handle (rc );
275295
276296 verify (accessTokenVerifier ).decode (eq ("testAccessToken" ));
277- verify (jwt , times (5 )).getClaims ();
297+ verify (jwt , times (4 )).getClaims ();
278298 verify (innerHandler ).handle (eq (rc ));
279299 }
280300}
0 commit comments