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

OZG-2653 Fixed Auslesen der OrganisationsEinheitIds aus den Gruppen

parent 2df50a8f
Branches
Tags
No related merge requests found
package de.itvsh.kop.user; package de.itvsh.kop.user;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.UserResource; import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.representations.idm.ClientMappingsRepresentation; import org.keycloak.representations.idm.ClientMappingsRepresentation;
import org.keycloak.representations.idm.GroupRepresentation;
import org.keycloak.representations.idm.RoleRepresentation; import org.keycloak.representations.idm.RoleRepresentation;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mapping; import org.mapstruct.Mapping;
...@@ -24,6 +27,9 @@ public abstract class UserResourceMapper { ...@@ -24,6 +27,9 @@ public abstract class UserResourceMapper {
@Inject @Inject
KeycloakApiProperties properties; KeycloakApiProperties properties;
@Inject
RealmResource realm;
@Mapping(target = "createdAt", expression = "java(mapCreatedAt(userRes))") @Mapping(target = "createdAt", expression = "java(mapCreatedAt(userRes))")
@Mapping(target = "email", expression = "java(mapEmail(userRes))") @Mapping(target = "email", expression = "java(mapEmail(userRes))")
@Mapping(target = "firstName", expression = "java(mapFirstName(userRes))") @Mapping(target = "firstName", expression = "java(mapFirstName(userRes))")
...@@ -42,13 +48,23 @@ public abstract class UserResourceMapper { ...@@ -42,13 +48,23 @@ public abstract class UserResourceMapper {
return createdAt != null ? new Date(createdAt) : new Date(); return createdAt != null ? new Date(createdAt) : new Date();
} }
List<String> mapOrganisationsEinheitIds(UserResource userRes) { Set<String> mapOrganisationsEinheitIds(UserResource userRes) {
return getOrganisationsEinheitIdsFromUserAttributes(userRes); var groups = userRes.groups();
var organisationsEinheitIds = getOrganisationsEinheitIdsFromGroups(groups);
return new HashSet<>(organisationsEinheitIds);
} }
private List<String> getOrganisationsEinheitIdsFromUserAttributes(UserResource userResource) { private List<String> getOrganisationsEinheitIdsFromGroups(List<GroupRepresentation> groups) {
var attributes = userResource.toRepresentation().getAttributes(); return groups.stream()
return attributes != null ? attributes.get(properties.organisationsEinheitIdKey()) : new ArrayList<>(); .map(group -> {
var groupFromRealm = realm.getGroupByPath(group.getPath());
return groupFromRealm != null ? groupFromRealm.getAttributes() : null;
})
.filter(Objects::nonNull)
.map(attributeMap -> attributeMap.get(properties.organisationsEinheitIdKey()))
.filter(Objects::nonNull)
.map(attributeValues -> attributeValues.get(0))
.toList();
} }
List<String> mapRoles(UserResource userRes) { List<String> mapRoles(UserResource userRes) {
......
package de.itvsh.kop.user;
import java.util.List;
import java.util.Map;
import org.keycloak.representations.idm.GroupRepresentation;
class GroupRepresentationTestFactory {
static final GroupRepresentation createGroup(String path) {
var group = new GroupRepresentation();
group.setPath(path);
return group;
}
public static GroupRepresentation createByPathAndOrganisationEinheitId(String groupPath, String organisationEinheitId) {
var groupRepresentation = new GroupRepresentation();
groupRepresentation.setName(groupPath);
groupRepresentation.setPath(groupPath);
groupRepresentation.setAttributes(Map.of(UserResourceMapperTest.ORGANISATIONS_EINHEIT_ID_KEY,
List.of(organisationEinheitId)));
return groupRepresentation;
}
}
...@@ -22,11 +22,16 @@ import lombok.NoArgsConstructor; ...@@ -22,11 +22,16 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor @NoArgsConstructor
class StubUserResource implements UserResource { class StubUserResource implements UserResource {
private UserRepresentation userRepresentation = UserRepresentationTestFactory.create(); private UserRepresentation userRepresentation = UserRepresentationTestFactory.create();
private List<GroupRepresentation> groups = List.of(GroupRepresentationTestFactory.createGroup(UserResourceMapperTest.GROUP_1_PATH));
public StubUserResource(Map<String, List<String>> attributes) { public StubUserResource(Map<String, List<String>> attributes) {
userRepresentation = UserRepresentationTestFactory.createWithAttributes(attributes); userRepresentation = UserRepresentationTestFactory.createWithAttributes(attributes);
} }
public StubUserResource(List<GroupRepresentation> groups) {
this.groups = groups;
}
@Override @Override
public UserRepresentation toRepresentation() { public UserRepresentation toRepresentation() {
return userRepresentation; return userRepresentation;
...@@ -44,13 +49,7 @@ class StubUserResource implements UserResource { ...@@ -44,13 +49,7 @@ class StubUserResource implements UserResource {
@Override @Override
public List<GroupRepresentation> groups() { public List<GroupRepresentation> groups() {
return List.of(createGroup()); return groups;
}
private GroupRepresentation createGroup() {
var group = new GroupRepresentation();
group.setAttributes(UserRepresentationTestFactory.ATTRIBUTES);
return group;
} }
@Override @Override
......
...@@ -24,12 +24,7 @@ public class UserRepresentationTestFactory { ...@@ -24,12 +24,7 @@ public class UserRepresentationTestFactory {
static final String ORGANSISATIONS_EINHEIT_ID = "0815"; static final String ORGANSISATIONS_EINHEIT_ID = "0815";
private static final String LDAP_ID_KEY = "LDAP_ID"; private static final String LDAP_ID_KEY = "LDAP_ID";
private static final String ORGANISATIONS_EINHEIT_ID_KEY = "organisationseinheitId"; static final Map<String, List<String>> ATTRIBUTES = Map.of(LDAP_ID_KEY, List.of(EXTERNAL_ID));
// CHECKME: Wie verhaelt es sich, wenn nur ein Wert kommt?
static final Map<String, List<String>> ATTRIBUTES = Map.of(
LDAP_ID_KEY, List.of(EXTERNAL_ID),
ORGANISATIONS_EINHEIT_ID_KEY, List.of(ORGANSISATIONS_EINHEIT_ID));
private static final String CLIENT_KEY = "sh-kiel-dev-goofy"; private static final String CLIENT_KEY = "sh-kiel-dev-goofy";
private static final Map<String, List<String>> CLIENT_ROLED = Map.of(CLIENT_KEY, List.of(ROLE)); private static final Map<String, List<String>> CLIENT_ROLED = Map.of(CLIENT_KEY, List.of(ROLE));
......
...@@ -3,11 +3,14 @@ package de.itvsh.kop.user; ...@@ -3,11 +3,14 @@ package de.itvsh.kop.user;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.GroupRepresentation;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
...@@ -15,12 +18,24 @@ import org.mockito.Mock; ...@@ -15,12 +18,24 @@ import org.mockito.Mock;
import de.itvsh.kop.user.keycloak.KeycloakApiProperties; import de.itvsh.kop.user.keycloak.KeycloakApiProperties;
class UserResourceMapperTest { class UserResourceMapperTest {
static final String ORGANISATIONS_EINHEIT_ID_KEY = "organisationseinheitId";
static final String ORGANISATIONS_EINHEIT_ID_1 = "0815";
static final String ORGANISATIONS_EINHEIT_ID_2 = "4711";
static final String GROUP_1_PATH = "/group1";
static final String GROUP_2_PATH = "/group2";
static final Map<String, List<String>> ATTRIBUTES_1 = Map.of(ORGANISATIONS_EINHEIT_ID_KEY, List.of(ORGANISATIONS_EINHEIT_ID_1));
static final Map<String, List<String>> ATTRIBUTES_2 = Map.of(ORGANISATIONS_EINHEIT_ID_KEY, List.of(ORGANISATIONS_EINHEIT_ID_2));
@InjectMocks @InjectMocks
private UserResourceMapper mapper = Mappers.getMapper(UserResourceMapper.class); private UserResourceMapper mapper = Mappers.getMapper(UserResourceMapper.class);
@Mock @Mock
private KeycloakApiProperties apiProperties; private KeycloakApiProperties apiProperties;
@Mock
private RealmResource realm;
@Nested @Nested
class TestMapping { class TestMapping {
...@@ -29,6 +44,8 @@ class UserResourceMapperTest { ...@@ -29,6 +44,8 @@ class UserResourceMapperTest {
when(apiProperties.ldapIdKey()).thenReturn("LDAP_ID"); when(apiProperties.ldapIdKey()).thenReturn("LDAP_ID");
when(apiProperties.organisationsEinheitIdKey()).thenReturn("organisationseinheitId"); when(apiProperties.organisationsEinheitIdKey()).thenReturn("organisationseinheitId");
when(apiProperties.client()).thenReturn("sh-kiel-dev-goofy"); when(apiProperties.client()).thenReturn("sh-kiel-dev-goofy");
when(realm.getGroupByPath(GROUP_1_PATH))
.thenReturn(GroupRepresentationTestFactory.createByPathAndOrganisationEinheitId(GROUP_1_PATH, ORGANISATIONS_EINHEIT_ID_1));
} }
@Test @Test
...@@ -84,7 +101,21 @@ class UserResourceMapperTest { ...@@ -84,7 +101,21 @@ class UserResourceMapperTest {
void shouldMapOrganisationsEinheitIds() { void shouldMapOrganisationsEinheitIds() {
User user = mapper.toKopUser(UserResourceTestFactory.create()); User user = mapper.toKopUser(UserResourceTestFactory.create());
assertThat(user.getOrganisationsEinheitIds()).isNotEmpty().contains(UserRepresentationTestFactory.ORGANSISATIONS_EINHEIT_ID); assertThat(user.getOrganisationsEinheitIds()).isNotEmpty().contains(ORGANISATIONS_EINHEIT_ID_1);
}
@Test
void shouldMapMultipleOrganisationsEinheitIds() {
GroupRepresentation groupRepresentation = GroupRepresentationTestFactory.createByPathAndOrganisationEinheitId(GROUP_2_PATH,
ORGANISATIONS_EINHEIT_ID_2);
when(realm.getGroupByPath(GROUP_2_PATH)).thenReturn(groupRepresentation);
User user = mapper.toKopUser(
UserResourceTestFactory.createWithGroups(List.of(GroupRepresentationTestFactory.createGroup(UserResourceMapperTest.GROUP_1_PATH),
GroupRepresentationTestFactory.createGroup(UserResourceMapperTest.GROUP_2_PATH))));
assertThat(user.getOrganisationsEinheitIds()).isNotEmpty().hasSize(2).contains(ORGANISATIONS_EINHEIT_ID_2);
} }
@Test @Test
......
...@@ -4,15 +4,19 @@ import java.util.List; ...@@ -4,15 +4,19 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.keycloak.admin.client.resource.UserResource; import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.representations.idm.GroupRepresentation;
public class UserResourceTestFactory { public class UserResourceTestFactory {
public static UserResource create() { public static UserResource create() {
return new StubUserResource(); return new StubUserResource();
} }
public static UserResource createWithAttributes(Map<String, List<String>> attributes) { public static UserResource createWithAttributes(Map<String, List<String>> attributes) {
return new StubUserResource(attributes); return new StubUserResource(attributes);
} }
public static UserResource createWithGroups(List<GroupRepresentation> groups) {
return new StubUserResource(groups);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment