From 5b39f70aa14cac635b6084170e9d0bd3f548b911 Mon Sep 17 00:00:00 2001
From: Dennis Fink
Date: Mon, 10 Aug 2026 20:00:28 +0200
Subject: test: simplify mocks and localize test fixtures
Replace shared HTTPX and DNS fixtures with targeted unittest.mock
patches close to the tests that need them.
Keep conftest focused on application and client setup, while moving
receiver-specific helpers into the receiver test module.
---
tests/conftest.py | 122 +--------
tests/tasks/test_receiver.py | 627 +++++++++++++++++--------------------------
tests/test_forms.py | 12 +-
tests/test_url_security.py | 18 +-
tests/test_views.py | 72 +++--
5 files changed, 289 insertions(+), 562 deletions(-)
diff --git a/tests/conftest.py b/tests/conftest.py
index f1ef4b5..45658b4 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,8 +1,5 @@
-import uuid
-from collections.abc import Callable, Iterator
-from types import ModuleType
+from collections.abc import Iterator
-import httpx
import pytest
from flask import Flask
from flask.testing import FlaskClient
@@ -10,29 +7,10 @@ from flask.testing import FlaskClient
from webmentions_ssg import DATABASE as db
from webmentions_ssg import create_app
from webmentions_ssg.config import TestingConfig
-from webmentions_ssg.models import ReceivedWebmention
-
-HTTPHandler = Callable[
- [httpx.Request],
- httpx.Response,
-]
-
-
-class FakeHostAnswers:
- def __init__(self, *addresses: str):
- self._addresses = addresses
-
- def addresses(self):
- return iter(self._addresses)
@pytest.fixture
def app() -> Iterator[Flask]:
- """
- Create a new Flask application and in-memory database
- for every test.
- """
-
application = create_app(TestingConfig)
with application.app_context():
@@ -49,101 +27,3 @@ def app() -> Iterator[Flask]:
@pytest.fixture
def client(app: Flask) -> FlaskClient:
return app.test_client()
-
-
-@pytest.fixture
-def receiver_module(app: Flask) -> ModuleType:
- """
- Ensure the application and Huey extension are initialized
- before retrieving the tasks module.
- """
-
- from webmentions_ssg.tasks import receiver
-
- return receiver
-
-
-@pytest.fixture
-def views_module(app: Flask) -> ModuleType:
- from webmentions_ssg import views
-
- return views
-
-
-@pytest.fixture
-def make_webmention(
- app: Flask,
-) -> Callable[..., uuid.UUID]:
- def create(
- *,
- source: str = "https://source.example/post",
- target: str = ("https://dennisfink.me/blog/example/"),
- status: str = "received",
- failure_reason: str | None = None,
- ) -> uuid.UUID:
- identifier = uuid.uuid7()
-
- with app.app_context():
- webmention = ReceivedWebmention(
- uuid=identifier,
- source=source,
- target=target,
- status=status,
- failure_reason=failure_reason,
- )
-
- db.session.add(webmention)
- db.session.commit()
-
- return identifier
-
- return create
-
-
-@pytest.fixture
-def install_httpx_mock(
- monkeypatch: pytest.MonkeyPatch,
- receiver_module: ModuleType,
-) -> Callable[[HTTPHandler], None]:
- """
- Replace the HTTPX transport without replacing HTTPX itself.
- """
-
- monkeypatch.setattr(
- receiver_module,
- "ensure_public_url",
- lambda url: None,
- )
-
- real_client = httpx.Client
-
- def install(handler: HTTPHandler) -> None:
- transport = httpx.MockTransport(handler)
-
- def create_client(
- *args,
- **kwargs,
- ) -> httpx.Client:
- return real_client(
- *args,
- transport=transport,
- **kwargs,
- )
-
- monkeypatch.setattr(
- receiver_module.httpx,
- "Client",
- create_client,
- )
-
- return install
-
-
-@pytest.fixture
-def public_dns(
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
- monkeypatch.setattr(
- "webmentions_ssg.url_security.dns.resolver.resolve_name",
- lambda hostname: FakeHostAnswers("93.184.216.34"),
- )
diff --git a/tests/tasks/test_receiver.py b/tests/tasks/test_receiver.py
index 4545121..19630c3 100644
--- a/tests/tasks/test_receiver.py
+++ b/tests/tasks/test_receiver.py
@@ -1,7 +1,7 @@
import logging
import uuid
-from collections.abc import Callable
from types import ModuleType
+from unittest.mock import Mock, patch
import httpx
import pytest
@@ -9,15 +9,45 @@ from flask import Flask
from webmentions_ssg import DATABASE as db
from webmentions_ssg.models import ReceivedWebmention
+from webmentions_ssg.url_security import (
+ AddressResolutionError,
+ NonPublicAddressError,
+)
SOURCE_URL = "https://source.example/article"
TARGET_URL = "https://dennisfink.me/blog/example/"
-ReceivedWebmentionFactory = Callable[..., uuid.UUID]
-HTTPXMockInstaller = Callable[
- [Callable[[httpx.Request], httpx.Response]],
- None,
-]
+
+@pytest.fixture
+def receiver(app: Flask) -> ModuleType:
+ from webmentions_ssg.tasks import receiver
+
+ return receiver
+
+
+def create_webmention(
+ app: Flask,
+ *,
+ source: str = SOURCE_URL,
+ target: str = TARGET_URL,
+ status: str = "received",
+ failure_reason: str | None = None,
+) -> uuid.UUID:
+ identifier = uuid.uuid7()
+
+ with app.app_context():
+ webmention = ReceivedWebmention(
+ uuid=identifier,
+ source=source,
+ target=target,
+ status=status,
+ failure_reason=failure_reason,
+ )
+
+ db.session.add(webmention)
+ db.session.commit()
+
+ return identifier
def get_webmention_state(
@@ -38,257 +68,200 @@ def get_webmention_state(
)
+def set_stream_response(
+ httpx_client: Mock,
+ response: httpx.Response,
+) -> Mock:
+ client = httpx_client.return_value.__enter__.return_value
+ client.stream.return_value.__enter__.return_value = response
+ return client
+
+
@pytest.mark.parametrize(
- (
- "body",
- "source_url",
- "target_url",
- "expected",
- ),
+ ("body", "expected"),
[
pytest.param(
f'Reply',
- SOURCE_URL,
- TARGET_URL,
True,
id="a-href",
),
pytest.param(
f'',
- SOURCE_URL,
- TARGET_URL,
True,
id="area-href",
),
pytest.param(
f'',
- SOURCE_URL,
- TARGET_URL,
True,
id="link-href",
),
pytest.param(
f'
',
- SOURCE_URL,
- TARGET_URL,
True,
id="img-src",
),
pytest.param(
f'',
- SOURCE_URL,
- TARGET_URL,
True,
id="audio-src",
),
pytest.param(
f'',
- SOURCE_URL,
- TARGET_URL,
True,
id="video-src",
),
pytest.param(
- (f''),
- SOURCE_URL,
- TARGET_URL,
+ f'',
True,
id="audio-source-src",
),
pytest.param(
- (f''),
- SOURCE_URL,
- TARGET_URL,
+ f'',
True,
id="video-source-src",
),
pytest.param(
f'',
- SOURCE_URL,
- TARGET_URL,
True,
id="iframe-src",
),
pytest.param(
f'