Skip to content

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

Add basePrice validation in product create and update mutations - prevent from creating product with negative price

IKarbowiakf2f9f0f2c4b6
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 14f3c3f295..6496765557 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -823,6 +823,17 @@ class ProductCreate(ModelMutation):
}
)
+ base_price = cleaned_input.get("base_price")
+ if base_price is not None and base_price < 0:
+ raise ValidationError(
+ {
+ "base_price": ValidationError(
+ "Product price cannot be lower than 0.",
+ code=ProductErrorCode.INVALID.value,
+ )
+ }
+ )
+
# Attributes are provided as list of `AttributeValueInput` objects.
# We need to transform them into the format they're stored in the
# `Product` model, which is HStore field that maps attribute's PK to
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index ee6a3be744..41397b1521 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -1356,6 +1356,40 @@ def test_create_product_with_negative_weight(
assert error["code"] == ProductErrorCode.INVALID.name
+def test_create_product_with_negative_base_price(
+ staff_api_client,
+ product_type,
+ category,
+ description_json,
+ permission_manage_products,
+):
+ query = CREATE_PRODUCT_MUTATION
+
+ description_json = json.dumps(description_json)
+
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+ product_name = "test name"
+
+ variables = {
+ "input": {
+ "productType": product_type_id,
+ "category": category_id,
+ "name": product_name,
+ "basePrice": -1,
+ }
+ }
+
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productCreate"]
+ error = data["productErrors"][0]
+ assert error["field"] == "basePrice"
+ assert error["code"] == ProductErrorCode.INVALID.name
+
+
def test_create_product_with_unicode_in_slug_and_name(
staff_api_client,
product_type,
@@ -2247,6 +2281,46 @@ def test_update_product_with_negative_weight(
assert error["code"] == ProductErrorCode.INVALID.name
+def test_update_product_with_negative_base_price(
+ staff_api_client, product_with_default_variant, permission_manage_products, product
+):
+ query = """
+ mutation updateProduct(
+ $productId: ID!,
+ $basePrice: Decimal)
+ {
+ productUpdate(
+ id: $productId,
+ input: {
+ basePrice: $basePrice
+ })
+ {
+ product {
+ id
+ }
+ productErrors {
+ field
+ message
+ code
+ }
+ }
+ }
+ """
+ product = product_with_default_variant
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+
+ variables = {"productId": product_id, "basePrice": -1}
+
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productUpdate"]
+ error = data["productErrors"][0]
+ assert error["field"] == "basePrice"
+ assert error["code"] == ProductErrorCode.INVALID.name
+
+
def test_update_product_without_category_and_true_is_published_value(
staff_api_client, permission_manage_products, product
):