diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/forms/FederationProvidersIntegrationTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/forms/FederationProvidersIntegrationTest.java index 485aea13f1..69a8724389 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/forms/FederationProvidersIntegrationTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/forms/FederationProvidersIntegrationTest.java @@ -239,83 +239,98 @@ public class FederationProvidersIntegrationTest { Assert.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType()); KeycloakSession session = keycloakRule.startSession(); - RealmModel appRealm = session.realms().getRealmByName("test"); - UserModel user = session.users().getUserByUsername("registerUserSuccess2", appRealm); - Assert.assertNotNull(user); - Assert.assertNotNull(user.getFederationLink()); - Assert.assertEquals(user.getFederationLink(), ldapModel.getId()); - keycloakRule.stopSession(session, false); + try { + RealmModel appRealm = session.realms().getRealmByName("test"); + UserModel user = session.users().getUserByUsername("registerUserSuccess2", appRealm); + Assert.assertNotNull(user); + Assert.assertNotNull(user.getFederationLink()); + Assert.assertEquals(user.getFederationLink(), ldapModel.getId()); + } finally { + keycloakRule.stopSession(session, false); + } } @Test public void testReadonly() { KeycloakSession session = keycloakRule.startSession(); - RealmModel appRealm = session.realms().getRealmByName("test"); - - UserFederationProviderModel model = new UserFederationProviderModel(ldapModel.getId(), ldapModel.getProviderName(), ldapModel.getConfig(), ldapModel.getPriority(), ldapModel.getDisplayName()); - model.getConfig().put(LDAPFederationProvider.EDIT_MODE, UserFederationProvider.EditMode.READ_ONLY.toString()); - appRealm.updateUserFederationProvider(model); - UserModel user = session.users().getUserByUsername("johnkeycloak", appRealm); - Assert.assertNotNull(user); - Assert.assertNotNull(user.getFederationLink()); - Assert.assertEquals(user.getFederationLink(), ldapModel.getId()); try { - user.setEmail("error@error.com"); - Assert.fail("should fail"); - } catch (Exception e) { + RealmModel appRealm = session.realms().getRealmByName("test"); - } - try { - user.setLastName("Berk"); - Assert.fail("should fail"); - } catch (Exception e) { + UserFederationProviderModel model = new UserFederationProviderModel(ldapModel.getId(), ldapModel.getProviderName(), ldapModel.getConfig(), ldapModel.getPriority(), ldapModel.getDisplayName()); + model.getConfig().put(LDAPFederationProvider.EDIT_MODE, UserFederationProvider.EditMode.READ_ONLY.toString()); + appRealm.updateUserFederationProvider(model); + UserModel user = session.users().getUserByUsername("johnkeycloak", appRealm); + Assert.assertNotNull(user); + Assert.assertNotNull(user.getFederationLink()); + Assert.assertEquals(user.getFederationLink(), ldapModel.getId()); + try { + user.setEmail("error@error.com"); + Assert.fail("should fail"); + } catch (Exception e) { - } - try { - user.setFirstName("Bilbo"); - Assert.fail("should fail"); - } catch (Exception e) { + } + try { + user.setLastName("Berk"); + Assert.fail("should fail"); + } catch (Exception e) { - } - try { - UserCredentialModel cred = UserCredentialModel.password("poop"); - user.updateCredential(cred); - Assert.fail("should fail"); - } catch (Exception e) { + } + try { + user.setFirstName("Bilbo"); + Assert.fail("should fail"); + } catch (Exception e) { + } + try { + UserCredentialModel cred = UserCredentialModel.password("poop"); + user.updateCredential(cred); + Assert.fail("should fail"); + } catch (Exception e) { + + } + } finally { + keycloakRule.stopSession(session, false); } - session.getTransaction().rollback(); - session.close(); + session = keycloakRule.startSession(); - appRealm = session.realms().getRealmByName("test"); - Assert.assertEquals(UserFederationProvider.EditMode.WRITABLE.toString(), appRealm.getUserFederationProviders().get(0).getConfig().get(LDAPFederationProvider.EDIT_MODE)); - keycloakRule.stopSession(session, false); + try { + RealmModel appRealm = session.realms().getRealmByName("test"); + Assert.assertEquals(UserFederationProvider.EditMode.WRITABLE.toString(), appRealm.getUserFederationProviders().get(0).getConfig().get(LDAPFederationProvider.EDIT_MODE)); + } finally { + keycloakRule.stopSession(session, false); + } } @Test public void testUnsynced() { KeycloakSession session = keycloakRule.startSession(); - RealmModel appRealm = session.realms().getRealmByName("test"); + try { + RealmModel appRealm = session.realms().getRealmByName("test"); - UserFederationProviderModel model = new UserFederationProviderModel(ldapModel.getId(), ldapModel.getProviderName(), ldapModel.getConfig(), ldapModel.getPriority(), ldapModel.getDisplayName()); - model.getConfig().put(LDAPFederationProvider.EDIT_MODE, UserFederationProvider.EditMode.UNSYNCED.toString()); - appRealm.updateUserFederationProvider(model); - UserModel user = session.users().getUserByUsername("johnkeycloak", appRealm); - Assert.assertNotNull(user); - Assert.assertNotNull(user.getFederationLink()); - Assert.assertEquals(user.getFederationLink(), ldapModel.getId()); + UserFederationProviderModel model = new UserFederationProviderModel(ldapModel.getId(), ldapModel.getProviderName(), ldapModel.getConfig(), ldapModel.getPriority(), ldapModel.getDisplayName()); + model.getConfig().put(LDAPFederationProvider.EDIT_MODE, UserFederationProvider.EditMode.UNSYNCED.toString()); + appRealm.updateUserFederationProvider(model); + UserModel user = session.users().getUserByUsername("johnkeycloak", appRealm); + Assert.assertNotNull(user); + Assert.assertNotNull(user.getFederationLink()); + Assert.assertEquals(user.getFederationLink(), ldapModel.getId()); + + UserCredentialModel cred = UserCredentialModel.password("candy"); + user.updateCredential(cred); + UserCredentialValueModel userCredentialValueModel = user.getCredentialsDirectly().get(0); + Assert.assertEquals(UserCredentialModel.PASSWORD, userCredentialValueModel.getType()); + Assert.assertTrue(session.users().validCredentials(appRealm, user, cred)); + } finally { + keycloakRule.stopSession(session, false); + } - UserCredentialModel cred = UserCredentialModel.password("candy"); - user.updateCredential(cred); - UserCredentialValueModel userCredentialValueModel = user.getCredentialsDirectly().get(0); - Assert.assertEquals(UserCredentialModel.PASSWORD, userCredentialValueModel.getType()); - Assert.assertTrue(session.users().validCredentials(appRealm, user, cred)); - session.getTransaction().rollback(); - session.close(); session = keycloakRule.startSession(); - appRealm = session.realms().getRealmByName("test"); - Assert.assertEquals(UserFederationProvider.EditMode.WRITABLE.toString(), appRealm.getUserFederationProviders().get(0).getConfig().get(LDAPFederationProvider.EDIT_MODE)); - keycloakRule.stopSession(session, false); + try { + RealmModel appRealm = session.realms().getRealmByName("test"); + Assert.assertEquals(UserFederationProvider.EditMode.WRITABLE.toString(), appRealm.getUserFederationProviders().get(0).getConfig().get(LDAPFederationProvider.EDIT_MODE)); + } finally { + keycloakRule.stopSession(session, false); + } } } diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java index 534505ca5c..0975f26831 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java @@ -9,6 +9,7 @@ import org.junit.rules.ExternalResource; import org.keycloak.Config; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSessionFactory; +import org.keycloak.models.KeycloakTransaction; import org.keycloak.models.RealmModel; import org.keycloak.models.UserModel; import org.keycloak.models.utils.ModelToRepresentation; @@ -149,8 +150,11 @@ public abstract class AbstractKeycloakRule extends ExternalResource { } public void stopSession(KeycloakSession session, boolean commit) { + KeycloakTransaction transaction = session.getTransaction(); if (commit) { - session.getTransaction().commit(); + transaction.commit(); + } else { + transaction.rollback(); } session.close(); }