Skip to content

Commit

Permalink
Handle cases where the site doesn't have ID_READER role (#331)
Browse files Browse the repository at this point in the history
* Handle cases where the side doesn't have ID_READER role

* Update test names
  • Loading branch information
cYKatherine authored Aug 8, 2024
1 parent 385ea06 commit 13d7c0f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.uid2.admin.managers.KeysetManager;
import com.uid2.admin.vertx.ResponseUtil;
import com.uid2.shared.Const;
import com.uid2.shared.auth.KeysetSnapshot;
import com.uid2.shared.auth.Role;
import com.uid2.shared.model.ClientType;
import com.uid2.shared.model.SiteUtil;
Expand Down Expand Up @@ -180,7 +179,8 @@ private void handleListAllKeysetsRelated(RoutingContext rc) {

// Check if this site has any client key that has an ID_READER role
boolean isIdReaderRole = false;
for (LegacyClientKey c : this.clientKeyProvider.getAll()) {
List<LegacyClientKey> clientKeysForThisSite = this.clientKeyProvider.getAll().stream().filter(legacyClientKey -> legacyClientKey.getSiteId() == siteId).collect(Collectors.toList());
for (LegacyClientKey c : clientKeysForThisSite) {
if (c.getRoles().contains(Role.ID_READER)) {
isIdReaderRole = true;
}
Expand Down
43 changes: 42 additions & 1 deletion src/test/java/com/uid2/admin/vertx/SharingServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,7 @@ void RelatedKeysetSetsWithSameSiteId(Vertx vertx, VertxTestContext testContext)
}

@Test
void RelatedKeysetSetsWithAllowSiteNull(Vertx vertx, VertxTestContext testContext) {
void RelatedKeysetSetsWithIdReader(Vertx vertx, VertxTestContext testContext) {
fakeAuth(Role.MAINTAINER);

AdminKeyset adminKeyset1 = new AdminKeyset(3, 1, "test", Set.of(4), Instant.now().getEpochSecond(),true, true, new HashSet<>());
Expand Down Expand Up @@ -1404,4 +1404,45 @@ void RelatedKeysetSetsWithAllowSiteNull(Vertx vertx, VertxTestContext testContex
testContext.completeNow();
});
}

@Test
void RelatedKeysetSetsWithoutIdReader(Vertx vertx, VertxTestContext testContext) {
fakeAuth(Role.MAINTAINER);

AdminKeyset adminKeyset1 = new AdminKeyset(3, 1, "test", Set.of(4), Instant.now().getEpochSecond(), true, true, new HashSet<>());
AdminKeyset adminKeyset2 = new AdminKeyset(4, 2, "test", Set.of(5), Instant.now().getEpochSecond(), true, true, new HashSet<>());
AdminKeyset adminKeyset3 = new AdminKeyset(5, 3, "test", null, Instant.now().getEpochSecond(), true, true, new HashSet<>());

Map<Integer, AdminKeyset> keysets = new HashMap<Integer, AdminKeyset>() {{
put(3, adminKeyset1);
put(4, adminKeyset2);
put(5, adminKeyset3);
}};

setAdminKeysets(keysets);
mockSiteExistence(1, 2, 3, 4, 5, 8);
doReturn(new Site(8, "test-name", true, null)).when(siteProvider).getSite(8);
setClientKeys(
new ClientKeyServiceTest.LegacyClientBuilder()
.withRoles(new HashSet<>(Arrays.asList(Role.ID_READER)))
.withSiteId(3)
.build());


get(vertx, testContext, "/api/sharing/keysets/related?site_id=8", response -> {
assertEquals(200, response.statusCode());

Set<Integer> expectedKeysetIds = new HashSet<>(Arrays.asList(adminKeyset3.getKeysetId()));

Set<Integer> actualKeysetIds = new HashSet<>();
JsonArray responseArray = response.bodyAsJsonArray();
for (int i = 0; i < responseArray.size(); i++) {
JsonObject item = responseArray.getJsonObject(i);
int keysetId = item.getInteger("keyset_id");
actualKeysetIds.add(keysetId);
}
assertEquals(false, actualKeysetIds.containsAll(expectedKeysetIds));
testContext.completeNow();
});
}
}

0 comments on commit 13d7c0f

Please sign in to comment.