# SPDX-FileCopyrightText: 2026 Dennis Fink # # SPDX-License-Identifier: BSD-3-Clause import uuid from types import ModuleType from unittest.mock import patch import pytest from flask import Flask from huey.exceptions import TaskLockedException from webmentions_ssg import db from webmentions_ssg.extensions import huey 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 def create_mention(app: Flask, status: str = "verified") -> uuid.UUID: identifier = uuid.uuid7() with app.app_context(): db.session.add( ReceivedWebmention( uuid=identifier, source=f"https://source.example/{identifier}", target="https://dennisfink.me/post", status=status, ) ) db.session.commit() return identifier def test_digest_sends_one_email_for_multiple_verified_mentions( notifications: ModuleType, app: Flask ) -> None: app.config["DIGEST_EMAIL"] = "owner@example.com" first = create_mention(app) second = create_mention(app) unverified = create_mention(app, "failed") with app.app_context(): with patch("webmentions_ssg.tasks.notifications.mail.send") as send: notifications.email_verified_digest() notifications.email_verified_digest() send.assert_called_once() message = send.call_args.args[0] assert message.recipients == ["owner@example.com"] 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) unverified_mention = db.session.get(ReceivedWebmention, unverified) assert first_mention is not None assert second_mention is not None assert unverified_mention is not None assert first_mention.notification_sent_at is not None assert second_mention.notification_sent_at is not None 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=