use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class AbstractProtocolAdapterBaseTest method testGetRegistrationAssertionSucceedsForExistingDevice.
/**
* Verifies that the adapter successfully retrieves a registration assertion
* for an existing device.
*
* @param ctx The vert.x test context.
*/
@Test
public void testGetRegistrationAssertionSucceedsForExistingDevice(final TestContext ctx) {
// GIVEN an adapter connected to a registration service
final JsonObject assertionResult = newRegistrationAssertionResult("token");
when(registrationClient.assertRegistration(eq("device"), any())).thenReturn(Future.succeededFuture(assertionResult));
// WHEN an assertion for the device is retrieved
adapter.getRegistrationAssertion("tenant", "device", null).setHandler(ctx.asyncAssertSuccess(result -> {
// THEN the result contains the registration assertion
ctx.assertEquals(assertionResult, result);
}));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class CredentialsApiAuthProviderTest method testAuthenticateFailsWithExceptionReportedByCredentialsClient.
/**
* Verifies that the auth provider propagates the exception reported by a failed invocation
* of the credentials service.
*
* @param ctx The vert.x test context.
*/
@Test
public void testAuthenticateFailsWithExceptionReportedByCredentialsClient(final TestContext ctx) {
final ServerErrorException reportedException = new ServerErrorException(503);
when(credentialsClient.isOpen()).thenReturn(Boolean.TRUE);
when(credentialsClient.get(anyString(), anyString())).thenReturn(Future.failedFuture(reportedException));
provider.authenticate(UsernamePasswordCredentials.create("user@TENANT", "pwd", false), ctx.asyncAssertFailure(t -> {
ctx.assertEquals(t, reportedException);
}));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class CredentialsApiAuthProviderTest method testAuthenticateFailsWith401ForMalformedCredentials.
/**
* Verifies that the auth provider fails an authentication request with a 401
* {@code ClientErrorException} if the credentials cannot be parsed.
*
* @param ctx The vert.x test context.
*/
@Test
public void testAuthenticateFailsWith401ForMalformedCredentials(final TestContext ctx) {
// WHEN trying to authenticate using malformed credentials
// that do not contain a tenant
final JsonObject authInfo = new JsonObject().put("username", "no-tenant").put("password", "secret");
provider.authenticate(authInfo, ctx.asyncAssertFailure(t -> {
// THEN authentication fails with a 401 client error
ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode());
}));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class CredentialsApiAuthProviderTest method testAuthenticateFailsWith401ForNonExistingAuthId.
/**
* Verifies that the auth provider fails an authentication request with a 401
* {@code ClientErrorException} if the auth-id is unknown.
*
* @param ctx The vert.x test context.
*/
@Test
public void testAuthenticateFailsWith401ForNonExistingAuthId(final TestContext ctx) {
// WHEN trying to authenticate using an auth-id that is not known
final JsonObject authInfo = new JsonObject().put("username", "unknown@TENANT").put("password", "secret");
when(credentialsClient.isOpen()).thenReturn(Boolean.TRUE);
when(credentialsClient.get(anyString(), eq("unknown"))).thenReturn(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND)));
provider.authenticate(authInfo, ctx.asyncAssertFailure(t -> {
// THEN authentication fails with a 401 client error
ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode());
}));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class BaseCredentialsServiceTest method testGetFailsForMissingType.
/**
* Verifies that the base service fails a request for getting credentials
* with a 400 error code if the type is missing.
*
* @param ctx The vert.x test context.
*/
@Test
public void testGetFailsForMissingType(final TestContext ctx) {
// GIVEN a request for getting credentials that does not specify a type
final CredentialsObject malformedPayload = new CredentialsObject().setAuthId("bumlux").addSecret(CredentialsObject.emptySecret(null, null));
final EventBusMessage request = createRequestForPayload(CredentialsConstants.CredentialsAction.get, JsonObject.mapFrom(malformedPayload));
// WHEN processing the request
service.processRequest(request).setHandler(ctx.asyncAssertFailure(t -> {
// THEN the response contains a 400 error code
ctx.assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, ((ServiceInvocationException) t).getErrorCode());
}));
}
Aggregations