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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
|
import logging
import uuid
from datetime import datetime, timezone
from types import ModuleType
from unittest.mock import MagicMock, Mock
import httpx
import pytest
from flask import Flask
from webmentions_ssg import DATABASE as db
from webmentions_ssg.models import SentWebmention, SentWebmentionStatus, Source
SOURCE_URL = "https://dennisfink.me/blog/example/"
TARGET_URL = "https://example.com/post"
ENDPOINT_URL = "https://example.com/webmention"
STATUS_URL = "https://example.com/webmention/status/123"
def create_sent_webmention(
app: Flask,
*,
active: bool = True,
desired_revision: int = 1,
processed_revision: int | None = None,
sent_revision: int | None = None,
status: SentWebmentionStatus | None = None,
failure_reason: str | None = None,
endpoint: str | None = None,
response_status: int | None = None,
status_url: str | None = None,
) -> uuid.UUID:
now = datetime.now(timezone.utc)
with app.app_context():
source = Source(
path="example/index.html",
url=SOURCE_URL,
content_hash="0" * 32,
revision=desired_revision,
last_seen_at=now,
revised_at=now,
)
webmention = SentWebmention(
target=TARGET_URL,
active=active,
desired_revision=desired_revision,
processed_revision=processed_revision,
sent_revision=sent_revision,
status=status,
failure_reason=failure_reason,
endpoint=endpoint,
response_status=response_status,
status_url=status_url,
)
source.sent_webmentions.append(webmention)
db.session.add(source)
db.session.commit()
return webmention.uuid
def mock_sender_requests(
sender_module: ModuleType,
monkeypatch: pytest.MonkeyPatch,
*,
endpoint: str | None = ENDPOINT_URL,
result: tuple[int, str | None] = (202, None),
) -> tuple[Mock, Mock, Mock, Mock]:
httpx_client = MagicMock()
client = httpx_client.return_value.__enter__.return_value
discover = Mock(return_value=endpoint)
post = Mock(return_value=result)
monkeypatch.setattr(sender_module.httpx, "Client", httpx_client)
monkeypatch.setattr(sender_module, "discover_webmention_endpoint", discover)
monkeypatch.setattr(sender_module, "post_webmention", post)
return httpx_client, client, discover, post
@pytest.fixture
def sender_module(app: Flask) -> ModuleType:
# Importing sender registers Huey tasks. The dependency on the
# app fixture guarantees that Huey has been initialized first.
_ = app
from webmentions_ssg.tasks import sender
return sender
@pytest.mark.parametrize(
("value", "expected"),
(
(
'<https://example.com/webmention>; rel="webmention"',
("https://example.com/webmention", {"webmention"}),
),
(
"<https://example.com/webmention>; rel=webmention",
("https://example.com/webmention", {"webmention"}),
),
(
'<https://example.com/webmention>; rel="webmention alternate"',
("https://example.com/webmention", {"webmention", "alternate"}),
),
(
'<https://example.com/webmention>; REL="WebMention Alternate"',
("https://example.com/webmention", {"webmention", "alternate"}),
),
(
"<https://example.com/webmention>; "
'type="text/html"; rel="webmention"; title="Endpoint"',
("https://example.com/webmention", {"webmention"}),
),
(
'<https://example.com/stylesheet>; type="text/css"',
("https://example.com/stylesheet", set()),
),
),
)
def test_parse_link_value(
sender_module: ModuleType, value: str, expected: tuple[str, set[str]]
) -> None:
assert sender_module.parse_link_value(value) == expected
@pytest.mark.parametrize(
"value", ("", "https://example.com/webmention", "<https://example.com/webmention")
)
def test_parse_link_value_rejects_malformed_value(
sender_module: ModuleType, value: str
) -> None:
assert sender_module.parse_link_value(value) is None
@pytest.mark.parametrize(
("target", "headers", "html", "expected_endpoint"),
(
pytest.param(
"https://example.com/test/1",
(("Link", "</test/1/webmention?head=true>; rel=webmention"),),
"",
"https://example.com/test/1/webmention?head=true",
id="1-http-link-unquoted-rel-relative-url",
),
pytest.param(
"https://example.com/test/2",
(
(
"Link",
"<https://example.com/test/2/webmention?head=true>; rel=webmention",
),
),
"",
"https://example.com/test/2/webmention?head=true",
id="2-http-link-unquoted-rel-absolute-url",
),
pytest.param(
"https://example.com/test/3",
(),
"""
<link
rel="webmention"
href="/test/3/webmention"
>
""",
"https://example.com/test/3/webmention",
id="3-html-link-relative-url",
),
pytest.param(
"https://example.com/test/4",
(),
"""
<link
rel="webmention"
href="https://example.com/test/4/webmention"
>
""",
"https://example.com/test/4/webmention",
id="4-html-link-absolute-url",
),
pytest.param(
"https://example.com/test/5",
(),
"""
<a
rel="webmention"
href="/test/5/webmention"
>
Endpoint
</a>
""",
"https://example.com/test/5/webmention",
id="5-html-a-relative-url",
),
pytest.param(
"https://example.com/test/6",
(),
"""
<a
rel="webmention"
href="https://example.com/test/6/webmention"
>
Endpoint
</a>
""",
"https://example.com/test/6/webmention",
id="6-html-a-absolute-url",
),
pytest.param(
"https://example.com/test/7",
(
(
"LinK",
"<https://example.com/test/7/webmention?head=true>; rel=webmention",
),
),
"",
"https://example.com/test/7/webmention?head=true",
id="7-http-link-strange-casing",
),
pytest.param(
"https://example.com/test/8",
(
(
"Link",
'<https://example.com/test/8/webmention?head=true>; rel="webmention"',
),
),
"",
"https://example.com/test/8/webmention?head=true",
id="8-http-link-quoted-rel",
),
pytest.param(
"https://example.com/test/9",
(),
"""
<link
rel="webmention somethingelse"
href="https://example.com/test/9/webmention"
>
""",
"https://example.com/test/9/webmention",
id="9-multiple-html-rel-values",
),
pytest.param(
"https://example.com/test/10",
(
(
"Link",
"<https://example.com/test/10/webmention?head=true>; "
'rel="webmention somethingelse"',
),
),
"",
"https://example.com/test/10/webmention?head=true",
id="10-multiple-link-header-rel-values",
),
pytest.param(
"https://example.com/test/11",
(("Link", '</test/11/webmention>; rel="webmention"'),),
"""
<link
rel="webmention"
href="/test/11/webmention/error"
>
<a
rel="webmention"
href="/test/11/webmention/error"
>
Wrong endpoint
</a>
""",
"https://example.com/test/11/webmention",
id="11-http-link-precedence",
),
pytest.param(
"https://example.com/test/12",
(),
"""
<link
rel="not-webmention"
href="/test/12/webmention/error"
>
<a
rel="webmention"
href="/test/12/webmention"
>
Correct endpoint
</a>
""",
"https://example.com/test/12/webmention",
id="12-exact-rel-match",
),
pytest.param(
"https://example.com/test/13",
(),
"""
<!--
<a
rel="webmention"
href="/test/13/webmention/error"
>
False endpoint
</a>
-->
<a
rel="webmention"
href="/test/13/webmention"
>
Correct endpoint
</a>
""",
"https://example.com/test/13/webmention",
id="13-endpoint-inside-html-comment",
),
pytest.param(
"https://example.com/test/14",
(),
"""
<code>
<a
rel="webmention"
href="/test/14/webmention/error"
>
False endpoint
</a>
</code>
<a
rel="webmention"
href="/test/14/webmention"
>
Correct endpoint
</a>
""",
"https://example.com/test/14/webmention",
id="14-endpoint-in-escaped-html",
),
pytest.param(
"https://example.com/test/15",
(),
"""
<link
rel="webmention"
href=""
>
""",
"https://example.com/test/15",
id="15-empty-href",
),
pytest.param(
"https://example.com/test/16",
(),
"""
<a
rel="webmention"
href="/test/16/webmention"
>
First endpoint
</a>
<link
rel="webmention"
href="/test/16/webmention/error"
>
""",
"https://example.com/test/16/webmention",
id="16-a-before-link",
),
pytest.param(
"https://example.com/test/17",
(),
"""
<link
rel="webmention"
href="/test/17/webmention"
>
<a
rel="webmention"
href="/test/17/webmention/error"
>
Second endpoint
</a>
""",
"https://example.com/test/17/webmention",
id="17-link-before-a",
),
pytest.param(
"https://example.com/test/18",
(
("Link", '<https://example.com/test/18/webmention/error>; rel="other"'),
(
"Link",
"<https://example.com/test/18/webmention?head=true>; "
'rel="webmention"',
),
),
"",
"https://example.com/test/18/webmention?head=true",
id="18-multiple-http-link-headers",
),
pytest.param(
"https://example.com/test/19",
(
(
"Link",
"<https://example.com/test/19/webmention/error>; "
'rel="other", '
"<https://example.com/test/19/webmention?head=true>; "
'rel="webmention"',
),
),
"",
"https://example.com/test/19/webmention?head=true",
id="19-single-header-multiple-values",
),
pytest.param(
"https://example.com/test/20",
(),
"""
<link rel="webmention">
<a
rel="webmention"
href="/test/20/webmention"
>
Correct endpoint
</a>
""",
"https://example.com/test/20/webmention",
id="20-link-without-href",
),
pytest.param(
"https://example.com/test/21",
(),
"""
<link
rel="webmention"
href="/test/21/webmention?query=yes"
>
""",
"https://example.com/test/21/webmention?query=yes",
id="21-endpoint-query-string",
),
pytest.param(
"https://example.com/test/22",
(),
"""
<link
rel="webmention"
href="22/webmention"
>
""",
"https://example.com/test/22/webmention",
id="22-endpoint-relative-to-path",
),
),
)
def test_discovery(
app: Flask,
sender_module: ModuleType,
target: str,
headers: tuple[tuple[str, str], ...],
html: str,
expected_endpoint: str,
) -> None:
def handler(request: httpx.Request) -> httpx.Response:
if request.method == "HEAD":
return httpx.Response(200, headers=headers)
return httpx.Response(200, headers={"Content-Type": "text/html"}, text=html)
with (
app.app_context(),
httpx.Client(
transport=httpx.MockTransport(handler), follow_redirects=True
) as client,
):
endpoint = sender_module.discover_webmention_endpoint(client, target)
assert endpoint == expected_endpoint
@pytest.mark.parametrize(
("target", "responses", "expected_endpoint"),
(
pytest.param(
"https://example.com/test/23/page",
{
"/test/23/page": (302, {"Location": "page/redirect-key"}),
"/test/23/page/redirect-key": (
200,
{"Link": ("<webmention-endpoint/endpoint-key>; rel=webmention")},
),
},
("https://example.com/test/23/page/webmention-endpoint/endpoint-key"),
id="relative-endpoint-after-redirect",
),
),
)
def test_discovery_redirect(
app: Flask,
sender_module: ModuleType,
target: str,
responses: dict[str, tuple[int, dict[str, str]]],
expected_endpoint: str,
) -> None:
def handler(request: httpx.Request) -> httpx.Response:
try:
status, headers = responses[request.url.path]
except KeyError:
raise AssertionError(f"Unexpected request: {request.url}") from None
return httpx.Response(status, headers=headers)
with (
app.app_context(),
httpx.Client(
transport=httpx.MockTransport(handler), follow_redirects=True
) as client,
):
endpoint = sender_module.discover_webmention_endpoint(client, target)
assert endpoint == expected_endpoint
def test_discovery_stops_after_head(app: Flask, sender_module: ModuleType) -> None:
requests: list[httpx.Request] = []
def handler(request: httpx.Request) -> httpx.Response:
requests.append(request)
return httpx.Response(200, headers={"Link": "</webmention>; rel=webmention"})
with (
app.app_context(),
httpx.Client(
transport=httpx.MockTransport(handler), follow_redirects=True
) as client,
):
endpoint = sender_module.discover_webmention_endpoint(
client, "https://example.com/post"
)
assert endpoint == "https://example.com/webmention"
assert [request.method for request in requests] == ["HEAD"]
def test_discovery_falls_back_to_get(app: Flask, sender_module: ModuleType) -> None:
requests: list[httpx.Request] = []
def handler(request: httpx.Request) -> httpx.Response:
requests.append(request)
if request.method == "HEAD":
return httpx.Response(200)
return httpx.Response(
200,
headers={"Content-Type": "text/html"},
text="""
<link
rel="webmention"
href="/webmention"
>
""",
)
with (
app.app_context(),
httpx.Client(
transport=httpx.MockTransport(handler), follow_redirects=True
) as client,
):
endpoint = sender_module.discover_webmention_endpoint(
client, "https://example.com/post"
)
assert endpoint == "https://example.com/webmention"
assert [request.method for request in requests] == ["HEAD", "GET"]
def test_send_webmention_persists_success(
app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch
) -> None:
identifier = create_sent_webmention(app)
_, client, discover, post = mock_sender_requests(
sender_module, monkeypatch, result=(201, STATUS_URL)
)
sender_module.send_webmention.call_local(identifier)
discover.assert_called_once_with(client, TARGET_URL)
post.assert_called_once_with(
client, endpoint=ENDPOINT_URL, source=SOURCE_URL, target=TARGET_URL
)
with app.app_context():
webmention = db.session.get(SentWebmention, identifier)
assert webmention is not None
assert webmention.processed_revision == 1
assert webmention.sent_revision == 1
assert webmention.status == SentWebmentionStatus.SENT
assert webmention.failure_reason is None
assert webmention.endpoint == ENDPOINT_URL
assert webmention.response_status == 201
assert webmention.status_url == STATUS_URL
assert webmention.last_attempted_at is not None
assert webmention.last_sent_at is not None
assert not webmention.pending
def test_send_webmention_marks_unsupported_target_processed(
app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch
) -> None:
identifier = create_sent_webmention(
app,
desired_revision=2,
processed_revision=1,
sent_revision=1,
status=SentWebmentionStatus.SENT,
endpoint="https://old.example/webmention",
response_status=201,
status_url="https://old.example/status/123",
)
_, client, discover, post = mock_sender_requests(
sender_module, monkeypatch, endpoint=None
)
sender_module.send_webmention.call_local(identifier)
discover.assert_called_once_with(client, TARGET_URL)
post.assert_not_called()
with app.app_context():
webmention = db.session.get(SentWebmention, identifier)
assert webmention is not None
assert webmention.processed_revision == 2
assert webmention.sent_revision == 1
assert webmention.status == SentWebmentionStatus.UNSUPPORTED
assert webmention.failure_reason == "No Webmention endpoint discovered"
assert webmention.endpoint is None
assert webmention.response_status is None
assert webmention.status_url is None
assert webmention.last_attempted_at is not None
assert not webmention.pending
def test_send_webmention_persists_permanent_failure(
app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch
) -> None:
identifier = create_sent_webmention(
app,
desired_revision=2,
processed_revision=1,
sent_revision=1,
status=SentWebmentionStatus.SENT,
status_url="https://old.example/status/123",
)
mock_sender_requests(sender_module, monkeypatch, result=(400, None))
sender_module.send_webmention.call_local(identifier)
with app.app_context():
webmention = db.session.get(SentWebmention, identifier)
assert webmention is not None
assert webmention.processed_revision == 2
assert webmention.sent_revision == 1
assert webmention.status == SentWebmentionStatus.FAILED
assert webmention.failure_reason == "Webmention endpoint returned HTTP 400"
assert webmention.endpoint == ENDPOINT_URL
assert webmention.response_status == 400
assert webmention.status_url is None
assert not webmention.pending
def test_send_webmention_persists_temporary_failure_and_reraises(
app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch
) -> None:
identifier = create_sent_webmention(
app,
desired_revision=2,
processed_revision=1,
sent_revision=1,
status=SentWebmentionStatus.SENT,
status_url="https://old.example/status/123",
)
mock_sender_requests(sender_module, monkeypatch, result=(503, None))
with pytest.raises(
sender_module.TemporarySenderError,
match="Webmention endpoint returned HTTP 503",
):
sender_module.send_webmention.call_local(identifier)
with app.app_context():
webmention = db.session.get(SentWebmention, identifier)
assert webmention is not None
assert webmention.processed_revision == 1
assert webmention.sent_revision == 1
assert webmention.status == SentWebmentionStatus.FAILED
assert webmention.failure_reason == "Webmention endpoint returned HTTP 503"
assert webmention.endpoint == ENDPOINT_URL
assert webmention.response_status == 503
assert webmention.status_url is None
assert webmention.pending
def test_send_webmention_ignores_processed_revision(
app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch
) -> None:
identifier = create_sent_webmention(
app,
desired_revision=2,
processed_revision=2,
sent_revision=2,
status=SentWebmentionStatus.SENT,
)
httpx_client, _, discover, post = mock_sender_requests(sender_module, monkeypatch)
sender_module.send_webmention.call_local(identifier)
httpx_client.assert_not_called()
discover.assert_not_called()
post.assert_not_called()
def test_send_webmention_ignores_result_for_superseded_revision(
app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch
) -> None:
identifier = create_sent_webmention(app)
_, _, _, post = mock_sender_requests(sender_module, monkeypatch)
def supersede_revision(*args, **kwargs) -> tuple[int, None]:
webmention = db.session.get(SentWebmention, identifier)
assert webmention is not None
webmention.desired_revision = 2
db.session.commit()
return (202, None)
post.side_effect = supersede_revision
sender_module.send_webmention.call_local(identifier)
with app.app_context():
webmention = db.session.get(SentWebmention, identifier)
assert webmention is not None
assert webmention.desired_revision == 2
assert webmention.processed_revision is None
assert webmention.sent_revision is None
assert webmention.status is None
assert webmention.pending
def test_send_webmention_ignores_unknown_identifier(
sender_module: ModuleType, caplog: pytest.LogCaptureFixture
) -> None:
identifier = uuid.uuid7()
with caplog.at_level(logging.WARNING):
sender_module.send_webmention.call_local(identifier)
assert f"Cannot send unknown SentWebmention {identifier}" in caplog.text
|