diff options
| author | Dennis Fink | 2026-11-22 11:11:23 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-22 11:11:23 +0100 |
| commit | 779d10051556242ba1e70b70f6c6e8f1158e861d (patch) | |
| tree | 284d8228491f0a7ee9422a2100149adebad5aaa7 /tests/tasks | |
| parent | ef90dc381322122ecfb9af039cf790de92807514 (diff) | |
| download | webmentions-ssg-main.tar.gz webmentions-ssg-main.zip | |
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.
Diffstat (limited to 'tests/tasks')
| -rw-r--r-- | tests/tasks/test_notifications.py | 169 |
1 files changed, 167 insertions, 2 deletions
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=<script>' + target = 'https://dennisfink.me/post?tag=<strong>&other="test"' + + with app.app_context(): + mention = db.session.get(ReceivedWebmention, identifier) + assert mention is not None + mention.source = source + mention.target = target + db.session.commit() + + with patch("webmentions_ssg.tasks.notifications.mail.send") as send: + notifications.email_verified_digest() + + message = send.call_args.args[0] + assert source in message.body + assert target in message.body + assert message.html is None + + +def test_digest_only_marks_mentions_in_the_sent_message( + notifications: ModuleType, app: Flask +) -> None: + app.config["DIGEST_EMAIL"] = "owner@example.com" + first = create_mention(app) + new_arrival: list[uuid.UUID] = [] + + def receive_during_send(message) -> None: + assert str(first) in message.body + new_arrival.append(create_mention(app)) + + with app.app_context(): + with patch( + "webmentions_ssg.tasks.notifications.mail.send", + side_effect=receive_during_send, + ): + notifications.email_verified_digest() + + first_mention = db.session.get(ReceivedWebmention, first) + later_mention = db.session.get(ReceivedWebmention, new_arrival[0]) + assert first_mention is not None + assert later_mention is not None + assert first_mention.notification_sent_at is not None + assert later_mention.notification_sent_at is None + + def test_digest_skips_when_notifications_disabled( notifications: ModuleType, app: Flask ) -> None: |
