Skip to content
Snippets Groups Projects
Commit f689723f authored by OZGCloud's avatar OZGCloud
Browse files

OZG-3961 - get client role by real clientId

parent 81bef49f
No related branches found
No related tags found
No related merge requests found
...@@ -37,12 +37,10 @@ public class KeycloakGenericRemoteService { ...@@ -37,12 +37,10 @@ public class KeycloakGenericRemoteService {
.anyMatch(group -> Objects.equals(groupName, group.getName())); .anyMatch(group -> Objects.equals(groupName, group.getName()));
} }
public Optional<RoleRepresentation> getClientRole(String roleName, String clientId, String realm) { public Optional<RoleRepresentation> getClientRole(String roleName, String realClientId, String realm) {
log.log(Level.INFO, "Get role " + roleName + " from client " + clientId + " in realm " + realm); log.log(Level.INFO, "Get role " + roleName + " from client with ID " + realClientId + " in realm " + realm);
return getByClientId(clientId, realm) return Optional.ofNullable(keycloak.realm(realm).clients().get(realClientId))
.map(ClientRepresentation::getId) .orElseThrow(() -> new KeycloakException("Client with ID " + realClientId + " for realm " + realm + " not found."))
.map(realClientId -> keycloak.realm(realm).clients().get(realClientId))
.orElseThrow(() -> new KeycloakException("Client with ID " + clientId + " not found."))
.roles() .roles()
.list() .list()
.stream().filter(role -> Objects.equals(roleName, role.getName())) .stream().filter(role -> Objects.equals(roleName, role.getName()))
......
...@@ -32,6 +32,7 @@ class KeycloakGenericRemoteServiceTest { ...@@ -32,6 +32,7 @@ class KeycloakGenericRemoteServiceTest {
private static final String REALM = "TestNamespace"; private static final String REALM = "TestNamespace";
private static final String CLIENT_ID = "TestClientID"; private static final String CLIENT_ID = "TestClientID";
private static final String REAL_CLIENT_ID = "RealTestClientID";
@Spy @Spy
@InjectMocks @InjectMocks
...@@ -143,10 +144,10 @@ class KeycloakGenericRemoteServiceTest { ...@@ -143,10 +144,10 @@ class KeycloakGenericRemoteServiceTest {
void init() { void init() {
when(keycloak.realm(REALM)).thenReturn(realmResource); when(keycloak.realm(REALM)).thenReturn(realmResource);
when(realmResource.clients()).thenReturn(clientsResource); when(realmResource.clients()).thenReturn(clientsResource);
when(clientsResource.get(CLIENT_ID)).thenReturn(clientResource); when(clientsResource.get(REAL_CLIENT_ID)).thenReturn(clientResource);
doReturn(Optional.of(clientRepresentation)).when(service).getByClientId(CLIENT_ID, REALM); // doReturn(Optional.of(clientRepresentation)).when(service).getByClientId(CLIENT_ID, REALM);
when(clientRepresentation.getId()).thenReturn(CLIENT_ID); // when(clientRepresentation.getId()).thenReturn(CLIENT_ID);
} }
@Test @Test
...@@ -155,17 +156,17 @@ class KeycloakGenericRemoteServiceTest { ...@@ -155,17 +156,17 @@ class KeycloakGenericRemoteServiceTest {
when(rolesResource.list()).thenReturn(List.of(roleRepresentation)); when(rolesResource.list()).thenReturn(List.of(roleRepresentation));
when(roleRepresentation.getName()).thenReturn(UserRepresentationTestFactory.ROLE1); when(roleRepresentation.getName()).thenReturn(UserRepresentationTestFactory.ROLE1);
Optional<RoleRepresentation> role = service.getClientRole(UserRepresentationTestFactory.ROLE1, CLIENT_ID, REALM); Optional<RoleRepresentation> role = service.getClientRole(UserRepresentationTestFactory.ROLE1, REAL_CLIENT_ID, REALM);
assertThat(role).isPresent().contains(roleRepresentation); assertThat(role).isPresent().contains(roleRepresentation);
} }
@Test @Test
void shouldThrowOnClientNotFound() { void shouldThrowOnClientNotFound() {
when(clientsResource.get(CLIENT_ID)).thenReturn(null); when(clientsResource.get(REAL_CLIENT_ID)).thenReturn(null);
assertThrows(KeycloakException.class, assertThrows(KeycloakException.class,
() -> service.getClientRole(UserRepresentationTestFactory.ROLE1, CLIENT_ID, REALM)); () -> service.getClientRole(UserRepresentationTestFactory.ROLE1, REAL_CLIENT_ID, REALM));
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment