mirror of
https://github.com/st-tu-dresden-praktikum/swt23w23
synced 2024-07-19 21:04:36 +02:00
Restrict product types for inventory edit
The error message gets slightly less meaningful, but either are currently just a mess.
This commit is contained in:
parent
c5f6b18cd3
commit
4d89fdc10a
|
@ -31,6 +31,8 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
import catering.catalog.CateringCatalog;
|
import catering.catalog.CateringCatalog;
|
||||||
|
import catering.catalog.Consumable;
|
||||||
|
import catering.catalog.Rentable;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -94,13 +96,13 @@ class InventoryController {
|
||||||
|
|
||||||
@PostMapping(path = "/inventory/edit/{pid}", params = "type=Consumable")
|
@PostMapping(path = "/inventory/edit/{pid}", params = "type=Consumable")
|
||||||
String editConsumable(@Valid @ModelAttribute("form") ConsumableMutateForm form, Errors result,
|
String editConsumable(@Valid @ModelAttribute("form") ConsumableMutateForm form, Errors result,
|
||||||
@PathVariable Product pid, Model model) {
|
@PathVariable Consumable pid, Model model) {
|
||||||
return edit(form, result, pid, model);
|
return edit(form, result, pid, model);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(path = "/inventory/edit/{pid}", params = "type=Rentable")
|
@PostMapping(path = "/inventory/edit/{pid}", params = "type=Rentable")
|
||||||
String editRentable(@Valid @ModelAttribute("form") RentableMutateForm form, Errors result,
|
String editRentable(@Valid @ModelAttribute("form") RentableMutateForm form, Errors result,
|
||||||
@PathVariable Product pid, Model model) {
|
@PathVariable Rentable pid, Model model) {
|
||||||
return edit(form, result, pid, model);
|
return edit(form, result, pid, model);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -55,6 +55,7 @@ import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.ResultActions;
|
import org.springframework.test.web.servlet.ResultActions;
|
||||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||||
|
|
||||||
|
import catering.catalog.CateringCatalog;
|
||||||
import catering.catalog.Consumable;
|
import catering.catalog.Consumable;
|
||||||
import catering.catalog.Rentable;
|
import catering.catalog.Rentable;
|
||||||
import catering.order.OrderType;
|
import catering.order.OrderType;
|
||||||
|
@ -68,15 +69,39 @@ class InventoryControllerIntegrationTests {
|
||||||
@Autowired
|
@Autowired
|
||||||
UniqueInventory<UniqueInventoryItem> inventory;
|
UniqueInventory<UniqueInventoryItem> inventory;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
CateringCatalog catalog;
|
||||||
|
|
||||||
UniqueInventoryItem anyInventoryItem;
|
UniqueInventoryItem anyInventoryItem;
|
||||||
Product anyProduct;
|
Product anyProduct;
|
||||||
ProductIdentifier anyPid;
|
ProductIdentifier anyPid;
|
||||||
|
UniqueInventoryItem anyConsumableItem;
|
||||||
|
UniqueInventoryItem anyRentableItem;
|
||||||
|
|
||||||
|
UniqueInventoryItem anyItemOf(Class<? extends Product> type) {
|
||||||
|
switch (type.getSimpleName()) {
|
||||||
|
case "Consumable":
|
||||||
|
return anyConsumableItem;
|
||||||
|
case "Rentable":
|
||||||
|
return anyRentableItem;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Invalid product type!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void populateAnyInventoryItem() {
|
void populateAnyInventoryItem() {
|
||||||
anyInventoryItem = inventory.findAll().stream().findAny().get();
|
anyInventoryItem = inventory.findAll().stream().findAny().get();
|
||||||
anyProduct = anyInventoryItem.getProduct();
|
anyProduct = anyInventoryItem.getProduct();
|
||||||
anyPid = anyProduct.getId();
|
anyPid = anyProduct.getId();
|
||||||
|
anyConsumableItem = inventory.save(new UniqueInventoryItem(
|
||||||
|
catalog.save(new Consumable("Any Consumable", Money.of(1, EURO), Money.of(0.5, EURO),
|
||||||
|
Optional.of(Money.of(0.75, EURO)), Set.of(OrderType.EVENT_CATERING, OrderType.PARTY_SERVICE))),
|
||||||
|
Quantity.of(1)));
|
||||||
|
anyRentableItem = inventory.save(new UniqueInventoryItem(
|
||||||
|
catalog.save(new Rentable("Any Rentable", Money.of(1, EURO), Money.of(0.5, EURO),
|
||||||
|
Set.of(OrderType.EVENT_CATERING, OrderType.PARTY_SERVICE))),
|
||||||
|
Quantity.of(1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -295,7 +320,9 @@ class InventoryControllerIntegrationTests {
|
||||||
@Test
|
@Test
|
||||||
void checkPermissionsForEditPage() throws Exception {
|
void checkPermissionsForEditPage() throws Exception {
|
||||||
for (Class<? extends Product> type : Set.of(Consumable.class, Rentable.class)) {
|
for (Class<? extends Product> type : Set.of(Consumable.class, Rentable.class)) {
|
||||||
checkPermissions(get("/inventory/edit/" + anyPid).queryParam("type", type.getSimpleName()),
|
checkPermissions(
|
||||||
|
get("/inventory/edit/" + anyItemOf(type).getProduct().getId())
|
||||||
|
.queryParam("type", type.getSimpleName()),
|
||||||
LOGIN, FORBIDDEN, OK);
|
LOGIN, FORBIDDEN, OK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -303,7 +330,9 @@ class InventoryControllerIntegrationTests {
|
||||||
@Test
|
@Test
|
||||||
void checkPermissionsForEdit() throws Exception {
|
void checkPermissionsForEdit() throws Exception {
|
||||||
for (Class<? extends Product> type : Set.of(Consumable.class, Rentable.class)) {
|
for (Class<? extends Product> type : Set.of(Consumable.class, Rentable.class)) {
|
||||||
checkPermissions(post("/inventory/edit/" + anyPid).queryParam("type", type.getSimpleName()),
|
checkPermissions(
|
||||||
|
post("/inventory/edit/" + anyItemOf(type).getProduct().getId())
|
||||||
|
.queryParam("type", type.getSimpleName()),
|
||||||
LOGIN, FORBIDDEN, OK);
|
LOGIN, FORBIDDEN, OK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue