use of org.eclipse.hono.adapter.auth.device.UsernamePasswordCredentials in project hono by eclipse.
the class HonoBasicAuthHandlerTest method testHandleFailsWithStatusCodeFromAuthProvider.
/**
* Verifies that the handler returns the status code conveyed in a
* failed {@code AuthenticationProvider} invocation in the response.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testHandleFailsWithStatusCodeFromAuthProvider() {
// GIVEN an auth handler configured with an auth provider that
// fails with a 503 error code during authentication
final DeviceCredentialsAuthProvider<UsernamePasswordCredentials> authProvider = mock(DeviceCredentialsAuthProvider.class);
final ServiceInvocationException error = new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE);
doAnswer(invocation -> {
final Handler handler = invocation.getArgument(2);
handler.handle(Future.failedFuture(error));
return null;
}).when(authProvider).authenticate(any(UsernamePasswordCredentials.class), any(), VertxMockSupport.anyHandler());
when(authProvider.getCredentials(any(JsonObject.class))).thenReturn(UsernamePasswordCredentials.create("device1@DEFAULT_TENANT", "secret"));
final HonoBasicAuthHandler authHandler = new HonoBasicAuthHandler(authProvider, "test");
// WHEN trying to authenticate a request using the HTTP BASIC scheme
final String authorization = "BASIC " + Base64.getEncoder().encodeToString("user:password".getBytes(StandardCharsets.UTF_8));
final MultiMap headers = MultiMap.caseInsensitiveMultiMap();
headers.add(HttpHeaders.AUTHORIZATION, authorization);
final HttpServerRequest req = mock(HttpServerRequest.class);
when(req.headers()).thenReturn(headers);
final HttpServerResponse resp = mock(HttpServerResponse.class);
final RoutingContext ctx = mock(RoutingContext.class);
when(ctx.request()).thenReturn(req);
when(ctx.response()).thenReturn(resp);
authHandler.handle(ctx);
// THEN the request context is failed with the 503 error code
verify(ctx).fail(error);
}
Aggregations