use of org.eclipse.hono.adapter.auth.device.AbstractDeviceCredentials in project hono by eclipse.
the class HonoBasicAuthHandlerTest method testPreCredentialsValidationHandlerGetsInvoked.
/**
* Verifies that the PreCredentialsValidationHandler given for the AuthHandler is invoked
* when authenticating.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testPreCredentialsValidationHandlerGetsInvoked() {
final AbstractDeviceCredentials deviceCredentials = mock(AbstractDeviceCredentials.class);
final DeviceUser deviceUser = new DeviceUser("tenant", "device");
// prepare authProvider
final DeviceCredentialsAuthProvider<AbstractDeviceCredentials> authProvider = mock(DeviceCredentialsAuthProvider.class);
doReturn(deviceCredentials).when(authProvider).getCredentials(any(JsonObject.class));
doAnswer(invocation -> {
final Handler handler = invocation.getArgument(2);
handler.handle(Future.succeededFuture(deviceUser));
return null;
}).when(authProvider).authenticate(any(AbstractDeviceCredentials.class), any(), VertxMockSupport.anyHandler());
// prepare PreCredentialsValidationHandler
final PreCredentialsValidationHandler<HttpContext> preCredValidationHandler = mock(PreCredentialsValidationHandler.class);
when(preCredValidationHandler.handle(eq(deviceCredentials), any(HttpContext.class))).thenReturn(Future.succeededFuture());
// GIVEN an auth handler with a PreCredentialsValidationHandler
final HonoBasicAuthHandler authHandler = new HonoBasicAuthHandler(authProvider, "test", preCredValidationHandler);
// WHEN the auth handler handles a request
final String authorization = "BASIC " + Base64.getEncoder().encodeToString("user:password".getBytes(StandardCharsets.UTF_8));
final MultiMap headers = mock(MultiMap.class);
when(headers.get(eq(HttpHeaders.AUTHORIZATION))).thenReturn(authorization);
final HttpServerRequest req = mock(HttpServerRequest.class);
when(req.headers()).thenReturn(headers);
final HttpServerResponse resp = mock(HttpServerResponse.class);
final RoutingContext routingContext = mock(RoutingContext.class);
final Map<String, Object> routingContextMap = new HashMap<>();
when(routingContext.put(any(), any())).thenAnswer(invocation -> {
routingContextMap.put(invocation.getArgument(0), invocation.getArgument(1));
return routingContext;
});
when(routingContext.get(any())).thenAnswer(invocation -> routingContextMap.get(invocation.getArgument(0)));
when(routingContext.request()).thenReturn(req);
when(routingContext.response()).thenReturn(resp);
when(routingContext.currentRoute()).thenReturn(mock(Route.class));
authHandler.handle(routingContext);
// THEN authentication succeeds and the PreCredentialsValidationHandler has been invoked
verify(routingContext).setUser(eq(deviceUser));
verify(preCredValidationHandler).handle(eq(deviceCredentials), any(HttpContext.class));
}
Aggregations