Skip to content

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

Add price amount validation in Voucher mutations

IKarbowiak1aa674a55047
diff --git a/saleor/graphql/discount/mutations.py b/saleor/graphql/discount/mutations.py
index fcc2f2d335..0739cae235 100644
--- a/saleor/graphql/discount/mutations.py
+++ b/saleor/graphql/discount/mutations.py
@@ -12,6 +12,7 @@ from ...product.tasks import (
from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ..core.scalars import PositiveDecimal
from ..core.types.common import DiscountError
+from ..core.validators import validate_price_amount
from ..product.types import Category, Collection, Product
from .enums import DiscountValueTypeEnum, VoucherTypeEnum
from .types import Sale, Voucher
@@ -161,6 +162,11 @@ class VoucherCreate(ModelMutation):
min_spent_amount = cleaned_input.pop("min_amount_spent", None)
if min_spent_amount is not None:
+ try:
+ validate_price_amount(min_spent_amount, instance.currency)
+ except ValidationError as error:
+ error.code = DiscountErrorCode.INVALID.value
+ raise ValidationError({"min_spent_amount": error})
cleaned_input["min_spent_amount"] = min_spent_amount
return cleaned_input
diff --git a/saleor/graphql/discount/tests/test_discount.py b/saleor/graphql/discount/tests/test_discount.py
index ef5c7c52c2..68bd383825 100644
--- a/saleor/graphql/discount/tests/test_discount.py
+++ b/saleor/graphql/discount/tests/test_discount.py
@@ -223,7 +223,7 @@ def test_create_voucher_with_empty_code(staff_api_client, permission_manage_disc
"code": "",
"discountValueType": DiscountValueTypeEnum.FIXED.name,
"discountValue": 10.12,
- "minSpent": 1.12,
+ "minAmountSpent": 1.12,
"startDate": start_date.isoformat(),
"endDate": end_date.isoformat(),
"usageLimit": None,
@@ -266,6 +266,33 @@ def test_create_voucher_with_existing_gift_card_code(
assert errors[0]["code"] == DiscountErrorCode.ALREADY_EXISTS.name
+def test_create_voucher_with_too_many_decimal_values_in_min_spent(
+ staff_api_client, permission_manage_discounts
+):
+ start_date = timezone.now() - timedelta(days=365)
+ end_date = timezone.now() + timedelta(days=365)
+ variables = {
+ "name": "test voucher",
+ "type": VoucherTypeEnum.ENTIRE_ORDER.name,
+ "code": "test123",
+ "discountValueType": DiscountValueTypeEnum.FIXED.name,
+ "discountValue": 10.12,
+ "minAmountSpent": 1.1201,
+ "startDate": start_date.isoformat(),
+ "endDate": end_date.isoformat(),
+ "usageLimit": None,
+ }
+
+ response = staff_api_client.post_graphql(
+ CREATE_VOUCHER_MUTATION, variables, permissions=[permission_manage_discounts]
+ )
+ content = get_graphql_content(response)
+ errors = content["data"]["voucherCreate"]["discountErrors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "minSpentAmount"
+ assert errors[0]["code"] == DiscountErrorCode.INVALID.name
+
+
def test_create_voucher_with_existing_voucher_code(
staff_api_client, voucher_shipping_type, permission_manage_discounts
):