aboutsummaryrefslogtreecommitdiff
path: root/tests/views/helpers.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-12 19:57:08 +0100
committerDennis Fink2026-11-12 19:57:08 +0100
commit4bd1d9e348918b2e55db47a1ac3489d035cf55e2 (patch)
treef7ed41a374a8a4136792039a262dc62aa8cf8c59 /tests/views/helpers.py
parent7035e9f9467684cccae2b7ff3faaa62e6a9f91aa (diff)
downloadwebmentions-ssg-4bd1d9e348918b2e55db47a1ac3489d035cf55e2.tar.gz
webmentions-ssg-4bd1d9e348918b2e55db47a1ac3489d035cf55e2.zip
refactor(core): split modules by responsibility
Break up monolithic views, task helpers, networking, CLI, and application extension setup so each module owns a focused concern. Reorganize the test suite around the new boundaries while preserving existing Webmention behavior and coverage.
Diffstat (limited to '')
-rw-r--r--tests/views/helpers.py210
1 files changed, 210 insertions, 0 deletions
diff --git a/tests/views/helpers.py b/tests/views/helpers.py
new file mode 100644
index 0000000..b32a066
--- /dev/null
+++ b/tests/views/helpers.py
@@ -0,0 +1,210 @@
+# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+import uuid
+from datetime import UTC, datetime
+from types import ModuleType
+from unittest.mock import Mock
+
+import pytest
+from flask import Flask
+from flask.testing import FlaskClient
+
+from webmentions_ssg import DATABASE as db
+from webmentions_ssg.models import (
+ ReceivedWebmention,
+ SentWebmention,
+ SentWebmentionStatus,
+ Source,
+ User,
+)
+
+SOURCE_URL = "https://source.example/post"
+TARGET_URL = "https://dennisfink.me/blog/example/"
+
+
+def mock_endpoint_form(
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+ *,
+ valid: bool,
+ source: str = SOURCE_URL,
+ target: str = TARGET_URL,
+ errors: dict[str, list[str]] | None = None,
+) -> Mock:
+ form = Mock()
+ form.validate_on_submit.return_value = valid
+ form.source.data = source
+ form.target.data = target
+ form.errors = errors or {}
+
+ monkeypatch.setattr(views_module.forms, "EndpointForm", Mock(return_value=form))
+ return form
+
+
+def log_in(app: Flask, client: FlaskClient) -> None:
+ with app.app_context():
+ user = User(username="admin", password="test")
+
+ db.session.add(user)
+ db.session.commit()
+
+ user_id = user.get_id()
+
+ with client.session_transaction() as session:
+ session["_user_id"] = str(user_id)
+ session["_fresh"] = True
+
+
+def create_sent_source(app: Flask) -> uuid.UUID:
+ now = datetime.now(UTC)
+
+ with app.app_context():
+ source = Source(
+ path="blog/example/index.html",
+ url="https://dennisfink.me/blog/example/",
+ content_hash="0" * 32,
+ revision=2,
+ last_seen_at=now,
+ revised_at=now,
+ )
+
+ source.sent_webmentions.extend(
+ [
+ SentWebmention(
+ target="https://example.com/b",
+ active=True,
+ desired_revision=2,
+ processed_revision=2,
+ sent_revision=2,
+ status=SentWebmentionStatus.SENT,
+ endpoint="https://example.com/webmention",
+ response_status=202,
+ ),
+ SentWebmention(
+ target="https://example.com/a",
+ active=True,
+ desired_revision=2,
+ processed_revision=2,
+ sent_revision=1,
+ status=SentWebmentionStatus.FAILED,
+ failure_reason="Webmention endpoint returned HTTP 400",
+ endpoint="https://example.com/webmention",
+ response_status=400,
+ ),
+ ]
+ )
+
+ db.session.add(source)
+ db.session.commit()
+
+ return source.uuid
+
+
+def mock_login_form(
+ views_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+ *,
+ valid: bool,
+ username: str = "admin",
+ password: str | None = "test",
+) -> Mock:
+ form = Mock()
+ form.validate_on_submit.return_value = valid
+ form.username.data = username
+ form.password.data = password
+
+ monkeypatch.setattr(views_module.forms, "LoginForm", Mock(return_value=form))
+ return form
+
+
+def mock_admin_action_form(
+ views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, *, valid: bool
+) -> Mock:
+ form = Mock()
+ form.validate_on_submit.return_value = valid
+
+ monkeypatch.setattr(views_module.forms, "AdminActionForm", Mock(return_value=form))
+ return form
+
+
+def create_received_webmention(
+ app: Flask,
+ *,
+ source: str = SOURCE_URL,
+ target: str = TARGET_URL,
+ status: str = "verified",
+ failure_reason: str | None = None,
+) -> uuid.UUID:
+ with app.app_context():
+ webmention = ReceivedWebmention(
+ uuid=uuid.uuid7(),
+ source=source,
+ target=target,
+ status=status,
+ failure_reason=failure_reason,
+ )
+
+ db.session.add(webmention)
+ db.session.commit()
+
+ return webmention.uuid
+
+
+def create_resendable_sent_webmention(app: Flask) -> tuple[uuid.UUID, uuid.UUID]:
+ now = datetime.now(UTC)
+
+ with app.app_context():
+ source = Source(
+ path="blog/resend/index.html",
+ url="https://dennisfink.me/blog/resend/",
+ content_hash="1" * 32,
+ revision=3,
+ last_seen_at=now,
+ revised_at=now,
+ )
+ webmention = SentWebmention(
+ target="https://example.com/resend",
+ active=True,
+ desired_revision=3,
+ processed_revision=3,
+ sent_revision=2,
+ status=SentWebmentionStatus.FAILED,
+ failure_reason="Previous failure",
+ endpoint="https://example.com/webmention",
+ response_status=500,
+ status_url="https://example.com/webmention/status/123",
+ )
+ source.sent_webmentions.append(webmention)
+
+ db.session.add(source)
+ db.session.commit()
+
+ return source.uuid, webmention.uuid
+
+
+def create_outgoing(
+ app: Flask, *, status_url: str | None = None
+) -> tuple[uuid.UUID, uuid.UUID]:
+ now = datetime.now(UTC)
+ with app.app_context():
+ source = Source(
+ path="blog/post/index.html",
+ url="https://source.example/blog/post",
+ content_hash="0" * 32,
+ revision=1,
+ last_seen_at=now,
+ revised_at=now,
+ )
+ mention = SentWebmention(
+ target="https://example.com/article",
+ active=True,
+ desired_revision=1,
+ status_url=status_url,
+ response_status=201 if status_url is not None else None,
+ )
+ source.sent_webmentions.append(mention)
+ db.session.add(source)
+ db.session.commit()
+ return source.uuid, mention.uuid