Newer
Older
* Copyright (C) 2022 Das Land Schleswig-Holstein vertreten durch den
* Ministerpräsidenten des Landes Schleswig-Holstein
* Staatskanzlei
* Abteilung Digitalisierung und zentrales IT-Management der Landesregierung
*
* Lizenziert unter der EUPL, Version 1.2 oder - sobald
* diese von der Europäischen Kommission genehmigt wurden -
* Folgeversionen der EUPL ("Lizenz");
* Sie dürfen dieses Werk ausschließlich gemäß
* dieser Lizenz nutzen.
* Eine Kopie der Lizenz finden Sie hier:
*
* https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* Sofern nicht durch anwendbare Rechtsvorschriften
* gefordert oder in schriftlicher Form vereinbart, wird
* die unter der Lizenz verbreitete Software "so wie sie
* ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
* ausdrücklich oder stillschweigend - verbreitet.
* Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen.
*/
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.RoleMappingResource;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.representations.idm.ClientMappingsRepresentation;
import org.keycloak.representations.idm.GroupRepresentation;
import org.keycloak.representations.idm.MappingsRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
import org.mapstruct.factory.Mappers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import de.ozgcloud.user.keycloak.KeycloakApiProperties;

OZGCloud
committed
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 String GROUP_3_PATH = "/group3";

OZGCloud
committed
UserResourceMapper mapper = Mappers.getMapper(UserResourceMapper.class);

OZGCloud
committed
KeycloakApiProperties properties;

OZGCloud
committed
RealmResource realm;
final Set<String> organisationsEinheitIds = Set.of(ORGANISATIONS_EINHEIT_ID_1);

OZGCloud
committed
@DisplayName("To kop user")

OZGCloud
committed
class TestToKopUser {
when(properties.ldapIdKey()).thenReturn("LDAP_ID");
doReturn(organisationsEinheitIds).when(mapper).mapOrganisationsEinheitIds(any());
}
@Test
void shouldMapToUser() {

OZGCloud
committed
var user = toKopUser();
assertThat(user).isNotNull();
}
@Test
void shouldMapEmail() {

OZGCloud
committed
var user = toKopUser();
assertThat(user.getEmail()).isEqualTo(UserRepresentationTestFactory.EMAIL);
}

OZGCloud
committed
@DisplayName("externalId")
@Nested
class TestMapExternalId {

OZGCloud
committed
@Test
void shouldMap() {
var user = toKopUser();

OZGCloud
committed
assertThat(user.getExternalId()).isEqualTo(UserRepresentationTestFactory.EXTERNAL_ID);
}

OZGCloud
committed
@Test
void shouldMapFallbackOnEmptyAttributes() {
var user = toKopUser(UserResourceTestFactory.createWithAttributes(Collections.emptyMap()));
assertThat(user.getExternalId()).isEqualTo(UserRepresentationTestFactory.EXTERNAL_ID_FALLBACK);
}
@Test
void shouldMapFirstName() {

OZGCloud
committed
var user = toKopUser();
assertThat(user.getFirstName()).isEqualTo(UserRepresentationTestFactory.FIRST_NAME);
}
@Test
void shouldMapLastName() {

OZGCloud
committed
var user = toKopUser();
assertThat(user.getLastName()).isEqualTo(UserRepresentationTestFactory.LAST_NAME);
}
@Test
void shouldMapUserName() {

OZGCloud
committed
var user = toKopUser();
assertThat(user.getUsername()).isEqualTo(UserRepresentationTestFactory.USER_NAME);
}
@Test
void shouldMapOrganisationsEinheitIds() {
assertThat(result.getOrganisationsEinheitIds()).hasSameElementsAs(organisationsEinheitIds);

OZGCloud
committed
var user = toKopUser();
assertThat(user.getRoles()).isNotEmpty().contains(UserRepresentationTestFactory.ROLE_NAME);

OZGCloud
committed
@Test
void shouldMapKeycloakUserId() {
var user = toKopUser();
assertThat(user.getKeycloakUserId()).isEqualTo(UserRepresentationTestFactory.EXTERNAL_ID_FALLBACK);
}
@Test
void shouldMapFullName() {
var user = toKopUser();
assertThat(user.getFullName()).isEqualTo(UserRepresentationTestFactory.FIRST_NAME + " " + UserRepresentationTestFactory.LAST_NAME);
}
@Test
void shouldMapFullNameReversed() {
var user = toKopUser();
assertThat(user.getFullNameReversed()).isEqualTo(UserRepresentationTestFactory.LAST_NAME + " " + UserRepresentationTestFactory.FIRST_NAME);
}

OZGCloud
committed
private User toKopUser() {
return toKopUser(UserResourceTestFactory.create());

OZGCloud
committed
}
private User toKopUser(UserResource userResource) {
return mapper.toKopUser(userResource);
}
@DisplayName("Map organisations einheit ids")
@Nested
class TestMapOrganisationsEinheitIds {
static final String ORGANISATIONS_EINHEIT_ID_3 = "6287";
private final GroupRepresentation group1 = GroupRepresentationTestFactory.createByPathAndOrganisationEinheitIds(
GROUP_1_PATH, ORGANISATIONS_EINHEIT_ID_1, ORGANISATIONS_EINHEIT_ID_3);
private final GroupRepresentation group2 = GroupRepresentationTestFactory.createByPathAndOrganisationEinheitIds(
GROUP_2_PATH, ORGANISATIONS_EINHEIT_ID_2);
private final GroupRepresentation group3 = GroupRepresentationTestFactory.createWithPathAndAttributes(
GROUP_3_PATH, Map.of());
@BeforeEach
void beforeEach() {
when(properties.organisationsEinheitIdKey()).thenReturn(ORGANISATIONS_EINHEIT_ID_KEY);
}
@DisplayName("should map no organisations einheit id if group has none")
@Test
void shouldMapNoOrganisationsEinheitIdIfGroupHasNone() {
when(realm.getGroupByPath(GROUP_3_PATH)).thenReturn(group3);
var userResource = UserResourceTestFactory.createWithGroups(group3);
var result = mapper.mapOrganisationsEinheitIds(userResource);
assertThat(result).isEmpty();
}
@Test
void shouldMapOrganisationsEinheitIdsFromSingleGroup() {
var userResource = UserResourceTestFactory.createWithGroups(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() {
var userResource = UserResourceTestFactory.createWithGroups(group1, group2, group3);
when(realm.getGroupByPath(GROUP_1_PATH)).thenReturn(group1);
when(realm.getGroupByPath(GROUP_2_PATH)).thenReturn(group2);
when(realm.getGroupByPath(GROUP_3_PATH)).thenReturn(group3);
var result = mapper.mapOrganisationsEinheitIds(userResource);
assertThat(result).containsExactlyInAnyOrder(
ORGANISATIONS_EINHEIT_ID_1,
ORGANISATIONS_EINHEIT_ID_2,
ORGANISATIONS_EINHEIT_ID_3
);
}
@Test
void shouldMapOrganisationsEinheitIdsFromUser() {
var attributes = Map.of(ORGANISATIONS_EINHEIT_ID_KEY, List.of(ORGANISATIONS_EINHEIT_ID_1, ORGANISATIONS_EINHEIT_ID_2));
var userResource = UserResourceTestFactory.createWithAttributes(attributes);
var result = mapper.mapOrganisationsEinheitIds(userResource);
assertThat(result).containsExactlyInAnyOrder(ORGANISATIONS_EINHEIT_ID_1, ORGANISATIONS_EINHEIT_ID_2);
}
@Test
void shouldReturnEmptyIfNoOrganisationsEinheitIds() {
var userResource = UserResourceTestFactory.create();
var result = mapper.mapOrganisationsEinheitIds(userResource);
assertThat(result).isEmpty();
}
}
@DisplayName("Get client roles")
@Nested
class TestGetClientRoles {
@Mock

OZGCloud
committed
UserResource userResource;

OZGCloud
committed
RoleMappingResource roleMappingResource;

OZGCloud
committed
MappingsRepresentation mappingsRepresentation;

OZGCloud
committed
Map<String, ClientMappingsRepresentation> clientMappingsRepresentation;

OZGCloud
committed
ClientMappingsRepresentation clientMappingRepresentation;
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
@BeforeEach
void init() {
when(userResource.roles()).thenReturn(roleMappingResource);
when(roleMappingResource.getAll()).thenReturn(mappingsRepresentation);
}
@DisplayName("on existing roles")
@Nested
class TestOnAssignedRoles {
@BeforeEach
void init() {
when(properties.client()).thenReturn(UserRepresentationTestFactory.CLIENT_KEY);
when(mappingsRepresentation.getClientMappings()).thenReturn(clientMappingsRepresentation);
when(clientMappingsRepresentation.containsKey(UserRepresentationTestFactory.CLIENT_KEY)).thenReturn(true);
when(clientMappingsRepresentation.get(UserRepresentationTestFactory.CLIENT_KEY)).thenReturn(clientMappingRepresentation);
when(clientMappingRepresentation.getMappings()).thenReturn(List.of(createRoleRepresentation()));
}
private RoleRepresentation createRoleRepresentation() {
var roleRepresentation = new RoleRepresentation();
roleRepresentation.setName(UserRepresentationTestFactory.ROLE_NAME);
return roleRepresentation;
}
@Test
void shouldReturnRolesIfExists() {
var roles = mapper.mapRoles(userResource);
assertThat(roles).isNotEmpty();
assertThat(roles.get(0)).isEqualTo(UserRepresentationTestFactory.ROLE_NAME);
}
}
@Nested
class TestOnNonExistingClient {
@BeforeEach
void init() {
when(properties.client()).thenReturn(UserRepresentationTestFactory.CLIENT_KEY);
when(mappingsRepresentation.getClientMappings()).thenReturn(Collections.emptyMap());
}
@Test
void shouldReturnEmptyListIfNoRolesAttached() {
var roles = mapper.mapRoles(userResource);
assertThat(roles).isEmpty();
}
}
@Nested
class TestNullClientMappings {
@BeforeEach
void init() {
when(mappingsRepresentation.getClientMappings()).thenReturn(null);
}
@Test
void shouldReturnEmptyListIfNoRolesAttached() {
var roles = mapper.mapRoles(userResource);
assertThat(roles).isEmpty();
}