Search in sources :

Example 21 with EchoResponse

use of io.apiman.test.common.mock.EchoResponse 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 EchoResponse

use of io.apiman.test.common.mock.EchoResponse in project apiman-plugins by apiman.

the class SoapAuthorizationPolicyTest method testNoneMatchedPass.

@Test
@Configuration("{\r\n" + " \"requestUnmatched\" : \"pass\"," + "  \"rules\" : [\r\n" + "    { \"action\" : \"viewIncident\", \"role\" : \"user\" },\r\n" + "    { \"action\" : \"viewIncident\", \"role\" : \"admin\" }\r\n" + "  ]\r\n" + "}")
public void testNoneMatchedPass() throws Throwable {
    HashSet<String> userRoles = new HashSet<>();
    // Should Succeed
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/other/resource");
    request.header("SOAPAction", "resolveIncident");
    request.contextAttribute(SoapAuthorizationPolicy.AUTHENTICATED_USER_ROLES, userRoles);
    PolicyTestResponse response = send(request);
    EchoResponse echo = response.entity(EchoResponse.class);
    Assert.assertNotNull(echo);
    // Should Succeed
    request = PolicyTestRequest.build(PolicyTestRequestType.PUT, "/admin/resource");
    request.header("SOAPAction", "closeIncident");
    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) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) HashSet(java.util.HashSet) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 23 with EchoResponse

use of io.apiman.test.common.mock.EchoResponse in project apiman-plugins by apiman.

the class SoapAuthorizationPolicyTest method testSimple.

@Test
@Configuration("{\r\n" + "  \"rules\" : [\r\n" + "    { \"action\" : \"*\", \"role\" : \"role-1\" }\r\n" + "  ]\r\n" + "}")
public void testSimple() throws Throwable {
    HashSet<String> userRoles = new HashSet<>();
    userRoles.add("role-1");
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/invoices/1");
    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) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) HashSet(java.util.HashSet) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 24 with EchoResponse

use of io.apiman.test.common.mock.EchoResponse 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 25 with EchoResponse

use of io.apiman.test.common.mock.EchoResponse in project apiman-plugins by apiman.

the class LogHeadersPolicyTest method testLogHeadersHappyFlowResponseOnly.

/**
 * A simple happy flow test to verify the policy does not blow up in our face.
 */
@Test
@Configuration("{ \"direction\" : \"response\", \"logStatusCode\" : true }")
public void testLogHeadersHappyFlowResponseOnly() throws PolicyFailureError, Throwable {
    PrintStream out = System.out;
    ByteArrayOutputStream testOutput = new ByteArrayOutputStream();
    System.setOut(new PrintStream(testOutput));
    try {
        PolicyTestResponse response = send(PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource").header("X-Test-Name", "testGet"));
        Assert.assertEquals(200, response.code());
        EchoResponse entity = response.entity(EchoResponse.class);
        Assert.assertEquals("testGet", entity.getHeaders().get("X-Test-Name"));
        String output = testOutput.toString("UTF-8");
        output = redactDates(output);
        output = normalize(output);
        String expected = "INFO: Status code 200 for io.apiman.test.policies.EchoBackEndApi\n" + "INFO: Logging 4 HTTP Response headers for io.apiman.test.policies.EchoBackEndApi\n" + "Key : Content-Length, Value : 199\n" + "Key : Content-Type, Value : application/json\n" + "Key : Date, Value : XXX\n" + "Key : Server, Value : apiman.policy-test\n" + "";
        Assert.assertEquals(expected, output);
    } finally {
        System.setOut(out);
    }
}
Also used : EchoResponse(io.apiman.test.common.mock.EchoResponse) PrintStream(java.io.PrintStream) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Aggregations

EchoResponse (io.apiman.test.common.mock.EchoResponse)25 ApimanPolicyTest (io.apiman.test.policies.ApimanPolicyTest)23 Configuration (io.apiman.test.policies.Configuration)23 PolicyTestResponse (io.apiman.test.policies.PolicyTestResponse)23 Test (org.junit.Test)23 PolicyTestRequest (io.apiman.test.policies.PolicyTestRequest)18 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)7 PolicyFailureError (io.apiman.test.policies.PolicyFailureError)7 HashSet (java.util.HashSet)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PrintStream (java.io.PrintStream)3 ApiResponse (io.apiman.gateway.engine.beans.ApiResponse)1 IOException (java.io.IOException)1 RSAPublicKey (java.security.interfaces.RSAPublicKey)1 Date (java.util.Date)1