Skip to content

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

Price values validation for ShippingMethodChannelListingUpdate mutation (#8686)

Jakuba91ea48a60a7
diff --git a/saleor/graphql/core/validators/__init__.py b/saleor/graphql/core/validators/__init__.py
index 562dd466ad..11cc57f9a7 100644
--- a/saleor/graphql/core/validators/__init__.py
+++ b/saleor/graphql/core/validators/__init__.py
@@ -54,6 +54,16 @@ def validate_price_precision(value: Optional["Decimal"], currency: str):
)
+def validate_decimal_max_value(value: "Decimal", max_value=10 ** 9):
+ """Validate if price amount is not higher than the limit for precision field.
+
+ Decimal fields in database have value limits.
+ By default its 10^9 for fields with precision 12.
+ """
+ if value >= max_value:
+ raise ValidationError(f"Value must be lower than {max_value}.")
+
+
def validate_variants_available_in_channel(
variants_id,
channel_id,
diff --git a/saleor/graphql/shipping/mutations/channels.py b/saleor/graphql/shipping/mutations/channels.py
index 3ce3f56f74..9d023f181d 100644
--- a/saleor/graphql/shipping/mutations/channels.py
+++ b/saleor/graphql/shipping/mutations/channels.py
@@ -15,7 +15,7 @@ from ...channel import ChannelContext
from ...channel.mutations import BaseChannelListingMutation
from ...core.scalars import PositiveDecimal
from ...core.types.common import ShippingError
-from ...core.validators import validate_price_precision
+from ...core.validators import validate_decimal_max_value, validate_price_precision
from ...shipping.utils import get_shipping_model_by_object_id
from ..types import ShippingMethod
@@ -144,6 +144,7 @@ class ShippingMethodChannelListingUpdate(BaseChannelListingMutation):
validate_price_precision(
price_amount, channel_input["channel"].currency_code
)
+ validate_decimal_max_value(price_amount)
channel_input["price_amount"] = price_amount
except ValidationError as error:
error.code = ShippingErrorCode.INVALID.value
@@ -170,6 +171,7 @@ class ShippingMethodChannelListingUpdate(BaseChannelListingMutation):
validate_price_precision(
min_price, channel_input["channel"].currency_code
)
+ validate_decimal_max_value(min_price)
except ValidationError as error:
error.code = ShippingErrorCode.INVALID.value
error.params = {
@@ -183,6 +185,7 @@ class ShippingMethodChannelListingUpdate(BaseChannelListingMutation):
validate_price_precision(
max_price, channel_input["channel"].currency_code
)
+ validate_decimal_max_value(max_price)
except ValidationError as error:
error.code = ShippingErrorCode.INVALID.value
error.params = {
diff --git a/saleor/graphql/shipping/tests/test_shipping_method_channel_listing_update.py b/saleor/graphql/shipping/tests/test_shipping_method_channel_listing_update.py
index 929e17eb39..a0fba2585e 100644
--- a/saleor/graphql/shipping/tests/test_shipping_method_channel_listing_update.py
+++ b/saleor/graphql/shipping/tests/test_shipping_method_channel_listing_update.py
@@ -610,3 +610,52 @@ def test_shipping_method_channel_listing_update_remove_channels(
mocked_drop_invalid_shipping_methods_relations.assert_called_once_with(
[shipping_method.pk], [str(channel.pk)]
)
+
+
+ "price, min_price, max_price, invalid_field",
+ [(10 ** 9, 2, 3, "price"), (1, 2, 10 ** 11, "maximumOrderPrice")],
+)
+def test_shipping_method_channel_listing_create_channel_max_value_validation(
+ price,
+ min_price,
+ max_price,
+ invalid_field,
+ staff_api_client,
+ shipping_method,
+ permission_manage_shipping,
+ channel_PLN,
+):
+ # given
+ shipping_method.shipping_zone.channels.add(channel_PLN)
+ shipping_method_id = graphene.Node.to_global_id(
+ "ShippingMethodType", shipping_method.pk
+ )
+ channel_id = graphene.Node.to_global_id("Channel", channel_PLN.id)
+ variables = {
+ "id": shipping_method_id,
+ "input": {
+ "addChannels": [
+ {
+ "channelId": channel_id,
+ "price": price,
+ "minimumOrderPrice": min_price,
+ "maximumOrderPrice": max_price,
+ }
+ ]
+ },
+ }
+
+ # when
+
+ response = staff_api_client.post_graphql(
+ SHIPPING_METHOD_CHANNEL_LISTING_UPDATE_MUTATION,
+ variables=variables,
+ permissions=(permission_manage_shipping,),
+ )
+ content = get_graphql_content(response)
+
+ # then
+ data = content["data"]["shippingMethodChannelListingUpdate"]
+ assert data["errors"][0]["field"] == invalid_field
+ assert data["errors"][0]["code"] == ShippingErrorCode.INVALID.name