Skip to content
Snippets Groups Projects
Commit 0d1a6be8 authored by Lukas Malte Monnerjahn's avatar Lukas Malte Monnerjahn
Browse files

OZG-6897 map organisations einheit ids tests

parent a6ff720d
Branches
Tags
No related merge requests found
...@@ -92,6 +92,10 @@ public abstract class UserResourceMapper { ...@@ -92,6 +92,10 @@ public abstract class UserResourceMapper {
.toList(); .toList();
} }
private List<String> getOrganisationsEinheitIdsFromUser(UserResource userRes) {
return List.of();
}
private Map<String, List<String>> mapGroup(GroupRepresentation group) { private Map<String, List<String>> mapGroup(GroupRepresentation group) {
var groupFromRealm = realm.getGroupByPath(group.getPath()); var groupFromRealm = realm.getGroupByPath(group.getPath());
......
...@@ -43,4 +43,13 @@ class GroupRepresentationTestFactory { ...@@ -43,4 +43,13 @@ class GroupRepresentationTestFactory {
List.of(organisationEinheitId))); List.of(organisationEinheitId)));
return groupRepresentation; return groupRepresentation;
} }
public static GroupRepresentation createByPathAndOrganisationEinheitIds(String groupPath, List<String> organisationEinheitIds) {
var groupRepresentation = new GroupRepresentation();
groupRepresentation.setName(groupPath);
groupRepresentation.setPath(groupPath);
groupRepresentation.setAttributes(Map.of(UserResourceMapperTest.ORGANISATIONS_EINHEIT_ID_KEY,
organisationEinheitIds));
return groupRepresentation;
}
} }
...@@ -39,11 +39,13 @@ import org.keycloak.admin.client.resource.RoleMappingResource; ...@@ -39,11 +39,13 @@ import org.keycloak.admin.client.resource.RoleMappingResource;
import org.keycloak.admin.client.resource.RoleScopeResource; import org.keycloak.admin.client.resource.RoleScopeResource;
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.MappingsRepresentation; import org.keycloak.representations.idm.MappingsRepresentation;
import org.keycloak.representations.idm.RoleRepresentation; import org.keycloak.representations.idm.RoleRepresentation;
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;
import org.mockito.Spy;
import de.ozgcloud.user.keycloak.KeycloakApiProperties; import de.ozgcloud.user.keycloak.KeycloakApiProperties;
...@@ -55,9 +57,7 @@ class UserResourceMapperTest { ...@@ -55,9 +57,7 @@ class UserResourceMapperTest {
static final String GROUP_1_PATH = "/group1"; static final String GROUP_1_PATH = "/group1";
static final String GROUP_2_PATH = "/group2"; 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)); @Spy
static final Map<String, List<String>> ATTRIBUTES_2 = Map.of(ORGANISATIONS_EINHEIT_ID_KEY, List.of(ORGANISATIONS_EINHEIT_ID_2));
@InjectMocks @InjectMocks
UserResourceMapper mapper = Mappers.getMapper(UserResourceMapper.class); UserResourceMapper mapper = Mappers.getMapper(UserResourceMapper.class);
...@@ -67,6 +67,8 @@ class UserResourceMapperTest { ...@@ -67,6 +67,8 @@ class UserResourceMapperTest {
@Mock @Mock
RealmResource realm; RealmResource realm;
private UserResource userResource;
@DisplayName("To kop user") @DisplayName("To kop user")
@Nested @Nested
class TestToKopUser { class TestToKopUser {
...@@ -81,6 +83,8 @@ class UserResourceMapperTest { ...@@ -81,6 +83,8 @@ class UserResourceMapperTest {
when(properties.ldapIdKey()).thenReturn("LDAP_ID"); when(properties.ldapIdKey()).thenReturn("LDAP_ID");
when(properties.organisationsEinheitIdKey()).thenReturn("organisationseinheitId"); when(properties.organisationsEinheitIdKey()).thenReturn("organisationseinheitId");
when(properties.client()).thenReturn("alfa"); when(properties.client()).thenReturn("alfa");
userResource = UserResourceTestFactory.create();
} }
@Test @Test
...@@ -139,9 +143,9 @@ class UserResourceMapperTest { ...@@ -139,9 +143,9 @@ class UserResourceMapperTest {
@Test @Test
void shouldMapOrganisationsEinheitIds() { void shouldMapOrganisationsEinheitIds() {
var user = toKopUser(); toKopUser();
assertThat(user.getOrganisationsEinheitIds()).isNotEmpty().contains(ORGANISATIONS_EINHEIT_ID_1); verify(mapper).mapOrganisationsEinheitIds(userResource);
} }
@Test @Test
...@@ -190,7 +194,7 @@ class UserResourceMapperTest { ...@@ -190,7 +194,7 @@ class UserResourceMapperTest {
} }
private User toKopUser() { private User toKopUser() {
return toKopUser(UserResourceTestFactory.create()); return toKopUser(userResource);
} }
private User toKopUser(UserResource userResource) { private User toKopUser(UserResource userResource) {
...@@ -198,6 +202,64 @@ class UserResourceMapperTest { ...@@ -198,6 +202,64 @@ class UserResourceMapperTest {
} }
} }
@DisplayName("Map organisations einheit ids")
@Nested
class TestMapOrganisationsEinheitIds {
private UserResource userResource;
static final String ORGANISATIONS_EINHEIT_ID_3 = "6287";
private final GroupRepresentation group1 = GroupRepresentationTestFactory.createByPathAndOrganisationEinheitIds(
GROUP_1_PATH, List.of(ORGANISATIONS_EINHEIT_ID_1, ORGANISATIONS_EINHEIT_ID_3));
private final GroupRepresentation group2 = GroupRepresentationTestFactory.createByPathAndOrganisationEinheitId(
GROUP_2_PATH, ORGANISATIONS_EINHEIT_ID_2);
private final Map<String, List<String>> attributes = Map.of(
ORGANISATIONS_EINHEIT_ID_KEY, List.of(ORGANISATIONS_EINHEIT_ID_1, ORGANISATIONS_EINHEIT_ID_2));
@BeforeEach
void beforeEach() {
when(properties.organisationsEinheitIdKey()).thenReturn(ORGANISATIONS_EINHEIT_ID_KEY);
}
@Test
void shouldMapOrganisationsEinheitIdsFromSingleGroup() {
userResource = UserResourceTestFactory.createWithGroups(List.of(group1));
when(realm.getGroupByPath(GROUP_1_PATH)).thenReturn(group1);
var result = mapper.mapOrganisationsEinheitIds(userResource);
assertThat(result).containsExactlyInAnyOrder(ORGANISATIONS_EINHEIT_ID_1, ORGANISATIONS_EINHEIT_ID_3);
}
@Test
void shouldMapOrganisationsEinheitIdsFromGroups() {
userResource = UserResourceTestFactory.createWithGroups(List.of(group1, group2));
when(realm.getGroupByPath(GROUP_1_PATH)).thenReturn(group1);
when(realm.getGroupByPath(GROUP_2_PATH)).thenReturn(group2);
var result = mapper.mapOrganisationsEinheitIds(userResource);
assertThat(result).containsExactlyInAnyOrder(ORGANISATIONS_EINHEIT_ID_1, ORGANISATIONS_EINHEIT_ID_2, ORGANISATIONS_EINHEIT_ID_3);
}
@Test
void shouldMapOrganisationsEinheitIdsFromUser() {
userResource = UserResourceTestFactory.createWithAttributes(attributes);
var result = mapper.mapOrganisationsEinheitIds(userResource);
assertThat(result).containsExactlyInAnyOrder(ORGANISATIONS_EINHEIT_ID_1, ORGANISATIONS_EINHEIT_ID_2);
}
@Test
void shouldReturnEmptyIfNoOrganisationsEinheitIds() {
userResource = UserResourceTestFactory.create();
var result = mapper.mapOrganisationsEinheitIds(userResource);
assertThat(result).isEmpty();
}
}
@DisplayName("Get client roles") @DisplayName("Get client roles")
@Nested @Nested
class TestGetClientRoles { class TestGetClientRoles {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment