mirror of
https://github.com/st-tu-dresden-praktikum/swt23w23
synced 2024-07-19 21:04:36 +02:00
Add tests to staff package
Co-authored-by: Simon Bruder <simon.bruder@mailbox.tu-dresden.de>
This commit is contained in:
parent
79895d715b
commit
b944d04b5d
|
@ -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")));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue