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/tasks/test_receiver.py | 627 +++++++++++++++++--------------------------
1 file changed, 242 insertions(+), 385 deletions(-)
(limited to 'tests/tasks/test_receiver.py')
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'