Skip to content

Commit f5b32a2

Browse files
Add tests
1 parent f07aa9f commit f5b32a2

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

src/main/java/com/uid2/admin/auth/AdminAuthMiddleware.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ private void validateAccessToken(RoutingContext rc, String accessToken) {
152152
}
153153
List<String> scopes = (List<String>) jwt.getClaims().get("scp");
154154
JsonObject serviceAccountDetails = new JsonObject();
155-
serviceAccountDetails.put("scope", (List<String>) jwt.getClaims().get("scp"));
155+
serviceAccountDetails.put("scope", scopes);
156156
serviceAccountDetails.put("client_id", jwt.getClaims().get("client_id"));
157157
rc.put("userDetails", serviceAccountDetails);
158158
if(isAuthorizedService(scopes)) {
@@ -177,7 +177,7 @@ private void validateIdToken(RoutingContext rc, String idToken) {
177177
}
178178
List<String> groups = (List<String>) jwt.getClaims().get("groups");
179179
JsonObject userDetails = new JsonObject();
180-
userDetails.put("groups", (List<String>) jwt.getClaims().get("groups"));
180+
userDetails.put("groups", groups);
181181
userDetails.put("email", jwt.getClaims().get("email"));
182182
userDetails.put("sub", jwt.getClaims().get("sub"));
183183
rc.put("userDetails", userDetails);

src/test/java/com/uid2/admin/auth/AdminAuthMiddlewareTest.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.vertx.core.Handler;
1010
import io.vertx.core.http.HttpServerRequest;
1111
import io.vertx.core.http.HttpServerResponse;
12+
import io.vertx.core.json.JsonArray;
1213
import io.vertx.core.json.JsonObject;
1314
import io.vertx.ext.auth.User;
1415
import io.vertx.ext.web.RoutingContext;
@@ -22,10 +23,11 @@
2223
import org.mockito.Mock;
2324
import org.mockito.MockitoAnnotations;
2425

25-
import java.util.List;
26-
import java.util.Map;
26+
import java.util.*;
27+
import java.util.stream.Collectors;
2728
import java.util.stream.Stream;
2829

30+
import static org.junit.jupiter.api.Assertions.assertEquals;
2931
import static org.junit.jupiter.api.Assertions.assertThrows;
3032
import static org.mockito.ArgumentMatchers.any;
3133
import 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
}

src/test/java/com/uid2/admin/job/sitesync/SiteSyncJobTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public void overridesPreviouslySyncedSitesWhenThereAreChanges() throws Exception
136136
assertAll(
137137
"overridesPreviouslySyncedSitesWhenThereAreChanges",
138138
() -> assertThat(reader.getAll()).containsExactly(updatedSite),
139-
() -> assertThat(reader.getMetadata().getLong("version")).isGreaterThan(oldVersion)
139+
() -> assertThat(reader.getMetadata().getLong("version")).isGreaterThanOrEqualTo(oldVersion)
140140
);
141141
}
142142

0 commit comments

Comments
 (0)