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
|
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause
import logging
import uuid
from datetime import UTC, datetime, timedelta
from http import HTTPStatus
from types import ModuleType
from unittest.mock import Mock, patch
import httpx
import pytest
import sqlalchemy as sa
from flask import Flask
from webmentions_ssg import db
from webmentions_ssg.models.moderation import BlockedDomain
from webmentions_ssg.models.received import ReceivedWebmention
from webmentions_ssg.net import AddressResolutionError
SOURCE_URL = "https://source.example/article"
TARGET_URL = "https://dennisfink.me/blog/example/"
@pytest.fixture
def verification(app: Flask) -> ModuleType:
from webmentions_ssg.tasks import verification
return verification
def create_webmention(
app: Flask,
*,
source: str = SOURCE_URL,
target: str = TARGET_URL,
status: str = "received",
failure_reason: str | None = None,
recovery_attempts: int = 0,
) -> uuid.UUID:
identifier = uuid.uuid7()
with app.app_context():
webmention = ReceivedWebmention(
uuid=identifier,
source=source,
target=target,
status=status,
failure_reason=failure_reason,
recovery_attempts=recovery_attempts,
)
db.session.add(webmention)
db.session.commit()
return identifier
def get_webmention_state(app: Flask, identifier: uuid.UUID) -> tuple[str, str | None]:
with app.app_context():
webmention = db.session.get(ReceivedWebmention, identifier)
assert webmention is not None
return (webmention.status, webmention.failure_reason)
def set_stream_response(httpx_client: Mock, response: httpx.Response) -> Mock:
client = httpx_client.return_value.__enter__.return_value
client.stream.return_value.__enter__.return_value = response
return client
@patch("webmentions_ssg.tasks.verification.httpx.Client")
def test_fetch_source_document_returns_response_and_body(
httpx_client: Mock, app: Flask, verification: ModuleType
) -> None:
response = httpx.Response(
HTTPStatus.OK,
headers={"Content-Type": "text/html"},
content=b"<p>Document</p>",
request=httpx.Request("GET", SOURCE_URL),
)
client = set_stream_response(httpx_client, response)
with app.app_context():
fetched_response, body = verification.fetch_source_document(SOURCE_URL)
assert fetched_response is response
assert body == b"<p>Document</p>"
client.stream.assert_called_once_with("GET", SOURCE_URL)
options = httpx_client.call_args.kwargs
assert options["headers"] == {
"Accept": "text/html, application/xhtml+xml;q=0.9, text/plain;q=0.8",
"User-Agent": f"{verification.APP_NAME}/{verification.VERSION} ReceivedWebmention",
}
assert options["follow_redirects"] is True
assert options["max_redirects"] == app.config.get("MAX_REDIRECTS", 20)
assert options["trust_env"] is False
assert options["transport"].__class__.__name__ == "PublicHTTPTransport"
@patch("webmentions_ssg.tasks.verification.httpx.Client")
@pytest.mark.parametrize(
("status_code", "exception_name"),
[
(HTTPStatus.BAD_REQUEST, "VerificationError"),
(HTTPStatus.NOT_FOUND, "VerificationError"),
(HTTPStatus.GONE, "SourceGoneError"),
(HTTPStatus.REQUEST_TIMEOUT, "RetryableFetchError"),
(HTTPStatus.TOO_EARLY, "RetryableFetchError"),
(HTTPStatus.TOO_MANY_REQUESTS, "RetryableFetchError"),
(HTTPStatus.INTERNAL_SERVER_ERROR, "RetryableFetchError"),
(HTTPStatus.SERVICE_UNAVAILABLE, "RetryableFetchError"),
],
)
def test_fetch_source_document_maps_http_status_to_exception(
httpx_client: Mock,
app: Flask,
verification: ModuleType,
status_code: int,
exception_name: str,
) -> None:
set_stream_response(
httpx_client,
httpx.Response(status_code, request=httpx.Request("GET", SOURCE_URL)),
)
exception_type = getattr(verification, exception_name)
with app.app_context(), pytest.raises(exception_type, match=f"HTTP {status_code}"):
verification.fetch_source_document(SOURCE_URL)
@patch("webmentions_ssg.tasks.verification.httpx.Client")
def test_fetch_source_document_propagates_network_error(
httpx_client: Mock, app: Flask, verification: ModuleType
) -> None:
client = httpx_client.return_value.__enter__.return_value
client.stream.side_effect = httpx.ConnectError(
"Connection refused", request=httpx.Request("GET", SOURCE_URL)
)
with (
app.app_context(),
pytest.raises(httpx.ConnectError, match="Connection refused"),
):
verification.fetch_source_document(SOURCE_URL)
@patch("webmentions_ssg.tasks.verification.httpx.Client")
def test_fetch_source_document_rejects_declared_oversized_body(
httpx_client: Mock, app: Flask, verification: ModuleType
) -> None:
app.config["MAX_RECEIVED_SOURCE_BYTES"] = 10
set_stream_response(
httpx_client,
httpx.Response(
HTTPStatus.OK,
headers={"Content-Type": "text/html", "Content-Length": "11"},
content=b"x" * 11,
request=httpx.Request("GET", SOURCE_URL),
),
)
with (
app.app_context(),
pytest.raises(
verification.VerificationError, match="Source document is too large"
),
):
verification.fetch_source_document(SOURCE_URL)
@patch("webmentions_ssg.tasks.verification.httpx.Client")
def test_fetch_source_document_rejects_streamed_oversized_body(
httpx_client: Mock, app: Flask, verification: ModuleType
) -> None:
app.config["MAX_RECEIVED_SOURCE_BYTES"] = 10
class BodyStream(httpx.SyncByteStream):
def __iter__(self):
yield b"x" * 6
yield b"x" * 6
set_stream_response(
httpx_client,
httpx.Response(
HTTPStatus.OK,
headers={"Content-Type": "text/html"},
stream=BodyStream(),
request=httpx.Request("GET", SOURCE_URL),
),
)
with (
app.app_context(),
pytest.raises(
verification.VerificationError, match="Source document is too large"
),
):
verification.fetch_source_document(SOURCE_URL)
@patch("webmentions_ssg.tasks.verification.httpx.Client")
def test_fetch_source_document_does_not_mask_unexpected_value_error(
httpx_client: Mock,
app: Flask,
verification: ModuleType,
monkeypatch: pytest.MonkeyPatch,
) -> None:
set_stream_response(
httpx_client,
httpx.Response(HTTPStatus.OK, request=httpx.Request("GET", SOURCE_URL)),
)
monkeypatch.setattr(
verification,
"read_limited_body",
Mock(side_effect=ValueError("unexpected bug")),
)
with app.app_context(), pytest.raises(ValueError, match="unexpected bug"):
verification.fetch_source_document(SOURCE_URL)
@patch("webmentions_ssg.tasks.verification.fetch_source_document")
@pytest.mark.parametrize(
("content_type", "body", "expected"),
[
pytest.param(
"text/html; charset=utf-8",
f'<a href="{TARGET_URL}">Reply</a>'.encode(),
True,
id="html",
),
pytest.param(
"TEXT/HTML; CHARSET=UTF-8",
f'<a href="{TARGET_URL}">Reply</a>'.encode(),
True,
id="case-insensitive-html",
),
pytest.param(
"application/xhtml+xml",
b'<a href="https://example.com/">Other</a>',
False,
id="xhtml-without-target",
),
pytest.param(
"text/plain; charset=utf-8",
f"Reply to {TARGET_URL}".encode(),
True,
id="plain-text-utf-8",
),
pytest.param(
"text/plain; charset=utf-8",
f"See ({TARGET_URL}), thanks".encode(),
True,
id="plain-text-with-surrounding-punctuation",
),
pytest.param(
"text/plain; charset=iso-8859-1",
(f"Grüße. Reply to {TARGET_URL}").encode("iso-8859-1"),
True,
id="plain-text-declared-encoding",
),
pytest.param(
"text/plain; charset=utf-8",
b"\xff Reply to " + TARGET_URL.encode(),
True,
id="plain-text-invalid-byte",
),
pytest.param(
"text/plain", b"No target here.", False, id="plain-text-without-target"
),
],
)
def test_source_mentions_target_by_media_type(
fetch_source_document: Mock,
verification: ModuleType,
content_type: str,
body: bytes,
expected: bool,
) -> None:
response = httpx.Response(
HTTPStatus.OK,
headers={"Content-Type": content_type},
content=body,
request=httpx.Request("GET", SOURCE_URL),
)
fetch_source_document.return_value = response, body
assert verification.source_mentions_target(SOURCE_URL, TARGET_URL) is expected
fetch_source_document.assert_called_once_with(SOURCE_URL)
@patch("webmentions_ssg.tasks.verification.fetch_source_document")
@pytest.mark.parametrize(
("content_type", "expected_media_type"),
[
("application/json", "application/json"),
("application/pdf", "application/pdf"),
("", "missing"),
],
)
def test_source_mentions_target_rejects_unsupported_media_type(
fetch_source_document: Mock,
verification: ModuleType,
content_type: str,
expected_media_type: str,
) -> None:
headers = {}
if content_type:
headers["Content-Type"] = content_type
response = httpx.Response(
HTTPStatus.OK,
headers=headers,
content=b"Document",
request=httpx.Request("GET", SOURCE_URL),
)
fetch_source_document.return_value = response, b"Document"
with pytest.raises(
verification.VerificationError,
match=f"Unsupported source content type: {expected_media_type}",
):
verification.source_mentions_target(SOURCE_URL, TARGET_URL)
@pytest.mark.parametrize(
("mentions", "expected_status", "expected_reason"),
[
(True, "verified", None),
(False, "source_removed", "Source does not mention target"),
],
)
def test_determine_verification_outcome_returns_outcome(
app: Flask,
verification: ModuleType,
monkeypatch: pytest.MonkeyPatch,
mentions: bool,
expected_status: str,
expected_reason: str | None,
) -> None:
monkeypatch.setattr(
verification, "source_mentions_target", Mock(return_value=mentions)
)
claim = verification.VerificationClaim(
uuid.uuid7(), SOURCE_URL, TARGET_URL, datetime.now(UTC)
)
with app.app_context():
assert verification.determine_verification_outcome(claim) == (
expected_status,
expected_reason,
)
def test_verify_webmention_refuses_domain_blocked_after_enqueue(
app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch
) -> None:
identifier = create_webmention(app)
with app.app_context():
db.session.add(BlockedDomain(domain="source.example"))
db.session.commit()
fetch = Mock(return_value=True)
monkeypatch.setattr(verification, "source_mentions_target", fetch)
verification.verify_webmention.call_local(identifier)
assert get_webmention_state(app, identifier) == (
"failed",
"Source domain is blocked",
)
fetch.assert_not_called()
@patch("webmentions_ssg.tasks.verification.source_mentions_target")
def test_verify_webmention_marks_row_verifying_before_check(
source_mentions_target: Mock, app: Flask, verification: ModuleType
) -> None:
identifier = create_webmention(
app, status="failed", failure_reason="Earlier failure"
)
def verify_source(source_url: str, target_url: str) -> bool:
webmention = db.session.get(ReceivedWebmention, identifier)
assert webmention is not None
assert webmention.status == "verifying"
assert webmention.failure_reason is None
assert source_url == webmention.source
assert target_url == webmention.target
return True
source_mentions_target.side_effect = verify_source
verification.verify_webmention.call_local(identifier)
assert get_webmention_state(app, identifier) == ("verified", None)
@patch("webmentions_ssg.tasks.verification.source_mentions_target")
@pytest.mark.parametrize(
("outcome", "expected_status", "expected_reason"),
[
("verified", "verified", None),
("missing", "source_removed", "Source does not mention target"),
("gone", "source_removed", "Source returned HTTP 410"),
("permanent-failure", "failed", "Source returned HTTP 404"),
],
)
def test_verify_webmention_persists_final_state(
source_mentions_target: Mock,
app: Flask,
verification: ModuleType,
outcome: str,
expected_status: str,
expected_reason: str | None,
) -> None:
identifier = create_webmention(app)
match outcome:
case "verified":
source_mentions_target.return_value = True
case "missing":
source_mentions_target.return_value = False
case "gone":
source_mentions_target.side_effect = verification.SourceGoneError(
"Source returned HTTP 410"
)
case "permanent-failure":
source_mentions_target.side_effect = verification.VerificationError(
"Source returned HTTP 404"
)
case _:
raise AssertionError(f"Unexpected outcome: {outcome}")
verification.verify_webmention.call_local(identifier)
assert get_webmention_state(app, identifier) == (expected_status, expected_reason)
@patch("webmentions_ssg.tasks.verification.source_mentions_target")
@pytest.mark.parametrize("failure", ["temporary-http", "network"])
def test_verify_webmention_persists_retryable_failure_and_reraises(
source_mentions_target: Mock, app: Flask, verification: ModuleType, failure: str
) -> None:
identifier = create_webmention(app)
match failure:
case "temporary-http":
exception = verification.RetryableFetchError("Source returned HTTP 503")
case "network":
exception = httpx.ConnectError(
"Connection refused", request=httpx.Request("GET", SOURCE_URL)
)
case _:
raise AssertionError(f"Unexpected failure: {failure}")
source_mentions_target.side_effect = exception
with pytest.raises(type(exception), match=str(exception)):
verification.verify_webmention.call_local(identifier)
assert get_webmention_state(app, identifier) == ("failed", str(exception))
@patch("webmentions_ssg.tasks.verification.source_mentions_target")
@pytest.mark.parametrize("status", ["verifying", "verified", "source_removed"])
def test_verify_webmention_skips_active_and_terminal_states(
source_mentions_target: Mock, app: Flask, verification: ModuleType, status: str
) -> None:
identifier = create_webmention(app, status=status)
verification.verify_webmention.call_local(identifier)
assert get_webmention_state(app, identifier) == (status, None)
source_mentions_target.assert_not_called()
def set_webmention_updated_at(
app: Flask, identifier: uuid.UUID, when: datetime
) -> None:
with app.app_context():
db.session.execute(
sa.update(ReceivedWebmention)
.where(ReceivedWebmention.uuid == identifier)
.values(updated_at=when)
)
db.session.commit()
@pytest.mark.parametrize("status", ["verifying", "received"])
def test_reaper_requeues_stale_verifications(
app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch, status: str
) -> None:
app.config["VERIFICATION_STALE_AFTER_SECONDS"] = 900
identifier = create_webmention(app, status=status, failure_reason="Interrupted")
set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
enqueue = Mock()
monkeypatch.setattr(verification, "verify_webmention", enqueue)
with app.app_context():
assert verification.recover_stale_verifications() == 1
assert verification.recover_stale_verifications() == 0
row = db.session.get(ReceivedWebmention, identifier)
assert row is not None
assert row.updated_at.replace(tzinfo=UTC) > datetime.now(UTC) - timedelta(
seconds=10
)
assert get_webmention_state(app, identifier) == ("received", None)
enqueue.assert_called_once_with(identifier)
def test_reaper_does_not_touch_recent_or_terminal_webmentions(
app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch
) -> None:
recent_verifying = create_webmention(app, status="verifying")
recent_received = create_webmention(
app, source="https://source.example/received", status="received"
)
old_verified = create_webmention(
app, source="https://source.example/verified", status="verified"
)
set_webmention_updated_at(app, old_verified, datetime.now(UTC) - timedelta(hours=1))
enqueue = Mock()
monkeypatch.setattr(verification, "verify_webmention", enqueue)
with app.app_context():
assert verification.recover_stale_verifications() == 0
assert get_webmention_state(app, recent_verifying) == ("verifying", None)
assert get_webmention_state(app, recent_received) == ("received", None)
assert get_webmention_state(app, old_verified) == ("verified", None)
enqueue.assert_not_called()
def test_reaper_recovers_after_enqueue_failure(
app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch
) -> None:
identifier = create_webmention(app, status="verifying")
set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
monkeypatch.setattr(
verification, "verify_webmention", Mock(side_effect=RuntimeError)
)
with app.app_context(), pytest.raises(RuntimeError):
verification.recover_stale_verifications()
assert get_webmention_state(app, identifier) == ("received", None)
# The reaper can recover the row again if the process died after the commit
# but before Huey persisted the task.
app.config["VERIFICATION_STALE_AFTER_SECONDS"] = 0
enqueue = Mock()
monkeypatch.setattr(verification, "verify_webmention", enqueue)
with app.app_context():
assert verification.recover_stale_verifications() == 1
enqueue.assert_called_once_with(identifier)
def test_late_worker_cannot_overwrite_recovered_verification(
app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch
) -> None:
identifier = create_webmention(app)
original_verify = verification.verify_webmention
def concurrent_recovery(_source: str, _target: str) -> bool:
# The reaper supersedes this still-running attempt; a newer attempt
# subsequently completes before the original fetch returns.
app.config["VERIFICATION_STALE_AFTER_SECONDS"] = 0
with app.app_context():
with patch.object(verification, "verify_webmention", Mock()) as enqueue:
assert verification.recover_stale_verifications() == 1
enqueue.assert_called_once_with(identifier)
db.session.execute(
sa.update(ReceivedWebmention)
.where(ReceivedWebmention.uuid == identifier)
.values(status="verified", updated_at=datetime.now(UTC))
)
db.session.commit()
return False
monkeypatch.setattr(verification, "source_mentions_target", concurrent_recovery)
original_verify.call_local(identifier)
assert get_webmention_state(app, identifier) == ("verified", None)
def test_reaper_requeues_and_verifies_integration(
app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch
) -> None:
identifier = create_webmention(app, status="verifying")
set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
check = Mock(return_value=True)
monkeypatch.setattr(verification, "source_mentions_target", check)
with app.app_context():
assert verification.recover_stale_verifications() == 1
assert get_webmention_state(app, identifier) == ("verified", None)
check.assert_called_once_with(SOURCE_URL, TARGET_URL)
@patch("webmentions_ssg.tasks.verification.source_mentions_target")
@pytest.mark.parametrize(
"unexpected_error",
[
pytest.param(httpx.InvalidURL("Invalid IDNA hostname"), id="invalid-url"),
pytest.param(
RuntimeError("Unexpected HTML parser error"), id="unexpected-error"
),
],
)
def test_verify_webmention_records_unexpected_error_as_terminal_failure(
source_mentions_target: Mock,
app: Flask,
verification: ModuleType,
unexpected_error: Exception,
) -> None:
identifier = create_webmention(app, source="http://ex☃mple.test/")
source_mentions_target.side_effect = unexpected_error
verification.verify_webmention.call_local(identifier)
assert get_webmention_state(app, identifier) == ("failed", str(unexpected_error))
source_mentions_target.assert_called_once()
# Failed rows are not eligible for automatic reaper recovery.
set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
with app.app_context():
assert verification.recover_stale_verifications() == 0
def test_reaper_caps_stale_verification_recoveries(
app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch
) -> None:
app.config["VERIFICATION_MAX_RECOVERIES"] = 3
identifier = create_webmention(app, status="verifying")
enqueue = Mock()
monkeypatch.setattr(verification, "verify_webmention", enqueue)
# Simulate workers that repeatedly crash before completing the task.
for attempt in range(3):
set_webmention_updated_at(
app, identifier, datetime.now(UTC) - timedelta(hours=1)
)
with app.app_context():
assert verification.recover_stale_verifications() == 1
row = db.session.get(ReceivedWebmention, identifier)
assert row is not None
assert row.status == "received"
assert row.recovery_attempts == attempt + 1
set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
with app.app_context():
assert verification.recover_stale_verifications() == 0
row = db.session.get(ReceivedWebmention, identifier)
assert row is not None
assert row.status == "failed"
assert row.failure_reason == verification.RECOVERY_EXHAUSTED_REASON
assert row.recovery_attempts == 3
assert verification.recover_stale_verifications() == 0
assert enqueue.call_count == 3
def test_reaper_caps_received_rows_lost_before_first_task(
app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch
) -> None:
app.config["VERIFICATION_MAX_RECOVERIES"] = 1
identifier = create_webmention(app, status="received", recovery_attempts=1)
set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
enqueue = Mock()
monkeypatch.setattr(verification, "verify_webmention", enqueue)
with app.app_context():
assert verification.recover_stale_verifications() == 0
assert get_webmention_state(app, identifier) == (
"failed",
verification.RECOVERY_EXHAUSTED_REASON,
)
enqueue.assert_not_called()
def test_exhausted_row_cannot_be_revived_by_late_huey_retry(
app: Flask, verification: ModuleType, monkeypatch: pytest.MonkeyPatch
) -> None:
identifier = create_webmention(
app,
status="failed",
failure_reason=verification.RECOVERY_EXHAUSTED_REASON,
recovery_attempts=3,
)
source_mentions_target = Mock(return_value=True)
monkeypatch.setattr(verification, "source_mentions_target", source_mentions_target)
verification.verify_webmention.call_local(identifier)
assert get_webmention_state(app, identifier) == (
"failed",
verification.RECOVERY_EXHAUSTED_REASON,
)
source_mentions_target.assert_not_called()
def test_verify_webmention_ignores_unknown_identifier(
verification: ModuleType, caplog: pytest.LogCaptureFixture
) -> None:
identifier = uuid.uuid7()
with caplog.at_level(logging.WARNING):
verification.verify_webmention.call_local(identifier)
assert f"Cannot verify unknown ReceivedWebmention {identifier}" in caplog.text
@patch("webmentions_ssg.tasks.verification.httpx.Client")
def test_fetch_source_document_rejects_unsafe_destination(
httpx_client: Mock, app: Flask, verification: ModuleType
) -> None:
from webmentions_ssg.net import UnsafeAddressError
client = httpx_client.return_value.__enter__.return_value
client.stream.side_effect = UnsafeAddressError("Local address")
with (
app.app_context(),
pytest.raises(verification.VerificationError, match="non-public address"),
):
verification.fetch_source_document(SOURCE_URL)
@patch("webmentions_ssg.tasks.verification.httpx.Client")
def test_fetch_source_document_treats_dns_failure_as_temporary(
httpx_client: Mock, app: Flask, verification: ModuleType
) -> None:
client = httpx_client.return_value.__enter__.return_value
client.stream.side_effect = AddressResolutionError("DNS timeout")
with (
app.app_context(),
pytest.raises(verification.RetryableFetchError, match="could not be resolved"),
):
verification.fetch_source_document(SOURCE_URL)
@patch("webmentions_ssg.tasks.verification.httpx.Client")
def test_fetch_source_document_ignores_invalid_content_length(
httpx_client: Mock, app: Flask, verification: ModuleType
) -> None:
response = httpx.Response(
HTTPStatus.OK,
headers={"Content-Type": "text/html", "Content-Length": "invalid"},
content=b"<p>Document</p>",
request=httpx.Request("GET", SOURCE_URL),
)
set_stream_response(httpx_client, response)
with app.app_context():
fetched_response, body = verification.fetch_source_document(SOURCE_URL)
assert fetched_response is response
assert body == b"<p>Document</p>"
@patch("webmentions_ssg.tasks.verification.fetch_source_document")
def test_source_mentions_target_falls_back_for_unknown_encoding(
fetch_source_document: Mock, verification: ModuleType
) -> None:
body = f"Reply to {TARGET_URL}".encode()
response = httpx.Response(
HTTPStatus.OK,
headers={"Content-Type": "text/plain"},
content=body,
request=httpx.Request("GET", SOURCE_URL),
)
response.encoding = "unknown-encoding"
fetch_source_document.return_value = response, body
assert verification.source_mentions_target(SOURCE_URL, TARGET_URL)
fetch_source_document.assert_called_once_with(SOURCE_URL)
|