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

Merge pull request 'OZG-5424 do not delete user if realm already deleted'...

Merge pull request 'OZG-5424 do not delete user if realm already deleted' (#23) from OZG-5424-check-realm-before-delete-uiser into master

Reviewed-on: https://git.ozg-sh.de/ozgcloud-devops/operator/pulls/23


Reviewed-by: default avatarOZGCloud <ozgcloud@mgm-tp.com>
parents e4b321d8 3c3bb416
No related merge requests found
...@@ -41,7 +41,7 @@ class KeycloakUserPreconditionService { ...@@ -41,7 +41,7 @@ class KeycloakUserPreconditionService {
public Optional<String> getPreconditionErrors(OzgCloudKeycloakUser user) { public Optional<String> getPreconditionErrors(OzgCloudKeycloakUser user) {
var namespace = user.getMetadata().getNamespace(); var namespace = user.getMetadata().getNamespace();
if (!keycloakGenericRemoteService.realmExists(namespace)) { if (!realmExists(namespace)) {
return Optional.of(String.format("Realm %s does not yet exist", namespace)); return Optional.of(String.format("Realm %s does not yet exist", namespace));
} }
...@@ -58,6 +58,10 @@ class KeycloakUserPreconditionService { ...@@ -58,6 +58,10 @@ class KeycloakUserPreconditionService {
return Optional.empty(); return Optional.empty();
} }
boolean realmExists(String realmName) {
return keycloakGenericRemoteService.realmExists(realmName);
}
Optional<String> clientsExists(OzgCloudKeycloakUser user, String realm) { Optional<String> clientsExists(OzgCloudKeycloakUser user, String realm) {
return user.getSpec().getKeycloakUser().getClientRoles().stream() return user.getSpec().getKeycloakUser().getClientRoles().stream()
.map(KeycloakUserSpecClientRole::getClientId) .map(KeycloakUserSpecClientRole::getClientId)
......
...@@ -88,6 +88,11 @@ public class KeycloakUserReconciler implements Reconciler<OzgCloudKeycloakUser>, ...@@ -88,6 +88,11 @@ public class KeycloakUserReconciler implements Reconciler<OzgCloudKeycloakUser>,
LOG.info("keep data"); LOG.info("keep data");
return DeleteControl.defaultDelete(); return DeleteControl.defaultDelete();
} }
if (!preconditionService.realmExists(user.getMetadata().getNamespace())) {
return DeleteControl.defaultDelete();
}
return deleteUser(user); return deleteUser(user);
} }
......
...@@ -44,6 +44,8 @@ import de.ozgcloud.operator.keycloak.KeycloakGenericRemoteService; ...@@ -44,6 +44,8 @@ import de.ozgcloud.operator.keycloak.KeycloakGenericRemoteService;
class KeycloakUserPreconditionServiceTest { class KeycloakUserPreconditionServiceTest {
private String REALM = OzgCloudKeycloakUserTestFactory.METADATA_NAMESPACE;
@Spy @Spy
@InjectMocks @InjectMocks
private KeycloakUserPreconditionService service; private KeycloakUserPreconditionService service;
...@@ -70,9 +72,18 @@ class KeycloakUserPreconditionServiceTest { ...@@ -70,9 +72,18 @@ class KeycloakUserPreconditionServiceTest {
verify(keycloakGenericRemoteService).realmExists(OzgCloudKeycloakUserTestFactory.METADATA_NAMESPACE); verify(keycloakGenericRemoteService).realmExists(OzgCloudKeycloakUserTestFactory.METADATA_NAMESPACE);
} }
@Test
void shouldReturnResponseFromRealmExists() {
when(service.realmExists(REALM)).thenReturn(true);
var response = service.getPreconditionErrors(OzgCloudKeycloakUserTestFactory.create());
assertThat(response).isPresent();
}
@Test @Test
void shouldReturnErrorIfRealmNotExists() { void shouldReturnErrorIfRealmNotExists() {
doReturn(false).when(keycloakGenericRemoteService).realmExists(anyString()); doReturn(false).when(service).realmExists(REALM);
var response = service.getPreconditionErrors(OzgCloudKeycloakUserTestFactory.create()); var response = service.getPreconditionErrors(OzgCloudKeycloakUserTestFactory.create());
...@@ -113,6 +124,26 @@ class KeycloakUserPreconditionServiceTest { ...@@ -113,6 +124,26 @@ class KeycloakUserPreconditionServiceTest {
} }
} }
@Nested
class TestRealmExists {
@Test
void shouleCallRemoteService() {
service.realmExists(REALM);
verify(keycloakGenericRemoteService).realmExists(REALM);
}
@Test
void shouldReturnResponseFromRemoteService() {
when(keycloakGenericRemoteService.realmExists(REALM)).thenReturn(true);
var response = service.realmExists(REALM);
assertThat(response).isTrue();
}
}
@Nested @Nested
class TestClientExists { class TestClientExists {
......
...@@ -45,6 +45,8 @@ import io.javaoperatorsdk.operator.api.reconciler.DeleteControl; ...@@ -45,6 +45,8 @@ import io.javaoperatorsdk.operator.api.reconciler.DeleteControl;
class KeycloakUserReconcilerTest { class KeycloakUserReconcilerTest {
private final String REALM = OzgCloudKeycloakUserTestFactory.METADATA_NAMESPACE;
@Spy @Spy
@InjectMocks @InjectMocks
private KeycloakUserReconciler reconciler; private KeycloakUserReconciler reconciler;
...@@ -134,6 +136,8 @@ class KeycloakUserReconcilerTest { ...@@ -134,6 +136,8 @@ class KeycloakUserReconcilerTest {
@Test @Test
void shouldCallDeleteUser() { void shouldCallDeleteUser() {
when(preconditionService.realmExists(REALM)).thenReturn(true);
reconciler.cleanup(user, null); reconciler.cleanup(user, null);
verify(reconciler).deleteUser(user); verify(reconciler).deleteUser(user);
...@@ -143,11 +147,37 @@ class KeycloakUserReconcilerTest { ...@@ -143,11 +147,37 @@ class KeycloakUserReconcilerTest {
void shouldReturnValueFromDeleteUser() { void shouldReturnValueFromDeleteUser() {
DeleteControl expected = DeleteControl.defaultDelete(); DeleteControl expected = DeleteControl.defaultDelete();
when(reconciler.deleteUser(user)).thenReturn(expected); when(reconciler.deleteUser(user)).thenReturn(expected);
when(preconditionService.realmExists(REALM)).thenReturn(true);
DeleteControl response = reconciler.cleanup(user, null); DeleteControl response = reconciler.cleanup(user, null);
assertThat(response).isEqualTo(expected); assertThat(response).isEqualTo(expected);
} }
@Test
void shouldCallRealmExists() {
reconciler.cleanup(user, null);
verify(preconditionService).realmExists(REALM);
}
@Test
void shouldDeleteUserIfRealmExists() {
when(preconditionService.realmExists(REALM)).thenReturn(true);
reconciler.cleanup(user, null);
verify(reconciler).deleteUser(user);
}
@Test
void shouldNotDeleteUserIfRealmNotExists() {
when(preconditionService.realmExists(REALM)).thenReturn(false);
reconciler.cleanup(user, null);
verify(reconciler, never()).deleteUser(user);
}
} }
@DisplayName("test delete") @DisplayName("test delete")
...@@ -172,6 +202,7 @@ class KeycloakUserReconcilerTest { ...@@ -172,6 +202,7 @@ class KeycloakUserReconcilerTest {
@Test @Test
void shouldRescheduleOnError() { void shouldRescheduleOnError() {
when(preconditionService.realmExists(REALM)).thenReturn(true);
doThrow(RuntimeException.class).when(service) doThrow(RuntimeException.class).when(service)
.deleteUser(user.getSpec(), OzgCloudKeycloakRealmTestFactory.METADATA_NAMESPACE); .deleteUser(user.getSpec(), OzgCloudKeycloakRealmTestFactory.METADATA_NAMESPACE);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment