Added some basic tests for BaseController

This commit is contained in:
neviyn 2021-05-12 17:29:39 +01:00
parent a0c207b46b
commit 6025642bbb
2 changed files with 99 additions and 1 deletions

View File

@ -0,0 +1,91 @@
package uk.co.neviyn.booru
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
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.web.servlet.request.SecurityMockMvcRequestPostProcessors
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
@SpringBootTest
@AutoConfigureMockMvc
class BaseControllerTest {
@Autowired
lateinit var mockMvc: MockMvc
@Autowired
lateinit var memberRepository: MemberRepository
@Test
fun `go to the root endpoint`() {
mockMvc
.perform(MockMvcRequestBuilders.get("/"))
.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.view().name("landing"))
.andExpect(MockMvcResultMatchers.model().attribute("count", 0L))
}
@Test
fun `show the login page`() {
mockMvc
.perform(MockMvcRequestBuilders.get("/login?redirect=/foo"))
.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.view().name("login"))
.andExpect(MockMvcResultMatchers.model().attribute("redirect", "/foo"))
}
@Test
fun `show the registration page`() {
mockMvc
.perform(MockMvcRequestBuilders.get("/register"))
.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.view().name("register"))
.andExpect(MockMvcResultMatchers.model().attribute("user_details", Member(name = "", email = "", password = "")))
.andExpect(MockMvcResultMatchers.model().attribute("submitted", false))
}
@Test
fun `try and register with no entered details`() {
mockMvc
.perform(MockMvcRequestBuilders.post("/register")
.with(SecurityMockMvcRequestPostProcessors.csrf())
)
.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.view().name("register"))
.andExpect(MockMvcResultMatchers.model().hasErrors())
}
@Test
fun `try and register with only a username`() {
mockMvc
.perform(MockMvcRequestBuilders.post("/register")
.with(SecurityMockMvcRequestPostProcessors.csrf())
.flashAttr("user_details", Member(name = "sample_username"))
)
.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.view().name("register"))
.andExpect(MockMvcResultMatchers.model().hasErrors())
.andExpect(MockMvcResultMatchers.model().errorCount(2))
.andExpect(MockMvcResultMatchers.model().attributeHasFieldErrors("user_details", "password", "email"))
}
@Test
fun `successfully register`() {
mockMvc
.perform(MockMvcRequestBuilders.post("/register")
.with(SecurityMockMvcRequestPostProcessors.csrf())
.flashAttr("user_details", Member(name = "sample", email = "sample@example.org", password = "sample"))
)
.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.view().name("login"))
val insertedUser = memberRepository.findByName("sample")
Assertions.assertNotNull(insertedUser)
Assertions.assertEquals("sample", insertedUser!!.name)
Assertions.assertEquals("sample@example.org", insertedUser.email)
Assertions.assertTrue(passwordEncoder().matches("sample", insertedUser.password))
}
}

View File

@ -1,3 +1,10 @@
spring.datasource.url=jdbc:h2:mem:testdb;MODE=PostgreSQL
spring.datasource.driverClassName=org.h2.Driver
images.directory=./images
images.directory=./images
spring.jpa.hibernate.ddl-auto=validate
spring.datasource.initialization-mode=always
spring.jpa.properties.hibernate.default_schema=booru
spring.mvc.hiddenmethod.filter.enabled=true
images.types=jpg,png,gif
spring.servlet.multipart.max-file-size=20000000
spring.servlet.multipart.max-request-size=20000000