Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix saving seo fields #12581

Merged
merged 2 commits into from Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions saleor/graphql/core/validators/__init__.py
Expand Up @@ -141,10 +141,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"]
szdrasiak marked this conversation as resolved.
Show resolved Hide resolved


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 @@ -278,6 +278,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