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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
|
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause
from pathlib import Path
from types import ModuleType
from unittest.mock import Mock
import pytest
from flask import Flask
from pytest import MonkeyPatch
from webmentions_ssg.tasks import source_parser
from .scanner_helpers import BASE_URL, SOURCE_URL, parse_html, write_post
@pytest.fixture
def source_parser_module(app: Flask, monkeypatch: pytest.MonkeyPatch) -> ModuleType:
_ = app
from webmentions_ssg.tasks import source_parser
monkeypatch.setattr(source_parser, "is_http_url", Mock(return_value=True))
return source_parser
def test_parse_h_entry_returns_microformats_entry(
source_parser_module: ModuleType,
) -> None:
document = parse_html(
f"""
<article class="h-entry">
<a class="u-url" href="{SOURCE_URL}">
Permalink
</a>
<div class="e-content">
Hello world
</div>
</article>
"""
)
element = document.find(class_="h-entry")
assert element is not None
entry = source_parser_module.parse_h_entry(element, SOURCE_URL)
assert "h-entry" in entry["type"]
assert entry["properties"]["url"] == [SOURCE_URL]
def test_parse_h_entry_rejects_non_entry(source_parser_module: ModuleType) -> None:
document = parse_html(
"""
<article>
<p>Hello world</p>
</article>
"""
)
element = document.find("article")
assert element is not None
with pytest.raises(
source_parser_module.SourceScanError,
match="Expected exactly one parsed h-entry",
):
source_parser_module.parse_h_entry(element, SOURCE_URL)
def test_find_primary_h_entry_returns_source_entry(
source_parser_module: ModuleType,
) -> None:
document = parse_html(
f"""
<article class="h-entry" id="source">
<a class="u-url" href="{SOURCE_URL}">
Permalink
</a>
<p class="p-name">
Example post
</p>
<div class="e-content">
Hello world
</div>
</article>
"""
)
element, entry = source_parser_module.find_primary_h_entry(document, SOURCE_URL)
assert element["id"] == "source"
assert entry["properties"]["name"] == ["Example post"]
assert entry["properties"]["url"] == [SOURCE_URL]
def test_find_primary_h_entry_rejects_missing_h_entry(
source_parser_module: ModuleType,
) -> None:
document = parse_html(
"""
<main>
<p>No microformats here.</p>
</main>
"""
)
with pytest.raises(
source_parser_module.SourceScanError,
match="Expected exactly one h-entry, found 0",
):
source_parser_module.find_primary_h_entry(document, SOURCE_URL)
def test_find_primary_h_entry_rejects_multiple_h_entries(
source_parser_module: ModuleType,
) -> None:
document = parse_html(
f"""
<article class="h-entry">
<a class="u-url" href="{SOURCE_URL}">
First
</a>
</article>
<article class="h-entry">
<a
class="u-url"
href="https://example.com/other/"
>
Second
</a>
</article>
"""
)
with pytest.raises(
source_parser_module.SourceScanError,
match="Expected exactly one h-entry, found 2",
):
source_parser_module.find_primary_h_entry(document, SOURCE_URL)
def test_find_primary_h_entry_rejects_nested_h_entry(
source_parser_module: ModuleType,
) -> None:
document = parse_html(
f"""
<article class="h-entry">
<a class="u-url" href="{SOURCE_URL}">
Permalink
</a>
<div class="e-content">
<article class="h-entry">
<a
class="u-url"
href="https://example.com/nested/"
>
Nested
</a>
</article>
</div>
</article>
"""
)
with pytest.raises(
source_parser_module.SourceScanError,
match="Expected exactly one h-entry, found 2",
):
source_parser_module.find_primary_h_entry(document, SOURCE_URL)
def test_find_primary_h_entry_rejects_missing_u_url(
source_parser_module: ModuleType,
) -> None:
document = parse_html(
"""
<article class="h-entry">
<div class="e-content">
Hello world
</div>
</article>
"""
)
with pytest.raises(source_parser_module.SourceScanError, match="does not match"):
source_parser_module.find_primary_h_entry(document, SOURCE_URL)
def test_find_primary_h_entry_rejects_nonmatching_u_url(
source_parser_module: ModuleType,
) -> None:
document = parse_html(
"""
<article class="h-entry">
<a
class="u-url"
href="https://example.com/other/"
>
Other
</a>
<div class="e-content">
Hello world
</div>
</article>
"""
)
with pytest.raises(source_parser_module.SourceScanError, match="does not match"):
source_parser_module.find_primary_h_entry(document, SOURCE_URL)
def test_find_e_content_returns_e_content(source_parser_module: ModuleType) -> None:
document = parse_html(
"""
<article class="h-entry">
<div class="e-content" id="content">
Hello world
</div>
</article>
"""
)
entry = document.find(class_="h-entry")
assert entry is not None
content = source_parser_module.find_e_content(entry)
assert content["id"] == "content"
def test_find_e_content_rejects_missing_e_content(
source_parser_module: ModuleType,
) -> None:
document = parse_html(
"""
<article class="h-entry">
<p>Hello world</p>
</article>
"""
)
entry = document.find(class_="h-entry")
assert entry is not None
with pytest.raises(
source_parser_module.SourceScanError,
match="Expected exactly one e-content, found 0",
):
source_parser_module.find_e_content(entry)
def test_find_e_content_rejects_multiple_e_content(
source_parser_module: ModuleType,
) -> None:
document = parse_html(
"""
<article class="h-entry">
<div class="e-content">
First
</div>
<div class="e-content">
Second
</div>
</article>
"""
)
entry = document.find(class_="h-entry")
assert entry is not None
with pytest.raises(
source_parser_module.SourceScanError,
match="Expected exactly one e-content, found 2",
):
source_parser_module.find_e_content(entry)
def test_declared_canonical_url_returns_absolute_url(
source_parser_module: ModuleType,
) -> None:
document = parse_html(
f"""
<html>
<head>
<link
rel="canonical"
href="{SOURCE_URL}"
>
</head>
</html>
"""
)
result = source_parser_module.declared_canonical_url(
document, relative_path=Path("example/index.html"), base_url=None
)
assert result == SOURCE_URL
def test_declared_canonical_url_returns_none_when_missing(
source_parser_module: ModuleType,
) -> None:
document = parse_html(
"""
<html>
<head>
<title>Example</title>
</head>
</html>
"""
)
result = source_parser_module.declared_canonical_url(
document, relative_path=Path("example/index.html"), base_url=None
)
assert result is None
def test_declared_canonical_url_resolves_relative_url_with_base_url(
source_parser_module: ModuleType,
) -> None:
source_parser_module.is_http_url.side_effect = [False, True]
document = parse_html(
"""
<html>
<head>
<link
rel="canonical"
href="./canonical/"
>
</head>
</html>
"""
)
result = source_parser_module.declared_canonical_url(
document, relative_path=Path("example/index.html"), base_url=BASE_URL
)
assert result == ("https://dennisfink.me/blog/example/canonical/")
def test_relative_canonical_requires_base_url(source_parser_module: ModuleType) -> None:
source_parser_module.is_http_url.return_value = False
document = parse_html(
"""
<html>
<head>
<link
rel="canonical"
href="./canonical/"
>
</head>
</html>
"""
)
with pytest.raises(
source_parser_module.SourceScanError, match="relative canonical"
):
source_parser_module.declared_canonical_url(
document, relative_path=Path("example/index.html"), base_url=None
)
def test_empty_canonical_is_rejected(source_parser_module: ModuleType) -> None:
document = parse_html(
"""
<html>
<head>
<link
rel="canonical"
href=" "
>
</head>
</html>
"""
)
with pytest.raises(
source_parser_module.SourceScanError, match="empty canonical URL"
):
source_parser_module.declared_canonical_url(
document, relative_path=Path("example/index.html"), base_url=BASE_URL
)
def test_scan_source_uses_canonical_without_base_url(
source_parser_module: ModuleType, tmp_path: Path
) -> None:
path = write_post(
tmp_path,
"example",
"""
<a href="https://example.com/target">
Target
</a>
""",
)
scanned = source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
assert scanned.path == "example/index.html"
assert scanned.url == SOURCE_URL
assert scanned.targets == frozenset({"https://example.com/target"})
assert len(scanned.content_hash) == 32
def test_scan_source_ignores_markup_noise_in_content(
source_parser_module: ModuleType, tmp_path: Path
) -> None:
path = write_post(
tmp_path,
"example",
"<p>Hello <strong>world</strong>!</p>"
'<a href="https://example.com/target">Target</a>',
)
first = source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
write_post(
tmp_path,
"example",
'<p class="generated" data-build="123"> Hello world! </p>'
'<a class="external" href="https://example.com/target"> Target </a>',
)
second = source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
assert first.targets == second.targets
assert first.content_hash == second.content_hash
def test_scan_source_uses_stable_time_value_inside_content(
source_parser_module: ModuleType, tmp_path: Path
) -> None:
path = write_post(
tmp_path,
"example",
'<time datetime="2026-10-30T12:00:00Z">5 minutes ago</time>',
)
first = source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
path.write_text(
path.read_text(encoding="utf-8").replace("5 minutes ago", "6 minutes ago"),
encoding="utf-8",
)
second = source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
assert first.content_hash == second.content_hash
def test_scan_source_hash_changes_when_image_changes(
source_parser_module: ModuleType, tmp_path: Path
) -> None:
path = write_post(tmp_path, "example", '<img src="/images/a.jpg" alt="A photo">')
first = source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
path.write_text(
path.read_text(encoding="utf-8").replace("/images/a.jpg", "/images/b.jpg"),
encoding="utf-8",
)
second = source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
assert first.content_hash != second.content_hash
def test_scan_source_hash_includes_reaction_targets_outside_content(
source_parser_module: ModuleType, tmp_path: Path
) -> None:
path = write_post(tmp_path, "example", "<p>Same content.</p>")
html = path.read_text(encoding="utf-8")
path.write_text(
html.replace(
'<div class="e-content">',
'<a class="u-like-of" href="https://example.com/first">Like</a>'
'<div class="e-content">',
),
encoding="utf-8",
)
first = source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
path.write_text(
path.read_text(encoding="utf-8").replace(
"https://example.com/first", "https://example.com/second"
),
encoding="utf-8",
)
second = source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
assert first.targets == frozenset({"https://example.com/first"})
assert second.targets == frozenset({"https://example.com/second"})
assert first.content_hash != second.content_hash
def test_scan_source_hash_ignores_reaction_target_order(
source_parser_module: ModuleType, tmp_path: Path
) -> None:
path = write_post(tmp_path, "example", "<p>Same content.</p>")
html = path.read_text(encoding="utf-8")
first_link = '<a class="u-like-of" href="https://example.com/a">A</a>'
second_link = '<a class="u-like-of" href="https://example.com/b">B</a>'
path.write_text(
html.replace(
'<div class="e-content">',
first_link + second_link + '<div class="e-content">',
),
encoding="utf-8",
)
first = source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
path.write_text(
path.read_text(encoding="utf-8").replace(
first_link + second_link, second_link + first_link
),
encoding="utf-8",
)
second = source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
assert first.targets == second.targets
assert first.content_hash == second.content_hash
def test_scan_source_falls_back_to_base_url(
source_parser_module: ModuleType, tmp_path: Path
) -> None:
directory = tmp_path / "example"
directory.mkdir()
path = directory / "index.html"
path.write_text(
"""
<!doctype html>
<html>
<body>
<article class="h-entry">
<a class="u-url" href="./">
Permalink
</a>
<div class="e-content">
<a href="https://example.com/target">
Target
</a>
</div>
</article>
</body>
</html>
""",
encoding="utf-8",
)
scanned = source_parser_module.scan_source_file(
path, root=tmp_path, base_url=BASE_URL
)
assert scanned.url == SOURCE_URL
assert scanned.targets == frozenset({"https://example.com/target"})
def test_scan_source_requires_canonical_or_base_url(
source_parser_module: ModuleType, tmp_path: Path
) -> None:
directory = tmp_path / "example"
directory.mkdir()
path = directory / "index.html"
path.write_text(
"""
<!doctype html>
<html>
<body>
<article class="h-entry">
<a class="u-url" href="./">
Permalink
</a>
<div class="e-content">
<p>Hello world.</p>
</div>
</article>
</body>
</html>
""",
encoding="utf-8",
)
with pytest.raises(source_parser_module.SourceScanError, match="no canonical URL"):
source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
def test_scan_source_extracts_only_content_links(
source_parser_module: ModuleType, tmp_path: Path
) -> None:
directory = tmp_path / "example"
directory.mkdir()
path = directory / "index.html"
path.write_text(
f"""
<!doctype html>
<html>
<head>
<link
rel="canonical"
href="{SOURCE_URL}"
>
</head>
<body>
<article class="h-entry">
<a class="u-url" href="{SOURCE_URL}">
Permalink
</a>
<a href="https://example.com/metadata">
Metadata link
</a>
<div class="e-content">
<a href="https://example.com/content">
Content link
</a>
</div>
</article>
</body>
</html>
""",
encoding="utf-8",
)
scanned = source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
assert scanned.targets == frozenset({"https://example.com/content"})
def test_scan_source_extracts_relative_content_link(
source_parser_module: ModuleType, tmp_path: Path
) -> None:
path = write_post(
tmp_path,
"example",
"""
<a href="../other/">
Other post
</a>
""",
)
scanned = source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
assert scanned.targets == frozenset({"https://dennisfink.me/blog/other/"})
@pytest.mark.parametrize(
"property_name", ("in-reply-to", "like-of", "repost-of", "bookmark-of")
)
def test_scan_source_extracts_reaction_properties(
source_parser_module: ModuleType, tmp_path: Path, property_name: str
) -> None:
directory = tmp_path / "example"
directory.mkdir()
target = f"https://example.com/{property_name}"
path = directory / "index.html"
path.write_text(
f"""
<!doctype html>
<html>
<head>
<link
rel="canonical"
href="{SOURCE_URL}"
>
</head>
<body>
<article class="h-entry">
<a class="u-url" href="{SOURCE_URL}">
Permalink
</a>
<a
class="u-{property_name}"
href="{target}"
>
Reaction target
</a>
<div class="e-content">
<p>Post content.</p>
</div>
</article>
</body>
</html>
""",
encoding="utf-8",
)
scanned = source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
assert scanned.targets == frozenset({target})
@pytest.mark.parametrize("tag", ("link", "area"))
def test_scan_source_ignores_non_anchor_href_elements(
source_parser_module: ModuleType, tmp_path: Path, tag: str
) -> None:
path = write_post(
tmp_path,
"example",
f"""
<{tag} href="https://example.com/target">
""",
)
scanned = source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
assert scanned.targets == frozenset()
def test_scan_source_ignores_self_fragment(
source_parser_module: ModuleType, tmp_path: Path
) -> None:
path = write_post(
tmp_path,
"example",
"""
<a href="#section">
Section
</a>
<a href="https://example.com/target">
Target
</a>
""",
)
scanned = source_parser_module.scan_source_file(path, root=tmp_path, base_url=None)
assert scanned.targets == frozenset({"https://example.com/target"})
def test_scan_source_rejects_symlink_outside_root_before_read(
source_parser_module: ModuleType, tmp_path: Path, monkeypatch: MonkeyPatch
) -> None:
root = tmp_path / "site"
root.mkdir()
external = write_post(tmp_path / "outside", "private", "Sensitive content")
linked = root / "escaped" / "index.html"
linked.parent.mkdir()
linked.symlink_to(external)
read_bytes = Mock(side_effect=AssertionError("External file must not be read"))
monkeypatch.setattr(Path, "read_bytes", read_bytes)
with pytest.raises(
source_parser_module.SourceScanError, match="escapes source directory"
):
source_parser_module.scan_source_file(linked, root=root, base_url=None)
read_bytes.assert_not_called()
def test_scan_source_accepts_symlink_within_root(
source_parser_module: ModuleType, tmp_path: Path
) -> None:
root = tmp_path / "site"
root.mkdir()
linked = write_post(
root, "example", '<a href="https://example.com/target">Target</a>'
)
shared = root / "shared.html"
linked.rename(shared)
linked.symlink_to(shared)
scanned = source_parser_module.scan_source_file(linked, root=root, base_url=None)
assert scanned.path == "example/index.html"
assert scanned.url == SOURCE_URL
assert scanned.targets == frozenset({"https://example.com/target"})
@pytest.mark.parametrize(
"href",
(
"https://dennisfink.me/blog/other/",
"https://www.dennisfink.me/blog/other/",
"https://webmentions.dennisfink.me/endpoint",
"https://foo.bar.dennisfink.me/example",
),
)
def test_scan_source_ignores_matching_hostname(
source_parser_module: ModuleType, tmp_path: Path, href: str
) -> None:
path = write_post(
tmp_path,
"example",
f"""
<a href="{href}">
Ignored
</a>
<a href="https://example.com/target">
External
</a>
""",
)
scanned = source_parser_module.scan_source_file(
path,
root=tmp_path,
base_url=None,
ignored_hostnames=("dennisfink.me", "*.dennisfink.me"),
)
assert scanned.targets == frozenset({"https://example.com/target"})
@pytest.mark.parametrize(
"href",
(
"https://notdennisfink.me/example",
"https://dennisfink.me.example.com/example",
"https://example.com/example",
),
)
def test_scan_source_keeps_nonmatching_hostname(
source_parser_module: ModuleType, tmp_path: Path, href: str
) -> None:
path = write_post(
tmp_path,
"example",
f"""
<a href="{href}">
Target
</a>
""",
)
scanned = source_parser_module.scan_source_file(
path,
root=tmp_path,
base_url=None,
ignored_hostnames=("dennisfink.me", "*.dennisfink.me"),
)
assert scanned.targets == frozenset({href})
def test_scan_source_ignores_reaction_to_matching_hostname(
source_parser_module: ModuleType, tmp_path: Path
) -> None:
directory = tmp_path / "example"
directory.mkdir()
path = directory / "index.html"
path.write_text(
f"""
<!doctype html>
<html>
<head>
<link
rel="canonical"
href="{SOURCE_URL}"
>
</head>
<body>
<article class="h-entry">
<a
class="u-url"
href="{SOURCE_URL}"
>
Permalink
</a>
<a
class="u-in-reply-to"
href="https://www.dennisfink.me/blog/other/"
>
Reply target
</a>
<div class="e-content">
<p>Reply content.</p>
</div>
</article>
</body>
</html>
""",
encoding="utf-8",
)
scanned = source_parser_module.scan_source_file(
path,
root=tmp_path,
base_url=None,
ignored_hostnames=("dennisfink.me", "*.dennisfink.me"),
)
assert scanned.targets == frozenset()
def test_declared_canonical_url_rejects_non_http_resolved_url(
source_parser_module: ModuleType,
) -> None:
source_parser_module.is_http_url.return_value = False
document = parse_html(
"""
<html>
<head>
<link
rel="canonical"
href="mailto:example@example.com"
>
</head>
</html>
"""
)
with pytest.raises(
source_parser_module.SourceScanError,
match="Canonical URL is not a valid HTTP or HTTPS URL",
):
source_parser_module.declared_canonical_url(
document, relative_path=Path("example/index.html"), base_url=BASE_URL
)
def test_mf2_string_values_ignores_non_mapping_properties(
source_parser_module: ModuleType,
) -> None:
entry = {"properties": "invalid"}
assert tuple(source_parser_module.mf2_string_values(entry, "url")) == ()
@pytest.mark.parametrize(
("parsed", "message"),
[
pytest.param(
[],
"Microformats parser did not return a document object",
id="non-object-document",
),
pytest.param(
{"items": [None]}, "Parsed h-entry is not an object", id="non-object-entry"
),
pytest.param(
{"items": [{"type": ["h-card"]}]},
"Parsed microformats item is not an h-entry",
id="wrong-item-type",
),
],
)
def test_parse_h_entry_rejects_invalid_parser_output(
source_parser_module: ModuleType,
monkeypatch: MonkeyPatch,
parsed: object,
message: str,
) -> None:
document = parse_html(
"""
<article class="h-entry">
<p>Hello world</p>
</article>
"""
)
element = document.find(class_="h-entry")
assert element is not None
monkeypatch.setattr(source_parser.mf2py, "parse", lambda **kwargs: parsed)
with pytest.raises(source_parser_module.SourceScanError, match=message):
source_parser_module.parse_h_entry(element, SOURCE_URL)
def test_normalize_target_url_ignores_empty_target(
source_parser_module: ModuleType,
) -> None:
assert (
source_parser_module.normalize_target_url(
" ", base_url=SOURCE_URL, source_url=SOURCE_URL
)
is None
)
def test_normalize_target_url_ignores_url_rejected_by_validator(
source_parser_module: ModuleType,
) -> None:
source_parser_module.is_http_url.return_value = False
assert (
source_parser_module.normalize_target_url(
"https://example.com/target", base_url=SOURCE_URL, source_url=SOURCE_URL
)
is None
)
def test_outgoing_targets_skip_urls_with_unparseable_hostname(
source_parser_module: ModuleType, monkeypatch: MonkeyPatch
) -> None:
document = parse_html(
'<div><a href="https://bad.example/post">bad</a>'
'<a href="https://good.example/post">good</a></div>'
)
content = document.find("div")
assert content is not None
original = source_parser_module.normalize_hostname
def invalid_hostname(url: str, *, from_url: bool = False) -> str:
if "bad.example" in url:
raise ValueError("Malformed URL hostname")
return original(url, from_url=from_url)
monkeypatch.setattr(source_parser_module, "normalize_hostname", invalid_hostname)
assert list(
source_parser_module.iter_outgoing_targets(
content, {}, base_url=SOURCE_URL, source_url=SOURCE_URL
)
) == ["https://good.example/post"]
def test_stable_content_text_excludes_hidden_markup(
source_parser_module: ModuleType,
) -> None:
document = parse_html(
"<div>Hello<script>secret-script</script>"
"<style>secret-style</style><template>secret-template</template>world</div>"
)
content = document.find("div")
assert content is not None
normalized = source_parser_module.stable_content_text(content)
assert "Hello" in normalized and "world" in normalized
assert "secret-" not in normalized
|