use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class HttpTestBase method testUploadMessageFailsForDisabledTenant.
/**
* Verifies that the HTTP adapter rejects messages from a device
* that belongs to a tenant for which the HTTP adapter has been disabled.
*
* @param ctx The test context
*/
@Test
public void testUploadMessageFailsForDisabledTenant(final TestContext ctx) {
// GIVEN a tenant for which the HTTP adapter is disabled
final String tenantId = helper.getRandomTenantId();
final String deviceId = helper.getRandomDeviceId(tenantId);
final JsonObject adapterDetailsHttp = new JsonObject().put(TenantConstants.FIELD_ADAPTERS_TYPE, Constants.PROTOCOL_ADAPTER_TYPE_HTTP).put(TenantConstants.FIELD_ENABLED, Boolean.FALSE);
final TenantObject tenant = TenantObject.from(tenantId, true);
tenant.addAdapterConfiguration(adapterDetailsHttp);
final Async setup = ctx.async();
helper.registry.addTenant(JsonObject.mapFrom(tenant)).compose(ok -> helper.registry.registerDevice(tenantId, deviceId)).setHandler(ctx.asyncAssertSuccess(ok -> setup.complete()));
setup.await();
// WHEN a device that belongs to the tenant uploads a message
send(tenantId, deviceId, Buffer.buffer("hello")).setHandler(ctx.asyncAssertFailure(t -> {
// THEN the message gets rejected by the HTTP adapter
ctx.assertEquals(HttpURLConnection.HTTP_FORBIDDEN, ((ServiceInvocationException) t).getErrorCode());
}));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class TelemetryMqttIT method testConnectFailsForDisabledTenant.
/**
* Verifies that the adapter rejects connection attempts from devices
* that belong to a tenant for which the adapter has been disabled.
*
* @param ctx The test context
*/
@Test
public void testConnectFailsForDisabledTenant(final TestContext ctx) {
// GIVEN a tenant for which the HTTP adapter is disabled
final String tenantId = helper.getRandomTenantId();
final String deviceId = helper.getRandomDeviceId(tenantId);
final String password = "secret";
final JsonObject adapterDetailsHttp = new JsonObject().put(TenantConstants.FIELD_ADAPTERS_TYPE, Constants.PROTOCOL_ADAPTER_TYPE_MQTT).put(TenantConstants.FIELD_ENABLED, Boolean.FALSE);
final TenantObject tenant = TenantObject.from(tenantId, true);
tenant.addAdapterConfiguration(adapterDetailsHttp);
helper.registry.addDeviceForTenant(tenant, deviceId, password).compose(ok -> {
final Future<MqttConnAckMessage> result = Future.future();
final MqttClientOptions options = new MqttClientOptions().setUsername(IntegrationTestSupport.getUsername(deviceId, tenantId)).setPassword(password);
mqttClient = MqttClient.create(VERTX, options);
// WHEN a device that belongs to the tenant tries to connect to the adapter
mqttClient.connect(IntegrationTestSupport.MQTT_PORT, IntegrationTestSupport.MQTT_HOST, result.completer());
return result;
}).setHandler(ctx.asyncAssertFailure(t -> {
// THEN the connection attempt gets rejected
}));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class CredentialsHttpIT method testGetCredentialsForDeviceRegardlessOfType.
/**
* Verifies that the service returns all credentials registered for a given device regardless of type.
* <p>
* The returned JsonObject must consist of the total number of entries and contain all previously added credentials
* in the provided JsonArray that is found under the key of the endpoint {@link CredentialsConstants#CREDENTIALS_ENDPOINT}.
*
* @param context The vert.x test context.
* @throws InterruptedException if registration of credentials is interrupted.
*/
@Test
public void testGetCredentialsForDeviceRegardlessOfType(final TestContext context) throws InterruptedException {
final String pskAuthId = getRandomAuthId(TEST_AUTH_ID);
final List<JsonObject> credentialsToAdd = new ArrayList<>();
for (int i = 0; i < 5; i++) {
final JsonObject requestBody = newPskCredentials(deviceId, pskAuthId);
requestBody.put(CredentialsConstants.FIELD_TYPE, "type" + i);
credentialsToAdd.add(requestBody);
}
addMultipleCredentials(credentialsToAdd).compose(ar -> registry.getCredentials(TENANT, deviceId)).setHandler(context.asyncAssertSuccess(b -> {
assertResponseBodyContainsAllCredentials(context, b.toJsonObject(), credentialsToAdd);
}));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class CredentialsHttpIT method testUpdateCredentialsFailsForNonMatchingAuthIdInPayload.
/**
* Verifies that the service rejects an update request for credentials containing a different authentication
* identifier.
*
* @param context The vert.x test context.
*/
@Test
public void testUpdateCredentialsFailsForNonMatchingAuthIdInPayload(final TestContext context) {
final JsonObject altered = hashedPasswordCredentials.copy().put(CredentialsConstants.FIELD_AUTH_ID, "non-matching-auth-id");
registry.addCredentials(TENANT, hashedPasswordCredentials).compose(ar -> {
return registry.updateCredentials(TENANT, authId, CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, altered, HttpURLConnection.HTTP_BAD_REQUEST);
}).setHandler(context.asyncAssertSuccess());
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class DeviceRegistrationHttpIT method testUpdateDeviceSucceeds.
/**
* Verifies that the registration information provided when updating
* a device replaces the existing information.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUpdateDeviceSucceeds(final TestContext ctx) {
final JsonObject originalData = new JsonObject().put("key1", "value1").put("key2", "value2").put(RegistrationConstants.FIELD_ENABLED, Boolean.TRUE);
final JsonObject updatedData = new JsonObject().put("newKey1", "newValue1").put(RegistrationConstants.FIELD_ENABLED, Boolean.FALSE);
registry.registerDevice(TENANT, deviceId, originalData).compose(ok -> registry.updateDevice(TENANT, deviceId, updatedData)).compose(ok -> registry.getRegistrationInfo(TENANT, deviceId)).compose(info -> {
assertRegistrationInformation(ctx, info.toJsonObject(), deviceId, updatedData);
return Future.succeededFuture();
}).setHandler(ctx.asyncAssertSuccess());
}
Aggregations