diff options
Diffstat (limited to 'tests/tasks')
| -rw-r--r-- | tests/tasks/test_receiver.py | 58 | ||||
| -rw-r--r-- | tests/tasks/test_sender.py | 37 |
2 files changed, 18 insertions, 77 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") diff --git a/tests/tasks/test_sender.py b/tests/tasks/test_sender.py index 0db0d8c..38bae11 100644 --- a/tests/tasks/test_sender.py +++ b/tests/tasks/test_sender.py @@ -1214,43 +1214,6 @@ def test_send_webmention_ignores_unknown_identifier( assert f"Cannot send unknown SentWebmention {identifier}" in caplog.text -def test_ensure_public_request_accepts_public_url( - sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch -) -> None: - is_public_url = Mock(return_value=True) - monkeypatch.setattr(sender_module, "is_public_url", is_public_url) - sender_module.ensure_public_request(httpx.Request("GET", TARGET_URL)) - is_public_url.assert_called_once_with(TARGET_URL) - - -def test_ensure_public_request_rejects_non_public_address( - sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch -) -> None: - is_public_url = Mock(return_value=False) - monkeypatch.setattr(sender_module, "is_public_url", is_public_url) - with pytest.raises( - sender_module.PermanentSenderError, - match="Request resolves to a non-public address", - ): - sender_module.ensure_public_request(httpx.Request("GET", TARGET_URL)) - is_public_url.assert_called_once_with(TARGET_URL) - - -def test_ensure_public_request_maps_dns_failure_to_temporary_error( - sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch -) -> None: - is_public_url = Mock( - side_effect=AddressResolutionError("Could not resolve hostname") - ) - monkeypatch.setattr(sender_module, "is_public_url", is_public_url) - with pytest.raises( - sender_module.TemporarySenderError, - match="Request hostname could not be resolved", - ): - sender_module.ensure_public_request(httpx.Request("GET", TARGET_URL)) - is_public_url.assert_called_once_with(TARGET_URL) - - def test_resolve_endpoint_rejects_invalid_url( sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: |
