Search in sources :

Example 1 with BasicAuthenticationPolicy

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);
    }
}
Also used : IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) BasicAuthenticationPolicy(io.apiman.gateway.engine.policies.BasicAuthenticationPolicy) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) BasicAuthenticationConfig(io.apiman.gateway.engine.policies.config.BasicAuthenticationConfig) PolicyFailureType(io.apiman.gateway.engine.beans.PolicyFailureType) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest)

Aggregations

ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)1 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)1 PolicyFailureType (io.apiman.gateway.engine.beans.PolicyFailureType)1 BasicAuthenticationPolicy (io.apiman.gateway.engine.policies.BasicAuthenticationPolicy)1 BasicAuthenticationConfig (io.apiman.gateway.engine.policies.config.BasicAuthenticationConfig)1 IPolicyContext (io.apiman.gateway.engine.policy.IPolicyContext)1