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

OZG-2652 OZG-2855 readd Test

parent 2df50a8f
No related branches found
No related tags found
No related merge requests found
......@@ -212,7 +212,7 @@ class StubUserResource implements UserResource {
public MappingsRepresentation getAll() {
var rep = new MappingsRepresentation();
var clientMapRep = new ClientMappingsRepresentation();
var roleRep = new RoleRepresentation(UserRepresentationTestFactory.ROLE, "Test role", false);
var roleRep = new RoleRepresentation(UserRepresentationTestFactory.ROLE_NAME, "Test role", false);
clientMapRep.setMappings(List.of(roleRep));
rep.setClientMappings(Map.of("sh-kiel-dev-goofy", clientMapRep));
return rep;
......
......@@ -17,7 +17,7 @@ public class UserRepresentationTestFactory {
static final String EXTERNAL_ID = "external-id-1-ldap";
static final String EXTERNAL_ID_FALLBACK = "external-id-2-keykloak";
static final String ROLE = "VERWALTUNG_USER";
static final String ROLE_NAME = "VERWALTUNG_USER";
private static final long CREATED = Instant.now().toEpochMilli();
......@@ -31,8 +31,8 @@ public class UserRepresentationTestFactory {
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 Map<String, List<String>> CLIENT_ROLED = Map.of(CLIENT_KEY, List.of(ROLE));
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_NAME));
static UserRepresentation createWithAttributes(Map<String, List<String>> attributes) {
var user = create();
......
package de.itvsh.kop.user;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.resource.RoleMappingResource;
import org.keycloak.admin.client.resource.RoleScopeResource;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.representations.idm.ClientMappingsRepresentation;
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 static org.assertj.core.api.Assertions.*;
import de.itvsh.kop.user.keycloak.KeycloakApiProperties;
class UserResourceMapperTest {
@InjectMocks
private UserResourceMapper mapper = Mappers.getMapper(UserResourceMapper.class);
@Mock
private KeycloakApiProperties apiProperties;
private KeycloakApiProperties properties;
@Nested
class TestMapping {
@BeforeEach
void init() {
when(apiProperties.ldapIdKey()).thenReturn("LDAP_ID");
when(apiProperties.organisationsEinheitIdKey()).thenReturn("organisationseinheitId");
when(apiProperties.client()).thenReturn("sh-kiel-dev-goofy");
when(properties.ldapIdKey()).thenReturn("LDAP_ID");
when(properties.organisationsEinheitIdKey()).thenReturn("organisationseinheitId");
when(properties.client()).thenReturn("sh-kiel-dev-goofy");
}
@Test
......@@ -91,7 +102,95 @@ class UserResourceMapperTest {
void shouldMapRoles() {
User user = mapper.toKopUser(UserResourceTestFactory.create());
assertThat(user.getRoles()).isNotEmpty().contains(UserRepresentationTestFactory.ROLE);
assertThat(user.getRoles()).isNotEmpty().contains(UserRepresentationTestFactory.ROLE_NAME);
}
}
@DisplayName("Get client roles")
@Nested
class TestGetClientRoles {
@Mock
private UserResource userResource;
@Mock
private RoleMappingResource roleMappingResource;
@Mock
private RoleScopeResource roleScopeResource;
@Mock
private MappingsRepresentation mappingsRepresentation;
@Mock
private Map<String, ClientMappingsRepresentation> clientMappingsRepresentation;
@Mock
private ClientMappingsRepresentation clientMappingRepresentation;
@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();
}
}
}
}
\ 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