From b944d04b5dfdb4a9fde0716315d7f1da7454035c Mon Sep 17 00:00:00 2001 From: Denis Natusch Date: Wed, 22 Nov 2023 21:30:57 +0100 Subject: [PATCH] Add tests to staff package Co-authored-by: Simon Bruder --- .../StaffControllerIntegrationTests.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/test/java/catering/staff/StaffControllerIntegrationTests.java diff --git a/src/test/java/catering/staff/StaffControllerIntegrationTests.java b/src/test/java/catering/staff/StaffControllerIntegrationTests.java new file mode 100644 index 0000000..704b3d9 --- /dev/null +++ b/src/test/java/catering/staff/StaffControllerIntegrationTests.java @@ -0,0 +1,107 @@ +package catering.staff; + +import static org.hamcrest.CoreMatchers.endsWith; +import static org.hamcrest.CoreMatchers.containsString; +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.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.BeforeEach; +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.context.support.WithAnonymousUser; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.http.HttpHeaders; + +@AutoConfigureMockMvc +@SpringBootTest +class StaffControllerIntegrationTests { + @Autowired + MockMvc mvc; + + MockHttpServletRequestBuilder createStaff = post("/staff/add") + .param("name", "Karl Baum") + .param("job", "COOK"); + + @Autowired + private StaffManagement staffManagement; + + Staff defaultEmployee; + Long defaultEmployeeId; + + @BeforeEach + void setup() throws Exception { + defaultEmployee = staffManagement.addStaff(new Staff("Dieter Baum", JobType.COOK)); + defaultEmployeeId = defaultEmployee.getId(); + } + + @Test + @WithMockUser(username = "admin", password = "admin", roles = "ADMIN") + void viewStaff() throws Exception { + mvc.perform(get("/staff")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString(defaultEmployee.getName()))); + } + + @Test + @WithMockUser(username = "admin", password = "admin", roles = "ADMIN") + void viewStaffEditPage() throws Exception { + mvc.perform(get("/staff/edit/" + defaultEmployeeId.toString())) + .andExpect(status().isOk()) + .andExpect(content().string(containsString(defaultEmployee.getName()))); + } + + @Test + @WithMockUser(username = "admin", password = "admin", roles = "ADMIN") + void addStaff() throws Exception { + // Needs to be updated if findAll() returns something useful + mvc.perform(createStaff).andExpect(redirectedUrl("/staff")); + } + + @Test + @WithMockUser(username = "admin", password = "admin", roles = "ADMIN") + void modifyStaff() throws Exception { + mvc.perform(post("/staff/edit/" + defaultEmployeeId.toString()) + .param("name", "Dieter Bäume") + .param("job", "SERVICE")).andExpect(redirectedUrl("/staff")) + .andExpect(redirectedUrl("/staff")); + assertThat(staffManagement.findById(defaultEmployeeId).get()) + .extracting("name", "job") + .containsExactly("Dieter Bäume", JobType.SERVICE); + } + + @Test + @WithMockUser(username = "admin", password = "admin", roles = "ADMIN") + void removeStaff() throws Exception { + mvc.perform(post("/staff/remove").param("staffID",defaultEmployeeId.toString())); + // this should be replaced once we have proper access to all employees + mvc.perform(get("/staff/edit/" + defaultEmployeeId.toString())) + .andExpect(status().isInternalServerError()); + } + + @Test + @WithMockUser(username = "dieter", password = "123", roles = "CUSTOMER") + void refuseCustomer() throws Exception { + mvc.perform(get("/staff")).andExpect(status().isForbidden()); + mvc.perform(get("/staff/edit/" + defaultEmployeeId.toString())).andExpect(status().isForbidden()); + } + + @Test + @WithAnonymousUser + void refuseAnonymous() throws Exception { + mvc.perform(get("/staff")) + .andExpect(status().is3xxRedirection()) + .andExpect(header().string(HttpHeaders.LOCATION, endsWith("/login"))); + mvc.perform(get("/staff/edit/1")) + .andExpect(status().is3xxRedirection()) + .andExpect(header().string(HttpHeaders.LOCATION, endsWith("/login"))); + } +}