From f9b13510032d5819f3ea822758d6676da831bf4b Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Thu, 10 Nov 2022 17:21:41 +0800 Subject: [PATCH] test: add Actuator REST API tests --- .../komga/interfaces/api/rest/ActuatorTest.kt | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 komga/src/test/kotlin/org/gotson/komga/interfaces/api/rest/ActuatorTest.kt diff --git a/komga/src/test/kotlin/org/gotson/komga/interfaces/api/rest/ActuatorTest.kt b/komga/src/test/kotlin/org/gotson/komga/interfaces/api/rest/ActuatorTest.kt new file mode 100644 index 00000000..c34b9a50 --- /dev/null +++ b/komga/src/test/kotlin/org/gotson/komga/interfaces/api/rest/ActuatorTest.kt @@ -0,0 +1,75 @@ +package org.gotson.komga.interfaces.api.rest + +import org.gotson.komga.domain.model.ROLE_ADMIN +import org.gotson.komga.domain.model.ROLE_USER +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.security.test.context.support.WithAnonymousUser +import org.springframework.security.test.context.support.WithMockUser +import org.springframework.test.context.ActiveProfiles +import org.springframework.test.context.junit.jupiter.SpringExtension +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.get + +@ExtendWith(SpringExtension::class) +@SpringBootTest +@AutoConfigureMockMvc(printOnlyOnFailure = false) +@ActiveProfiles("test") +class ActuatorTest( + @Autowired private val mockMvc: MockMvc, +) { + + @Test + @WithAnonymousUser + fun `given anonymous user when getting actuator endpoints then returns unauthorized`() { + mockMvc.get("/actuator") + .andExpect { + status { isUnauthorized() } + } + + mockMvc.get("/actuator/beans") + .andExpect { + status { isUnauthorized() } + } + } + + @Test + @WithAnonymousUser + fun `given anonymous user when getting actuator health endpoint then returns ok`() { + mockMvc.get("/actuator/health") + .andExpect { + status { isOk() } + } + } + + @Test + @WithMockUser(roles = [ROLE_USER]) + fun `given regular user when getting actuator endpoints then returns forbidden`() { + mockMvc.get("/actuator") + .andExpect { + status { isForbidden() } + } + + mockMvc.get("/actuator/beans") + .andExpect { + status { isForbidden() } + } + } + + @Test + @WithMockUser(roles = [ROLE_ADMIN]) + fun `given admin user when getting actuator endpoints then returns ok`() { + mockMvc.get("/actuator") + .andExpect { + status { isOk() } + } + + mockMvc.get("/actuator/beans") + .andExpect { + status { isOk() } + } + } +}