Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
3.1 Add checking if given attributes are variant attributes in ProductVariantCreate mutation (#9134)
L3str4nge409ca7d23f23
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex 2908f86bad..f76a0a3704 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -930,6 +930,21 @@ class ProductVariantCreate(ModelMutation):cleaned_input["product"])+ variant_attributes_ids = {+ graphene.Node.to_global_id("Attribute", attr_id)+ for attr_id in list(+ product_type.variant_attributes.all().values_list("pk", flat=True)+ )+ }+ attributes_ids = {attr["id"] for attr in data.get("attributes") or []}+ invalid_attributes = attributes_ids - variant_attributes_ids+ if len(invalid_attributes) > 0:+ raise ValidationError(+ "Given attributes are not a variant attributes.",+ code=ProductErrorCode.ATTRIBUTE_CANNOT_BE_ASSIGNED.value,+ params={"attributes": invalid_attributes},+ )+# Run the validation only if product type is configurableif product_type.has_variants:# Attributes are provided as list of `AttributeValueInput` objects.@@ -956,6 +971,11 @@ class ProductVariantCreate(ModelMutation):)except ValidationError as exc:raise ValidationError({"attributes": exc})+ else:+ raise ValidationError(+ "Product type is not configurable.",+ ProductErrorCode.ATTRIBUTE_CANNOT_BE_ASSIGNED.value,+ )if "sku" in cleaned_input:cleaned_input["sku"] = clean_variant_sku(cleaned_input.get("sku"))diff --git a/saleor/graphql/product/tests/test_variant.py b/saleor/graphql/product/tests/test_variant.pyindex 5c82cf89f3..4a306d0620 100644--- a/saleor/graphql/product/tests/test_variant.py+++ b/saleor/graphql/product/tests/test_variant.py@@ -6080,3 +6080,77 @@ def test_product_variant_deactivate_preorder_as_app()assert_no_permission(response)+++VARIANT_CREATE_MUTATION = """+ mutation variantCreate($productId: ID!, $attrId: ID!) {+ productVariantCreate (+ input: {+ sku: "my-sku",+ product: $productId,+ attributes: [{id: $attrId, values:["1"]}]+ }+ )+ {+ productVariant {+ id+ }+ errors {+ field,+ message,+ code,+ attributes+ }+ }+ }+"""+++def test_variant_create_product_without_variant_attributes(+ product_with_product_attributes, staff_api_client, permission_manage_products+):+ product = product_with_product_attributes++ prod_id = graphene.Node.to_global_id("Product", product.pk)+ attr_id = graphene.Node.to_global_id(+ "Attribute", product.product_type.product_attributes.first().pk+ )++ response = staff_api_client.post_graphql(+ VARIANT_CREATE_MUTATION,+ variables={"productId": prod_id, "attrId": attr_id},+ permissions=[permission_manage_products],+ )+ content = get_graphql_content(response)++ errors = content["data"]["productVariantCreate"]["errors"]+ assert errors+ assert errors[0]["code"] == ProductErrorCode.ATTRIBUTE_CANNOT_BE_ASSIGNED.name+ assert len(errors[0]["attributes"]) == 1+ assert errors[0]["attributes"][0] == attr_id+++def test_variant_create_product_with_variant_attributes_variant_flag_false(+ product_with_variant_attributes, staff_api_client, permission_manage_products+):+ product = product_with_variant_attributes++ product_type = product.product_type+ product_type.has_variants = False+ product_type.save()++ prod_id = graphene.Node.to_global_id("Product", product.pk)+ attr_id = graphene.Node.to_global_id(+ "Attribute", product.product_type.variant_attributes.first().pk+ )++ response = staff_api_client.post_graphql(+ VARIANT_CREATE_MUTATION,+ variables={"productId": prod_id, "attrId": attr_id},+ permissions=[permission_manage_products],+ )+ content = get_graphql_content(response)++ errors = content["data"]["productVariantCreate"]["errors"]+ assert errors+ assert errors[0]["code"] == ProductErrorCode.ATTRIBUTE_CANNOT_BE_ASSIGNED.namediff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.pyindex f7bcfd1d52..76aaacc09a 100644--- a/saleor/tests/fixtures.py+++ b/saleor/tests/fixtures.py@@ -1095,6 +1095,74 @@ def color_attribute(db):return attribute+def attribute_without_values():+ attribute = Attribute.objects.create(+ slug="dropdown",+ name="Dropdown",+ type=AttributeType.PRODUCT_TYPE,+ filterable_in_storefront=True,+ filterable_in_dashboard=True,+ available_in_grid=True,+ visible_in_storefront=True,+ entity_type=None,+ )++ return attribute+++def product_type_with_product_attributes(attribute_without_values):+ product_type = ProductType.objects.create(+ name="product_type_with_product_attributes",+ slug="product-type-with-product-attributes",+ has_variants=False,+ is_shipping_required=False,+ weight=0,+ )+ product_type.product_attributes.add(attribute_without_values)+ return product_type+++def product_type_with_variant_attributes(attribute_without_values):+ product_type = ProductType.objects.create(+ name="product_type_with_variant_attributes",+ slug="product-type-with-variant-attributes",+ has_variants=False,+ is_shipping_required=False,+ weight=0,+ )+ product_type.variant_attributes.add(attribute_without_values)+ return product_type+++def product_with_product_attributes(+ product_type_with_product_attributes, non_default_category+):+ product = Product.objects.create(+ name="product_with_product_attributes",+ slug="product-with-product-attributes",+ product_type=product_type_with_product_attributes,+ category=non_default_category,+ )+ return product+++def product_with_variant_attributes(+ product_type_with_variant_attributes, non_default_category+):+ product = Product.objects.create(+ name="product_with_variant_attributes",+ slug="product-with-variant-attributes",+ product_type=product_type_with_variant_attributes,+ category=non_default_category,+ )+ return product++@pytest.fixturedef date_attribute(db):attribute = Attribute.objects.create(