use of io.apiman.gateway.engine.policies.BasicAuthenticationPolicy 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