From d4708d14a4e739d835ae75885c4f323d23e65c58 Mon Sep 17 00:00:00 2001 From: William Harrington Date: Wed, 12 Feb 2025 20:21:11 -0600 Subject: Rename test class and remove public modifiers. --- .../controllers/BuilddbAccountControllerTest.java | 137 +++++++++++++++++++++ .../controllers/BuilddbTestAccountController.java | 137 --------------------- 2 files changed, 137 insertions(+), 137 deletions(-) create mode 100644 src/test/java/org/berzerkula/builddb/controllers/BuilddbAccountControllerTest.java delete mode 100644 src/test/java/org/berzerkula/builddb/controllers/BuilddbTestAccountController.java diff --git a/src/test/java/org/berzerkula/builddb/controllers/BuilddbAccountControllerTest.java b/src/test/java/org/berzerkula/builddb/controllers/BuilddbAccountControllerTest.java new file mode 100644 index 0000000..a1ab349 --- /dev/null +++ b/src/test/java/org/berzerkula/builddb/controllers/BuilddbAccountControllerTest.java @@ -0,0 +1,137 @@ +package org.berzerkula.builddb.controllers; + +import org.berzerkula.builddb.config.SecurityConfig; +import org.berzerkula.builddb.models.AppUser; +import org.berzerkula.builddb.repositories.AppUserRepository; +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.context.annotation.Import; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.Date; + +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +@Import(SecurityConfig.class) +@AutoConfigureMockMvc +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) + +class BuilddbAccountControllerTest { + + @Autowired + private AppUserRepository appUserRepository; + + @Autowired + private MockMvc mockMvc; + + @Test + void shouldReturnIndexOk() throws Exception { + mockMvc.perform(get("/")) + .andExpect(status().isOk()) + .andExpect(view().name("index")) + .andDo(print()); + } + + @Test + void shouldReturnLoginOk() throws Exception { + mockMvc.perform(get("/login")) + .andExpect(status().isOk()) + .andExpect(view().name("login")) + .andDo(print()); + } + + @Test + void shouldProfileRedirectToLogin() throws Exception { + mockMvc.perform(get("/profile")) + .andExpect(status().is3xxRedirection()) + .andDo(print()); + } + + @WithMockUser(roles="client") + @Test + void shouldReturnProfileView() throws Exception { + + AppUser appUser = new AppUser(); + appUser.setFirstName("John"); + appUser.setLastName("Doe"); + appUser.setEmail("test@berzerkula.org"); + appUser.setPhone("555-555-5555"); + appUser.setAddress("612 Wolf Avenue"); + appUser.setRole("client"); + appUser.setPassword("password"); + appUser.setCreatedAt(new Date()); + appUserRepository.save(appUser); + + this.mockMvc.perform(get("/profile") + .with(user("test@berzerkula.org"))) + .andExpect(status().isOk()) + .andExpect(view().name("profile")) + .andDo(print()); + } + + @Test + void shouldReturnRegisterOk() throws Exception { + mockMvc.perform(get("/register")) + .andExpect(status().isOk()) + .andExpect(view().name("register")) + .andDo(print()); + } + + @Test + void shouldRegisterRequiredValidationError() throws Exception { + mockMvc.perform(post("/register") + .with(csrf()) + .formField("firstName", "") + .formField("lastName", "") + .formField("email", "") + .formField("phone", "") + .formField("address", "") + .formField("password", "") + .formField("confirmPassword", "")) + .andExpect(status().isOk()) + .andExpect(view().name("register")) + .andDo(print()); + } + + @Test + void shouldRegisterPasswordValidationError() throws Exception { + mockMvc.perform(post("/register") + .with(csrf()) + .formField("firstName", "John") + .formField("lastName", "Doe") + .formField("email", "test@berzerkula.org") + .formField("phone", "") + .formField("address", "") + .formField("password", "password") + .formField("confirmPassword", "passwrd")) + .andExpect(status().isOk()) + .andExpect(view().name("register")) + .andDo(print()); + } + + @Test + void shouldRegisterSuccess() throws Exception { + mockMvc.perform(post("/register") + .with(csrf()) + .formField("firstName", "John") + .formField("lastName", "Doe") + .formField("email", "test@berzerkula.org") + .formField("phone", "555-555-5555") + .formField("address", "612 Wolf Avenue") + .formField("password", "password") + .formField("confirmPassword", "password")) + .andExpect(status().isOk()) + .andExpect(view().name("register")) + .andDo(print()); + } + +} diff --git a/src/test/java/org/berzerkula/builddb/controllers/BuilddbTestAccountController.java b/src/test/java/org/berzerkula/builddb/controllers/BuilddbTestAccountController.java deleted file mode 100644 index 50548fd..0000000 --- a/src/test/java/org/berzerkula/builddb/controllers/BuilddbTestAccountController.java +++ /dev/null @@ -1,137 +0,0 @@ -package org.berzerkula.builddb.controllers; - -import org.berzerkula.builddb.config.SecurityConfig; -import org.berzerkula.builddb.models.AppUser; -import org.berzerkula.builddb.repositories.AppUserRepository; -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.context.annotation.Import; -import org.springframework.security.test.context.support.WithMockUser; -import org.springframework.test.web.servlet.MockMvc; - -import java.util.Date; - -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; - -@Import(SecurityConfig.class) -@AutoConfigureMockMvc -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) - -public class BuilddbTestAccountController { - - @Autowired - private AppUserRepository appUserRepository; - - @Autowired - private MockMvc mockMvc; - - @Test - public void shouldReturnIndexOk() throws Exception { - mockMvc.perform(get("/")) - .andExpect(status().isOk()) - .andExpect(view().name("index")) - .andDo(print()); - } - - @Test - public void shouldReturnLoginOk() throws Exception { - mockMvc.perform(get("/login")) - .andExpect(status().isOk()) - .andExpect(view().name("login")) - .andDo(print()); - } - - @Test - public void shouldProfileRedirectToLogin() throws Exception { - mockMvc.perform(get("/profile")) - .andExpect(status().is3xxRedirection()) - .andDo(print()); - } - - @WithMockUser(roles="client") - @Test - public void shouldReturnProfileView() throws Exception { - - AppUser appUser = new AppUser(); - appUser.setFirstName("John"); - appUser.setLastName("Doe"); - appUser.setEmail("test@berzerkula.org"); - appUser.setPhone("555-555-5555"); - appUser.setAddress("612 Wolf Avenue"); - appUser.setRole("client"); - appUser.setPassword("password"); - appUser.setCreatedAt(new Date()); - appUserRepository.save(appUser); - - this.mockMvc.perform(get("/profile") - .with(user("test@berzerkula.org"))) - .andExpect(status().isOk()) - .andExpect(view().name("profile")) - .andDo(print()); - } - - @Test - public void shouldReturnRegisterOk() throws Exception { - mockMvc.perform(get("/register")) - .andExpect(status().isOk()) - .andExpect(view().name("register")) - .andDo(print()); - } - - @Test - public void shouldRegisterRequiredValidationError() throws Exception { - mockMvc.perform(post("/register") - .with(csrf()) - .formField("firstName", "") - .formField("lastName", "") - .formField("email", "") - .formField("phone", "") - .formField("address", "") - .formField("password", "") - .formField("confirmPassword", "")) - .andExpect(status().isOk()) - .andExpect(view().name("register")) - .andDo(print()); - } - - @Test - public void shouldRegisterPasswordValidationError() throws Exception { - mockMvc.perform(post("/register") - .with(csrf()) - .formField("firstName", "John") - .formField("lastName", "Doe") - .formField("email", "test@berzerkula.org") - .formField("phone", "") - .formField("address", "") - .formField("password", "password") - .formField("confirmPassword", "passwrd")) - .andExpect(status().isOk()) - .andExpect(view().name("register")) - .andDo(print()); - } - - @Test - public void shouldRegisterSuccess() throws Exception { - mockMvc.perform(post("/register") - .with(csrf()) - .formField("firstName", "John") - .formField("lastName", "Doe") - .formField("email", "test@berzerkula.org") - .formField("phone", "555-555-5555") - .formField("address", "612 Wolf Avenue") - .formField("password", "password") - .formField("confirmPassword", "password")) - .andExpect(status().isOk()) - .andExpect(view().name("register")) - .andDo(print()); - } - -} -- cgit v1.2.3-54-g00ecf