aboutsummaryrefslogtreecommitdiff
path: root/tests/tasks/test_receiver.py
diff options
context:
space:
mode:
authorDennis Fink2026-10-25 20:47:14 +0100
committerDennis Fink2026-10-25 20:47:14 +0100
commita231913ab273f427697f2d27d615b495f6e160bb (patch)
treeb9d84e3aab3555ec88c648f790f9c31d8a4266a6 /tests/tasks/test_receiver.py
parentae25061b14f8ea713ec7695848421edf84e4f0fc (diff)
downloadwebmentions-ssg-a231913ab273f427697f2d27d615b495f6e160bb.tar.gz
webmentions-ssg-a231913ab273f427697f2d27d615b495f6e160bb.zip
feat(receiver): add email notifications for new webmentions
Send an email after a newly received Webmention is successfully verified, with SMTP delivery configurable through Flask-Mail. Avoid notifications for resubmissions and manual reverification, and keep the persisted verification result unchanged if notification enqueueing fails.
Diffstat (limited to 'tests/tasks/test_receiver.py')
-rw-r--r--tests/tasks/test_receiver.py44
1 files changed, 42 insertions, 2 deletions
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