Search in sources :

Example 1 with RunOnServerException

use of org.keycloak.testsuite.runonserver.RunOnServerException in project keycloak by keycloak.

the class MultiVersionClusterTest method verifyFailureOnCurrent.

@Test
public void verifyFailureOnCurrent() throws Exception {
    deploy(deployment(), legacyNode);
    try {
        backendTestingClients.get(legacyNode).server().run(session -> {
            try {
                Class<?> itShouldFail = Module.getContextModuleLoader().loadModule("deployment.negative.jar").getClassLoader().loadClassLocal(SerializableTestClass.class.getName());
                session.getProvider(InfinispanConnectionProvider.class).getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME).put("itShouldFail", Reflections.newInstance(itShouldFail));
            } catch (Exception ex) {
                throw new RunOnServerException(ex);
            }
        });
    } catch (Exception e) {
        assertThat(e, instanceOf(RunOnServerException.class));
        assertThat(e.getCause().getCause(), instanceOf(TimeoutException.class));
    } finally {
        undeploy(deployment(), legacyNode);
    }
}
Also used : TimeoutException(org.infinispan.util.concurrent.TimeoutException) IOException(java.io.IOException) RunOnServerException(org.keycloak.testsuite.runonserver.RunOnServerException) RunOnServerException(org.keycloak.testsuite.runonserver.RunOnServerException) AbstractClusterTest(org.keycloak.testsuite.cluster.AbstractClusterTest) Test(org.junit.Test)

Example 2 with RunOnServerException

use of org.keycloak.testsuite.runonserver.RunOnServerException in project keycloak by keycloak.

the class MultiVersionClusterTest method verifyFailureOnLegacy.

@Test
public void verifyFailureOnLegacy() throws Exception {
    deploy(deployment(), currentNode);
    try {
        backendTestingClients.get(currentNode).server().run(session -> {
            try {
                Class<?> itShouldFail = Module.getContextModuleLoader().loadModule("deployment.negative.jar").getClassLoader().loadClassLocal(SerializableTestClass.class.getName());
                session.getProvider(InfinispanConnectionProvider.class).getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME).put("itShouldFail", Reflections.newInstance(itShouldFail));
            } catch (Exception ex) {
                throw new RunOnServerException(ex);
            }
        });
    } catch (Exception e) {
        assertThat(e, instanceOf(RunOnServerException.class));
        assertThat(e.getCause().getCause(), instanceOf(TimeoutException.class));
    } finally {
        undeploy(deployment(), currentNode);
    }
}
Also used : TimeoutException(org.infinispan.util.concurrent.TimeoutException) IOException(java.io.IOException) RunOnServerException(org.keycloak.testsuite.runonserver.RunOnServerException) RunOnServerException(org.keycloak.testsuite.runonserver.RunOnServerException) AbstractClusterTest(org.keycloak.testsuite.cluster.AbstractClusterTest) Test(org.junit.Test)

Example 3 with RunOnServerException

use of org.keycloak.testsuite.runonserver.RunOnServerException in project keycloak by keycloak.

the class ImportTest method importWithoutRequestContext.

// KEYCLOAK-12921 NPE importing realm with no request context
@Test
public void importWithoutRequestContext() throws IOException {
    final String realmString = IOUtils.toString(getClass().getResourceAsStream("/model/realm-validation.json"), StandardCharsets.UTF_8);
    testingClient.server().run(session -> {
        RealmRepresentation testRealm = JsonSerialization.readValue(realmString, RealmRepresentation.class);
        AtomicReference<Throwable> err = new AtomicReference<>();
        // Need a new thread to not get context from thread processing request to run-on-server endpoint
        Thread t = new Thread(() -> {
            try {
                KeycloakSession ses = session.getKeycloakSessionFactory().create();
                ses.getContext().setRealm(session.getContext().getRealm());
                ses.getTransactionManager().begin();
                RealmModel realmModel = new RealmManager(ses).importRealm(testRealm);
                ses.getTransactionManager().commit();
                ses.close();
                ses = session.getKeycloakSessionFactory().create();
                ses.getTransactionManager().begin();
                session.realms().removeRealm(realmModel.getId());
                ses.getTransactionManager().commit();
                ses.close();
            } catch (Throwable th) {
                err.set(th);
            }
        });
        synchronized (t) {
            t.start();
            try {
                t.wait(10000);
            } catch (InterruptedException e) {
                throw new RunOnServerException(e);
            }
        }
        if (err.get() != null) {
            throw new RunOnServerException(err.get());
        }
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) KeycloakSession(org.keycloak.models.KeycloakSession) AtomicReference(java.util.concurrent.atomic.AtomicReference) RealmManager(org.keycloak.services.managers.RealmManager) RunOnServerException(org.keycloak.testsuite.runonserver.RunOnServerException) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 4 with RunOnServerException

use of org.keycloak.testsuite.runonserver.RunOnServerException in project keycloak by keycloak.

the class LDAPUserLoginTest method afterImportTestRealm.

@Override
protected void afterImportTestRealm() {
    try {
        getTestingClient().server().run(session -> {
            LDAPTestContext ctx = LDAPTestContext.init(session);
            RealmModel appRealm = ctx.getRealm();
            // Delete all LDAP users
            LDAPTestUtils.removeAllLDAPUsers(ctx.getLdapProvider(), appRealm);
            // Add some new LDAP users for testing
            LDAPObject john = LDAPTestUtils.addLDAPUser(ctx.getLdapProvider(), appRealm, DEFAULT_TEST_USERS.get("VALID_USER_NAME"), DEFAULT_TEST_USERS.get("VALID_USER_FIRST_NAME"), DEFAULT_TEST_USERS.get("VALID_USER_LAST_NAME"), DEFAULT_TEST_USERS.get("VALID_USER_EMAIL"), DEFAULT_TEST_USERS.get("VALID_USER_STREET"), DEFAULT_TEST_USERS.get("VALID_USER_POSTAL_CODE"));
            LDAPTestUtils.updateLDAPPassword(ctx.getLdapProvider(), john, DEFAULT_TEST_USERS.get("VALID_USER_PASSWORD"));
        });
    } catch (RunOnServerException ex) {
        Assume.assumeFalse("Work around JDK-8214440", ex.getCause() instanceof ModelException && ex.getCause().getCause() instanceof ModelException && ex.getCause().getCause().getCause() instanceof javax.naming.AuthenticationException && Objects.equals(ex.getCause().getCause().getCause().getMessage(), "Could not negotiate TLS"));
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) ModelException(org.keycloak.models.ModelException) LDAPObject(org.keycloak.storage.ldap.idm.model.LDAPObject) RunOnServerException(org.keycloak.testsuite.runonserver.RunOnServerException)

Example 5 with RunOnServerException

use of org.keycloak.testsuite.runonserver.RunOnServerException in project keycloak by keycloak.

the class LdapUsernameAttributeTest method testUsernameChangeAlreadyExists.

@Test
public void testUsernameChangeAlreadyExists() {
    // create a user johndow and johndow2
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        RealmModel appRealm = ctx.getRealm();
        UserModel john = session.users().addUser(appRealm, "johndow");
        john.setEmail("johndow@email.cz");
        john.setFirstName("johndow");
        john.setLastName("johndow");
        UserModel john2 = session.users().addUser(appRealm, "johndow2");
        john.setEmail("johndow2@email.cz");
        john.setFirstName("johndow2");
        john.setLastName("johndow2");
    });
    // check they are there
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        RealmModel appRealm = ctx.getRealm();
        UserModel john = session.users().getUserByUsername(appRealm, "johndow");
        Assert.assertNotNull(john);
        Assert.assertNotNull(john.getFederationLink());
        UserModel john2 = session.users().getUserByUsername(appRealm, "johndow2");
        Assert.assertNotNull(john2);
        Assert.assertNotNull(john2.getFederationLink());
    });
    // rename johndow to johndow2 => it should fail
    try {
        testingClient.server().run(session -> {
            LDAPTestContext ctx = LDAPTestContext.init(session);
            RealmModel appRealm = ctx.getRealm();
            UserModel john = session.users().getUserByUsername(appRealm, "johndow");
            john.setUsername("johndow2");
        });
        Assert.assertFalse("Model exception is expected here, so it should not reach this point", true);
    } catch (RunOnServerException e) {
        Assert.assertTrue("Model exception is expected here but another error found", e.getCause() instanceof ModelDuplicateException);
        Assert.assertEquals(UserModel.USERNAME, ((ModelDuplicateException) e.getCause()).getDuplicateFieldName());
    }
    // remove both users
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        RealmModel appRealm = ctx.getRealm();
        UserModel john = session.users().getUserByUsername(appRealm, "johndow");
        Assert.assertNotNull(john);
        UserModel john2 = session.users().getUserByUsername(appRealm, "johndow2");
        Assert.assertNotNull(john2);
        session.users().removeUser(appRealm, john);
        session.users().removeUser(appRealm, john2);
        Assert.assertNull(session.users().getUserByUsername(appRealm, "johndow"));
        Assert.assertNull(session.users().getUserByUsername(appRealm, "johndow2"));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) ModelDuplicateException(org.keycloak.models.ModelDuplicateException) RunOnServerException(org.keycloak.testsuite.runonserver.RunOnServerException) Test(org.junit.Test)

Aggregations

RunOnServerException (org.keycloak.testsuite.runonserver.RunOnServerException)5 Test (org.junit.Test)4 RealmModel (org.keycloak.models.RealmModel)3 IOException (java.io.IOException)2 TimeoutException (org.infinispan.util.concurrent.TimeoutException)2 AbstractClusterTest (org.keycloak.testsuite.cluster.AbstractClusterTest)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 KeycloakSession (org.keycloak.models.KeycloakSession)1 ModelDuplicateException (org.keycloak.models.ModelDuplicateException)1 ModelException (org.keycloak.models.ModelException)1 UserModel (org.keycloak.models.UserModel)1 RealmRepresentation (org.keycloak.representations.idm.RealmRepresentation)1 RealmManager (org.keycloak.services.managers.RealmManager)1 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)1 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)1