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; Employee defaultEmployee; Long defaultEmployeeId; @BeforeEach void setup() throws Exception { defaultEmployee = staffManagement.save(new Employee("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("id", 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"))); } }