# SPDX-FileCopyrightText: 2026 Dennis Fink # # 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 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