1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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()
|