aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/tasks/test_notifications.py154
-rw-r--r--tests/tasks/test_receiver.py44
-rw-r--r--tests/test_views.py15
3 files changed, 206 insertions, 7 deletions
diff --git a/tests/tasks/test_notifications.py b/tests/tasks/test_notifications.py
new file mode 100644
index 0000000..6069d79
--- /dev/null
+++ b/tests/tasks/test_notifications.py
@@ -0,0 +1,154 @@
+# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+import uuid
+from unittest.mock import patch
+
+import pytest
+from flask import Flask
+
+from webmentions_ssg import DATABASE as db
+from webmentions_ssg.models import ReceivedWebmention
+
+
+def test_sends_email_for_verified_webmention(app: Flask) -> None:
+ from webmentions_ssg.tasks.notifications import send_webmention_notification
+
+ app.config["WEBMENTIONS_SSG_NOTIFICATION_EMAIL"] = "owner@example.com"
+ identifier = uuid.uuid7()
+
+ with app.app_context():
+ db.session.add(
+ ReceivedWebmention(
+ uuid=identifier,
+ source="https://source.example.com/post",
+ target="https://dennisfink.me/post",
+ status="verified",
+ )
+ )
+ db.session.commit()
+
+ with patch("webmentions_ssg.tasks.notifications.mail.send") as send:
+ send_webmention_notification.call_local(identifier)
+
+ send.assert_called_once()
+ message = send.call_args.args[0]
+ assert message.recipients == ["owner@example.com"]
+ assert "https://source.example.com/post" in message.body
+ assert "https://dennisfink.me/post" in message.body
+
+
+@pytest.mark.parametrize("status", ["received", "verifying", "failed", "deleted"])
+def test_skips_unverified_webmentions(app: Flask, status: str) -> None:
+ from webmentions_ssg.tasks.notifications import send_webmention_notification
+
+ app.config["WEBMENTIONS_SSG_NOTIFICATION_EMAIL"] = "owner@example.com"
+ identifier = uuid.uuid7()
+
+ with app.app_context():
+ db.session.add(
+ ReceivedWebmention(
+ uuid=identifier,
+ source="https://source.example.com/post",
+ target="https://dennisfink.me/post",
+ status=status,
+ )
+ )
+ db.session.commit()
+
+ with patch("webmentions_ssg.tasks.notifications.mail.send") as send:
+ send_webmention_notification.call_local(identifier)
+
+ send.assert_not_called()
+
+
+def test_skips_when_notifications_disabled(app: Flask) -> None:
+ from webmentions_ssg.tasks.notifications import send_webmention_notification
+
+ app.config["WEBMENTIONS_SSG_NOTIFICATION_EMAIL"] = None
+ identifier = uuid.uuid7()
+
+ with app.app_context():
+ db.session.add(
+ ReceivedWebmention(
+ uuid=identifier,
+ source="https://source.example.com/post",
+ target="https://dennisfink.me/post",
+ status="verified",
+ )
+ )
+ db.session.commit()
+
+ with patch("webmentions_ssg.tasks.notifications.mail.send") as send:
+ send_webmention_notification.call_local(identifier)
+
+ send.assert_not_called()
+
+
+def test_skips_unknown_webmention(app: Flask) -> None:
+ from webmentions_ssg.tasks.notifications import send_webmention_notification
+
+ app.config["WEBMENTIONS_SSG_NOTIFICATION_EMAIL"] = "owner@example.com"
+
+ with app.app_context():
+ with patch("webmentions_ssg.tasks.notifications.mail.send") as send:
+ send_webmention_notification.call_local(uuid.uuid7())
+
+ send.assert_not_called()
+
+
+@pytest.mark.parametrize(
+ ("send_notification", "recipient", "should_enqueue"),
+ [
+ pytest.param(None, "owner@example.com", True, id="default-enabled"),
+ pytest.param(True, "owner@example.com", True, id="explicitly-enabled"),
+ pytest.param(False, "owner@example.com", False, id="explicitly-disabled"),
+ pytest.param(True, None, True, id="no-recipient-configured"),
+ ],
+)
+def test_queues_notification_after_verification(
+ app: Flask,
+ send_notification: bool | None,
+ recipient: str | None,
+ should_enqueue: bool,
+) -> None:
+ from webmentions_ssg.tasks import receiver
+
+ app.config["WEBMENTIONS_SSG_NOTIFICATION_EMAIL"] = recipient
+ identifier = uuid.uuid7()
+
+ with app.app_context():
+ db.session.add(
+ ReceivedWebmention(
+ uuid=identifier,
+ source="https://source.example.com/post",
+ target="https://dennisfink.me/post",
+ status="received",
+ )
+ )
+ db.session.commit()
+
+ with (
+ patch(
+ "webmentions_ssg.tasks.receiver.source_mentions_target", return_value=True
+ ),
+ patch("webmentions_ssg.tasks.receiver.send_webmention_notification") as enqueue,
+ ):
+ if send_notification is None:
+ # Exercise the task's default argument.
+ receiver.verify_webmention.call_local(identifier)
+ else:
+ receiver.verify_webmention.call_local(
+ identifier, send_notification=send_notification
+ )
+
+ with app.app_context():
+ webmention = db.session.get(ReceivedWebmention, identifier)
+ assert webmention is not None
+ assert webmention.status == "verified"
+
+ if should_enqueue:
+ enqueue.assert_called_once_with(identifier)
+ else:
+ enqueue.assert_not_called()
diff --git a/tests/tasks/test_receiver.py b/tests/tasks/test_receiver.py
index daa271d..15289ac 100644
--- a/tests/tasks/test_receiver.py
+++ b/tests/tasks/test_receiver.py
@@ -471,10 +471,17 @@ def test_verify_webmention_persists_final_state(
case _:
raise AssertionError(f"Unexpected outcome: {outcome}")
- receiver.verify_webmention.call_local(identifier)
+ app.config["WEBMENTIONS_SSG_NOTIFICATION_EMAIL"] = "owner@example.com"
+
+ with patch.object(receiver, "send_webmention_notification") as enqueue:
+ receiver.verify_webmention.call_local(identifier)
assert get_webmention_state(app, identifier) == (expected_status, expected_reason)
+ # The receiver always enqueues when requested; the notification task
+ # checks the persisted status before deciding whether to send an email.
+ enqueue.assert_called_once_with(identifier)
+
@patch("webmentions_ssg.tasks.receiver.source_mentions_target")
@pytest.mark.parametrize("failure", ["temporary-http", "network"])
@@ -495,10 +502,16 @@ def test_verify_webmention_persists_retryable_failure_and_reraises(
source_mentions_target.side_effect = exception
- with pytest.raises(type(exception), match=str(exception)):
+ app.config["WEBMENTIONS_SSG_NOTIFICATION_EMAIL"] = "owner@example.com"
+
+ with (
+ patch.object(receiver, "send_webmention_notification") as enqueue,
+ pytest.raises(type(exception), match=str(exception)),
+ ):
receiver.verify_webmention.call_local(identifier)
assert get_webmention_state(app, identifier) == ("failed", str(exception))
+ enqueue.assert_not_called()
def test_verify_webmention_ignores_unknown_identifier(
@@ -610,3 +623,30 @@ def test_source_mentions_target_falls_back_for_unknown_encoding(
assert receiver.source_mentions_target(SOURCE_URL, TARGET_URL)
fetch_source.assert_called_once_with(SOURCE_URL)
+
+
+@patch("webmentions_ssg.tasks.receiver.source_mentions_target", return_value=True)
+def test_verify_webmention_keeps_verified_state_when_notification_enqueue_fails(
+ source_mentions_target: Mock,
+ app: Flask,
+ receiver: ModuleType,
+ caplog: pytest.LogCaptureFixture,
+) -> None:
+ identifier = create_webmention(app)
+
+ with (
+ patch.object(
+ receiver,
+ "send_webmention_notification",
+ side_effect=RuntimeError("Huey unavailable"),
+ ) as enqueue,
+ caplog.at_level(logging.ERROR),
+ ):
+ # Enqueueing fails, but verification must return without raising.
+ receiver.verify_webmention.call_local(identifier)
+
+ source_mentions_target.assert_called_once_with(SOURCE_URL, TARGET_URL)
+ enqueue.assert_called_once_with(identifier)
+ assert get_webmention_state(app, identifier) == ("verified", None)
+ assert f"Could not enqueue webmention notification for {identifier}" in caplog.text
+ assert "RuntimeError: Huey unavailable" in caplog.text
diff --git a/tests/test_views.py b/tests/test_views.py
index 040e005..51b94b9 100644
--- a/tests/test_views.py
+++ b/tests/test_views.py
@@ -155,7 +155,7 @@ def test_endpoint_creates_webmention(
identifier = webmention.uuid
assert response.headers["Location"].endswith(str(identifier))
- verify_webmention.assert_called_once_with(identifier)
+ verify_webmention.assert_called_once_with(identifier, send_notification=True)
def test_endpoint_is_idempotent(
@@ -189,7 +189,10 @@ def test_endpoint_is_idempotent(
identifier = webmention.uuid
- assert verify_webmention.call_args_list == [call(identifier), call(identifier)]
+ assert verify_webmention.call_args_list == [
+ call(identifier, send_notification=True),
+ call(identifier, send_notification=False),
+ ]
def test_resending_resets_failure_state(
@@ -227,7 +230,7 @@ def test_resending_resets_failure_state(
assert webmention.status == "received"
assert webmention.failure_reason is None
- verify_webmention.assert_called_once_with(identifier)
+ verify_webmention.assert_called_once_with(identifier, send_notification=False)
@pytest.mark.parametrize("path", ["/sent", f"/sent/{uuid.uuid7()}"])
@@ -571,7 +574,7 @@ def test_reverify_received_webmention(
assert webmention.status == "received"
assert webmention.failure_reason is None
- verify_webmention.assert_called_once_with(identifier)
+ verify_webmention.assert_called_once_with(identifier, send_notification=False)
def test_rescan_sent_sources_requires_login(client: FlaskClient) -> None:
@@ -766,4 +769,6 @@ def test_endpoint_recovers_from_concurrent_insert(
assert competing_webmention.status == "received"
assert competing_webmention.failure_reason is None
- verify_webmention.assert_called_once_with(competing_identifier)
+ verify_webmention.assert_called_once_with(
+ competing_identifier, send_notification=False
+ )