Skip to content

Commit

Permalink
Fix saving seo fields (#12581) (#12783)
Browse files Browse the repository at this point in the history
  • Loading branch information
szdrasiak committed May 10, 2023
1 parent f86c6a8 commit 6b2cf36
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
11 changes: 7 additions & 4 deletions saleor/graphql/core/validators/__init__.py
Expand Up @@ -167,10 +167,13 @@ def validate_slug_value(cleaned_input, slug_field_name: str = "slug"):

def clean_seo_fields(data):
"""Extract and assign seo fields to given dictionary."""
seo_fields = data.pop("seo", None)
if seo_fields:
data["seo_title"] = seo_fields.get("title")
data["seo_description"] = seo_fields.get("description")
seo_fields = data.pop("seo", {})

if "title" in seo_fields:
data["seo_title"] = seo_fields["title"]

if "description" in seo_fields:
data["seo_description"] = seo_fields["description"]


def validate_required_string_field(cleaned_input, field_name: str):
Expand Down
76 changes: 76 additions & 0 deletions saleor/graphql/product/tests/mutations/test_product_update.py
Expand Up @@ -283,6 +283,82 @@ def test_update_product_doesnt_clear_description_plaintext_when_no_description(
assert product.description_plaintext == description_plaintext


def test_update_product_seo_field_title(
staff_api_client,
non_default_category,
product,
other_description_json,
permission_manage_products,
color_attribute,
):
query = MUTATION_UPDATE_PRODUCT
old_seo_description = "old seo description"
product.seo_description = old_seo_description
product.seo_title = "old_seo_title"
product.save(update_fields=["seo_description", "seo_title"])

product_id = graphene.Node.to_global_id("Product", product.pk)
new_seo_title = "new_seo_title"

variables = {
"productId": product_id,
"input": {
"seo": {
"title": new_seo_title,
},
},
}

response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products]
)
content = get_graphql_content(response)
data = content["data"]["productUpdate"]
assert not data["errors"]

product.refresh_from_db()
assert product.seo_description == old_seo_description
assert product.seo_title == new_seo_title


def test_update_product_seo_field_description(
staff_api_client,
non_default_category,
product,
other_description_json,
permission_manage_products,
color_attribute,
):
query = MUTATION_UPDATE_PRODUCT
old_seo_title = "old_seo_title"
product.seo_description = "old seo description"
product.seo_title = old_seo_title
product.save(update_fields=["seo_description", "seo_title"])

product_id = graphene.Node.to_global_id("Product", product.pk)
new_seo_description = "new_seo_description"

variables = {
"productId": product_id,
"input": {
"seo": {
"description": new_seo_description,
},
},
}

response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products]
)
content = get_graphql_content(response)
data = content["data"]["productUpdate"]
assert not data["errors"]

product.refresh_from_db()
assert product.seo_description == new_seo_description
assert product.seo_title == old_seo_title


@patch("saleor.plugins.manager.PluginsManager.product_updated")
def test_update_product_with_boolean_attribute_value(
updated_webhook_mock,
Expand Down

0 comments on commit 6b2cf36

Please sign in to comment.