Search in sources :

Example 1 with KeycloakTestingClient

use of org.keycloak.testsuite.client.KeycloakTestingClient in project keycloak by keycloak.

the class AuthServerTestEnricher method afterClass.

public void afterClass(@Observes(precedence = 1) AfterClass event) throws Exception {
    // check if a test accidentally left the auth-server not running
    ContainerController controller = containerConroller.get();
    if (!controller.isStarted(suiteContext.getAuthServerInfo().getQualifier())) {
        log.warn("Auth server wasn't running. Starting " + suiteContext.getAuthServerInfo().getQualifier());
        controller.start(suiteContext.getAuthServerInfo().getQualifier());
    }
    TestContext testContext = testContextProducer.get();
    Keycloak adminClient = testContext.getAdminClient();
    KeycloakTestingClient testingClient = testContext.getTestingClient();
    removeTestRealms(testContext, adminClient);
    if (!isAuthServerRemote()) {
        boolean wasUpdated = false;
        if (event.getTestClass().isAnnotationPresent(SetDefaultProvider.class)) {
            SpiProvidersSwitchingUtils.resetProvider(suiteContext, event.getTestClass().getAnnotation(SetDefaultProvider.class));
            wasUpdated = true;
        }
        if (event.getTestClass().isAnnotationPresent(EnableVault.class) && !isAuthServerQuarkus()) {
            VaultUtils.disableVault(suiteContext, event.getTestClass().getAnnotation(EnableVault.class).providerId());
            wasUpdated = true;
        }
        if (wasUpdated) {
            restartAuthServer();
            testContext.reconnectAdminClient();
        }
    }
    if (adminClient != null) {
        adminClient.close();
    }
    if (testingClient != null) {
        testingClient.close();
    }
}
Also used : EnableVault(org.keycloak.testsuite.arquillian.annotation.EnableVault) SetDefaultProvider(org.keycloak.testsuite.arquillian.annotation.SetDefaultProvider) ContainerController(org.jboss.arquillian.container.test.api.ContainerController) KeycloakTestingClient(org.keycloak.testsuite.client.KeycloakTestingClient) Keycloak(org.keycloak.admin.client.Keycloak)

Example 2 with KeycloakTestingClient

use of org.keycloak.testsuite.client.KeycloakTestingClient in project keycloak by keycloak.

the class ModelTestExecutor method execute.

@Override
public void execute(LocalExecutionEvent event) throws Exception {
    Method testMethod = event.getExecutor().getMethod();
    ModelTest annotation = testMethod.getAnnotation(ModelTest.class);
    if (annotation == null) {
        // Not a model test
        super.execute(event);
    } else {
        TestResult result = new TestResult();
        try {
            // Model test - wrap the call inside the
            TestContext ctx = testContext.get();
            KeycloakTestingClient testingClient = ctx.getTestingClient();
            testingClient.server().runModelTest(testMethod.getDeclaringClass().getName(), testMethod.getName());
            result.setStatus(TestResult.Status.PASSED);
        } catch (Throwable e) {
            result.setStatus(TestResult.Status.FAILED);
            result.setThrowable(e);
        } finally {
            result.setEnd(System.currentTimeMillis());
        }
        // Need to use reflection this way...
        Field testResultField = Reflections.findDeclaredField(LocalTestExecuter.class, "testResult");
        testResultField.setAccessible(true);
        InstanceProducer<TestResult> thisTestResult = (InstanceProducer<TestResult>) testResultField.get(this);
        thisTestResult.set(result);
    }
}
Also used : ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Field(java.lang.reflect.Field) InstanceProducer(org.jboss.arquillian.core.api.InstanceProducer) TestResult(org.jboss.arquillian.test.spi.TestResult) KeycloakTestingClient(org.keycloak.testsuite.client.KeycloakTestingClient) Method(java.lang.reflect.Method)

Example 3 with KeycloakTestingClient

use of org.keycloak.testsuite.client.KeycloakTestingClient in project keycloak by keycloak.

the class BrokerRunOnServerUtil method configureBrokerFlowToReAuthenticationWithPasswordOrTotp.

// Configure the variant of firstBrokerLogin flow, which will allow to reauthenticate user with password OR totp
// TOTP will be available just if configured for the user
static void configureBrokerFlowToReAuthenticationWithPasswordOrTotp(KeycloakTestingClient testingClient, String consumerRealmName, String idpAlias, String newFlowAlias) {
    testingClient.server(consumerRealmName).run(session -> FlowUtil.inCurrentRealm(session).copyFirstBrokerLoginFlow(newFlowAlias));
    testingClient.server(consumerRealmName).run(session -> {
        AuthenticationFlowModel flowModel = FlowUtil.createFlowModel("password or otp", "basic-flow", "Flow to authenticate user with password or otp", false, true);
        FlowUtil.inCurrentRealm(session).selectFlow(newFlowAlias).inVerifyExistingAccountByReAuthentication(flowUtil -> flowUtil.clear().addAuthenticatorExecution(AuthenticationExecutionModel.Requirement.REQUIRED, IdpAutoLinkAuthenticatorFactory.PROVIDER_ID).addSubFlowExecution(flowModel, AuthenticationExecutionModel.Requirement.REQUIRED, subFlow -> subFlow.addAuthenticatorExecution(AuthenticationExecutionModel.Requirement.ALTERNATIVE, PasswordFormFactory.PROVIDER_ID).addAuthenticatorExecution(AuthenticationExecutionModel.Requirement.ALTERNATIVE, OTPFormAuthenticatorFactory.PROVIDER_ID))).usesInIdentityProvider(idpAlias);
    });
}
Also used : ClientModel(org.keycloak.models.ClientModel) RealmModel(org.keycloak.models.RealmModel) Constants(org.keycloak.models.Constants) RoleModel(org.keycloak.models.RoleModel) IdentityProviderModel(org.keycloak.models.IdentityProviderModel) UserSessionModel(org.keycloak.models.UserSessionModel) KeycloakTestingClient(org.keycloak.testsuite.client.KeycloakTestingClient) PasswordFormFactory(org.keycloak.authentication.authenticators.browser.PasswordFormFactory) OTPFormAuthenticatorFactory(org.keycloak.authentication.authenticators.browser.OTPFormAuthenticatorFactory) List(java.util.List) UserModel(org.keycloak.models.UserModel) IdpAutoLinkAuthenticatorFactory(org.keycloak.authentication.authenticators.broker.IdpAutoLinkAuthenticatorFactory) AuthenticationExecutionModel(org.keycloak.models.AuthenticationExecutionModel) AuthenticationFlowModel(org.keycloak.models.AuthenticationFlowModel) FlowUtil(org.keycloak.testsuite.util.FlowUtil) RunOnServer(org.keycloak.testsuite.runonserver.RunOnServer) Assert.assertEquals(org.junit.Assert.assertEquals) AuthenticationFlowModel(org.keycloak.models.AuthenticationFlowModel)

Aggregations

KeycloakTestingClient (org.keycloak.testsuite.client.KeycloakTestingClient)3 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 ContainerController (org.jboss.arquillian.container.test.api.ContainerController)1 InstanceProducer (org.jboss.arquillian.core.api.InstanceProducer)1 TestResult (org.jboss.arquillian.test.spi.TestResult)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Keycloak (org.keycloak.admin.client.Keycloak)1 IdpAutoLinkAuthenticatorFactory (org.keycloak.authentication.authenticators.broker.IdpAutoLinkAuthenticatorFactory)1 OTPFormAuthenticatorFactory (org.keycloak.authentication.authenticators.browser.OTPFormAuthenticatorFactory)1 PasswordFormFactory (org.keycloak.authentication.authenticators.browser.PasswordFormFactory)1 AuthenticationExecutionModel (org.keycloak.models.AuthenticationExecutionModel)1 AuthenticationFlowModel (org.keycloak.models.AuthenticationFlowModel)1 ClientModel (org.keycloak.models.ClientModel)1 Constants (org.keycloak.models.Constants)1 IdentityProviderModel (org.keycloak.models.IdentityProviderModel)1 RealmModel (org.keycloak.models.RealmModel)1 RoleModel (org.keycloak.models.RoleModel)1 UserModel (org.keycloak.models.UserModel)1