Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add shipping rate price validation

Krzysztof Świątek3f148ab44656
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9494061377..89186637b3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -86,6 +86,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add ability to assign multiple warehouses in mutations to create/update a shipping zone - #5399 by @fowczarek
- Add filter by ids to warehouses query - #5414 by @fowczarek
+- Add shipping rate price validation - #5411 by @kswiatek92
## 2.9.0
diff --git a/saleor/graphql/shipping/mutations.py b/saleor/graphql/shipping/mutations.py
index 1be51b70d1..2f9308ae69 100644
--- a/saleor/graphql/shipping/mutations.py
+++ b/saleor/graphql/shipping/mutations.py
@@ -179,6 +179,15 @@ class ShippingPriceMixin:
# Rename the price field to price_amount (the model's)
price_amount = cleaned_input.pop("price", None)
if price_amount is not None:
+ if price_amount < 0:
+ raise ValidationError(
+ {
+ "price": ValidationError(
+ ("Shipping rate price cannot be lower than 0."),
+ code=ShippingErrorCode.INVALID,
+ )
+ }
+ )
cleaned_input["price_amount"] = price_amount
cleaned_type = cleaned_input.get("type")
diff --git a/tests/api/test_shipping_method.py b/tests/api/test_shipping_method.py
index 1f08765423..53fbb5e2d8 100644
--- a/tests/api/test_shipping_method.py
+++ b/tests/api/test_shipping_method.py
@@ -468,6 +468,10 @@ PRICE_BASED_SHIPPING_QUERY = """
field
code
}
+ shippingErrors {
+ field
+ code
+ }
shippingZone {
id
}
@@ -530,6 +534,30 @@ def test_create_shipping_method(
assert data["shippingZone"]["id"] == shipping_zone_id
+def test_create_shipping_method_with_invalid_price(
+ staff_api_client, shipping_zone, permission_manage_shipping,
+):
+ query = PRICE_BASED_SHIPPING_QUERY
+ name = "DHL"
+ price = -12.34
+ shipping_zone_id = graphene.Node.to_global_id("ShippingZone", shipping_zone.pk)
+ variables = {
+ "shippingZone": shipping_zone_id,
+ "name": name,
+ "price": price,
+ "minimumOrderPrice": 0,
+ "maximumOrderPrice": 20,
+ "type": ShippingMethodTypeEnum.PRICE.name,
+ }
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_shipping]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["shippingPriceCreate"]
+ assert data["shippingErrors"][0]["field"] == "price"
+ assert data["shippingErrors"][0]["code"] == "INVALID"
+
+
def test_create_price_shipping_method_errors(
shipping_zone, staff_api_client, permission_manage_shipping
):