/** * Verifies that the service removes all credentials for a device but keeps credentials * of other devices. * * @param ctx The vert.x test context. */ @Test public void testRemoveCredentialsByDeviceSucceeds(final TestContext ctx) { register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", ctx); register(getCompleteCredentialsService(), "tenant", "device", "myOtherId", "myOtherType", ctx); register(getCompleteCredentialsService(), "tenant", "other-device", "thirdId", "myType", ctx); final Async remove = ctx.async(); getCompleteCredentialsService().removeAll("tenant", "device", ctx.asyncAssertSuccess(s -> { assertThat(s.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT)); assertNotRegistered(getCompleteCredentialsService(), "tenant", "myId", "myType", ctx); assertNotRegistered(getCompleteCredentialsService(), "tenant", "myOtherId", "myOtherType", ctx); assertRegistered(getCompleteCredentialsService(), "tenant", "thirdId", "myType", ctx); remove.complete(); })); remove.await(); }
/** * Verifies that the service removes credentials for a given auth-id and type. * * @param ctx The vert.x test context. */ @Test public void testRemoveCredentialsByAuthIdAndTypeSucceeds(final TestContext ctx) { register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", ctx); final Async remove = ctx.async(); getCompleteCredentialsService().remove("tenant", "myType", "myId", ctx.asyncAssertSuccess(s -> { assertThat(s.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT)); assertNotRegistered(getCompleteCredentialsService(), "tenant", "myId", "myType", ctx); remove.complete(); })); remove.await(); }
/** * Verifies that only one set of credentials can be registered for an auth-id and type (per tenant). * * @param ctx The vert.x test context. */ @Test public void testAddCredentialsRefusesDuplicateRegistration(final TestContext ctx) { register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", ctx); final JsonObject payload2 = new JsonObject() .put(CredentialsConstants.FIELD_PAYLOAD_DEVICE_ID, "other-device") .put(CredentialsConstants.FIELD_AUTH_ID, "myId") .put(CredentialsConstants.FIELD_TYPE, "myType") .put(CredentialsConstants.FIELD_SECRETS, new JsonArray()); final Async add = ctx.async(); getCompleteCredentialsService().add("tenant", payload2, ctx.asyncAssertSuccess(s -> { assertThat(s.getStatus(), is(HttpURLConnection.HTTP_CONFLICT)); add.complete(); })); add.await(); }
protected static void register( final CompleteCredentialsService svc, final String tenant, final String deviceId, final String authId, final String type, final JsonObject clientContext, final JsonArray secrets, final TestContext ctx) { final JsonObject data = new JsonObject() .put(CredentialsConstants.FIELD_PAYLOAD_DEVICE_ID, deviceId) .put(CredentialsConstants.FIELD_AUTH_ID, authId) .put(CredentialsConstants.FIELD_TYPE, type) .put(CredentialsConstants.FIELD_SECRETS, secrets); if (clientContext != null) { data.mergeIn(clientContext); } final Async registration = ctx.async(); svc.add("tenant", data, ctx.asyncAssertSuccess(s -> { assertThat(s.getStatus(), is(HttpURLConnection.HTTP_CREATED)); registration.complete(); })); registration.await(); } }
/** * Verifies that the service returns 404 if a client wants to retrieve non-existing credentials. * * @param ctx The vert.x test context. */ @Test public void testGetCredentialsFailsForNonExistingCredentials(final TestContext ctx) { final Async get = ctx.async(); getCompleteCredentialsService().get("tenant", "myType", "non-existing", ctx.asyncAssertSuccess(s -> { assertThat(s.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND)); get.complete(); })); get.await(); }
/** * Verifies that service returns 404 if a client provides wrong client context. * * @param ctx The vert.x test context. */ @Test public void testGetCredentialsFailsForWrongClientContext(final TestContext ctx) { final JsonObject clientContext = new JsonObject() .put("client-id", "gateway-one"); register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", clientContext, new JsonArray(), ctx); final JsonObject testContext = new JsonObject() .put("client-id", "gateway-two"); final Async get = ctx.async(); getCompleteCredentialsService().get("tenant", "myType", "myId", testContext, ctx.asyncAssertSuccess(s -> { assertThat(s.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND)); get.complete(); })); get.await(2000); }
/** * Verifies that service returns existing credentials for proper client context. * * @param ctx The vert.x test context. */ @Test public void testGetCredentialsSucceedsForProperClientContext(final TestContext ctx) { final JsonObject clientContext = new JsonObject() .put("client-id", "gateway-one"); register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", clientContext, new JsonArray(), ctx); final Async get = ctx.async(); getCompleteCredentialsService().get("tenant", "myType", "myId", clientContext, ctx.asyncAssertSuccess(s -> { assertThat(s.getStatus(), is(HttpURLConnection.HTTP_OK)); assertThat(s.getPayload().getString(CredentialsConstants.FIELD_AUTH_ID), is("myId")); assertThat(s.getPayload().getString(CredentialsConstants.FIELD_TYPE), is("myType")); get.complete(); })); get.await(2000); }
/** * Verifies that the service returns existing credentials. * * @param ctx The vert.x test context. */ @Test public void testGetCredentialsSucceedsForExistingCredentials(final TestContext ctx) { register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", ctx); final Async get = ctx.async(); getCompleteCredentialsService().get("tenant", "myType", "myId", ctx.asyncAssertSuccess(s -> { assertThat(s.getStatus(), is(HttpURLConnection.HTTP_OK)); assertThat(s.getPayload().getString(CredentialsConstants.FIELD_AUTH_ID), is("myId")); assertThat(s.getPayload().getString(CredentialsConstants.FIELD_TYPE), is("myType")); get.complete(); })); get.await(); }