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

OZG-6867 OZG-6895 Test keycloak api for accessing groups

parent 92a9bfb4
No related branches found
No related tags found
No related merge requests found
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
<publishImage>false</publishImage> <publishImage>false</publishImage>
<build.number>SET_BY_JENKINS</build.number> <build.number>SET_BY_JENKINS</build.number>
<spring-cloud-config-server.version>4.1.2</spring-cloud-config-server.version> <spring-cloud-config-server.version>4.1.2</spring-cloud-config-server.version>
<testcontainers-keycloak.version>3.2.0</testcontainers-keycloak.version> <testcontainers-keycloak.version>3.4.0</testcontainers-keycloak.version>
<mongock.version>5.4.0</mongock.version> <mongock.version>5.4.0</mongock.version>
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version> <lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
<mapstruct-processor.version>${mapstruct.version}</mapstruct-processor.version> <mapstruct-processor.version>${mapstruct.version}</mapstruct-processor.version>
......
package de.ozgcloud.admin.keycloak; package de.ozgcloud.admin.keycloak;
import java.util.List; import java.util.List;
import java.util.Map;
import org.keycloak.representations.idm.GroupRepresentation; import org.keycloak.representations.idm.GroupRepresentation;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.NullValueCheckStrategy; import org.mapstruct.NullValueCheckStrategy;
@Mapper(nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS) @Mapper(nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
...@@ -11,6 +13,10 @@ interface GroupMapper { ...@@ -11,6 +13,10 @@ interface GroupMapper {
List<Group> fromGroupRepresentations(List<GroupRepresentation> groupRepresentations); List<Group> fromGroupRepresentations(List<GroupRepresentation> groupRepresentations);
@Mapping(target = "organisationsEinheitId", source = "attributes")
Group fromGroupRepresentation(GroupRepresentation groupRepresentation); Group fromGroupRepresentation(GroupRepresentation groupRepresentation);
default String getOrganisationsEinheitId(Map<String, List<String>> attributes) {
return "dummy";
}
} }
...@@ -35,8 +35,6 @@ import lombok.RequiredArgsConstructor; ...@@ -35,8 +35,6 @@ import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor @RequiredArgsConstructor
public class KeycloakConfiguration { public class KeycloakConfiguration {
private static final String CLIENT = "admin-cli";
private final KeycloakApiProperties keycloakApiProperties; private final KeycloakApiProperties keycloakApiProperties;
@Bean @Bean
...@@ -53,7 +51,7 @@ public class KeycloakConfiguration { ...@@ -53,7 +51,7 @@ public class KeycloakConfiguration {
return KeycloakBuilder.builder() return KeycloakBuilder.builder()
.serverUrl(keycloakApiProperties.getUrl()) .serverUrl(keycloakApiProperties.getUrl())
.realm(keycloakApiProperties.getRealm()) .realm(keycloakApiProperties.getRealm())
.clientId(CLIENT) .clientId(keycloakApiProperties.getClient())
.username(keycloakApiProperties.getUser()) .username(keycloakApiProperties.getUser())
.password(keycloakApiProperties.getPassword()) .password(keycloakApiProperties.getPassword())
.build(); .build();
......
package de.ozgcloud.admin.keycloak; package de.ozgcloud.admin.keycloak;
import java.util.List;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.keycloak.admin.client.resource.RealmResource; import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.GroupRepresentation;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
...@@ -17,4 +19,13 @@ public class KeycloakRemoteService { ...@@ -17,4 +19,13 @@ public class KeycloakRemoteService {
// realmResource.groups().groups(); // realmResource.groups().groups();
return Stream.empty(); return Stream.empty();
} }
// TODO: only for research
public List<GroupRepresentation> getGroupRepresentations() {
return realmResource.groups().groups();
}
public GroupRepresentation getGroupRepresentation(String id) {
return realmResource.groups().group(id).toRepresentation();
}
} }
...@@ -5,4 +5,4 @@ ozgcloud: ...@@ -5,4 +5,4 @@ ozgcloud:
resource: admin resource: admin
keycloak: keycloak:
api: api:
url: ${auth-server-url} url: ${ozgcloud.oauth2.auth-server-url}
\ No newline at end of file \ No newline at end of file
...@@ -78,7 +78,7 @@ ozgcloud: ...@@ -78,7 +78,7 @@ ozgcloud:
api: api:
ldap-id-key: LDAP_ID ldap-id-key: LDAP_ID
organisations-einheit-id-key: organisationseinheitId organisations-einheit-id-key: organisationseinheitId
user: userManagerApiUser user: administrationApiUser
password: userManagerApiUser password: administrationApiUser
realm: by-kiel-dev realm: by-kiel-dev
client: alfa client: admin-cli
\ No newline at end of file \ No newline at end of file
package de.ozgcloud.admin.common;
import java.time.Duration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.testcontainers.containers.wait.strategy.Wait;
import dasniko.testcontainers.keycloak.KeycloakContainer;
import lombok.extern.log4j.Log4j2;
@Log4j2
public class KeycloakInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private static final String AUTH_SERVER_URL = "ozgcloud.oauth2.auth-server-url";
private static KeycloakContainer keycloakContainer;
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
initContainer();
setProperties(applicationContext);
}
@SuppressWarnings("resource")
private void initContainer() {
if (keycloakContainer == null) {
log.info("Creating Keycloak-container...");
keycloakContainer = new KeycloakContainer().withRealmImportFile("keycloak/realm-export.json").withVerboseOutput();
}
if (!keycloakContainer.isRunning()) {
log.info("Starting Keycloak-container...");
keycloakContainer.setWaitStrategy(
Wait.forLogMessage(".*message\":\"started.*", 1).withStartupTimeout(Duration.ofMinutes(3)));
keycloakContainer.start();
log.info("Keycloak-container started");
}
}
private void setProperties(ConfigurableApplicationContext applicationContext) {
log.info("Keycloak URL: {}", keycloakContainer.getAuthServerUrl());
TestPropertyValues.of(AUTH_SERVER_URL + "=" + keycloakContainer.getAuthServerUrl()).applyTo(applicationContext);
}
}
package de.ozgcloud.admin.keycloak;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import de.ozgcloud.admin.common.KeycloakInitializer;
import de.ozgcloud.common.test.ITCase;
@ITCase
@ContextConfiguration(initializers = KeycloakInitializer.class)
class KeycloakRemoteServiceITCase {
@Autowired
private KeycloakRemoteService service;
@Nested
class TestGetGroupRepresentations {
@Test
void shouldReturnGroupRepresentations() {
var result = service.getGroupRepresentations();
assertThat(result).hasSize(6);
}
@Test
void shouldReturnAttributes() {
var result = service.getGroupRepresentation("94f0562a-cb66-41a5-96c9-a984f0bee3ec");
assertThat(result.getAttributes().get("organisationseinheitId").getFirst()).isEqualTo("248240886");
}
}
}
...@@ -8,4 +8,4 @@ ozgcloud: ...@@ -8,4 +8,4 @@ ozgcloud:
resource: admin resource: admin
keycloak: keycloak:
api: api:
url: ${auth-server-url} url: ${ozgcloud.oauth2.auth-server-url}
...@@ -44,6 +44,86 @@ ...@@ -44,6 +44,86 @@
"quickLoginCheckMilliSeconds": 1000, "quickLoginCheckMilliSeconds": 1000,
"maxDeltaTimeSeconds": 43200, "maxDeltaTimeSeconds": 43200,
"failureFactor": 30, "failureFactor": 30,
"groups": [
{
"id": "94f0562a-cb66-41a5-96c9-a984f0bee3ec",
"name": "Bauamt",
"path": "/Bauamt",
"subGroups": [],
"attributes": {
"organisationseinheitId": [
"248240886"
]
},
"realmRoles": [],
"clientRoles": {}
},
{
"id": "bfd284be-9d30-4e9a-82d2-daf9ac6593b6",
"name": "Denkmalpflege",
"path": "/Denkmalpflege",
"subGroups": [],
"attributes": {
"organisationseinheitId": [
"9093371"
]
},
"realmRoles": [],
"clientRoles": {}
},
{
"id": "e80b596b-7559-4437-8349-4732ac567d15",
"name": "Fundstelle",
"path": "/Fundstelle",
"subGroups": [],
"attributes": {
"organisationseinheitId": [
"10363455"
]
},
"realmRoles": [],
"clientRoles": {}
},
{
"id": "ffad390f-00c0-4459-9512-eb1f4be3631e",
"name": "Landesamt für Denkmalpflege",
"path": "/Landesamt für Denkmalpflege",
"subGroups": [],
"attributes": {
"organisationseinheitId": [
"9093371"
]
},
"realmRoles": [],
"clientRoles": {}
},
{
"id": "155b4752-bbc3-4c6a-afa7-7769f1b2ea8a",
"name": "Ordnungsamt",
"path": "/Ordnungsamt",
"subGroups": [],
"attributes": {
"organisationseinheitId": [
"9030229"
]
},
"realmRoles": [],
"clientRoles": {}
},
{
"id": "53c11c42-9f8e-4cbc-b6da-1c1ff2d46187",
"name": "Wirtschaftsförderung",
"path": "/Wirtschaftsförderung",
"subGroups": [],
"attributes": {
"organisationseinheitId": [
"9797773"
]
},
"realmRoles": [],
"clientRoles": {}
}
],
"defaultRole": { "defaultRole": {
"id": "dd27b699-836d-4ed8-9111-ee34acbaf5ce", "id": "dd27b699-836d-4ed8-9111-ee34acbaf5ce",
"name": "default-roles-by-kiel-dev", "name": "default-roles-by-kiel-dev",
...@@ -2037,16 +2117,20 @@ ...@@ -2037,16 +2117,20 @@
}, },
"users": [ "users": [
{ {
"username": "admin-test", "id": "b46def26-a599-4940-a32f-e070c478750d",
"username": "administrationApiUser",
"firstName": "Vorname", "firstName": "Vorname",
"lastName": "Nachname", "lastName": "Nachname",
"enabled": true, "enabled": true,
"credentials": [ "credentials": [
{ {
"type": "password", "type": "password",
"value": "Password" "value": "administrationApiUser"
}
],
"clientRoles" : {
"realm-management" : [ "view-users" ]
} }
]
} }
] ]
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment