Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move validation, add extra test
Piotr Grundasd21b3c214063
diff --git a/saleor/graphql/attribute/utils.py b/saleor/graphql/attribute/utils.pyindex a2684d26bc..bced2f44e3 100644--- a/saleor/graphql/attribute/utils.py+++ b/saleor/graphql/attribute/utils.py@@ -490,8 +490,6 @@ def validate_attributes_input(validate_file_attributes_input(*attrs)elif attribute.input_type == AttributeInputType.REFERENCE:validate_reference_attributes_input(*attrs)- elif attribute.input_type == AttributeInputType.TEXT:- validate_text_attributes_input(*attrs)# validation for other input typeselse:validate_standard_attributes_input(*attrs)@@ -550,6 +548,8 @@ def validate_standard_attributes_input(variant_validation: bool,):attribute_id = attr_values.global_id+ name_field = attribute.values.model.name.field # type: ignore+if not attr_values.values:if attribute.value_required or (variant_validation and is_variant_selection_attribute(attribute)@@ -564,25 +564,14 @@ def validate_standard_attributes_input(attribute_errors[AttributeInputErrors.ERROR_DROPDOWN_GET_MORE_THAN_ONE_VALUE].append(attribute_id)+for value in attr_values.values:if value is None or not value.strip():attribute_errors[AttributeInputErrors.ERROR_BLANK_VALUE].append(attribute_id)---def validate_text_attributes_input(- attribute: "Attribute",- attr_values: "AttrValuesInput",- attribute_errors: T_ERROR_DICT,- variant_validation: bool,-):- name_field = attribute.values.model.name.field # type: ignore- attribute_id = attr_values.global_id- text = attr_values.values[0] if attr_values.values else ""-- if len(text) > name_field.max_length:- attribute_errors[AttributeInputErrors.ERROR_MAX_LENGTH].append(attribute_id)+ elif len(value) > name_field.max_length:+ attribute_errors[AttributeInputErrors.ERROR_MAX_LENGTH].append(attribute_id)def is_variant_selection_attribute(attribute: attribute_models.Attribute):diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.pyindex 4323885d29..9d90214ebf 100644--- a/saleor/graphql/product/tests/test_product.py+++ b/saleor/graphql/product/tests/test_product.py@@ -2534,6 +2534,66 @@ def test_create_product_description_plaintext(assert product.description_plaintext == description+def test_create_product_with_text_attribute(+ staff_api_client,+ product_type,+ category,+ text_attribute,+ color_attribute,+ permission_manage_products,+ product,+):+ query = CREATE_PRODUCT_MUTATION++ 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"+ product_slug = "product-test-slug"++ # Add second attribute+ product_type.product_attributes.add(text_attribute)+ text_attribute_id = graphene.Node.to_global_id("Attribute", text_attribute.id)++ # test creating root product+ variables = {+ "input": {+ "productType": product_type_id,+ "category": category_id,+ "name": product_name,+ "slug": product_slug,+ "attributes": [{"id": text_attribute_id, "values": ["Cool text"]}],+ }+ }++ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products]+ )+ content = get_graphql_content(response)+ data = content["data"]["productCreate"]+ assert data["productErrors"] == []+ assert data["product"]["name"] == product_name+ assert data["product"]["slug"] == product_slug+ assert data["product"]["productType"]["name"] == product_type.name+ assert data["product"]["category"]["name"] == category.name+ _, product_id = graphene.Node.from_global_id(data["product"]["id"])+ expected_attributes_data = [+ {+ "attribute": {"slug": "text"},+ "values": [+ {+ "slug": "cool-text",+ "name": "Cool text",+ "reference": None,+ "file": None,+ }+ ],+ },+ {"attribute": {"slug": color_attribute.slug}, "values": []},+ ]+ for attr_data in data["product"]["attributes"]:+ assert attr_data in expected_attributes_data++SEARCH_PRODUCTS_QUERY = """query Products($filters: ProductFilterInput, $sortBy: ProductOrder, $channel: String@@ -3267,66 +3327,6 @@ def test_create_product_with_product_reference_attribute_required_no_references(]-def test_create_product_with_text_attribute(- staff_api_client,- product_type,- category,- text_attribute,- color_attribute,- permission_manage_products,- product,-):- query = CREATE_PRODUCT_MUTATION-- 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"- product_slug = "product-test-slug"-- # Add second attribute- product_type.product_attributes.add(text_attribute)- text_attribute_id = graphene.Node.to_global_id("Attribute", text_attribute.id)-- # test creating root product- variables = {- "input": {- "productType": product_type_id,- "category": category_id,- "name": product_name,- "slug": product_slug,- "attributes": [{"id": text_attribute_id, "values": ["Cool text"]}],- }- }-- response = staff_api_client.post_graphql(- query, variables, permissions=[permission_manage_products]- )- content = get_graphql_content(response)- data = content["data"]["productCreate"]- assert data["productErrors"] == []- assert data["product"]["name"] == product_name- assert data["product"]["slug"] == product_slug- assert data["product"]["productType"]["name"] == product_type.name- assert data["product"]["category"]["name"] == category.name- _, product_id = graphene.Node.from_global_id(data["product"]["id"])- expected_attributes_data = [- {- "attribute": {"slug": "text"},- "values": [- {- "slug": "cool-text",- "name": "Cool text",- "reference": None,- "file": None,- }- ],- },- {"attribute": {"slug": color_attribute.slug}, "values": []},- ]- for attr_data in data["product"]["attributes"]:- assert attr_data in expected_attributes_data--def test_create_product_no_values_given(staff_api_client,product_type,@@ -5619,6 +5619,47 @@ def test_product_type_create_mutation(assert tax_code == "wine"+def test_create_product_type_with_text_attribute(+ staff_api_client,+ product_type,+ permission_manage_product_types_and_attributes,+ text_attribute,+):+ query = PRODUCT_TYPE_CREATE_MUTATION+ product_type_name = "test type"+ slug = "test-type"++ product_type.product_attributes.add(text_attribute)++ product_attributes_ids = [+ graphene.Node.to_global_id("Attribute", attr.id)+ for attr in product_type.product_attributes.all()+ ]++ variables = {+ "name": product_type_name,+ "slug": slug,+ "productAttributes": product_attributes_ids,+ }++ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_product_types_and_attributes]+ )+ content = get_graphql_content(response)+ data = content["data"]["productTypeCreate"]["productType"]+ errors = content["data"]["productTypeCreate"]["productErrors"]++ assert not errors+ assert data["name"] == product_type_name+ assert data["slug"] == slug+ expected_attributes = [+ {"name": "Color", "values": [{"name": "Red"}, {"name": "Blue"}]},+ {"name": "Text", "values": [{"name": "Some cool text"}]},+ ]+ for attribute in data["productAttributes"]:+ assert attribute in expected_attributes++@pytest.mark.parametrize("input_slug, expected_slug",(diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.pyindex a00eb8642a..9a02389455 100644--- a/saleor/tests/fixtures.py+++ b/saleor/tests/fixtures.py@@ -778,7 +778,7 @@ def text_attribute(db):attribute = Attribute.objects.create(slug="text",name="Text",- type=AttributeInputType.TEXT,+ type=AttributeType.PRODUCT_TYPE,input_type=AttributeInputType.TEXT,filterable_in_storefront=True,filterable_in_dashboard=True,@@ -1148,6 +1148,7 @@ def product_type(color_attribute, size_attribute):has_variants=True,is_shipping_required=True,)+product_type.product_attributes.add(color_attribute)product_type.variant_attributes.add(size_attribute)return product_type