Add tests for healthcheck port requests in ApiAuthInterception 40/96840/1
authorRemigiusz Janeczek <remigiusz.janeczek@nokia.com>
Mon, 7 Oct 2019 12:47:02 +0000 (14:47 +0200)
committerRemigiusz Janeczek <remigiusz.janeczek@nokia.com>
Wed, 9 Oct 2019 07:18:51 +0000 (09:18 +0200)
Issue-ID: DCAEGEN2-1819
Change-Id: Id4f333e856bd5bbef14d4322a8eb7c01575f4796
Signed-off-by: Remigiusz Janeczek <remigiusz.janeczek@nokia.com>
src/test/java/org/onap/dcae/restapi/ApiAuthInterceptionTest.java

index e6d67cf..c80b56c 100644 (file)
@@ -174,4 +174,40 @@ public class ApiAuthInterceptionTest {
     verify(response).setStatus(HttpStatus.UNAUTHORIZED.value());
     verify(writer).write(ApiException.UNAUTHORIZED_USER.toJSON().toString());
   }
+
+  @Test
+  public void shouldSucceedForHealthcheckOnHealthcheckPort() throws IOException {
+    // given
+    final HttpServletRequest request =
+            MockMvcRequestBuilders
+                    .get("/healthcheck")
+                    .buildRequest(null);
+
+    when(settings.authMethod()).thenReturn(AuthMethodType.CERT_BASIC_AUTH.value());
+    when(settings.httpPort()).thenReturn(request.getServerPort());
+
+    // when
+    final boolean isAuthorized = sut.preHandle(request, response, obj);
+
+    // then
+    assertTrue(isAuthorized);
+  }
+
+  @Test
+  public void shouldFailDueToNotPermittedOperationOnHealthcheckPort() throws IOException {
+    // given
+    final HttpServletRequest request = createEmptyRequest();
+
+    when(settings.authMethod()).thenReturn(AuthMethodType.CERT_BASIC_AUTH.value());
+    when(settings.httpPort()).thenReturn(request.getServerPort());
+    when(response.getWriter()).thenReturn(writer);
+
+    // when
+    final boolean isAuthorized = sut.preHandle(request, response, obj);
+
+    // then
+    assertFalse(isAuthorized);
+    verify(response).setStatus(HttpStatus.BAD_REQUEST.value());
+  }
+
 }