/** * Sets up the fixture. */ @Before public void setUp() { credentialsClient = mock(CredentialsClient.class); when(credentialsClient.isOpen()).thenReturn(Boolean.TRUE); honoClient = mock(HonoClient.class); when(honoClient.getOrCreateCredentialsClient(anyString())).thenReturn(Future.succeededFuture(credentialsClient)); provider = getProvider(getDeviceCredentials("type", "TENANT", "user"), NoopTracerFactory.create()); }
/** * Verifies that credentials validation fails if the credentials on record are disabled. * * @param ctx The vert.x test context. */ @Test public void testValidateFailsIfCredentialsAreDisabled(final TestContext ctx) { // WHEN trying to authenticate a disabled device final AbstractDeviceCredentials creds = getDeviceCredentials("type", "tenant", "identity"); final CredentialsObject credentialsOnRecord = getCredentialsObject("type", "identity", "device", false) .addSecret(CredentialsObject.emptySecret(Instant.now().minusSeconds(120), null)); when(credentialsClient.get(eq("type"), eq("identity"), any(JsonObject.class), any())) .thenReturn(Future.succeededFuture(credentialsOnRecord)); provider.authenticate(creds, null, ctx.asyncAssertFailure(t -> { // THEN authentication fails with a 401 client error ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode()); })); }
/** * 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 provider = getProvider(null, NoopTracerFactory.create()); provider.authenticate(new JsonObject(), ctx.asyncAssertFailure(t -> { // THEN authentication fails with a 401 client error ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode()); })); }