',
- ),
- encoding="utf-8",
- )
- first = scanner_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 = scanner_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(
- scanner_module: ModuleType, tmp_path: Path
-) -> None:
- directory = tmp_path / "example"
- directory.mkdir()
-
- path = directory / "index.html"
- path.write_text(
- """
-
-
-
-
-
- Permalink
-
-
-
-
-
-
- """,
- encoding="utf-8",
- )
-
- scanned = scanner_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(
- scanner_module: ModuleType, tmp_path: Path
-) -> None:
- directory = tmp_path / "example"
- directory.mkdir()
-
- path = directory / "index.html"
- path.write_text(
- """
-
-
-
-
-
- Permalink
-
-
-
-
-
-
- """,
- encoding="utf-8",
- )
-
- with pytest.raises(scanner_module.SourceScanError, match="no canonical URL"):
- scanner_module.scan_source_file(path, root=tmp_path, base_url=None)
-
-
-def test_scan_source_extracts_only_content_links(
- scanner_module: ModuleType, tmp_path: Path
-) -> None:
- directory = tmp_path / "example"
- directory.mkdir()
-
- path = directory / "index.html"
- path.write_text(
- f"""
-
-
-
-
-
-
-
-
- Permalink
-
-
-
- Metadata link
-
-
-
-
-
-
- """,
- encoding="utf-8",
- )
-
- scanned = scanner_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(
- scanner_module: ModuleType, tmp_path: Path
-) -> None:
- path = write_post(
- tmp_path,
- "example",
- """
-
- Other post
-
- """,
- )
-
- scanned = scanner_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(
- scanner_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"""
-
-
-
-
-
-
-
-
- Permalink
-
-
-
- Reaction target
-
-
-
-
-
-
- """,
- encoding="utf-8",
- )
-
- scanned = scanner_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(
- scanner_module: ModuleType, tmp_path: Path, tag: str
-) -> None:
- path = write_post(
- tmp_path,
- "example",
- f"""
- <{tag} href="https://example.com/target">
- """,
- )
-
- scanned = scanner_module.scan_source_file(path, root=tmp_path, base_url=None)
-
- assert scanned.targets == frozenset()
-
-
-def test_scan_source_ignores_self_fragment(
- scanner_module: ModuleType, tmp_path: Path
-) -> None:
- path = write_post(
- tmp_path,
- "example",
- """
-
- Section
-
-
-
- Target
-
- """,
- )
-
- scanned = scanner_module.scan_source_file(path, root=tmp_path, base_url=None)
-
- assert scanned.targets == frozenset({"https://example.com/target"})
-
-
def test_scan_creates_source_and_webmentions(
app: Flask, scanner_module: ModuleType, tmp_path: Path, monkeypatch: MonkeyPatch
) -> None:
@@ -1885,23 +1128,23 @@ def test_unexpected_parser_error_does_not_abort_other_sources(
write_post(tmp_path, "good", '
Link')
if parser == "bs4":
- original_soup = scanner_module.BeautifulSoup
+ original_soup = source_parser.BeautifulSoup
def failing_soup(markup: bytes, features: str) -> BeautifulSoup:
if b"/bad/" in markup:
raise failure
return original_soup(markup, features)
- monkeypatch.setattr(scanner_module, "BeautifulSoup", failing_soup)
+ monkeypatch.setattr(source_parser, "BeautifulSoup", failing_soup)
else:
- original_parse = scanner_module.mf2py.parse
+ original_parse = source_parser.mf2py.parse
def failing_parse(*, doc: str, url: str) -> dict[str, Any]:
if "/bad/" in doc:
raise failure
return original_parse(doc=doc, url=url)
- monkeypatch.setattr(scanner_module.mf2py, "parse", failing_parse)
+ monkeypatch.setattr(source_parser.mf2py, "parse", failing_parse)
queued: list[UUID] = []
monkeypatch.setattr(scanner_module, "send_webmention", queued.append)
@@ -1979,46 +1222,6 @@ def test_unexpected_persistence_error_is_not_suppressed(
run_scan(app, scanner_module)
-def test_scan_source_rejects_symlink_outside_root_before_read(
- scanner_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(
- scanner_module.SourceScanError, match="escapes source directory"
- ):
- scanner_module.scan_source_file(linked, root=root, base_url=None)
-
- read_bytes.assert_not_called()
-
-
-def test_scan_source_accepts_symlink_within_root(
- scanner_module: ModuleType, tmp_path: Path
-) -> None:
- root = tmp_path / "site"
- root.mkdir()
- linked = write_post(
- root, "example", '
Target'
- )
- shared = root / "shared.html"
- linked.rename(shared)
- linked.symlink_to(shared)
-
- scanned = scanner_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"})
-
-
def test_scan_sources_skips_symlink_outside_root(
app: Flask,
scanner_module: ModuleType,
@@ -2043,16 +1246,6 @@ def test_scan_sources_skips_symlink_outside_root(
assert "escapes source directory" in caplog.text
-def test_scan_sources_rejects_malformed_runtime_ignored_hostnames(
- app: Flask, scanner_module: ModuleType, tmp_path: Path
-) -> None:
- configure_scanner(app, tmp_path)
- app.config["IGNORED_HOSTNAMES"] = "example.com"
-
- with pytest.raises(TypeError, match="IGNORED_HOSTNAMES"):
- run_scan(app, scanner_module)
-
-
def test_scan_sources_accepts_valid_base_url(
app: Flask, scanner_module: ModuleType, tmp_path: Path, monkeypatch: MonkeyPatch
) -> None:
@@ -2104,220 +1297,6 @@ def test_scan_sources_rejects_invalid_base_url(
run_scan(app, scanner_module)
-@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(
- scanner_module: ModuleType, tmp_path: Path, href: str
-) -> None:
- path = write_post(
- tmp_path,
- "example",
- f"""
-
- Ignored
-
-
-
- External
-
- """,
- )
-
- scanned = scanner_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(
- scanner_module: ModuleType, tmp_path: Path, href: str
-) -> None:
- path = write_post(
- tmp_path,
- "example",
- f"""
-
- Target
-
- """,
- )
-
- scanned = scanner_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(
- scanner_module: ModuleType, tmp_path: Path
-) -> None:
- directory = tmp_path / "example"
- directory.mkdir()
-
- path = directory / "index.html"
- path.write_text(
- f"""
-
-
-
-
-
-
-
-
- Permalink
-
-
-
- Reply target
-
-
-
-
-
-
- """,
- encoding="utf-8",
- )
-
- scanned = scanner_module.scan_source_file(
- path,
- root=tmp_path,
- base_url=None,
- ignored_hostnames=("dennisfink.me", "*.dennisfink.me"),
- )
-
- assert scanned.targets == frozenset()
-
-
-def test_canonical_url_rejects_non_http_resolved_url(
- scanner_module: ModuleType,
-) -> None:
- scanner_module.is_http_url.return_value = False
-
- document = parse_html(
- """
-
-
-
-
-
- """
- )
-
- with pytest.raises(
- scanner_module.SourceScanError,
- match="Canonical URL is not a valid HTTP or HTTPS URL",
- ):
- scanner_module.canonical_url(
- document, relative_path=Path("example/index.html"), base_url=BASE_URL
- )
-
-
-def test_property_urls_ignores_non_mapping_properties(
- scanner_module: ModuleType,
-) -> None:
- entry = {"properties": "invalid"}
-
- assert tuple(scanner_module.property_urls(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_entry_rejects_invalid_parser_output(
- scanner_module: ModuleType, monkeypatch: MonkeyPatch, parsed: object, message: str
-) -> None:
- document = parse_html(
- """
-
- Hello world
-
- """
- )
-
- element = document.find(class_="h-entry")
-
- assert element is not None
-
- monkeypatch.setattr(scanner_module.mf2py, "parse", lambda **kwargs: parsed)
-
- with pytest.raises(scanner_module.SourceScanError, match=message):
- scanner_module.parse_entry(element, SOURCE_URL)
-
-
-def test_normalize_target_ignores_empty_target(scanner_module: ModuleType) -> None:
- assert (
- scanner_module.normalize_target(" ", base_url=SOURCE_URL, source_url=SOURCE_URL)
- is None
- )
-
-
-def test_normalize_target_ignores_url_rejected_by_validator(
- scanner_module: ModuleType,
-) -> None:
- scanner_module.is_http_url.return_value = False
-
- assert (
- scanner_module.normalize_target(
- "https://example.com/target", base_url=SOURCE_URL, source_url=SOURCE_URL
- )
- is None
- )
-
-
def test_changed_source_url_is_rejected(
app: Flask,
scanner_module: ModuleType,
--
cgit v1.3.1