Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Raise an error when product is empty in ProductVariantCreate mutation (#15442)
Iga Karbowiake129a4d6f9b1
diff --git a/saleor/graphql/product/mutations/product_variant/product_variant_create.py b/saleor/graphql/product/mutations/product_variant/product_variant_create.pyindex 77fbc9cec9..c8df5fb86b 100644--- a/saleor/graphql/product/mutations/product_variant/product_variant_create.py+++ b/saleor/graphql/product/mutations/product_variant/product_variant_create.py@@ -214,6 +214,16 @@ class ProductVariantCreate(ModelMutation):else:# If the variant is getting created, no product type is associated yet,# retrieve it from the required "product" input field+ product = cleaned_input["product"]+ if not product:+ raise ValidationError(+ {+ "product": ValidationError(+ "Product cannot be set empty.",+ code=ProductErrorCode.INVALID.value,+ )+ }+ )product_type = cleaned_input["product"].product_typeused_attribute_values = get_used_variants_attribute_values(cleaned_input["product"]diff --git a/saleor/graphql/product/tests/mutations/test_product_variant_create.py b/saleor/graphql/product/tests/mutations/test_product_variant_create.pyindex 3fa6dcd3c7..56df11c2d0 100644--- a/saleor/graphql/product/tests/mutations/test_product_variant_create.py+++ b/saleor/graphql/product/tests/mutations/test_product_variant_create.py@@ -220,6 +220,52 @@ def test_create_variant_without_name(updated_webhook_mock.assert_not_called()+def test_create_variant_empty_product_id(+ staff_api_client,+ product_type,+ permission_manage_products,+ warehouse,+):+ # given+ query = CREATE_VARIANT_MUTATION+ sku = "1"+ weight = 10.22+ attribute_id = graphene.Node.to_global_id(+ "Attribute", product_type.variant_attributes.first().pk+ )+ variant_value = "test-value"+ stocks = [+ {+ "warehouse": graphene.Node.to_global_id("Warehouse", warehouse.pk),+ "quantity": 20,+ }+ ]++ variables = {+ "input": {+ "product": "",+ "sku": sku,+ "stocks": stocks,+ "weight": weight,+ "attributes": [{"id": attribute_id, "values": [variant_value]}],+ "trackInventory": True,+ }+ }++ # when+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products]+ )+ content = get_graphql_content(response)["data"]["productVariantCreate"]++ # then+ assert not content["productVariant"]+ assert len(content["errors"]) == 1+ error = content["errors"][0]+ assert error["field"] == "product"+ assert error["code"] == ProductErrorCode.INVALID.name++@patch("saleor.plugins.manager.PluginsManager.product_variant_created")@patch("saleor.plugins.manager.PluginsManager.product_variant_updated")def test_create_variant_preorder(