aboutsummaryrefslogtreecommitdiff
path: root/tests/test_net.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-19 21:18:37 +0100
committerDennis Fink2026-11-19 21:18:37 +0100
commiteb11fb64e9cad6fbda2065362189653416d98577 (patch)
tree0bc5737b1c273ea6f04adc015154565c385dc719 /tests/test_net.py
parent88f54d9a7699944b7d56f978e724b1c0b54dde98 (diff)
downloadwebmentions-ssg-eb11fb64e9cad6fbda2065362189653416d98577.tar.gz
webmentions-ssg-eb11fb64e9cad6fbda2065362189653416d98577.zip
test: expand failure and migration coverage
Add regression coverage for malformed input, lost task claims, moderation races, network failures, serializer validation, and production configuration guards. Exercise migration downgrade, backfill, and autogeneration paths plus additional receiver and sender edge cases.
Diffstat (limited to 'tests/test_net.py')
-rw-r--r--tests/test_net.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/test_net.py b/tests/test_net.py
index 6ff267d..ec62472 100644
--- a/tests/test_net.py
+++ b/tests/test_net.py
@@ -6,6 +6,8 @@
import ipaddress
import socket
+
+import dns.exception
from http import HTTPStatus
from unittest.mock import Mock, PropertyMock, patch
@@ -358,3 +360,75 @@ def test_is_retryable_status(app: Flask, status: int, expected: bool) -> None:
)
def test_media_type_of(headers: dict[str, str], expected: str) -> None:
assert media_type_of(httpx.Headers(headers)) == expected
+
+
+@pytest.mark.parametrize("seconds", [0, -0.1])
+def test_request_deadline_requires_positive_timeout(seconds: float) -> None:
+ with pytest.raises(ValueError, match="REQUEST_TOTAL_TIMEOUT_SECONDS"):
+ RequestDeadline(seconds)
+
+
+def test_deadline_stream_bounds_write_and_tls_timeouts() -> None:
+ stream = Mock(spec=httpcore.NetworkStream)
+ stream.start_tls.return_value = Mock(spec=httpcore.NetworkStream)
+ deadline = RequestDeadline(30)
+ wrapper = DeadlineStream(stream, deadline)
+
+ wrapper.write(b"payload", timeout=120)
+ written_timeout = stream.write.call_args.kwargs["timeout"]
+ assert 0 < written_timeout <= 30
+
+ secure = wrapper.start_tls(Mock(), server_hostname="example.org", timeout=120)
+ assert isinstance(secure, DeadlineStream)
+ assert secure._deadline is deadline
+ assert stream.start_tls.call_args.kwargs["server_hostname"] == "example.org"
+ tls_timeout = stream.start_tls.call_args.kwargs["timeout"]
+ assert 0 < tls_timeout <= 30
+
+
+def test_network_backend_applies_local_bind_and_socket_options(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ sock = Mock()
+ monkeypatch.setattr(socket, "socket", Mock(return_value=sock))
+ options = [(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)]
+
+ stream = PublicNetworkBackend(RequestDeadline(5)).connect_tcp(
+ "8.8.8.8", 443, local_address="0.0.0.0", socket_options=options
+ )
+
+ sock.bind.assert_called_once_with(("0.0.0.0", 0))
+ sock.setsockopt.assert_any_call(*options[0])
+ sock.connect.assert_called_once_with(("8.8.8.8", 443))
+ stream.close()
+ sock.close.assert_called_once()
+
+
+def test_network_backend_rejects_empty_public_address_list(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr(
+ "webmentions_ssg.net.resolve_public_addresses", lambda *args, **kwargs: ()
+ )
+ create_socket = Mock(side_effect=AssertionError("must not open socket"))
+ monkeypatch.setattr(socket, "socket", create_socket)
+
+ with pytest.raises(httpcore.ConnectError, match="No public addresses available"):
+ PublicNetworkBackend(RequestDeadline(5)).connect_tcp("example.org", 443)
+ create_socket.assert_not_called()
+
+
+@pytest.mark.parametrize(
+ "failure", [ValueError("invalid"), dns.exception.DNSException("lookup failed")]
+)
+def test_dns_resolution_errors_are_wrapped(
+ monkeypatch: pytest.MonkeyPatch, failure: Exception
+) -> None:
+ from webmentions_ssg.net import resolve_hostname
+
+ resolver = Mock(side_effect=failure)
+ monkeypatch.setattr("webmentions_ssg.net.dns.resolver.resolve_name", resolver)
+
+ with pytest.raises(AddressResolutionError, match="Could not resolve hostname"):
+ resolve_hostname("missing.example", lifetime=1.5)
+ resolver.assert_called_once_with("missing.example", lifetime=1.5)