From e2e356d0aebd2606baf7396d4839a6da37db3cd2 Mon Sep 17 00:00:00 2001 From: Yves Serrano Date: Wed, 15 Jun 2022 13:52:48 +0200 Subject: [PATCH] use an empty list instead of [None] if revision.user doesn't exists fixes #7879 (#8685) --- CHANGELOG.txt | 1 + CONTRIBUTORS.rst | 1 + docs/releases/4.0.md | 1 + wagtail/admin/mail.py | 2 +- wagtail/admin/tests/pages/test_moderation.py | 59 ++++++++++++++++++++ 5 files changed, 63 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index f874e81124..883fc732ad 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -48,6 +48,7 @@ Changelog * Fix: Ensure non-square avatar images will correctly show throughout the admin (LB (Ben) Johnston) * Fix: Ignore translations in test files and re-include some translations that were accidentally ignored (Stefan Hammer) * Fix: Show alternative message when no page types are available to be created (Jaspreet Singh) + * Fix: Prevent error on sending notifications for the legacy moderation process when no user was specified (Yves Serrano) 3.0.1 (16.06.2022) diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 2e210cf6bf..69776f4eab 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -602,6 +602,7 @@ Contributors * Benedict Faw * Lucie Le Frapper * Jaspreet Singh +* Yves Serrano Translators =========== diff --git a/docs/releases/4.0.md b/docs/releases/4.0.md index e603c854c4..1a220c9e20 100644 --- a/docs/releases/4.0.md +++ b/docs/releases/4.0.md @@ -62,6 +62,7 @@ When using a queryset to render a list of images, you can now use the ``prefetch * Ensure non-square avatar images will correctly show throughout the admin (LB (Ben) Johnston) * Ignore translations in test files and re-include some translations that were accidentally ignored (Stefan Hammer) * Show alternative message when no page types are available to be created (Jaspreet Singh) + * Prevent error on sending notifications for the legacy moderation process when no user was specified (Yves Serrano) ## Upgrade considerations diff --git a/wagtail/admin/mail.py b/wagtail/admin/mail.py index d3c26e1bf2..b8be166664 100644 --- a/wagtail/admin/mail.py +++ b/wagtail/admin/mail.py @@ -77,7 +77,7 @@ def send_moderation_notification(revision, notification, excluded_user=None): ) elif notification in ["rejected", "approved"]: # Get submitter - recipient_users = [revision.user] + recipient_users = [revision.user] if revision.user else [] else: return False diff --git a/wagtail/admin/tests/pages/test_moderation.py b/wagtail/admin/tests/pages/test_moderation.py index b08962a91a..b989938989 100644 --- a/wagtail/admin/tests/pages/test_moderation.py +++ b/wagtail/admin/tests/pages/test_moderation.py @@ -370,3 +370,62 @@ class TestNotificationPreferences(TestCase, WagtailTestUtils): headers.issubset(msg_headers), msg="Message is missing the Auto-Submitted header.", ) + + +class TestApproveRejectModerationWithoutUser(TestCase, WagtailTestUtils): + def setUp(self): + self.submitter = self.create_superuser( + username="submitter", + email="submitter@email.com", + password="password", + ) + + self.user = self.login() + + # Create a page and submit it for moderation + root_page = Page.objects.get(id=2) + self.page = SimplePage( + title="Hello world!", + slug="hello-world", + content="hello", + live=False, + has_unpublished_changes=True, + ) + root_page.add_child(instance=self.page) + + # save_revision without user + self.page.save_revision(submitted_for_moderation=True) + self.revision = self.page.get_latest_revision() + + def test_approve_moderation_view_without_user(self): + """ + This posts to the approve moderation view and checks that the page was approved + """ + # Connect a mock signal handler to page_published signal + mock_handler = mock.MagicMock() + page_published.connect(mock_handler) + + # Post + response = self.client.post( + reverse("wagtailadmin_pages:approve_moderation", args=(self.revision.id,)) + ) + + # Check that the user was redirected to the dashboard + self.assertRedirects(response, reverse("wagtailadmin_home")) + + page = Page.objects.get(id=self.page.id) + # Page must be live + self.assertTrue(page.live, "Approving moderation failed to set live=True") + # Page should now have no unpublished changes + self.assertFalse( + page.has_unpublished_changes, + "Approving moderation failed to set has_unpublished_changes=False", + ) + + # Check that the page_published signal was fired + self.assertEqual(mock_handler.call_count, 1) + mock_call = mock_handler.mock_calls[0][2] + + self.assertEqual(mock_call["sender"], self.page.specific_class) + self.assertEqual(mock_call["instance"], self.page) + self.assertIsInstance(mock_call["instance"], self.page.specific_class)