1
0
mirror of https://github.com/pcvolkmer/etl-processor.git synced 2025-04-20 17:56:50 +00:00

test: add tests to and reorganize ConfigControllerTest

This commit is contained in:
Paul-Christian Volkmer 2024-05-06 17:14:22 +02:00
parent 9c6bd64a7e
commit 24ebbf3b50

View File

@ -19,6 +19,8 @@
package dev.dnpm.etl.processor.web package dev.dnpm.etl.processor.web
import com.gargoylesoftware.htmlunit.WebClient
import com.gargoylesoftware.htmlunit.html.HtmlPage
import dev.dnpm.etl.processor.config.AppConfiguration import dev.dnpm.etl.processor.config.AppConfiguration
import dev.dnpm.etl.processor.config.AppSecurityConfiguration import dev.dnpm.etl.processor.config.AppSecurityConfiguration
import dev.dnpm.etl.processor.monitoring.GPasConnectionCheckService import dev.dnpm.etl.processor.monitoring.GPasConnectionCheckService
@ -32,6 +34,7 @@ import dev.dnpm.etl.processor.services.TransformationService
import dev.dnpm.etl.processor.services.UserRoleService import dev.dnpm.etl.processor.services.UserRoleService
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.ArgumentMatchers.anyString import org.mockito.ArgumentMatchers.anyString
@ -45,12 +48,14 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.mock.mockito.MockBean import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.http.HttpHeaders import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType import org.springframework.http.MediaType
import org.springframework.security.test.context.support.WithMockUser
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.anonymous import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.anonymous
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user
import org.springframework.test.context.ContextConfiguration import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.TestPropertySource import org.springframework.test.context.TestPropertySource
import org.springframework.test.context.junit.jupiter.SpringExtension import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.servlet.* import org.springframework.test.web.servlet.*
import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder
import reactor.core.publisher.Sinks import reactor.core.publisher.Sinks
abstract class MockSink : Sinks.Many<Boolean> abstract class MockSink : Sinks.Many<Boolean>
@ -66,10 +71,7 @@ abstract class MockSink : Sinks.Many<Boolean>
) )
@TestPropertySource( @TestPropertySource(
properties = [ properties = [
"app.pseudonymize.generator=BUILDIN", "app.pseudonymize.generator=BUILDIN"
"app.security.admin-user=admin",
"app.security.admin-password={noop}very-secret",
"app.security.enable-tokens=true"
] ]
) )
@MockBean(name = "configsUpdateProducer", classes = [MockSink::class]) @MockBean(name = "configsUpdateProducer", classes = [MockSink::class])
@ -80,28 +82,28 @@ abstract class MockSink : Sinks.Many<Boolean>
TransformationService::class, TransformationService::class,
GPasConnectionCheckService::class, GPasConnectionCheckService::class,
RestConnectionCheckService::class, RestConnectionCheckService::class,
TokenService::class,
UserRoleService::class UserRoleService::class
) )
class ConfigControllerTest { class ConfigControllerTest {
private lateinit var mockMvc: MockMvc private lateinit var mockMvc: MockMvc
private lateinit var webClient: WebClient
private lateinit var requestProcessor: RequestProcessor private lateinit var requestProcessor: RequestProcessor
private lateinit var tokenService: TokenService
private lateinit var userRoleService: UserRoleService private lateinit var userRoleService: UserRoleService
@BeforeEach @BeforeEach
fun setup( fun setup(
@Autowired mockMvc: MockMvc, @Autowired mockMvc: MockMvc,
@Autowired requestProcessor: RequestProcessor, @Autowired requestProcessor: RequestProcessor,
@Autowired tokenService: TokenService,
@Autowired userRoleService: UserRoleService @Autowired userRoleService: UserRoleService
) { ) {
this.mockMvc = mockMvc this.mockMvc = mockMvc
this.webClient = MockMvcWebClientBuilder.mockMvcSetup(mockMvc).build()
this.requestProcessor = requestProcessor this.requestProcessor = requestProcessor
this.tokenService = tokenService
this.userRoleService = userRoleService this.userRoleService = userRoleService
webClient.options.isThrowExceptionOnScriptError = false
} }
@Test @Test
@ -128,6 +130,28 @@ class ConfigControllerTest {
} }
} }
@Nested
@TestPropertySource(
properties = [
"app.security.enable-tokens=true",
"app.security.admin-user=admin"
]
)
@MockBean(
TokenService::class
)
inner class WithTokensEnabled {
private lateinit var tokenService: TokenService
@BeforeEach
fun setup(
@Autowired tokenService: TokenService
) {
webClient.options.isThrowExceptionOnScriptError = false
this.tokenService = tokenService
}
@Test @Test
fun testShouldSaveNewToken() { fun testShouldSaveNewToken() {
mockMvc.post("/configs/tokens") { mockMvc.post("/configs/tokens") {
@ -182,6 +206,52 @@ class ConfigControllerTest {
assertThat(captor.firstValue).isEqualTo(42) assertThat(captor.firstValue).isEqualTo(42)
} }
@Test
@WithMockUser(username = "admin", roles = ["ADMIN"])
fun testShouldRenderConfigPageWithTokens() {
val page = webClient.getPage<HtmlPage>("http://localhost/configs")
assertThat(
page.getElementById("tokens")
).isNotNull
}
}
@Nested
@TestPropertySource(
properties = [
"app.security.enable-tokens=false"
]
)
inner class WithTokensDisabled {
@BeforeEach
fun setup() {
webClient.options.isThrowExceptionOnScriptError = false
}
@Test
@WithMockUser(username = "admin", roles = ["ADMIN"])
fun testShouldRenderConfigPageWithoutTokens() {
val page = webClient.getPage<HtmlPage>("http://localhost/configs")
assertThat(
page.getElementById("tokens")
).isNull()
}
}
@Nested
@TestPropertySource(
properties = [
"app.security.enable-tokens=false",
"app.security.admin-user=admin",
"app.security.admin-password={noop}very-secret"
]
)
inner class WithUserRolesEnabled {
@BeforeEach
fun setup() {
webClient.options.isThrowExceptionOnScriptError = false
}
@Test @Test
fun testShouldDeleteUserRole() { fun testShouldDeleteUserRole() {
mockMvc.delete("/configs/userroles/42") { mockMvc.delete("/configs/userroles/42") {
@ -217,4 +287,31 @@ class ConfigControllerTest {
assertThat(idCaptor.firstValue).isEqualTo(42) assertThat(idCaptor.firstValue).isEqualTo(42)
assertThat(roleCaptor.firstValue).isEqualTo(Role.ADMIN) assertThat(roleCaptor.firstValue).isEqualTo(Role.ADMIN)
} }
@Test
@WithMockUser(username = "admin", roles = ["ADMIN"])
fun testShouldRenderConfigPageWithUserRoles() {
val page = webClient.getPage<HtmlPage>("http://localhost/configs")
assertThat(
page.getElementById("userroles")
).isNotNull
}
}
@Nested
inner class WithUserRolesDisabled {
@BeforeEach
fun setup() {
webClient.options.isThrowExceptionOnScriptError = false
}
@Test
fun testShouldRenderConfigPageWithoutUserRoles() {
val page = webClient.getPage<HtmlPage>("http://localhost/configs")
assertThat(
page.getElementById("userroles")
).isNull()
}
}
} }