Search in sources :

Example 21 with PolicyFailureError

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

the class SoapAuthorizationPolicyTest method testMultipleAllMatch.

@Test
@Configuration("{\r\n" + " \"multiMatch\" : \"all\"," + "  \"rules\" : [\r\n" + "    { \"action\" : \"reportIncident\", \"role\" : \"role-1\" },\r\n" + "    { \"action\" : \"reportIncident\", \"role\" : \"role-2\" }\r\n" + "  ]\r\n" + "}")
public void testMultipleAllMatch() throws Throwable {
    HashSet<String> userRoles = new HashSet<>();
    userRoles.add("other-role");
    // Should Fail
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/multi/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);
        // Expected <Authorization> but was <Other>
        Assert.assertEquals(PolicyFailureType.Authorization, policyFailure.getType());
    }
    userRoles.add("role-1");
    // Should Fail
    request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/multi/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());
    }
    userRoles.add("role-2");
    // Should Succeed
    request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/multi/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);
}
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 22 with PolicyFailureError

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

the class SoapAuthorizationPolicyTest method testNoSOAPHeader.

@Test
@Configuration("{\r\n" + "  \"rules\" : [\r\n" + "    { \"action\" : \"*\", \"role\" : \"role-1\" }\r\n" + "  ]\r\n" + "}")
public void testNoSOAPHeader() throws Throwable {
    HashSet<String> userRoles = new HashSet<>();
    userRoles.add("role-1");
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/invoices/1");
    try {
        request.contextAttribute(SoapAuthorizationPolicy.AUTHENTICATED_USER_ROLES, userRoles);
        send(request);
        Assert.fail("expected a failure response");
    } catch (PolicyFailureError failure) {
        Assert.assertNotNull(failure.getFailure());
        Assert.assertEquals(PolicyFailureType.Other, failure.getFailure().getType());
    }
}
Also used : 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 23 with PolicyFailureError

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

the class SoapAuthorizationPolicyTest method testMultipleAnyMatch.

@Test
@Configuration("{\r\n" + " \"multiMatch\" : \"any\"," + "  \"rules\" : [\r\n" + "    { \"action\" : \"*\", \"role\" : \"user\" },\r\n" + "    { \"action\" : \"*\", \"role\" : \"role-1\" },\r\n" + "    { \"action\" : \"*\", \"role\" : \"role-2\" },\r\n" + "    { \"action\" : \"*\", \"role\" : \"admin\" }\r\n" + "  ]\r\n" + "}")
public void testMultipleAnyMatch() throws Throwable {
    HashSet<String> userRoles = new HashSet<>();
    userRoles.add("other-role");
    // Should Fail
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/multi/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);
    }
    userRoles.add("role-1");
    // Should Succeed
    request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/multi/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);
    userRoles.add("role-2");
    // Should Succeed
    request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/multi/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 24 with PolicyFailureError

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

the class TimeoutPolicyTest method shouldsettingNothing_onEmptyValue.

/**
 * Execute with empty value
 */
@Test
@Configuration("{\"timeoutConnect\" : \"\", \"timeoutRead\" : \"\" }")
@BackEndApi(EndPointPropertiesEcho.class)
public void shouldsettingNothing_onEmptyValue() 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)

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