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);
}
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);
}
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);
}
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);
}
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);
}
}
Aggregations