From 94d375886fdd10b4e3bf0bf113ef8822f4d7a3e2 Mon Sep 17 00:00:00 2001 From: Mark True Date: Fri, 7 Apr 2017 09:26:22 -0400 Subject: [PATCH] Keycloak 4533 commit --- .../ldap/LDAPProvidersIntegrationTest.java | 39 ++++++++++++++++++- .../storage/ldap/LDAPTestUtils.java | 16 +++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/storage/ldap/LDAPProvidersIntegrationTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/storage/ldap/LDAPProvidersIntegrationTest.java index 0982d5d5e4..3f8e8545e1 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/storage/ldap/LDAPProvidersIntegrationTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/storage/ldap/LDAPProvidersIntegrationTest.java @@ -432,7 +432,7 @@ public class LDAPProvidersIntegrationTest { loginPage.open(); loginPage.clickRegister(); registerPage.assertCurrent(); - + // check existing username registerPage.register("firstName", "lastName", "email@mail.cz", "existing", "Password1", "Password1"); registerPage.assertCurrent(); @@ -443,7 +443,44 @@ public class LDAPProvidersIntegrationTest { registerPage.assertCurrent(); Assert.assertEquals("Email already exists.", registerPage.getError()); } + + + + // + // KEYCLOAK-4533 + // + @Test + public void testLDAPUserDeletionImport() { + + KeycloakSession session = keycloakRule.startSession(); + RealmModel appRealm = new RealmManager(session).getRealmByName("test"); + LDAPStorageProvider ldapProvider = LDAPTestUtils.getLdapProvider(session, ldapModel); + LDAPConfig config = ldapProvider.getLdapIdentityStore().getConfig(); + + // Make sure mary is gone + LDAPTestUtils.removeLDAPUserByUsername(ldapProvider, appRealm, config, "maryjane"); + + // Create the user in LDAP and register him + LDAPObject mary = LDAPTestUtils.addLDAPUser(ldapProvider, appRealm, "maryjane", "mary", "yram", "mj@testing.redhat.cz", null, "12398"); + LDAPTestUtils.updateLDAPPassword(ldapProvider, mary, "Password1"); + + try { + + // Log in and out of the user + loginSuccessAndLogout("maryjane", "Password1"); + + // Delete LDAP User + LDAPTestUtils.removeLDAPUserByUsername(ldapProvider, appRealm, config, "maryjane"); + + // Make sure the deletion took place. + List deletedUsers = session.users().searchForUser("mary yram", appRealm); + Assert.assertTrue(deletedUsers.isEmpty()); + + } finally { + keycloakRule.stopSession(session, false); + } + } @Test public void registerUserLdapSuccess() { loginPage.open(); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/storage/ldap/LDAPTestUtils.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/storage/ldap/LDAPTestUtils.java index f41a7bab74..6a906369bd 100644 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/storage/ldap/LDAPTestUtils.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/storage/ldap/LDAPTestUtils.java @@ -32,6 +32,7 @@ import org.keycloak.representations.idm.CredentialRepresentation; import org.keycloak.representations.idm.SynchronizationResultRepresentation; import org.keycloak.storage.UserStorageProvider; import org.keycloak.storage.ldap.LDAPStorageProvider; +import org.keycloak.storage.ldap.LDAPConfig; import org.keycloak.storage.ldap.LDAPUtils; import org.keycloak.storage.ldap.idm.model.LDAPObject; import org.keycloak.storage.ldap.idm.query.internal.LDAPQuery; @@ -263,7 +264,20 @@ public class LDAPTestUtils { ldapStore.remove(ldapUser); } } - + + public static void removeLDAPUserByUsername(LDAPStorageProvider ldapProvider, RealmModel realm, LDAPConfig config, String username) { + LDAPIdentityStore ldapStore = ldapProvider.getLdapIdentityStore(); + LDAPQuery ldapQuery = LDAPUtils.createQueryForUserSearch(ldapProvider, realm); + List allUsers = ldapQuery.getResultList(); + + // This is ugly, we are iterating over the entire set of ldap users and deleting the one where the username matches. TODO: Find a better way! + for (LDAPObject ldapUser : allUsers) { + if (username.equals(LDAPUtils.getUsername(ldapUser, config))) { + ldapStore.remove(ldapUser); + } + } + } + public static void removeAllLDAPRoles(KeycloakSession session, RealmModel appRealm, ComponentModel ldapModel, String mapperName) { ComponentModel mapperModel = getSubcomponentByName(appRealm, ldapModel, mapperName); LDAPStorageProvider ldapProvider = LDAPTestUtils.getLdapProvider(session, ldapModel);