From e6542dce7a7acb98a3ec9b30f366323b81a71849 Mon Sep 17 00:00:00 2001 From: Mathis Kral Date: Tue, 16 Jan 2024 20:04:51 +0100 Subject: [PATCH] Add missing order tests --- .../java/catering/order/OrderController.java | 1 + .../OrderControllerIntegrationTests.java | 79 ++++++++++++++----- .../order/OrderControllerUnitTests.java | 17 ---- 3 files changed, 61 insertions(+), 36 deletions(-) diff --git a/src/main/java/catering/order/OrderController.java b/src/main/java/catering/order/OrderController.java index 03d018f..683ff38 100644 --- a/src/main/java/catering/order/OrderController.java +++ b/src/main/java/catering/order/OrderController.java @@ -349,6 +349,7 @@ public class OrderController { } @GetMapping("/orders/calender") + @PreAuthorize("hasRole('ADMIN')") public String calender(Model model) { ArrayList> datesOfMonth = new ArrayList>(28); LocalDateTime startDateTime = LocalDateTime.now(); diff --git a/src/test/java/catering/order/OrderControllerIntegrationTests.java b/src/test/java/catering/order/OrderControllerIntegrationTests.java index 93e6bc8..e628a9e 100644 --- a/src/test/java/catering/order/OrderControllerIntegrationTests.java +++ b/src/test/java/catering/order/OrderControllerIntegrationTests.java @@ -39,6 +39,7 @@ 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 @@ -83,8 +84,8 @@ public class OrderControllerIntegrationTests { if (orderManagement.findAll(Pageable.unpaged()).stream().findAny().isEmpty()) { myCart = new CustomCart(OrderType.EVENT_CATERING, - LocalDateTime.of(2023, 12, 23, 10, 0), - LocalDateTime.of(2023, 12, 24, 10, 0)); + LocalDateTime.now(), + LocalDateTime.now().plusDays(1L)); myCart.addOrUpdateItem(inventory.findAll().stream().findFirst().get().getProduct(), Quantity.of(1)); myOrder = new CustomOrder(myUser.getId(), myCart); @@ -134,17 +135,39 @@ public class OrderControllerIntegrationTests { @DirtiesContext @WithMockUser(username = "admin", roles = "ADMIN") void adminViewsOrderByDate() throws Exception { - mvc.perform(get("/allOrders/2023-12-23")) + 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/2023-10-12")) + 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") @@ -200,17 +223,20 @@ public class OrderControllerIntegrationTests { @Test @DirtiesContext @WithMockUser(username = "andi", roles = "CUSTOMER") - void userCheckOut() throws Exception { - Product product = inventory.findAll().stream().findFirst().get().getProduct(); - mvc.perform(post("/event/addProduct") - .param("pid", product.getId().toString()) - .param("number", "2")); - + 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") @@ -220,6 +246,13 @@ public class OrderControllerIntegrationTests { .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 { @@ -236,6 +269,7 @@ public class OrderControllerIntegrationTests { .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"))) @@ -246,19 +280,26 @@ public class OrderControllerIntegrationTests { @Test @DirtiesContext - @WithMockUser(username = "admin", roles = "ADMIN") - void adminViewsInvoice() throws Exception { - mvc.perform(MockMvcRequestBuilders.get("/myOrders/" + myOrder.getId().toString() + "/invoice")) - .andExpect(status().isOk()) - .andExpect(content().string(containsString("Beschreibung"))) - .andExpect(model().attributeExists("order")); + 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 - void displayStaff() throws Exception { - mvc.perform(get("/myOrders/" + myOrder.getId().toString() + "/invoice") - .param("sid", myEmployee.getId().toString())); + @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()))); + } } diff --git a/src/test/java/catering/order/OrderControllerUnitTests.java b/src/test/java/catering/order/OrderControllerUnitTests.java index ba08ddc..3727b8c 100644 --- a/src/test/java/catering/order/OrderControllerUnitTests.java +++ b/src/test/java/catering/order/OrderControllerUnitTests.java @@ -96,23 +96,6 @@ public class OrderControllerUnitTests { ).stream().toList()).hasSize(1); } - @Test - @Order(4) - @Disabled // because Spring does shit that I don't understand - void freeAmountInInterval() { - myProduct = (Rentable) inventory.findAll() - .filter(item -> item.getProduct().getName().equals("Kerze Rot")) - .stream().findFirst().get().getProduct(); - - assertThat(OrderController.findFreeAmountInInterval( - myProduct, - LocalDateTime.of(2023, 12, 11, 0, 0), - LocalDateTime.of(2023, 12, 11, 23, 0), - inventory, - customOrderRepository) - ).isEqualTo(Quantity.of(7)); - } - @Test @Order(5) void ordersByOrderStatus() {