diff options
Diffstat (limited to 'src/test/java/org/berzerkula/builddb/controllers')
4 files changed, 321 insertions, 0 deletions
diff --git a/src/test/java/org/berzerkula/builddb/controllers/BuilddbTestAccountController.java b/src/test/java/org/berzerkula/builddb/controllers/BuilddbTestAccountController.java new file mode 100644 index 0000000..50548fd --- /dev/null +++ b/src/test/java/org/berzerkula/builddb/controllers/BuilddbTestAccountController.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) + +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()); + } + +} diff --git a/src/test/java/org/berzerkula/builddb/controllers/BuilddbTestDashboardController.java b/src/test/java/org/berzerkula/builddb/controllers/BuilddbTestDashboardController.java new file mode 100644 index 0000000..27944ce --- /dev/null +++ b/src/test/java/org/berzerkula/builddb/controllers/BuilddbTestDashboardController.java @@ -0,0 +1,62 @@ +package org.berzerkula.builddb.controllers; + +import org.berzerkula.builddb.config.SecurityConfig; +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 static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +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 BuilddbTestDashboardController { + + @Autowired + private MockMvc mockMvc; + + @Test + @WithMockUser + public void shouldReturnActuatorDashboardView() throws Exception { + this.mockMvc.perform(get("/actuatorDashboard")) + .andExpect(status().isOk()) + .andExpect(view().name("actuatorDashboard")) + .andDo(print()); + } + + @Test + @WithMockUser + public void shouldReturnAdminView() throws Exception { + this.mockMvc.perform(get("/admin")) + .andExpect(status().isOk()) + .andExpect(view().name("admin")) + .andDo(print()); + } + + @Test + @WithMockUser + public void shouldReturnClientView() throws Exception { + this.mockMvc.perform(get("/client")) + .andExpect(status().isOk()) + .andExpect(view().name("client")) + .andDo(print()); + } + + @Test + @WithMockUser + public void shouldReturnUserView() throws Exception { + this.mockMvc.perform(get("/user")) + .andExpect(status().isOk()) + .andExpect(view().name("user")) + .andDo(print()); + } + +} diff --git a/src/test/java/org/berzerkula/builddb/controllers/BuilddbTestHomeController.java b/src/test/java/org/berzerkula/builddb/controllers/BuilddbTestHomeController.java new file mode 100644 index 0000000..94ae655 --- /dev/null +++ b/src/test/java/org/berzerkula/builddb/controllers/BuilddbTestHomeController.java @@ -0,0 +1,33 @@ +package org.berzerkula.builddb.controllers; + +import org.berzerkula.builddb.config.SecurityConfig; +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.test.web.servlet.MockMvc; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +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 BuilddbTestHomeController { + + @Autowired + private MockMvc mockMvc; + + @Test + void shouldReturnContactOk() throws Exception { + mockMvc.perform(get("/contact")) + .andExpect(status().isOk()) + .andExpect(view().name("contact")) + .andDo(print()); + } + +} diff --git a/src/test/java/org/berzerkula/builddb/controllers/BuilddbTestPkgController.java b/src/test/java/org/berzerkula/builddb/controllers/BuilddbTestPkgController.java new file mode 100644 index 0000000..6043aa1 --- /dev/null +++ b/src/test/java/org/berzerkula/builddb/controllers/BuilddbTestPkgController.java @@ -0,0 +1,89 @@ +package org.berzerkula.builddb.controllers; + +import org.berzerkula.builddb.config.SecurityConfig; +import org.berzerkula.builddb.models.Pkg; +import org.berzerkula.builddb.repositories.TestH2PkgRepository; +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.List; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +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 BuilddbTestPkgController { + + @Autowired + private TestH2PkgRepository pkgRepository; + + @Autowired + private MockMvc mockMvc; + + @Test + @WithMockUser(roles="client") + public void shouldReturnEmptyPackageListView() throws Exception { + this.mockMvc.perform(get("/pkgs")) + .andExpect(status().isOk()) + .andExpect(view().name("pkgs/index")) + .andDo(print()); + } + + @Test + @WithMockUser(roles="client") + public void shouldReturnAddPackageView() throws Exception { + + this.mockMvc.perform(get("/pkgs/add")) + .andExpect(status().isOk()) + .andExpect(view().name("pkgs/add")) + .andDo(print()); + } + + @Test + @WithMockUser(roles="client") + public void shouldReturnEditPackageView() throws Exception { + + Pkg pkg = new Pkg(); + pkg.setSequence(1); + pkg.setName("test"); + pkg.setVersion("1.2.3"); + pkgRepository.save(pkg); + List<Pkg> pkgs = pkgRepository.findAll(); + pkg = pkgs.get(0); + Integer id = pkg.getId(); + + this.mockMvc.perform(get("/pkgs/edit?id=" + id)) + .andExpect(status().isOk()) + .andExpect(view().name("pkgs/edit")) + .andDo(print()); + } + + @Test + @WithMockUser(roles="client") + public void shouldGetPopupWhenDeletePackage() throws Exception { + + Pkg pkg = new Pkg(); + pkg.setSequence(1); + pkg.setName("test"); + pkg.setVersion("1.2.3"); + pkgRepository.save(pkg); + List<Pkg> pkgs = pkgRepository.findAll(); + pkg = pkgs.get(0); + Integer id = pkg.getId(); + + this.mockMvc.perform(get("/pkgs/delete?id=" + id)) + .andExpect(status().is3xxRedirection()) + .andExpect(view().name("redirect:/pkgs/")) + .andDo(print()); + } +} |