Skip to content

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

Fix plain text attribute validation (#10078)

Iga Karbowiak58155fc43985
diff --git a/saleor/graphql/attribute/utils.py b/saleor/graphql/attribute/utils.py
index 965b84cad9..1289b6437f 100644
--- a/saleor/graphql/attribute/utils.py
+++ b/saleor/graphql/attribute/utils.py
@@ -628,19 +628,19 @@ def validate_attributes_input(
attr_values,
attribute_errors,
)
- if attribute.input_type == AttributeInputType.FILE:
- validate_file_attributes_input(*attrs)
- elif attribute.input_type == AttributeInputType.REFERENCE:
- validate_reference_attributes_input(*attrs)
- elif attribute.input_type == AttributeInputType.RICH_TEXT:
- validate_rich_text_attributes_input(*attrs)
- elif attribute.input_type == AttributeInputType.BOOLEAN:
- validate_boolean_input(*attrs)
- elif attribute.input_type in [
- AttributeInputType.DATE,
- AttributeInputType.DATE_TIME,
- ]:
- validate_date_time_input(*attrs)
+ input_type_to_validation_func_mapping = {
+ AttributeInputType.FILE: validate_file_attributes_input,
+ AttributeInputType.REFERENCE: validate_reference_attributes_input,
+ AttributeInputType.RICH_TEXT: validate_rich_text_attributes_input,
+ AttributeInputType.PLAIN_TEXT: validate_plain_text_attributes_input,
+ AttributeInputType.BOOLEAN: validate_boolean_input,
+ AttributeInputType.DATE: validate_date_time_input,
+ AttributeInputType.DATE_TIME: validate_date_time_input,
+ }
+ if validation_func := input_type_to_validation_func_mapping.get(
+ attribute.input_type
+ ):
+ validation_func(*attrs)
# validation for other input types
else:
validate_standard_attributes_input(*attrs)
@@ -714,6 +714,19 @@ def validate_rich_text_attributes_input(
attribute_errors[AttributeInputErrors.ERROR_NO_VALUE_GIVEN].append(attribute_id)
+def validate_plain_text_attributes_input(
+ attribute: "Attribute",
+ attr_values: "AttrValuesInput",
+ attribute_errors: T_ERROR_DICT,
+):
+ attribute_id = attr_values.global_id
+
+ if (
+ not attr_values.plain_text or not attr_values.plain_text.strip()
+ ) and attribute.value_required:
+ attribute_errors[AttributeInputErrors.ERROR_NO_VALUE_GIVEN].append(attribute_id)
+
+
def validate_standard_attributes_input(
attribute: "Attribute",
attr_values: "AttrValuesInput",
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index 583a23e400..790a9aad8e 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -6790,6 +6790,7 @@ MUTATION_UPDATE_PRODUCT = """
slug
boolean
reference
+ plainText
file {
url
contentType
@@ -7007,6 +7008,7 @@ def test_update_product_with_boolean_attribute_value(
"slug": f"{boolean_attribute.id}_false",
"reference": None,
"file": None,
+ "plainText": None,
}
],
}
@@ -7065,6 +7067,7 @@ def test_update_product_with_file_attribute_value(
"contentType": None,
},
"boolean": None,
+ "plainText": None,
}
],
}
@@ -7127,6 +7130,7 @@ def test_update_product_with_file_attribute_value_new_value_is_not_created(
"contentType": existing_value.content_type,
},
"boolean": None,
+ "plainText": None,
}
],
}
@@ -7185,6 +7189,7 @@ def test_update_product_with_numeric_attribute_value(
"reference": None,
"file": None,
"boolean": None,
+ "plainText": None,
}
],
}
@@ -7247,6 +7252,7 @@ def test_update_product_with_numeric_attribute_value_new_value_is_not_created(
"reference": None,
"file": None,
"boolean": None,
+ "plainText": None,
}
],
}
@@ -7394,6 +7400,161 @@ def test_update_product_clean_file_attribute_value(
assert product_attr.values.count() == 0
+@patch("saleor.plugins.manager.PluginsManager.product_updated")
+def test_update_product_with_plain_text_attribute_value(
+ updated_webhook_mock,
+ staff_api_client,
+ product,
+ product_type,
+ plain_text_attribute,
+ permission_manage_products,
+):
+ # given
+ query = MUTATION_UPDATE_PRODUCT
+
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ attribute_id = graphene.Node.to_global_id("Attribute", plain_text_attribute.pk)
+ product_type.product_attributes.add(plain_text_attribute)
+
+ plain_text_attribute_value = plain_text_attribute.values.first()
+ text = plain_text_attribute_value.plain_text
+
+ variables = {
+ "productId": product_id,
+ "input": {"attributes": [{"id": attribute_id, "plainText": text}]},
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productUpdate"]
+ assert data["errors"] == []
+
+ attributes = data["product"]["attributes"]
+ assert len(attributes) == 2
+ expected_att_data = {
+ "attribute": {"id": attribute_id, "name": plain_text_attribute.name},
+ "values": [
+ {
+ "id": ANY,
+ "slug": f"{product.id}_{plain_text_attribute.id}",
+ "name": text,
+ "reference": None,
+ "file": None,
+ "boolean": None,
+ "plainText": text,
+ }
+ ],
+ }
+ assert expected_att_data in attributes
+
+ updated_webhook_mock.assert_called_once_with(product)
+
+
+@patch("saleor.plugins.manager.PluginsManager.product_updated")
+def test_update_product_with_plain_text_attribute_value_required(
+ updated_webhook_mock,
+ staff_api_client,
+ product,
+ product_type,
+ plain_text_attribute,
+ permission_manage_products,
+):
+ # given
+ query = MUTATION_UPDATE_PRODUCT
+
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ attribute_id = graphene.Node.to_global_id("Attribute", plain_text_attribute.pk)
+ product_type.product_attributes.add(plain_text_attribute)
+
+ plain_text_attribute.value_required = True
+ plain_text_attribute.save(update_fields=["value_required"])
+
+ plain_text_attribute_value = plain_text_attribute.values.first()
+ text = plain_text_attribute_value.plain_text
+
+ variables = {
+ "productId": product_id,
+ "input": {"attributes": [{"id": attribute_id, "plainText": text}]},
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productUpdate"]
+ assert data["errors"] == []
+
+ attributes = data["product"]["attributes"]
+ assert len(attributes) == 2
+ expected_att_data = {
+ "attribute": {"id": attribute_id, "name": plain_text_attribute.name},
+ "values": [
+ {
+ "id": ANY,
+ "slug": f"{product.id}_{plain_text_attribute.id}",
+ "name": text,
+ "reference": None,
+ "file": None,
+ "boolean": None,
+ "plainText": text,
+ }
+ ],
+ }
+ assert expected_att_data in attributes
+
+ updated_webhook_mock.assert_called_once_with(product)
+
+
[email protected]("value", ["", " ", None])
+@patch("saleor.plugins.manager.PluginsManager.product_updated")
+def test_update_product_with_plain_text_attribute_value_required_no_value_given(
+ updated_webhook_mock,
+ value,
+ staff_api_client,
+ product,
+ product_type,
+ plain_text_attribute,
+ permission_manage_products,
+):
+ # given
+ query = MUTATION_UPDATE_PRODUCT
+
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ attribute_id = graphene.Node.to_global_id("Attribute", plain_text_attribute.pk)
+ product_type.product_attributes.add(plain_text_attribute)
+
+ plain_text_attribute.value_required = True
+ plain_text_attribute.save(update_fields=["value_required"])
+
+ variables = {
+ "productId": product_id,
+ "input": {"attributes": [{"id": attribute_id, "plainText": value}]},
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productUpdate"]
+ errors = data["errors"]
+
+ assert not data["product"]
+ assert len(errors) == 1
+ assert errors[0]["code"] == ProductErrorCode.REQUIRED.name
+ assert errors[0]["field"] == "attributes"
+
+
@freeze_time("2020-03-18 12:00:00")
def test_update_product_rating(
staff_api_client,
@@ -7474,6 +7635,7 @@ def test_update_product_with_page_reference_attribute_value(
"file": None,
"reference": reference,
"boolean": None,
+ "plainText": None,
}
],
}
@@ -7535,6 +7697,7 @@ def test_update_product_without_supplying_required_product_attribute(
"file": None,
"reference": None,
"boolean": None,
+ "plainText": None,
}
],
} in attributes_data
@@ -7640,6 +7803,7 @@ def test_update_product_with_page_reference_attribute_existing_value(
"file": None,
"reference": reference,
"boolean": None,
+ "plainText": None,
}
],
}
@@ -7752,6 +7916,7 @@ def test_update_product_with_product_reference_attribute_value(
"file": None,
"reference": reference,
"boolean": None,
+ "plainText": None,
}
],
}
@@ -7828,6 +7993,7 @@ def test_update_product_with_product_reference_attribute_existing_value(
"file": None,
"reference": reference,
"boolean": None,
+ "plainText": None,
}
],
}
diff --git a/saleor/graphql/product/tests/test_variant.py b/saleor/graphql/product/tests/test_variant.py
index 41bde00410..8e0f7eb76a 100644
--- a/saleor/graphql/product/tests/test_variant.py
+++ b/saleor/graphql/product/tests/test_variant.py
@@ -2528,6 +2528,61 @@ def test_update_variant_with_plain_text_attribute(
product_variant_updated.assert_called_once_with(product.variants.last())
+@patch("saleor.plugins.manager.PluginsManager.product_variant_updated")
+def test_update_variant_with_plain_text_attribute_value_required(
+ product_variant_updated,
+ permission_manage_products,
+ product,
+ product_type,
+ staff_api_client,
+ plain_text_attribute,
+ warehouse,
+):
+ # given
+ product_type.variant_attributes.add(plain_text_attribute)
+ query = QUERY_UPDATE_VARIANT_ATTRIBUTES
+ variant = product.variants.first()
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+ sku = "123"
+ attr_id = graphene.Node.to_global_id("Attribute", plain_text_attribute.id)
+ plain_text_attribute_value = plain_text_attribute.values.first()
+ text = plain_text_attribute_value.plain_text
+ variables = {
+ "id": variant_id,
+ "sku": sku,
+ "attributes": [
+ {"id": attr_id, "plainText": text},
+ ],
+ }
+ plain_text_attribute_value.slug = f"{variant.id}_{plain_text_attribute.id}"
+ plain_text_attribute_value.save()
+
+ plain_text_attribute.value_required = True
+ plain_text_attribute.save(update_fields=["value_required"])
+
+ values_count = plain_text_attribute.values.count()
+ associate_attribute_values_to_instance(
+ variant, plain_text_attribute, plain_text_attribute.values.first()
+ )
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)["data"]["productVariantUpdate"]
+ variant.refresh_from_db()
+ data = content["productVariant"]
+
+ assert not content["errors"]
+ assert data["sku"] == sku
+ assert data["attributes"][-1]["attribute"]["slug"] == plain_text_attribute.slug
+ assert data["attributes"][-1]["values"][0]["plainText"] == text
+ assert plain_text_attribute.values.count() == values_count
+ product_variant_updated.assert_called_once_with(product.variants.last())
+
+
@pytest.mark.parametrize("value", ["", " ", None])
def test_update_variant_with_required_plain_text_attribute_no_value(
value,