Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add product price validation
Krzysztof Świątek75fb26e4accb
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 9494061377..f14171a4f7 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -86,6 +86,7 @@ All notable, unreleased changes to this project will be documented in this file.- Add ability to assign multiple warehouses in mutations to create/update a shipping zone - #5399 by @fowczarek- Add filter by ids to warehouses query - #5414 by @fowczarek+- Add product price validation - #5413 by @kswiatek92## 2.9.0diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex 72ae297ae2..c620088cad 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -833,6 +833,15 @@ class ProductCreate(ModelMutation):# from the schema, only "basePrice" should be used here.price = data.get("base_price", data.get("price"))if price is not None:+ if price < 0:+ raise ValidationError(+ {+ "basePrice": ValidationError(+ ("Product base price cannot be lower than 0."),+ code=ProductErrorCode.INVALID,+ )+ }+ )cleaned_input["price_amount"] = priceif instance.minimal_variant_price_amount is None:# Set the default "minimal_variant_price" to the "price"diff --git a/tests/api/test_product.py b/tests/api/test_product.pyindex 8f02221b31..2b418d4cd4 100644--- a/tests/api/test_product.py+++ b/tests/api/test_product.py@@ -1595,6 +1595,46 @@ def test_update_product_slug_and_name(assert errors[0]["code"] == ProductErrorCode.REQUIRED.name+UPDATE_PRODUCT_PRICE_MUTATION = """+ mutation($id: ID!, $basePrice: Decimal) {+ productUpdate(+ id: $id+ input: {+ basePrice: $basePrice+ }+ ) {+ product{+ name+ slug+ }+ productErrors {+ field+ message+ code+ }+ }+ }+"""+++def test_update_product_invalid_price(+ staff_api_client, product, permission_manage_products,+):++ node_id = graphene.Node.to_global_id("Product", product.id)+ variables = {"basePrice": Decimal("-19"), "id": node_id}+ response = staff_api_client.post_graphql(+ UPDATE_PRODUCT_PRICE_MUTATION,+ variables,+ permissions=[permission_manage_products],+ )+ content = get_graphql_content(response)+ data = content["data"]["productUpdate"]+ errors = data["productErrors"]+ assert errors[0]["field"] == "basePrice"+ assert errors[0]["code"] == "INVALID"++SET_ATTRIBUTES_TO_PRODUCT_QUERY = """mutation updateProduct($productId: ID!, $attributes: [AttributeValueInput!]) {productUpdate(id: $productId, input: { attributes: $attributes }) {