aboutsummaryrefslogtreecommitdiff
path: root/tests/tasks/test_receiver.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/tasks/test_receiver.py58
1 files changed, 18 insertions, 40 deletions
diff --git a/tests/tasks/test_receiver.py b/tests/tasks/test_receiver.py
index 4daca58..102410d 100644
--- a/tests/tasks/test_receiver.py
+++ b/tests/tasks/test_receiver.py
@@ -236,7 +236,7 @@ def test_fetch_source_returns_response_and_body(
assert options["follow_redirects"] is True
assert options["max_redirects"] == app.config.get("MAX_REDIRECTS", 20)
assert options["trust_env"] is False
- assert options["event_hooks"] == {"request": [receiver.ensure_public_request]}
+ assert options["transport"].__class__.__name__ == "PublicHTTPTransport"
@patch("webmentions_ssg.tasks.receiver.httpx.Client")
@@ -680,52 +680,30 @@ def test_verify_webmention_ignores_unknown_identifier(
assert f"Cannot verify unknown ReceivedWebmention {identifier}" in caplog.text
-@patch("webmentions_ssg.tasks.receiver.is_public_url", return_value=True)
-def test_ensure_public_request_accepts_public_url(
- is_public_url: Mock, receiver: ModuleType
+@patch("webmentions_ssg.tasks.receiver.httpx.Client")
+def test_fetch_source_rejects_unsafe_destination(
+ httpx_client: Mock, app: Flask, receiver: ModuleType
) -> None:
- receiver.ensure_public_request(httpx.Request("GET", SOURCE_URL))
- is_public_url.assert_called_once_with(SOURCE_URL)
-
+ from webmentions_ssg.url_security import UnsafeAddressError
-@patch("webmentions_ssg.tasks.receiver.is_public_url", return_value=False)
-def test_ensure_public_request_rejects_non_public_address(
- is_public_url: Mock, receiver: ModuleType
-) -> None:
- with pytest.raises(
- receiver.VerificationError, match="Source resolves to a non-public address"
+ client = httpx_client.return_value.__enter__.return_value
+ client.stream.side_effect = UnsafeAddressError("Local address")
+ with app.app_context(), pytest.raises(
+ receiver.VerificationError, match="non-public address"
):
- receiver.ensure_public_request(httpx.Request("GET", SOURCE_URL))
-
- is_public_url.assert_called_once_with(SOURCE_URL)
+ receiver.fetch_source(SOURCE_URL)
-@patch(
- "webmentions_ssg.tasks.receiver.is_public_url",
- side_effect=AddressResolutionError("Could not resolve hostname"),
-)
-def test_ensure_public_request_maps_dns_failure_to_temporary_error(
- is_public_url: Mock, receiver: ModuleType
+@patch("webmentions_ssg.tasks.receiver.httpx.Client")
+def test_fetch_source_treats_dns_failure_as_temporary(
+ httpx_client: Mock, app: Flask, receiver: ModuleType
) -> None:
- with pytest.raises(
- receiver.TemporaryFetchError, match="Source hostname could not be resolved"
+ client = httpx_client.return_value.__enter__.return_value
+ client.stream.side_effect = AddressResolutionError("DNS timeout")
+ with app.app_context(), pytest.raises(
+ receiver.TemporaryFetchError, match="could not be resolved"
):
- receiver.ensure_public_request(httpx.Request("GET", SOURCE_URL))
-
- is_public_url.assert_called_once_with(SOURCE_URL)
-
-
-@patch(
- "webmentions_ssg.tasks.receiver.is_public_url",
- side_effect=ValueError("No hostname was specified"),
-)
-def test_ensure_public_request_rejects_url_without_hostname(
- is_public_url: Mock, receiver: ModuleType
-) -> None:
- with pytest.raises(receiver.VerificationError, match="Source URL has no hostname"):
- receiver.ensure_public_request(httpx.Request("GET", SOURCE_URL))
-
- is_public_url.assert_called_once_with(SOURCE_URL)
+ receiver.fetch_source(SOURCE_URL)
@patch("webmentions_ssg.tasks.receiver.BeautifulSoup")