From 779d10051556242ba1e70b70f6c6e8f1158e861d Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sun, 22 Nov 2026 11:11:23 +0100 Subject: feat(receiver)!: enrich digest notification emails Render plain-text digests with source, target, per-Webmention detail links, and a filtered dashboard link while preserving singular and plural wording. Freeze the notified rows before sending so mentions arriving during delivery are left for the next digest. BREAKING CHANGE: enabling `DIGEST_EMAIL` now requires `SERVER_NAME` so background workers can generate absolute administration URLs. --- tests/tasks/test_notifications.py | 169 +++++++++++++++++++++++++++++++++++++- 1 file changed, 167 insertions(+), 2 deletions(-) (limited to 'tests/tasks/test_notifications.py') diff --git a/tests/tasks/test_notifications.py b/tests/tasks/test_notifications.py index dadec3e..1b1c1f1 100644 --- a/tests/tasks/test_notifications.py +++ b/tests/tasks/test_notifications.py @@ -18,6 +18,10 @@ from webmentions_ssg.models.received import ReceivedWebmention @pytest.fixture def notifications(app: Flask) -> ModuleType: + # Digest URLs are generated without a request context. Keep the public + # hostname local to these tests instead of changing all Flask test clients. + app.config.update(SERVER_NAME="mentions.example.com", PREFERRED_URL_SCHEME="https") + from webmentions_ssg.tasks import notifications return notifications @@ -56,8 +60,19 @@ def test_digest_sends_one_email_for_multiple_verified_mentions( message = send.call_args.args[0] assert message.recipients == ["owner@example.com"] - assert "2 new Webmentions" in message.body - assert "source.example" not in message.body + assert message.subject == "2 new verified webmentions on your site" + assert "2 new verified webmentions" in message.body + assert message.html is None + assert message.body.count("Mentioned by:") == 2 + for identifier in (first, second): + source_url = f"https://source.example/{identifier}" + assert source_url in message.body + detail_url = f"https://mentions.example.com/received/{identifier}" + assert f"View details: {detail_url}" in message.body + assert message.body.count("https://dennisfink.me/post") == 2 + assert str(unverified) not in message.body + dashboard_url = "https://mentions.example.com/received?status=verified" + assert dashboard_url in message.body first_mention = db.session.get(ReceivedWebmention, first) second_mention = db.session.get(ReceivedWebmention, second) @@ -72,6 +87,156 @@ def test_digest_sends_one_email_for_multiple_verified_mentions( assert unverified_mention.notification_sent_at is None +def test_digest_uses_singular_wording_for_one_verified_mention( + notifications: ModuleType, app: Flask +) -> None: + app.config["DIGEST_EMAIL"] = "owner@example.com" + identifier = create_mention(app) + + with app.app_context(): + with patch("webmentions_ssg.tasks.notifications.mail.send") as send: + notifications.email_verified_digest() + + message = send.call_args.args[0] + assert message.subject == "1 new verified webmention on your site" + assert "1 new verified webmention!" in message.body + assert message.html is None + assert f"https://source.example/{identifier}" in message.body + assert f"https://mentions.example.com/received/{identifier}" in message.body + + +def test_digest_links_handle_admin_url_path_prefix( + notifications: ModuleType, app: Flask +) -> None: + app.config["DIGEST_EMAIL"] = "owner@example.com" + app.config.update(APPLICATION_ROOT="/manage") + identifier = create_mention(app) + + with app.app_context(): + with patch("webmentions_ssg.tasks.notifications.mail.send") as send: + notifications.email_verified_digest() + + message = send.call_args.args[0] + detail_url = f"https://mentions.example.com/manage/received/{identifier}" + dashboard_url = "https://mentions.example.com/manage/received?status=verified" + assert detail_url in message.body + assert message.html is None + assert dashboard_url in message.body + + +def test_digest_uses_server_name_with_port( + notifications: ModuleType, app: Flask +) -> None: + app.config.update( + DIGEST_EMAIL="owner@example.com", + SERVER_NAME="mentions.example.com:8443", + PREFERRED_URL_SCHEME="https", + ) + identifier = create_mention(app) + + with app.app_context(): + with patch("webmentions_ssg.tasks.notifications.mail.send") as send: + notifications.email_verified_digest() + + message = send.call_args.args[0] + detail_url = f"https://mentions.example.com:8443/received/{identifier}" + dashboard_url = "https://mentions.example.com:8443/received?status=verified" + assert f"View details: {detail_url}" in message.body + assert message.html is None + assert dashboard_url in message.body + + +def test_digest_server_name_respects_application_root( + notifications: ModuleType, app: Flask +) -> None: + app.config.update( + DIGEST_EMAIL="owner@example.com", + SERVER_NAME="mentions.example.com", + PREFERRED_URL_SCHEME="https", + APPLICATION_ROOT="/manage", + ) + identifier = create_mention(app) + + with app.app_context(): + with patch("webmentions_ssg.tasks.notifications.mail.send") as send: + notifications.email_verified_digest() + + message = send.call_args.args[0] + detail_url = f"https://mentions.example.com/manage/received/{identifier}" + dashboard_url = "https://mentions.example.com/manage/received?status=verified" + assert detail_url in message.body + assert message.html is None + assert dashboard_url in message.body + + +def test_digest_without_server_name_does_not_send_or_mark_mentions( + notifications: ModuleType, app: Flask +) -> None: + app.config.update(DIGEST_EMAIL="owner@example.com", SERVER_NAME=None) + identifier = create_mention(app) + + with app.app_context(): + with ( + patch("webmentions_ssg.tasks.notifications.mail.send") as send, + pytest.raises(RuntimeError, match="SERVER_NAME"), + ): + notifications.email_verified_digest() + + send.assert_not_called() + mention = db.session.get(ReceivedWebmention, identifier) + assert mention is not None and mention.notification_sent_at is None + + +def test_digest_preserves_urls_in_plain_text_without_html( + notifications: ModuleType, app: Flask +) -> None: + app.config["DIGEST_EMAIL"] = "owner@example.com" + identifier = create_mention(app) + source = 'https://source.example/post?tag="onmouseover="alert(1)&x=