Search in sources :

Example 6 with PolicyFailureError

use of io.apiman.test.policies.PolicyFailureError in project apiman-plugins by apiman.

the class TimeoutPolicyTest method shouldsettingNothing_onNoValue.

/**
 * Execute with no value
 */
@Test
@Configuration("{}")
@BackEndApi(EndPointPropertiesEcho.class)
public void shouldsettingNothing_onNoValue() throws PolicyFailureError, Throwable {
    // WHEN Execute the policy
    PolicyTestResponse response = null;
    try {
        response = send(PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource"));
    } catch (PolicyFailureError ex) {
        fail("Configuration error");
    }
    // THEN timeouts are not set
    HashMap<?, ?> responseMap = new ObjectMapper().readValue(response.body(), HashMap.class);
    assertFalse("timeoutConnect", responseMap.containsKey("timeouts.connect"));
    assertFalse("timeoutRead", responseMap.containsKey("timeouts.read"));
}
Also used : PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IPolicyTestBackEndApi(io.apiman.test.policies.IPolicyTestBackEndApi) BackEndApi(io.apiman.test.policies.BackEndApi) Configuration(io.apiman.test.policies.Configuration) Test(org.junit.Test) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest)

Example 7 with PolicyFailureError

use of io.apiman.test.policies.PolicyFailureError in project apiman-plugins by apiman.

the class SoapAuthorizationPolicyTest method testAction.

@Test
@Configuration("{\r\n" + " \"requestUnmatched\" : \"pass\"," + "  \"rules\" : [\r\n" + "    { \"action\" : \"reportIncident\", \"role\" : \"the-role\" }\r\n" + "  ]\r\n" + "}")
public void testAction() throws Throwable {
    HashSet<String> userRoles = new HashSet<>();
    userRoles.add("other-role");
    // Should Succeed
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/auth/my-items");
    request.header("SOAPAction", "closeIncident");
    request.contextAttribute(SoapAuthorizationPolicy.AUTHENTICATED_USER_ROLES, userRoles);
    PolicyTestResponse response = send(request);
    EchoResponse echo = response.entity(EchoResponse.class);
    Assert.assertNotNull(echo);
    // Should Fail
    request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/auth/my-items");
    try {
        request.header("SOAPAction", "reportIncident");
        request.contextAttribute(SoapAuthorizationPolicy.AUTHENTICATED_USER_ROLES, userRoles);
        send(request);
        Assert.fail("Expected a failure response!");
    } catch (PolicyFailureError failure) {
        PolicyFailure policyFailure = failure.getFailure();
        Assert.assertNotNull(policyFailure);
        Assert.assertEquals(PolicyFailureType.Authorization, policyFailure.getType());
    }
}
Also used : EchoResponse(io.apiman.test.common.mock.EchoResponse) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) HashSet(java.util.HashSet) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 8 with PolicyFailureError

use of io.apiman.test.policies.PolicyFailureError in project apiman-plugins by apiman.

the class SoapAuthorizationPolicyTest method testMultiple.

@Test
@Configuration("{\r\n" + "  \"rules\" : [\r\n" + "    { \"action\" : \"reportIncident\", \"role\" : \"user\" },\r\n" + "    { \"action\" : \"resolveIncident\", \"role\" : \"admin\" }\r\n" + "  ]\r\n" + "}")
public void testMultiple() throws Throwable {
    HashSet<String> userRoles = new HashSet<>();
    userRoles.add("user");
    // Should Succeed
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/path/to/user/resource");
    request.header("SOAPAction", "reportIncident");
    request.contextAttribute(SoapAuthorizationPolicy.AUTHENTICATED_USER_ROLES, userRoles);
    PolicyTestResponse response = send(request);
    EchoResponse echo = response.entity(EchoResponse.class);
    Assert.assertNotNull(echo);
    // Should Fail
    request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/admin/path/to/admin/resource");
    try {
        request.header("SOAPAction", "resolveIncident");
        request.contextAttribute(SoapAuthorizationPolicy.AUTHENTICATED_USER_ROLES, userRoles);
        send(request);
        Assert.fail("Expected a failure response!");
    } catch (PolicyFailureError failure) {
        PolicyFailure policyFailure = failure.getFailure();
        Assert.assertNotNull(policyFailure);
        Assert.assertEquals(PolicyFailureType.Authorization, policyFailure.getType());
    }
    // 
    userRoles.add("admin");
    // Should Succeed
    request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/path/to/user/resource");
    request.header("SOAPAction", "reportIncident");
    request.contextAttribute(SoapAuthorizationPolicy.AUTHENTICATED_USER_ROLES, userRoles);
    response = send(request);
    echo = response.entity(EchoResponse.class);
    Assert.assertNotNull(echo);
    // Should Succeed
    request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/admin/path/to/admin/resource");
    request.header("SOAPAction", "reportIncident");
    request.contextAttribute(SoapAuthorizationPolicy.AUTHENTICATED_USER_ROLES, userRoles);
    response = send(request);
    echo = response.entity(EchoResponse.class);
    Assert.assertNotNull(echo);
}
Also used : EchoResponse(io.apiman.test.common.mock.EchoResponse) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) HashSet(java.util.HashSet) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 9 with PolicyFailureError

use of io.apiman.test.policies.PolicyFailureError in project apiman-plugins by apiman.

the class SoapAuthorizationPolicyTest method testNoneMatchedFail.

@Test
@Configuration("{\r\n" + " \"requestUnmatched\" : \"fail\"," + "  \"rules\" : [\r\n" + "    { \"action\" : \"reportIncident\", \"role\" : \"user\" },\r\n" + "    { \"action\" : \"reportIncident\", \"role\" : \"admin\" }\r\n" + "  ]\r\n" + "}")
public void testNoneMatchedFail() throws Throwable {
    HashSet<String> userRoles = new HashSet<>();
    // Should Fail
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/other/resource");
    try {
        request.header("SOAPAction", "reportIncident");
        request.contextAttribute(SoapAuthorizationPolicy.AUTHENTICATED_USER_ROLES, userRoles);
        send(request);
        Assert.fail("Expected a failure response!");
    } catch (PolicyFailureError failure) {
        PolicyFailure policyFailure = failure.getFailure();
        Assert.assertNotNull(policyFailure);
        Assert.assertEquals(PolicyFailureType.Authorization, policyFailure.getType());
    }
    // Should Fail
    request = PolicyTestRequest.build(PolicyTestRequestType.POST, "/admin/resource");
    try {
        request.header("SOAPAction", "reportIncident");
        request.contextAttribute(SoapAuthorizationPolicy.AUTHENTICATED_USER_ROLES, userRoles);
        send(request);
        Assert.fail("Expected a failure response!");
    } catch (PolicyFailureError failure) {
        PolicyFailure policyFailure = failure.getFailure();
        Assert.assertNotNull(policyFailure);
    }
}
Also used : PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) HashSet(java.util.HashSet) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 10 with PolicyFailureError

use of io.apiman.test.policies.PolicyFailureError in project apiman by apiman.

the class BasicAuthenticationPolicyTest method testStatic.

@Test
@Configuration("{\r\n" + "    \"realm\" : \"TestRealm\",\r\n" + "    \"forwardIdentityHttpHeader\" : \"X-Authenticated-Identity\",\r\n" + "    \"staticIdentity\" : {\r\n" + "      \"identities\" : [\r\n" + "        { \"username\" : \"ckent\", \"password\" : \"ckent123!\" },\r\n" + "        { \"username\" : \"bwayne\", \"password\" : \"bwayne123!\" },\r\n" + "        { \"username\" : \"dprince\", \"password\" : \"dprince123!\" }\r\n" + "      ]\r\n" + "    }\r\n" + "}")
public void testStatic() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
    // Failure
    try {
        send(request);
        Assert.fail("Expected a failure response!");
    } catch (PolicyFailureError failure) {
        PolicyFailure policyFailure = failure.getFailure();
        Assert.assertNotNull(policyFailure);
        Assert.assertEquals(PolicyFailureType.Authentication, policyFailure.getType());
        Assert.assertEquals(10004, policyFailure.getFailureCode());
    }
    // Failure
    try {
        request.basicAuth("ckent", "invalid_password");
        send(request);
        Assert.fail("Expected a failure response!");
    } catch (PolicyFailureError failure) {
        PolicyFailure policyFailure = failure.getFailure();
        Assert.assertNotNull(policyFailure);
        Assert.assertEquals(PolicyFailureType.Authentication, policyFailure.getType());
        Assert.assertEquals(10003, policyFailure.getFailureCode());
    }
    // Success
    request.basicAuth("ckent", "ckent123!");
    PolicyTestResponse response = send(request);
    Assert.assertEquals(200, response.code());
    EchoResponse echo = response.entity(EchoResponse.class);
    Assert.assertNotNull(echo);
    String header = echo.getHeaders().get("X-Authenticated-Identity");
    Assert.assertNotNull(header);
    Assert.assertEquals("ckent", header);
}
Also used : EchoResponse(io.apiman.test.common.mock.EchoResponse) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Aggregations

ApimanPolicyTest (io.apiman.test.policies.ApimanPolicyTest)24 Configuration (io.apiman.test.policies.Configuration)24 PolicyFailureError (io.apiman.test.policies.PolicyFailureError)24 Test (org.junit.Test)24 PolicyTestRequest (io.apiman.test.policies.PolicyTestRequest)21 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)19 PolicyTestResponse (io.apiman.test.policies.PolicyTestResponse)15 EchoResponse (io.apiman.test.common.mock.EchoResponse)7 BackEndApi (io.apiman.test.policies.BackEndApi)7 IPolicyTestBackEndApi (io.apiman.test.policies.IPolicyTestBackEndApi)7 HashSet (java.util.HashSet)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3