Skip to content

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

Add price amount validation to ShippingPrice mutations

IKarbowiak58a7d508f4df
diff --git a/saleor/graphql/shipping/mutations.py b/saleor/graphql/shipping/mutations.py
index aba39cf4d8..257e4b7535 100644
--- a/saleor/graphql/shipping/mutations.py
+++ b/saleor/graphql/shipping/mutations.py
@@ -13,6 +13,7 @@ from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ..core.scalars import PositiveDecimal, WeightScalar
from ..core.types.common import ShippingError
from ..core.utils import get_duplicates_ids
+from ..core.validators import validate_price_amount
from .enums import ShippingMethodTypeEnum
from .types import ShippingMethod, ShippingZone
@@ -170,6 +171,11 @@ 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:
+ try:
+ validate_price_amount(price_amount, instance.currency)
+ except ValidationError as error:
+ error.code = ShippingErrorCode.INVALID.value
+ raise ValidationError({"price": error})
cleaned_input["price_amount"] = price_amount
cleaned_type = cleaned_input.get("type")
@@ -179,9 +185,19 @@ class ShippingPriceMixin:
max_price = cleaned_input.pop("maximum_order_price", None)
if min_price is not None:
+ try:
+ validate_price_amount(min_price, instance.currency)
+ except ValidationError as error:
+ error.code = ShippingErrorCode.INVALID.value
+ raise ValidationError({"minimum_order_price": error})
cleaned_input["minimum_order_price_amount"] = min_price
if max_price is not None:
+ try:
+ validate_price_amount(max_price, instance.currency)
+ except ValidationError as error:
+ error.code = ShippingErrorCode.INVALID.value
+ raise ValidationError({"maximum_order_price": error})
cleaned_input["maximum_order_price_amount"] = max_price
if (
diff --git a/saleor/graphql/shipping/tests/test_shipping_method.py b/saleor/graphql/shipping/tests/test_shipping_method.py
index 7724e3af83..3ee23ad9ca 100644
--- a/saleor/graphql/shipping/tests/test_shipping_method.py
+++ b/saleor/graphql/shipping/tests/test_shipping_method.py
@@ -600,7 +600,7 @@ def test_create_shipping_method(
assert data["shippingZone"]["id"] == shipping_zone_id
-def test_create_shipping_method_with_invalid_price(
+def test_create_shipping_method_with_negative_price(
staff_api_client, shipping_zone, permission_manage_shipping,
):
query = PRICE_BASED_SHIPPING_QUERY
@@ -622,6 +622,90 @@ def test_create_shipping_method_with_invalid_price(
assert_negative_positive_decimal_value(response)
+def test_create_shipping_method_with_to_many_decimal_places_in_price(
+ staff_api_client, shipping_zone, permission_manage_shipping,
+): # given
+ query = PRICE_BASED_SHIPPING_QUERY
+ staff_api_client.user.user_permissions.add(permission_manage_shipping)
+ name = "DHL"
+ price = 12.345
+ 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,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["shippingPriceCreate"]
+ error = data["shippingErrors"][0]
+ assert error["field"] == "price"
+ assert error["code"] == ShippingErrorCode.INVALID.name
+
+
+def test_create_shipping_method_with_to_many_decimal_places_in_minimum_order_price(
+ staff_api_client, shipping_zone, permission_manage_shipping,
+): # given
+ query = PRICE_BASED_SHIPPING_QUERY
+ staff_api_client.user.user_permissions.add(permission_manage_shipping)
+ 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": 1.2001,
+ "maximumOrderPrice": 20,
+ "type": ShippingMethodTypeEnum.PRICE.name,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["shippingPriceCreate"]
+ error = data["shippingErrors"][0]
+ assert error["field"] == "minimumOrderPrice"
+ assert error["code"] == ShippingErrorCode.INVALID.name
+
+
+def test_create_shipping_method_with_to_many_decimal_places_in_maximum_order_price(
+ staff_api_client, shipping_zone, permission_manage_shipping,
+): # given
+ query = PRICE_BASED_SHIPPING_QUERY
+ staff_api_client.user.user_permissions.add(permission_manage_shipping)
+ 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.00001,
+ "type": ShippingMethodTypeEnum.PRICE.name,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["shippingPriceCreate"]
+ error = data["shippingErrors"][0]
+ assert error["field"] == "maximumOrderPrice"
+ assert error["code"] == ShippingErrorCode.INVALID.name
+
+
def test_create_price_shipping_method_errors(
shipping_zone, staff_api_client, permission_manage_shipping
):
@@ -631,7 +715,7 @@ def test_create_price_shipping_method_errors(
"name": "DHL",
"price": 12.34,
"minimumOrderPrice": 20,
- "maximumOrderPrice": 15,
+ "maximumOrderPrice": 10,
"type": ShippingMethodTypeEnum.PRICE.name,
}
response = staff_api_client.post_graphql(