/** * Verifies that the provider fails to authenticate a device when not * running on a vert.x Context. * * @param ctx The vert.x test context. */ @Test public void testAuthenticateRequiresVertxContext(final TestContext ctx) { provider.authenticate(deviceCredentials, null, ctx.asyncAssertFailure(e -> { ctx.assertTrue(e instanceof IllegalStateException); })); }
/** * Verifies that the provider fails to validate wrong credentials. * * @param ctx The vert.x test context. */ @Test public void testAuthenticateFailsForWrongCredentials(final TestContext ctx) { when(pwdEncoder.matches(eq("wrong_pwd"), any(JsonObject.class))).thenReturn(false); deviceCredentials = UsernamePasswordCredentials.create("device@DEFAULT_TENANT", "wrong_pwd", false); vertx.runOnContext(go -> { provider.authenticate(deviceCredentials, null, ctx.asyncAssertFailure(e -> { final ClientErrorException error = (ClientErrorException) e; ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, error.getErrorCode()); })); }); }
/** * Verifies that credentials validation fails if none of the secrets on record are * valid any more. * * @param ctx The vert.x test context. */ @Test public void testAuthenticateFailsIfNoSecretsAreValidAnymore(final TestContext ctx) { credentialsOnRecord.addSecret(CredentialsObject.emptySecret(null, Instant.now().minusSeconds(120))); vertx.runOnContext(go -> { provider.authenticate(deviceCredentials, null, ctx.asyncAssertFailure(t -> { // THEN authentication fails with a 401 client error ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode()); })); }); }
/** * Verifies that credentials validation fails if none of the secrets on record are * valid yet. * * @param ctx The vert.x test context. */ @Test public void testAuthenticateFailsIfNoSecretsAreValidYet(final TestContext ctx) { credentialsOnRecord.addSecret(CredentialsObject.emptySecret(Instant.now().plusSeconds(120), null)); vertx.runOnContext(go -> { provider.authenticate(deviceCredentials, null, ctx.asyncAssertFailure(t -> { // THEN authentication fails with a 401 client error ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode()); })); }); } }
/** * Verifies that the provider succeeds to validate matching credentials when * running on a vert.x Context. * * @param ctx The vert.x test context. */ @Test public void testAuthenticateSucceedsWhenRunningOnVertxContext(final TestContext ctx) { vertx.runOnContext(go -> { provider.authenticate(deviceCredentials, null, ctx.asyncAssertSuccess(device -> { ctx.assertEquals("4711", device.getDeviceId()); ctx.assertEquals("DEFAULT_TENANT", device.getTenantId()); })); }); }