Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle cases where the site doesn't have ID_READER role #331

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
}
}