From 6025642bbb640878a30dfdb017bb4fccfc80a99b Mon Sep 17 00:00:00 2001 From: neviyn Date: Wed, 12 May 2021 17:29:39 +0100 Subject: [PATCH] Added some basic tests for BaseController --- .../uk/co/neviyn/booru/BaseControllerTest.kt | 91 +++++++++++++++++++ src/test/resources/application.properties | 9 +- 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/uk/co/neviyn/booru/BaseControllerTest.kt diff --git a/src/test/kotlin/uk/co/neviyn/booru/BaseControllerTest.kt b/src/test/kotlin/uk/co/neviyn/booru/BaseControllerTest.kt new file mode 100644 index 0000000..322e5ed --- /dev/null +++ b/src/test/kotlin/uk/co/neviyn/booru/BaseControllerTest.kt @@ -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)) + } +} \ No newline at end of file diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 42ff3eb..c2f53cb 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -1,3 +1,10 @@ spring.datasource.url=jdbc:h2:mem:testdb;MODE=PostgreSQL spring.datasource.driverClassName=org.h2.Driver -images.directory=./images \ No newline at end of file +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 \ No newline at end of file