|
| 1 | +package com.uid2.core.handler; |
| 2 | + |
| 3 | +import com.uid2.shared.auth.IAuthorizable; |
| 4 | +import com.uid2.shared.middleware.AuthMiddleware; |
| 5 | +import io.vertx.core.http.HttpServerResponse; |
| 6 | +import io.vertx.core.json.JsonObject; |
| 7 | +import io.vertx.ext.web.RoutingContext; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.mockito.ArgumentCaptor; |
| 10 | + |
| 11 | +import java.net.HttpURLConnection; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 17 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 18 | +import static org.mockito.ArgumentMatchers.anyString; |
| 19 | +import static org.mockito.Mockito.mock; |
| 20 | +import static org.mockito.Mockito.verify; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +class GenericFailureHandlerTest { |
| 24 | + |
| 25 | + // Drives the handler for a given status code + resolved auth profile, returning the response body written. |
| 26 | + private static String handleAndCaptureBody(int statusCode, IAuthorizable profile) { |
| 27 | + RoutingContext ctx = mock(RoutingContext.class); |
| 28 | + HttpServerResponse response = mock(HttpServerResponse.class); |
| 29 | + |
| 30 | + Map<String, Object> data = new HashMap<>(); |
| 31 | + if (profile != null) { |
| 32 | + data.put(AuthMiddleware.API_CLIENT_PROP, profile); |
| 33 | + } |
| 34 | + |
| 35 | + when(ctx.statusCode()).thenReturn(statusCode); |
| 36 | + when(ctx.response()).thenReturn(response); |
| 37 | + when(ctx.normalizedPath()).thenReturn("/attest"); |
| 38 | + when(ctx.failure()).thenReturn(null); |
| 39 | + when(ctx.data()).thenReturn(data); |
| 40 | + |
| 41 | + when(response.ended()).thenReturn(false); |
| 42 | + when(response.closed()).thenReturn(false); |
| 43 | + when(response.putHeader(anyString(), anyString())).thenReturn(response); |
| 44 | + when(response.setStatusCode(anyInt())).thenReturn(response); |
| 45 | + |
| 46 | + new GenericFailureHandler().handle(ctx); |
| 47 | + |
| 48 | + ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class); |
| 49 | + verify(response).end(bodyCaptor.capture()); |
| 50 | + return bodyCaptor.getValue(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void unknownKeyReturnsUnrecognizedKeyReason() { |
| 55 | + // No auth profile resolved -> key was not recognized (the 4eyes.ai transcription-error case). |
| 56 | + JsonObject body = new JsonObject(handleAndCaptureBody(HttpURLConnection.HTTP_UNAUTHORIZED, null)); |
| 57 | + |
| 58 | + assertEquals("unauthorized", body.getString("status")); |
| 59 | + assertEquals("unrecognized_key", body.getString("reason")); |
| 60 | + assertTrue(body.getString("message").toLowerCase().contains("not recognized")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void disabledKeyReturnsKeyDisabledReason() { |
| 65 | + IAuthorizable disabledKey = mock(IAuthorizable.class); |
| 66 | + when(disabledKey.isDisabled()).thenReturn(true); |
| 67 | + |
| 68 | + JsonObject body = new JsonObject(handleAndCaptureBody(HttpURLConnection.HTTP_UNAUTHORIZED, disabledKey)); |
| 69 | + |
| 70 | + assertEquals("unauthorized", body.getString("status")); |
| 71 | + assertEquals("key_disabled", body.getString("reason")); |
| 72 | + assertTrue(body.getString("message").toLowerCase().contains("disabled")); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void recognizedButUnauthorizedKeyReturnsInsufficientRoleReason() { |
| 77 | + IAuthorizable wrongRoleKey = mock(IAuthorizable.class); |
| 78 | + when(wrongRoleKey.isDisabled()).thenReturn(false); |
| 79 | + |
| 80 | + JsonObject body = new JsonObject(handleAndCaptureBody(HttpURLConnection.HTTP_UNAUTHORIZED, wrongRoleKey)); |
| 81 | + |
| 82 | + assertEquals("unauthorized", body.getString("status")); |
| 83 | + assertEquals("insufficient_role", body.getString("reason")); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void nonUnauthorizedStatusKeepsPlainReasonPhrase() { |
| 88 | + // Non-401 failures must keep the existing bare reason-phrase body (no behaviour change). |
| 89 | + String body = handleAndCaptureBody(HttpURLConnection.HTTP_BAD_REQUEST, null); |
| 90 | + |
| 91 | + assertEquals("Bad Request", body); |
| 92 | + } |
| 93 | +} |
0 commit comments