Fix Wagtail article publish regressions
All checks were successful
CI / nightly-e2e (pull_request) Has been skipped
CI / deploy (pull_request) Has been skipped
CI / ci (pull_request) Successful in 1m17s
CI / pr-e2e (pull_request) Successful in 1m46s

This commit is contained in:
2026-03-15 16:53:49 +00:00
parent 15ef35e249
commit 1a0617fbd0
8 changed files with 161 additions and 34 deletions

View File

@@ -2,6 +2,9 @@ from datetime import timedelta
from types import SimpleNamespace
import pytest
from django.contrib import messages
from django.contrib.messages.storage.fallback import FallbackStorage
from django.contrib.sessions.middleware import SessionMiddleware
from django.test import override_settings
from django.utils import timezone
@@ -295,7 +298,7 @@ def test_article_admin_form_relaxes_initial_required_fields(article_index, djang
@pytest.mark.django_db
def test_article_admin_form_clean_applies_defaults(article_index, django_user_model, monkeypatch):
"""Form clean should auto-fill slug/author/category/summary when omitted."""
"""Form clean should populate defaults before parent validation runs."""
user = django_user_model.objects.create_user(
username="writer",
email="writer@example.com",
@@ -310,16 +313,18 @@ def test_article_admin_form_clean_applies_defaults(article_index, django_user_mo
SimpleNamespace(block_type="code", value=SimpleNamespace(raw_code="print('ignore')")),
SimpleNamespace(block_type="rich_text", value=SimpleNamespace(source="<p>Hello world body text.</p>")),
]
form.cleaned_data = {
"title": "Auto Defaults Title",
"slug": "",
"author": None,
"category": None,
"summary": "",
"body": body,
}
observed = {}
def fake_super_clean(_self):
_self.cleaned_data = {
"title": "Auto Defaults Title",
"slug": "",
"author": None,
"category": None,
"summary": "",
"body": body,
}
observed["slug_before_parent_clean"] = _self.cleaned_data.get("slug")
return _self.cleaned_data
mro = form.__class__.__mro__
@@ -327,6 +332,7 @@ def test_article_admin_form_clean_applies_defaults(article_index, django_user_mo
monkeypatch.setattr(super_form_class, "clean", fake_super_clean)
cleaned = form.clean()
assert observed["slug_before_parent_clean"] == "auto-defaults-title"
assert cleaned["slug"] == "auto-defaults-title"
assert cleaned["author"] is not None
assert cleaned["author"].user_id == user.id
@@ -378,3 +384,20 @@ def test_article_save_autogenerates_summary_when_missing(article_index):
article.save()
assert article.summary == "This should become the summary text."
@pytest.mark.django_db
def test_article_page_omits_admin_messages_on_frontend(article_page, rf):
"""Frontend templates should not render admin session messages."""
request = rf.get(article_page.url)
SessionMiddleware(lambda req: None).process_request(request)
request.session.save()
setattr(request, "_messages", FallbackStorage(request))
messages.success(request, "Page 'Test' has been published.")
response = article_page.serve(request)
response.render()
content = response.content.decode()
assert "Page 'Test' has been published." not in content
assert 'aria-label="Messages"' not in content