Search in sources :

Example 6 with AuthenticationExecutionExportRepresentation

use of org.keycloak.representations.idm.AuthenticationExecutionExportRepresentation in project keycloak by keycloak.

the class FlowTest method testAddRemoveFlow.

@Test
public void testAddRemoveFlow() {
    // test that built-in flow cannot be deleted
    List<AuthenticationFlowRepresentation> flows = authMgmtResource.getFlows();
    for (AuthenticationFlowRepresentation flow : flows) {
        try {
            authMgmtResource.deleteFlow(flow.getId());
            Assert.fail("deleteFlow should fail for built in flow");
        } catch (BadRequestException e) {
            break;
        }
    }
    // try create new flow using alias of already existing flow
    Response response = authMgmtResource.createFlow(newFlow("browser", "Browser flow", "basic-flow", true, false));
    try {
        Assert.assertEquals("createFlow using the alias of existing flow should fail", 409, response.getStatus());
    } finally {
        response.close();
    }
    // try create flow without alias
    response = authMgmtResource.createFlow(newFlow(null, "Browser flow", "basic-flow", true, false));
    try {
        Assert.assertEquals("createFlow using the alias of existing flow should fail", 409, response.getStatus());
    } finally {
        response.close();
    }
    // create new flow that should succeed
    AuthenticationFlowRepresentation newFlow = newFlow("browser-2", "Browser flow", "basic-flow", true, false);
    createFlow(newFlow);
    // check that new flow is returned in a children list
    flows = authMgmtResource.getFlows();
    AuthenticationFlowRepresentation found = findFlowByAlias("browser-2", flows);
    Assert.assertNotNull("created flow visible in parent", found);
    compareFlows(newFlow, found);
    // check lookup flow with unexistent ID
    try {
        authMgmtResource.getFlow("id-123-notExistent");
        Assert.fail("Not expected to find unexistent flow");
    } catch (NotFoundException nfe) {
    // Expected
    }
    // check that new flow is returned individually
    AuthenticationFlowRepresentation found2 = authMgmtResource.getFlow(found.getId());
    Assert.assertNotNull("created flow visible directly", found2);
    compareFlows(newFlow, found2);
    // add execution flow to some parent flow
    Map<String, String> data = new HashMap<>();
    data.put("alias", "SomeFlow");
    data.put("type", "basic-flow");
    data.put("description", "Test flow");
    // This tests against a regression in KEYCLOAK-16656
    data.put("provider", "registration-page-form");
    Map<String, String> data2 = new HashMap<>();
    data2.put("alias", "SomeFlow2");
    data2.put("type", "form-flow");
    data2.put("description", "Test flow 2");
    data2.put("provider", "registration-page-form");
    // inexistent parent flow - should fail
    try {
        authMgmtResource.addExecutionFlow("inexistent-parent-flow-alias", data);
        Assert.fail("addExecutionFlow for inexistent parent should have failed");
    } catch (Exception expected) {
    // Expected
    }
    // already existent flow - should fail
    try {
        data.put("alias", "browser");
        authMgmtResource.addExecutionFlow("browser-2", data);
        Assert.fail("addExecutionFlow should have failed as browser flow already exists");
    } catch (Exception expected) {
    // Expected
    }
    // Successfully add flow
    data.put("alias", "SomeFlow");
    authMgmtResource.addExecutionFlow("browser-2", data);
    authMgmtResource.addExecutionFlow("browser-2", data2);
    assertAdminEvents.assertEvent(REALM_NAME, OperationType.CREATE, AdminEventPaths.authAddExecutionFlowPath("browser-2"), data, ResourceType.AUTH_EXECUTION_FLOW);
    assertAdminEvents.assertEvent(REALM_NAME, OperationType.CREATE, AdminEventPaths.authAddExecutionFlowPath("browser-2"), data2, ResourceType.AUTH_EXECUTION_FLOW);
    // check that new flow is returned in a children list
    flows = authMgmtResource.getFlows();
    found2 = findFlowByAlias("browser-2", flows);
    Assert.assertNotNull("created flow visible in parent", found2);
    List<AuthenticationExecutionExportRepresentation> execs = found2.getAuthenticationExecutions();
    Assert.assertNotNull(execs);
    Assert.assertEquals("Size two", 2, execs.size());
    AuthenticationExecutionExportRepresentation expected = new AuthenticationExecutionExportRepresentation();
    expected.setFlowAlias("SomeFlow");
    expected.setUserSetupAllowed(false);
    expected.setAuthenticatorFlow(true);
    expected.setRequirement("DISABLED");
    expected.setPriority(0);
    compareExecution(expected, execs.get(0));
    expected = new AuthenticationExecutionExportRepresentation();
    expected.setFlowAlias("SomeFlow2");
    expected.setUserSetupAllowed(false);
    expected.setAuthenticator("registration-page-form");
    expected.setAuthenticatorFlow(true);
    expected.setRequirement("DISABLED");
    expected.setPriority(1);
    compareExecution(expected, execs.get(1));
    // delete non-built-in flow
    authMgmtResource.deleteFlow(found.getId());
    assertAdminEvents.assertEvent(REALM_NAME, OperationType.DELETE, AdminEventPaths.authFlowPath(found.getId()), ResourceType.AUTH_FLOW);
    // check the deleted flow is no longer returned
    flows = authMgmtResource.getFlows();
    found = findFlowByAlias("browser-2", flows);
    Assert.assertNull("flow deleted", found);
    // Check deleting flow second time will fail
    try {
        authMgmtResource.deleteFlow("id-123-notExistent");
        Assert.fail("Not expected to delete flow, which doesn't exist");
    } catch (NotFoundException nfe) {
    // Expected
    }
}
Also used : Response(javax.ws.rs.core.Response) HashMap(java.util.HashMap) AuthenticationFlowRepresentation(org.keycloak.representations.idm.AuthenticationFlowRepresentation) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) Matchers.containsString(org.hamcrest.Matchers.containsString) AuthenticationExecutionExportRepresentation(org.keycloak.representations.idm.AuthenticationExecutionExportRepresentation) ClientErrorException(javax.ws.rs.ClientErrorException) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotFoundException(javax.ws.rs.NotFoundException) Test(org.junit.Test)

Aggregations

AuthenticationExecutionExportRepresentation (org.keycloak.representations.idm.AuthenticationExecutionExportRepresentation)6 AuthenticationFlowRepresentation (org.keycloak.representations.idm.AuthenticationFlowRepresentation)4 HashMap (java.util.HashMap)2 BadRequestException (javax.ws.rs.BadRequestException)1 ClientErrorException (javax.ws.rs.ClientErrorException)1 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)1 NotFoundException (javax.ws.rs.NotFoundException)1 Response (javax.ws.rs.core.Response)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Test (org.junit.Test)1 RealmResource (org.keycloak.admin.client.resource.RealmResource)1 MultivaluedHashMap (org.keycloak.common.util.MultivaluedHashMap)1 AuthenticationExecutionModel (org.keycloak.models.AuthenticationExecutionModel)1 AuthenticationFlowModel (org.keycloak.models.AuthenticationFlowModel)1 AuthenticatorConfigModel (org.keycloak.models.AuthenticatorConfigModel)1 ArtifactBindingUtils.computeArtifactBindingIdentifierString (org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString)1 AuthenticatorConfigRepresentation (org.keycloak.representations.idm.AuthenticatorConfigRepresentation)1 IdentityProviderRepresentation (org.keycloak.representations.idm.IdentityProviderRepresentation)1