Skip to content

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

Add price validation to variant mutations

Krzysztof Świątek71a2d5228720
diff --git a/saleor/graphql/product/bulk_mutations/products.py b/saleor/graphql/product/bulk_mutations/products.py
index ee35fa4656..36f60a84a4 100644
--- a/saleor/graphql/product/bulk_mutations/products.py
+++ b/saleor/graphql/product/bulk_mutations/products.py
@@ -155,10 +155,28 @@ class ProductVariantBulkCreate(BaseMutation):
cost_price_amount = cleaned_input.pop("cost_price", None)
if cost_price_amount is not None:
+ if cost_price_amount < 0:
+ raise ValidationError(
+ {
+ "basePrice": ValidationError(
+ ("Product price cannot be lower than 0."),
+ code=ProductErrorCode.INVALID,
+ )
+ }
+ )
cleaned_input["cost_price_amount"] = cost_price_amount
price_override_amount = cleaned_input.pop("price_override", None)
if price_override_amount is not None:
+ if price_override_amount < 0:
+ raise ValidationError(
+ {
+ "basePrice": ValidationError(
+ ("Product price cannot be lower than 0."),
+ code=ProductErrorCode.INVALID,
+ )
+ }
+ )
cleaned_input["price_override_amount"] = price_override_amount
attributes = cleaned_input.get("attributes")
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index c620088cad..f05693bbac 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -1184,10 +1184,30 @@ class ProductVariantCreate(ModelMutation):
cleaned_input = super().clean_input(info, instance, data)
if "cost_price" in cleaned_input:
- cleaned_input["cost_price_amount"] = cleaned_input.pop("cost_price")
+ cost_price = cleaned_input.pop("cost_price")
+ if cost_price < 0:
+ raise ValidationError(
+ {
+ "costPrice": ValidationError(
+ ("Product price cannot be lower than 0."),
+ code=ProductErrorCode.INVALID,
+ )
+ }
+ )
+ cleaned_input["cost_price_amount"] = cost_price
if "price_override" in cleaned_input:
- cleaned_input["price_override_amount"] = cleaned_input.pop("price_override")
+ price_override = cleaned_input.pop("price_override")
+ if price_override < 0:
+ raise ValidationError(
+ {
+ "priceOverride": ValidationError(
+ ("Product price cannot be lower than 0."),
+ code=ProductErrorCode.INVALID,
+ )
+ }
+ )
+ cleaned_input["price_override_amount"] = price_override
stocks = cleaned_input.get("stocks")
if stocks:
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 2b418d4cd4..0fdc8e8349 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -1632,7 +1632,7 @@ def test_update_product_invalid_price(
data = content["data"]["productUpdate"]
errors = data["productErrors"]
assert errors[0]["field"] == "basePrice"
- assert errors[0]["code"] == "INVALID"
+ assert errors[0]["code"] == ProductErrorCode.INVALID.name
SET_ATTRIBUTES_TO_PRODUCT_QUERY = """
diff --git a/tests/api/test_product_minimal_variant_price.py b/tests/api/test_product_minimal_variant_price.py
index fcb391ff8a..ee58a4acbb 100644
--- a/tests/api/test_product_minimal_variant_price.py
+++ b/tests/api/test_product_minimal_variant_price.py
@@ -6,6 +6,7 @@ from graphql_relay import from_global_id, to_global_id
from prices import Money
from saleor.graphql.discount.enums import DiscountValueTypeEnum
+from saleor.product.error_codes import ProductErrorCode
from tests.api.utils import get_graphql_content
@@ -224,6 +225,100 @@ def test_product_variant_update_updates_minimal_variant_price(
)
+@patch(
+ "saleor.graphql.product.mutations.products"
+ ".update_product_minimal_variant_price_task"
+)
+def test_product_variant_update_updates_invalid_variant_price(
+ mock_update_product_minimal_variant_price_task,
+ staff_api_client,
+ product,
+ permission_manage_products,
+):
+ query = """
+ mutation ProductVariantUpdate(
+ $id: ID!,
+ $priceOverride: Decimal,
+ ) {
+ productVariantUpdate(
+ id: $id,
+ input: {
+ priceOverride: $priceOverride,
+ }
+ ) {
+ productVariant {
+ name
+ }
+ productErrors {
+ field
+ message
+ code
+ }
+ }
+ }
+ """
+ variant = product.variants.first()
+ variant_id = to_global_id("ProductVariant", variant.pk)
+ price_override = "-1.99"
+ variables = {"id": variant_id, "priceOverride": price_override}
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ assert response.status_code == 200
+
+ content = get_graphql_content(response)
+ data = content["data"]["productVariantUpdate"]
+ assert data["productErrors"][0]["field"] == "priceOverride"
+ assert data["productErrors"][0]["code"] == ProductErrorCode.INVALID.name
+
+
+@patch(
+ "saleor.graphql.product.mutations.products"
+ ".update_product_minimal_variant_price_task"
+)
+def test_product_variant_update_updates_invalid_cost_price(
+ mock_update_product_minimal_variant_price_task,
+ staff_api_client,
+ product,
+ permission_manage_products,
+):
+ query = """
+ mutation ProductVariantUpdate(
+ $id: ID!,
+ $costPrice: Decimal,
+ ) {
+ productVariantUpdate(
+ id: $id,
+ input: {
+ costPrice: $costPrice,
+ }
+ ) {
+ productVariant {
+ name
+ }
+ productErrors {
+ field
+ message
+ code
+ }
+ }
+ }
+ """
+ variant = product.variants.first()
+ variant_id = to_global_id("ProductVariant", variant.pk)
+ cost_price = "-1.99"
+ variables = {"id": variant_id, "costPrice": cost_price}
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ assert response.status_code == 200
+
+ content = get_graphql_content(response)
+ data = content["data"]["productVariantUpdate"]
+ assert data["productErrors"][0]["field"] == "costPrice"
+ assert data["productErrors"][0]["code"] == ProductErrorCode.INVALID.name
+
+
@patch(
"saleor.graphql.product.mutations.products."
"update_product_minimal_variant_price_task"