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

Merge branch 'OZG-2653_Usersynchronisation' of

https://git.ozg-sh.de/mgm/user-manager into OZG-2653_Usersynchronisation
parents afe85886 33825bed
No related branches found
No related tags found
No related merge requests found
......@@ -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();
......@@ -26,8 +26,8 @@ public class UserRepresentationTestFactory {
private static final String LDAP_ID_KEY = "LDAP_ID";
static final Map<String, List<String>> ATTRIBUTES = Map.of(LDAP_ID_KEY, List.of(EXTERNAL_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();
......
......@@ -3,14 +3,22 @@ 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.RealmResource;
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.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;
......@@ -31,7 +39,7 @@ class UserResourceMapperTest {
private UserResourceMapper mapper = Mappers.getMapper(UserResourceMapper.class);
@Mock
private KeycloakApiProperties apiProperties;
private KeycloakApiProperties properties;
@Mock
private RealmResource realm;
......@@ -41,11 +49,14 @@ class UserResourceMapperTest {
@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");
when(realm.getGroupByPath(GROUP_1_PATH))
.thenReturn(GroupRepresentationTestFactory.createByPathAndOrganisationEinheitId(GROUP_1_PATH, ORGANISATIONS_EINHEIT_ID_1));
when(properties.ldapIdKey()).thenReturn("LDAP_ID");
when(properties.organisationsEinheitIdKey()).thenReturn("organisationseinheitId");
when(properties.client()).thenReturn("sh-kiel-dev-goofy");
}
@Test
......@@ -122,7 +133,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
......@@ -20,15 +20,15 @@ import org.keycloak.representations.idm.UserSessionRepresentation;
import lombok.NoArgsConstructor;
@NoArgsConstructor
class StubUserResource implements UserResource {
class UserResourceStub implements UserResource {
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 UserResourceStub(Map<String, List<String>> attributes) {
userRepresentation = UserRepresentationTestFactory.createWithAttributes(attributes);
}
public StubUserResource(List<GroupRepresentation> groups) {
public UserResourceStub(List<GroupRepresentation> groups) {
this.groups = groups;
}
......@@ -211,7 +211,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;
......
......@@ -9,14 +9,14 @@ import org.keycloak.representations.idm.GroupRepresentation;
public class UserResourceTestFactory {
public static UserResource create() {
return new StubUserResource();
return new UserResourceStub();
}
public static UserResource createWithAttributes(Map<String, List<String>> attributes) {
return new StubUserResource(attributes);
return new UserResourceStub(attributes);
}
public static UserResource createWithGroups(List<GroupRepresentation> groups) {
return new StubUserResource(groups);
return new UserResourceStub(groups);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment