use of io.apiman.gateway.engine.impl.DefaultJdbcComponent in project apiman by apiman.
the class BasicAuthJDBCTest method testApplyJdbcWithRoles.
public void testApplyJdbcWithRoles(String json) throws Exception {
// A live LDAP server is required to run this test!
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);
Mockito.when(context.getComponent(IJdbcComponent.class)).thenReturn(new DefaultJdbcComponent());
IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
// Success
request.getHeaders().put("Authorization", createBasicAuthorization(JDBC_USER, JDBC_PASSWORD));
chain = Mockito.mock(IPolicyChain.class);
policy.apply(request, context, config, chain);
Mockito.verify(chain).doApply(request);
Set<String> expectedRoles = new HashSet<>();
expectedRoles.add("admin");
expectedRoles.add("user");
Mockito.verify(context).setAttribute(AuthorizationPolicy.AUTHENTICATED_USER_ROLES, expectedRoles);
}
use of io.apiman.gateway.engine.impl.DefaultJdbcComponent in project apiman by apiman.
the class BasicAuthJDBCTest method testApplyJdbcNoRoles.
/**
* @param json
* @throws Exception
*/
public void testApplyJdbcNoRoles(String json) throws Exception {
// A live LDAP server is required to run this test!
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(new IPolicyFailureFactoryComponent() {
@Override
public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) {
return failure;
}
});
Mockito.when(context.getComponent(IJdbcComponent.class)).thenReturn(new DefaultJdbcComponent());
IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
// Failure
policy.apply(request, context, config, chain);
Mockito.verify(chain).doFailure(failure);
// Failure
request.getHeaders().put("Authorization", createBasicAuthorization(JDBC_USER, "invalid_password"));
chain = Mockito.mock(IPolicyChain.class);
policy.apply(request, context, config, chain);
Mockito.verify(chain).doFailure(failure);
// Success
request.getHeaders().put("Authorization", createBasicAuthorization(JDBC_USER, JDBC_PASSWORD));
chain = Mockito.mock(IPolicyChain.class);
policy.apply(request, context, config, chain);
Mockito.verify(chain).doApply(request);
}
Aggregations