From 55e1fa428cf9b92ec7ef471fd7afaf8e90ebae9a Mon Sep 17 00:00:00 2001 From: Hamza Remmal <2861-remmal@users.noreply.gitlab.epfl.ch> Date: Fri, 27 Dec 2024 12:40:22 +0000 Subject: [PATCH] Polish the testing for '/api/v1/ping' --- ...va => PingControllerIntegrationTests.java} | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) rename autograde-service/src/test/java/ch/epfl/autograde/controller/api/v1/{PingControllerTest.java => PingControllerIntegrationTests.java} (79%) diff --git a/autograde-service/src/test/java/ch/epfl/autograde/controller/api/v1/PingControllerTest.java b/autograde-service/src/test/java/ch/epfl/autograde/controller/api/v1/PingControllerIntegrationTests.java similarity index 79% rename from autograde-service/src/test/java/ch/epfl/autograde/controller/api/v1/PingControllerTest.java rename to autograde-service/src/test/java/ch/epfl/autograde/controller/api/v1/PingControllerIntegrationTests.java index 572d31c8..ab1a2298 100644 --- a/autograde-service/src/test/java/ch/epfl/autograde/controller/api/v1/PingControllerTest.java +++ b/autograde-service/src/test/java/ch/epfl/autograde/controller/api/v1/PingControllerIntegrationTests.java @@ -1,6 +1,8 @@ package ch.epfl.autograde.controller.api.v1; +import ch.epfl.autograde.filters.AssignRequestIdFilter; import ch.epfl.autograde.utils.context.WithSharedSecretAuthentication; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -29,9 +31,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * * @author Hamza REMMAL (hamza.remmal@epfl.ch) */ -@AutoConfigureMockMvc -@SpringBootTest -public final class PingControllerTest { +@SpringBootTest @AutoConfigureMockMvc +@DisplayName("[Integration Tests] '/api/v1/ping'") +public final class PingControllerIntegrationTests { /** The endpoint to perform a ping */ private final static String PING_END_POINT = "/api/v1/ping"; @@ -40,7 +42,7 @@ public final class PingControllerTest { private MockMvc mockMvc; /** - * If <b>not</b> authenticated with the shared secret, the request to `/api/v1/ping` + * If <b>not</b> authenticated with the shared secret, the request to `GET:/api/v1/ping` * should be <b>successful</b> * <p> * The following test will verify that: @@ -49,21 +51,23 @@ public final class PingControllerTest { * <li>The {@link HttpHeaders.CONTENT_TYPE} is {@link MediaType.APPLICATION_JSON}</li> * <li>The request will not generate cookies</li> * <li>The {@code $.auth} field in the response's body is {@code false}</li> + * <li>A `X-Request-Id` header was set in the response</li> * </ul> */ - @Test + @Test @DisplayName("'GET:/api/v1/ping' without authentication works as expected") void successPingWithoutKey() throws Exception { mockMvc.perform(get(PING_END_POINT)) .andExpectAll( status().isOk(), content().contentType(MediaType.APPLICATION_JSON), header().doesNotExist(HttpHeaders.SET_COOKIE), + header().exists(AssignRequestIdFilter.REQUEST_ID_HEADER), jsonPath("$.auth").value(false) ); } /** - * If authenticated with the shared secret, the request to `/api/v1/ping` + * If authenticated with the shared secret, the request to `GET:/api/v1/ping` * should be <b>successful</b> * <p> * The following test will verify that: @@ -72,9 +76,10 @@ public final class PingControllerTest { * <li>The {@link HttpHeaders.CONTENT_TYPE} is {@link MediaType.APPLICATION_JSON}</li> * <li>The request will not generate cookies</li> * <li>The {@code $.auth} field in the response's body is {@code true}</li> + * <li>A `X-Request-Id` header was set in the response</li> * </ul> */ - @Test + @Test @DisplayName("'GET:/api/v1/ping' with authentication works as expected") @WithSharedSecretAuthentication void successPingWithKey() throws Exception { mockMvc.perform(get(PING_END_POINT)) @@ -82,10 +87,9 @@ public final class PingControllerTest { status().isOk(), content().contentType(MediaType.APPLICATION_JSON), header().doesNotExist(HttpHeaders.SET_COOKIE), + header().exists(AssignRequestIdFilter.REQUEST_ID_HEADER), jsonPath("$.auth").value(true) ); } - // TODO: We need to test authorization here when we implement it - } -- GitLab