Skip to content

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

Add price validations to ProductVariantBulkCreate mutation

IKarbowiakb081945cef53
diff --git a/saleor/graphql/product/bulk_mutations/products.py b/saleor/graphql/product/bulk_mutations/products.py
index 58d53e86f6..68513392e7 100644
--- a/saleor/graphql/product/bulk_mutations/products.py
+++ b/saleor/graphql/product/bulk_mutations/products.py
@@ -25,6 +25,7 @@ from ...core.types.common import (
StockError,
)
from ...core.utils import get_duplicated_values
+from ...core.validators import validate_price_precision
from ...utils import resolve_global_ids_to_primary_keys
from ...warehouse.types import Warehouse
from ..mutations.products import (
@@ -168,10 +169,20 @@ class ProductVariantBulkCreate(BaseMutation):
cost_price_amount = cleaned_input.pop("cost_price", None)
if cost_price_amount is not None:
+ try:
+ validate_price_precision(cost_price_amount)
+ except ValidationError as error:
+ error.code = ProductErrorCode.INVALID.value
+ raise ValidationError({"cost_price": error})
cleaned_input["cost_price_amount"] = cost_price_amount
price_amount = cleaned_input.pop("price", None)
if price_amount is not None:
+ try:
+ validate_price_precision(price_amount)
+ except ValidationError as error:
+ error.code = ProductErrorCode.INVALID.value
+ raise ValidationError({"price": error})
cleaned_input["price_amount"] = price_amount
attributes = cleaned_input.get("attributes")
diff --git a/saleor/graphql/product/tests/test_variant.py b/saleor/graphql/product/tests/test_variant.py
index 1f116199a8..f200b37956 100644
--- a/saleor/graphql/product/tests/test_variant.py
+++ b/saleor/graphql/product/tests/test_variant.py
@@ -1276,7 +1276,7 @@ def test_product_variant_bulk_create_by_attribute_id(
@pytest.mark.parametrize(
"price_field", ("price", "costPrice",),
)
-def test_product_variant_bulk_create_by_attribute_id_with_invalid_price(
+def test_product_variant_bulk_create_by_attribute_id_with_negative_price(
staff_api_client, product, size_attribute, permission_manage_products, price_field
):
# given
@@ -1305,6 +1305,42 @@ def test_product_variant_bulk_create_by_attribute_id_with_invalid_price(
assert_negative_positive_decimal_value(response)
+ "price_field", ("price", "costPrice",),
+)
+def test_product_variant_bulk_create_too_many_decimal_places_in_price(
+ staff_api_client, product, size_attribute, permission_manage_products, price_field
+):
+ # given
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ attribut_id = graphene.Node.to_global_id("Attribute", size_attribute.pk)
+ attribute_value = size_attribute.values.last()
+ sku = str(uuid4())[:12]
+ variant = {
+ "sku": sku,
+ "weight": 2.5,
+ "trackInventory": True,
+ "attributes": [{"id": attribut_id, "values": [attribute_value.name]}],
+ "price": 10,
+ price_field: 1.1234,
+ }
+
+ variables = {"productId": product_id, "variants": [variant]}
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ # when
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productVariantBulkCreate"]
+ error = data["bulkProductErrors"][0]
+ assert error["field"] == price_field
+ assert error["code"] == ProductErrorCode.INVALID.name
+
+
def test_product_variant_bulk_create_empty_attribute(
staff_api_client, product, size_attribute, permission_manage_products
):