// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-FileCopyrightText: 2023 swt23w23 package catering.order; import catering.staff.JobType; import catering.staff.Employee; import catering.staff.StaffManagement; import org.javamoney.moneta.Money; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.salespointframework.catalog.Product; import org.salespointframework.inventory.UniqueInventory; import org.salespointframework.inventory.UniqueInventoryItem; import org.salespointframework.order.CartItem; import org.salespointframework.order.OrderManagement; import org.salespointframework.order.OrderStatus; import org.salespointframework.quantity.Quantity; import org.salespointframework.useraccount.Password; import org.salespointframework.useraccount.Role; import org.salespointframework.useraccount.UserAccount; import org.salespointframework.useraccount.UserAccountManagement; 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.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import static org.hamcrest.Matchers.not; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.hamcrest.CoreMatchers.containsString; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.salespointframework.core.Currencies.EURO; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @AutoConfigureMockMvc @SpringBootTest public class OrderControllerIntegrationTests { @Autowired MockMvc mvc; @Autowired UniqueInventory inventory; @Autowired OrderManagement orderManagement; @Autowired UserAccountManagement userAccountManagement; @Autowired StaffManagement staffManagement; UserAccount myUser; UserAccount admin; CustomCart myCart; Employee myEmployee; CustomOrder myOrder; @BeforeEach void setUp() { if (userAccountManagement.findByUsername("andi").isEmpty()) { userAccountManagement.create("andi", Password.UnencryptedPassword.of("12345"), Role.of("CUSTOMER")); } myEmployee = new Employee("Sabrina", JobType.SERVICE, Money.of(10, EURO)); staffManagement.save(myEmployee); myUser = userAccountManagement.findByUsername("andi").get(); admin = userAccountManagement.findByUsername("admin").get(); orderManagement.findAll(Pageable.unpaged()).forEach(orderManagement::delete); if (orderManagement.findAll(Pageable.unpaged()).stream().findAny().isEmpty()) { myCart = new CustomCart(OrderType.EVENT_CATERING, LocalDateTime.now(), LocalDateTime.now().plusDays(1L)); myCart.addOrUpdateItem(inventory.findAll().stream().findFirst().get().getProduct(), Quantity.of(1)); myOrder = new CustomOrder(myUser.getId(), myCart); myCart.addItemsTo(myOrder); myCart.addStaffTo(myOrder); orderManagement.payOrder(myOrder); orderManagement.completeOrder(myOrder); } } @Test @DirtiesContext void customerViewsOrder() throws Exception { mvc.perform(get("/myOrders").with(user("andi").roles("CUSTOMER"))) .andExpect(status().isOk()) .andExpect(content().string(containsString("Menge"))) .andExpect(model().attributeExists("orders")); mvc.perform(get("/myOrders").param("orderStatus", OrderStatus.COMPLETED.toString()).with(user("andi").roles("CUSTOMER"))) .andExpect(status().isOk()) .andExpect(content().string(containsString("Menge"))) .andExpect(model().attributeExists("orders")); mvc.perform(get("/myOrders").with(user("admin").roles("CUSTOMER"))) // WTF SPRING!11!!!! .andExpect(status().isOk()) .andExpect(content().string(containsString("0"))); mvc.perform(get("/myOrders").with(user("admin").roles("ADMIN"))) .andExpect(status().isForbidden()); } @Test @DirtiesContext @WithMockUser(username = "admin", roles = "ADMIN") void adminViewsOrder() throws Exception { mvc.perform(get("/allOrders")) .andExpect(status().isOk()) .andExpect(content().string(containsString("Menge"))) .andExpect(model().attributeExists("orders")); mvc.perform(get("/allOrders").with(user("andi").roles("CUSTOMER"))) .andExpect(status().isForbidden()); } @Test @DirtiesContext @WithMockUser(username = "admin", roles = "ADMIN") void adminViewsOrderByDate() throws Exception { mvc.perform(get("/allOrders/" + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE))) .andExpect(status().isOk()) .andExpect(content().string(containsString("Menge"))) .andExpect(model().attributeExists("orders")); mvc.perform(get("/allOrders/" + LocalDateTime.now().plusDays(10L).format(DateTimeFormatter.ISO_DATE))) .andExpect(status().isOk()) .andExpect(content().string(not(containsString("Menge")))) .andExpect(model().attributeExists("orders")); } @Test @DirtiesContext @WithMockUser(username = "admin", roles = "ADMIN") void adminViewsOrderByDateAndStatus() throws Exception { mvc.perform(get("/allOrders").param("orderStatus", myOrder.getOrderStatus().toString())) .andExpect(status().isOk()) .andExpect(content().string(containsString(myUser.getId().toString()))); mvc.perform(get("/allOrders/" + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE)).param("orderStatus", myOrder.getOrderStatus().toString())) .andExpect(status().isOk()) .andExpect(content().string(containsString(myUser.getId().toString()))); mvc.perform(get("/allOrders/" + LocalDateTime.now().plusDays(10L).format(DateTimeFormatter.ISO_DATE)).param("orderStatus", "CANCELED")) .andExpect(status().isOk()) .andExpect(content().string(not(containsString(myUser.getId().toString())))); mvc.perform(get("/allOrders").param("orderStatus", "CANCELED")) .andExpect(status().isOk()) .andExpect(content().string(not(containsString(myUser.getId().toString())))); } @Test @DirtiesContext @WithMockUser(username = "admin", roles = "ADMIN") void adminRemovesOrder() throws Exception { mvc.perform(post("/allOrders/remove") .param("orderID", orderManagement.findAll(Pageable.unpaged()).stream().findFirst().get().getId().toString())) .andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl("/allOrders")); } @Test @DirtiesContext @WithMockUser(username = "andi", roles = "CUSTOMER") void userPlansEvent() throws Exception { mvc.perform(get("/event")) .andExpect(status().isOk()) .andExpect(model().attributeExists("invRentables")); Product product = inventory.findAll().stream().findFirst().get().getProduct(); mvc.perform(post("/event/addProduct") .param("pid", product.getId().toString()) .param("number", "2")) .andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl("/event")); } @Test @DirtiesContext @WithMockUser(username = "andi", roles = "CUSTOMER") void userRemovesItemFromCart() throws Exception { CartItem myItem = myCart.toList().get(0); mvc.perform(post("/event/removeProduct") .param("itemId", myItem.getId())) .andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl("/event")); } @Test @DirtiesContext @WithMockUser(username = "andi", roles = "CUSTOMER") void userChangesDate() throws Exception { mvc.perform(post("/event/changeDate") .param("startDate", "2024-10-10") .param("startHour", "10") .param("finishDate", "2024-10-11") .param("finishHour", "11")) .andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl("/event")); } @Test @DirtiesContext @WithMockUser(username = "andi", roles = "CUSTOMER") void emptyCartCheckOut() throws Exception { mvc.perform(post("/event/checkout")) .andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl("/event")); } @Test @WithMockUser(username = "andi", roles = "CUSTOMER") void changeOrderType() throws Exception { mvc.perform(post("/event/changeOrderType").param("type", "EVENT_CATERING")) .andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl("/event")); } @Test @DirtiesContext @WithMockUser(username = "andi", roles = "CUSTOMER") void addEmployee() throws Exception { mvc.perform(post("/event/addEmployee") .param("sid", myEmployee.getId().toString())) .andExpect(redirectedUrl("/event")); } @Test @WithMockUser(username = "andi", roles = "CUSTOMER") void removeWrongEmployee() throws Exception { mvc.perform(post("/event/removeEmployee").param("sid", "1234321234321")) .andExpect(content().string(containsString("Fehler"))); } @Test @WithMockUser(username = "andi", roles = "CUSTOMER") void addWrongProduct() throws Exception { myCart.setOrderType(OrderType.RENT_A_COOK); Product wrongProduct = inventory.findAll(Pageable.unpaged()).stream() .filter(item -> item.getProduct().getCategories().stream() .noneMatch(c -> c.equals(myCart.getOrderType().toString()))) .findFirst().get().getProduct(); mvc.perform(post("/event/addProduct") .param("pid", wrongProduct.getId().toString()) .param("number", "1")) .andExpect(redirectedUrl("/event")) .andExpect(content().string(not(containsString(wrongProduct.getName())))); } @Test @DirtiesContext void customerViewsInvoice() throws Exception { mvc.perform(get("/myOrders/" + myOrder.getId().toString() + "/invoice").with(user("andi").roles("CUSTOMER"))) .andExpect(status().isOk()) .andExpect(content().string(containsString("Beschreibung"))) .andExpect(model().attributeExists("order")); } @Test @DirtiesContext void wrongUserViewsInvoice() throws Exception { userAccountManagement.create("peter", Password.UnencryptedPassword.of("123"), Role.of("CUSTOMER")); mvc.perform(get("/myOrders/" + myOrder.getId().toString() + "/invoice").with(user("peter").roles("CUSTOMER"))) .andExpect(redirectedUrl("/unauthorized")); } @Test @DirtiesContext @WithMockUser(username = "andi", roles = "CUSTOMER") void wrongUserViewsCalender() throws Exception { mvc.perform(get("/orders/calender")) .andExpect(status().is4xxClientError()); } @Test @DirtiesContext @WithMockUser(username = "admin", roles = "ADMIN") void adminViewsCalender() throws Exception { mvc.perform(get("/orders/calender")) .andExpect(status().isOk()) .andExpect(content().string(containsString(myOrder.getId().toString()))); } }