use of io.apiman.gateway.engine.beans.PolicyFailureType in project apiman by apiman.
the class IPBlacklistPolicyTest method testApply.
/**
* Test method for {@link io.apiman.gateway.engine.policies.IPBlacklistPolicy#parseConfiguration(java.lang.String)}.
*/
@Test
public void testApply() {
IPBlacklistPolicy policy = new IPBlacklistPolicy();
String json = "{" + " \"ipList\" : [" + " \"1.2.3.4\"," + " \"3.4.5.6\"," + " \"10.0.0.11\"" + " ]" + "}";
IPListConfig config = policy.parseConfiguration(json);
ApiRequest request = new ApiRequest();
request.setType("GET");
request.setApiKey("12345");
request.setRemoteAddr("1.2.3.4");
request.setDestination("/");
IPolicyContext context = Mockito.mock(IPolicyContext.class);
final PolicyFailure failure = new PolicyFailure();
Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() {
@Override
public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) {
return failure;
}
});
IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
// Failure
policy.apply(request, context, config, chain);
Mockito.verify(chain).doFailure(failure);
// Success
request.setRemoteAddr("9.8.7.6");
chain = Mockito.mock(IPolicyChain.class);
policy.apply(request, context, config, chain);
Mockito.verify(chain).doApply(request);
}
use of io.apiman.gateway.engine.beans.PolicyFailureType in project apiman by apiman.
the class IPBlacklistPolicyTest method testApplyWithWildcards.
/**
* Test method for {@link io.apiman.gateway.engine.policies.IPBlacklistPolicy#parseConfiguration(java.lang.String)}.
*/
@Test
public void testApplyWithWildcards() {
IPBlacklistPolicy policy = new IPBlacklistPolicy();
String json = "{" + " \"ipList\" : [" + " \"10.0.*.*\"" + " ]" + "}";
IPListConfig config = policy.parseConfiguration(json);
ApiRequest request = new ApiRequest();
request.setType("GET");
request.setApiKey("12345");
request.setDestination("/");
IPolicyContext context = Mockito.mock(IPolicyContext.class);
final PolicyFailure failure = new PolicyFailure();
Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() {
@Override
public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) {
return failure;
}
});
IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
// Failures
request.setRemoteAddr("10.0.123.87");
policy.apply(request, context, config, chain);
Mockito.verify(chain).doFailure(failure);
request.setRemoteAddr("10.0.97.1");
chain = Mockito.mock(IPolicyChain.class);
policy.apply(request, context, config, chain);
Mockito.verify(chain).doFailure(failure);
// Successes
request.setRemoteAddr("9.8.7.6");
chain = Mockito.mock(IPolicyChain.class);
policy.apply(request, context, config, chain);
Mockito.verify(chain).doApply(request);
request.setRemoteAddr("10.10.15.123");
chain = Mockito.mock(IPolicyChain.class);
policy.apply(request, context, config, chain);
Mockito.verify(chain).doApply(request);
}
use of io.apiman.gateway.engine.beans.PolicyFailureType in project apiman by apiman.
the class IPWhitelistPolicyTest method testApply.
/**
* Test method for {@link io.apiman.gateway.engine.policies.IPWhitelistPolicy#parseConfiguration(java.lang.String)}.
*/
@Test
public void testApply() {
IPWhitelistPolicy policy = new IPWhitelistPolicy();
String json = "{" + " \"ipList\" : [" + " \"1.2.3.4\"," + " \"3.4.5.6\"," + " \"10.0.0.11\"" + " ]" + "}";
Object config = policy.parseConfiguration(json);
ApiRequest request = new ApiRequest();
request.setType("GET");
request.setApiKey("12345");
request.setRemoteAddr("1.2.3.4");
request.setDestination("/");
IPolicyContext context = Mockito.mock(IPolicyContext.class);
IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
// Success
policy.apply(request, context, config, chain);
Mockito.verify(chain).doApply(request);
// Failure
final PolicyFailure failure = new PolicyFailure();
Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() {
@Override
public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) {
return failure;
}
});
chain = Mockito.mock(IPolicyChain.class);
request.setRemoteAddr("9.8.7.6");
policy.apply(request, context, config, chain);
Mockito.verify(chain).doFailure(failure);
}
use of io.apiman.gateway.engine.beans.PolicyFailureType in project apiman by apiman.
the class AuthorizationPolicyTest method doTest.
private void doTest(String json, HashSet<String> userRoles, String verb, String path, boolean shouldSucceed) {
AuthorizationPolicy policy = new AuthorizationPolicy();
Object config = policy.parseConfiguration(json);
ApiRequest request = new ApiRequest();
request.setType(verb);
request.setDestination(path);
IPolicyContext context = Mockito.mock(IPolicyContext.class);
IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
Mockito.when(context.getAttribute(AuthorizationPolicy.AUTHENTICATED_USER_ROLES, (HashSet<String>) null)).thenReturn(userRoles);
final PolicyFailure failure = new PolicyFailure();
Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() {
@Override
public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) {
failure.setFailureCode(failureCode);
failure.setType(type);
failure.setMessage(message);
return failure;
}
});
// Success
policy.apply(request, context, config, chain);
if (shouldSucceed) {
Mockito.verify(chain).doApply(request);
} else {
Mockito.verify(chain).doFailure(failure);
}
}
use of io.apiman.gateway.engine.beans.PolicyFailureType in project apiman by apiman.
the class LdapTestMixin method doTest.
// pass null if you expect success
default void doTest(String json, String username, String password, Integer expectedFailureCode, Set<String> expectedRoles, ILdapComponent ldapComponentUnderTest) {
BasicAuthenticationPolicy policy = new BasicAuthenticationPolicy();
BasicAuthenticationConfig config = policy.parseConfiguration(json);
ApiRequest request = new ApiRequest();
request.setType("GET");
request.setApiKey("12345");
request.setRemoteAddr("1.2.3.4");
request.setDestination("/");
IPolicyContext context = Mockito.mock(IPolicyContext.class);
final PolicyFailure failure = new PolicyFailure();
Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn((PolicyFailureType type, int failureCode, String message) -> {
failure.setType(type);
failure.setFailureCode(failureCode);
failure.setMessage(message);
return failure;
});
// The LDAP stuff we're testing!
Mockito.when(context.getComponent(ILdapComponent.class)).thenReturn(ldapComponentUnderTest);
IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
if (username != null) {
request.getHeaders().put("Authorization", createBasicAuthorization(username, password));
}
if (expectedFailureCode == null) {
policy.apply(request, context, config, chain);
Mockito.verify(chain).doApply(request);
} else {
policy.apply(request, context, config, chain);
Mockito.verify(chain).doFailure(failure);
Assert.assertEquals(expectedFailureCode.intValue(), failure.getFailureCode());
}
if (expectedRoles != null && expectedFailureCode == null) {
Mockito.verify(context).setAttribute(AuthorizationPolicy.AUTHENTICATED_USER_ROLES, expectedRoles);
}
}
Aggregations