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:
Simon Bruder 2023-12-07 20:30:45 +01:00
parent c5f6b18cd3
commit 4d89fdc10a
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
2 changed files with 35 additions and 4 deletions

View file

@ -31,6 +31,8 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import catering.catalog.CateringCatalog;
import catering.catalog.Consumable;
import catering.catalog.Rentable;
import jakarta.validation.Valid;
/*
@ -94,13 +96,13 @@ class InventoryController {
@PostMapping(path = "/inventory/edit/{pid}", params = "type=Consumable")
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);
}
@PostMapping(path = "/inventory/edit/{pid}", params = "type=Rentable")
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);
}

View file

@ -55,6 +55,7 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import catering.catalog.CateringCatalog;
import catering.catalog.Consumable;
import catering.catalog.Rentable;
import catering.order.OrderType;
@ -68,15 +69,39 @@ class InventoryControllerIntegrationTests {
@Autowired
UniqueInventory<UniqueInventoryItem> inventory;
@Autowired
CateringCatalog catalog;
UniqueInventoryItem anyInventoryItem;
Product anyProduct;
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
void populateAnyInventoryItem() {
anyInventoryItem = inventory.findAll().stream().findAny().get();
anyProduct = anyInventoryItem.getProduct();
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
@ -295,7 +320,9 @@ class InventoryControllerIntegrationTests {
@Test
void checkPermissionsForEditPage() throws Exception {
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);
}
}
@ -303,7 +330,9 @@ class InventoryControllerIntegrationTests {
@Test
void checkPermissionsForEdit() throws Exception {
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);
}
}