aboutsummaryrefslogtreecommitdiff
path: root/tests/views
diff options
context:
space:
mode:
authorDennis Fink2026-11-17 17:09:59 +0100
committerDennis Fink2026-11-17 17:09:59 +0100
commit6d6d079a3a99d3d3a922a640b6925eb2adf044de (patch)
tree33cc19241f4ce86ea43b2f194087c58e3fdc8a4d /tests/views
parent227092b57acc886469e16bcc4c0d3256e86d3d1e (diff)
downloadwebmentions-ssg-6d6d079a3a99d3d3a922a640b6925eb2adf044de.tar.gz
webmentions-ssg-6d6d079a3a99d3d3a922a640b6925eb2adf044de.zip
refactor(core)!: split forms and models by feature
Replace monolithic form and model modules with focused packages for receiver, moderation, source, user, and shared validation concerns. Rename source policy concepts to domain moderation, including routes, models, tables, configuration, templates, and tests. BREAKING CHANGE: form and model import paths, moderation routes, and `SOURCE_ALLOWLIST_ENABLED` have changed; use `ALLOWLIST_ENABLED` and the new feature-specific modules.
Diffstat (limited to 'tests/views')
-rw-r--r--tests/views/helpers.py14
-rw-r--r--tests/views/test_endpoint.py2
-rw-r--r--tests/views/test_moderation.py (renamed from tests/views/test_source_domains.py)75
-rw-r--r--tests/views/test_received.py (renamed from tests/views/test_admin.py)235
-rw-r--r--tests/views/test_session.py2
-rw-r--r--tests/views/test_sources.py253
6 files changed, 304 insertions, 277 deletions
diff --git a/tests/views/helpers.py b/tests/views/helpers.py
index 354102b..2bea73e 100644
--- a/tests/views/helpers.py
+++ b/tests/views/helpers.py
@@ -13,13 +13,13 @@ from flask import Flask
from flask.testing import FlaskClient
from webmentions_ssg import db
-from webmentions_ssg.models import (
- ReceivedWebmention,
+from webmentions_ssg.models.received import ReceivedWebmention
+from webmentions_ssg.models.sources import (
SentWebmention,
SentWebmentionStatus,
SourceDocument,
- User,
)
+from webmentions_ssg.models.user import User
SOURCE_URL = "https://source.example/post"
TARGET_URL = "https://dennisfink.me/blog/example/"
@@ -41,7 +41,7 @@ def mock_endpoint_form(
form.errors = errors or {}
monkeypatch.setattr(
- views_module.forms, "WebmentionSubmissionForm", Mock(return_value=form)
+ views_module, "WebmentionSubmissionForm", Mock(return_value=form)
)
return form
@@ -118,7 +118,7 @@ def mock_login_form(
form.username.data = username
form.password.data = password
- monkeypatch.setattr(views_module.forms, "LoginForm", Mock(return_value=form))
+ monkeypatch.setattr(views_module, "LoginForm", Mock(return_value=form))
return form
@@ -128,9 +128,7 @@ def mock_admin_action_form(
form = Mock()
form.validate_on_submit.return_value = valid
- monkeypatch.setattr(
- views_module.forms, "ConfirmActionForm", Mock(return_value=form)
- )
+ monkeypatch.setattr(views_module, "ConfirmActionForm", Mock(return_value=form))
return form
diff --git a/tests/views/test_endpoint.py b/tests/views/test_endpoint.py
index 88b665f..23643d9 100644
--- a/tests/views/test_endpoint.py
+++ b/tests/views/test_endpoint.py
@@ -15,7 +15,7 @@ from flask.testing import FlaskClient
from sqlalchemy.exc import IntegrityError
from webmentions_ssg import db
-from webmentions_ssg.models import ReceivedWebmention
+from webmentions_ssg.models.received import ReceivedWebmention
from .helpers import (
SOURCE_URL,
diff --git a/tests/views/test_source_domains.py b/tests/views/test_moderation.py
index c9e72b2..7f683d4 100644
--- a/tests/views/test_source_domains.py
+++ b/tests/views/test_moderation.py
@@ -15,12 +15,9 @@ from flask import Flask
from flask.testing import FlaskClient
from webmentions_ssg import db
-from webmentions_ssg.models import (
- AllowedSource,
- BlockedSource,
- ReceivedWebmention,
- ReceivedWebmentionStatus,
-)
+from webmentions_ssg.models.moderation import AllowedDomain, BlockedDomain
+from webmentions_ssg.models.received import ReceivedWebmention, ReceivedWebmentionStatus
+from webmentions_ssg.moderation_policy import rejection_reason
from webmentions_ssg.tasks.verification import VerificationClaim
from .helpers import SOURCE_URL, TARGET_URL, create_received_webmention, log_in
@@ -28,9 +25,9 @@ from .helpers import SOURCE_URL, TARGET_URL, create_received_webmention, log_in
def test_source_rules_require_login(client: FlaskClient) -> None:
for method, path, data in (
- ("get", "/source-domains", None),
- ("post", "/source-domains/blocked/add", {"domain": "bad.example"}),
- ("post", "/source-domains/blocked/1/delete", None),
+ ("get", "/moderation", None),
+ ("post", "/moderation/blocked/add", {"domain": "bad.example"}),
+ ("post", "/moderation/blocked/1/delete", None),
):
response = client.open(path, method=method.upper(), data=data)
assert response.status_code == HTTPStatus.FOUND
@@ -39,31 +36,29 @@ def test_source_rules_require_login(client: FlaskClient) -> None:
def test_add_and_unblock_source_domain(app: Flask, client: FlaskClient) -> None:
log_in(app, client)
- response = client.post(
- "/source-domains/blocked/add", data={"domain": "SPAM.Example."}
- )
+ response = client.post("/moderation/blocked/add", data={"domain": "SPAM.Example."})
assert response.status_code == HTTPStatus.FOUND
- response = client.post(
- "/source-domains/blocked/add", data={"domain": "spam.example"}
- )
+ assert response.headers["Location"].endswith("/moderation")
+ response = client.post("/moderation/blocked/add", data={"domain": "spam.example"})
assert response.status_code == HTTPStatus.FOUND
with app.app_context():
- rows = db.session.scalars(sa.select(BlockedSource)).all()
+ rows = db.session.scalars(sa.select(BlockedDomain)).all()
assert len(rows) == 1
assert rows[0].domain == "spam.example"
entry_id = rows[0].id
- response = client.get("/source-domains")
+ response = client.get("/moderation")
assert response.status_code == HTTPStatus.OK
assert b"spam.example" in response.data
assert b"Unblock" in response.data
assert b"Source rules" in response.data
- response = client.post(f"/source-domains/blocked/{entry_id}/delete")
+ response = client.post(f"/moderation/blocked/{entry_id}/delete")
assert response.status_code == HTTPStatus.FOUND
+ assert response.headers["Location"].endswith("/moderation")
with app.app_context():
- assert db.session.scalar(sa.select(sa.func.count(BlockedSource.id))) == 0
+ assert db.session.scalar(sa.select(sa.func.count(BlockedDomain.id))) == 0
def test_allowlist_can_be_managed_while_mode_is_off(
@@ -71,14 +66,14 @@ def test_allowlist_can_be_managed_while_mode_is_off(
) -> None:
log_in(app, client)
response = client.post(
- "/source-domains/allowed/add", data={"domain": "friends.example"}
+ "/moderation/allowed/add", data={"domain": "friends.example"}
)
assert response.status_code == HTTPStatus.FOUND
with app.app_context():
- row = db.session.scalar(sa.select(AllowedSource))
+ row = db.session.scalar(sa.select(AllowedDomain))
assert row is not None
assert row.domain == "friends.example"
- assert b"disabled" in client.get("/source-domains").data
+ assert b"disabled" in client.get("/moderation").data
@pytest.mark.parametrize(
@@ -88,17 +83,17 @@ def test_invalid_domains_are_rejected(
client: FlaskClient, app: Flask, domain: str
) -> None:
log_in(app, client)
- response = client.post("/source-domains/blocked/add", data={"domain": domain})
+ response = client.post("/moderation/blocked/add", data={"domain": domain})
assert response.status_code == HTTPStatus.BAD_REQUEST
def test_invalid_list_kind_and_missing_entry(app: Flask, client: FlaskClient) -> None:
log_in(app, client)
assert (
- client.post("/source-domains/nope/add", data={"domain": "test.org"}).status_code
+ client.post("/moderation/nope/add", data={"domain": "test.org"}).status_code
== 404
)
- assert client.post("/source-domains/blocked/123/delete").status_code == 404
+ assert client.post("/moderation/blocked/123/delete").status_code == 404
def test_received_row_can_block_domain(app: Flask, client: FlaskClient) -> None:
@@ -109,7 +104,7 @@ def test_received_row_can_block_domain(app: Flask, client: FlaskClient) -> None:
response = client.post(f"/received/{mention_id}/block-source")
assert response.status_code == HTTPStatus.FOUND
with app.app_context():
- rule = db.session.scalar(sa.select(BlockedSource))
+ rule = db.session.scalar(sa.select(BlockedDomain))
assert rule is not None and rule.domain == "spam.example"
@@ -126,7 +121,7 @@ def test_block_selected_source_domains_deduplicates_and_preserves_mentions(
second = create_received_webmention(app, source="https://second.example/article")
unselected = create_received_webmention(app, source="https://other.example/article")
with app.app_context():
- db.session.add(BlockedSource(domain="second.example"))
+ db.session.add(BlockedDomain(domain="second.example"))
db.session.commit()
response = client.post(
@@ -142,19 +137,17 @@ def test_block_selected_source_domains_deduplicates_and_preserves_mentions(
"sort": ["source"],
}
with app.app_context():
- assert set(db.session.scalars(sa.select(BlockedSource.domain))) == {
+ assert set(db.session.scalars(sa.select(BlockedDomain.domain))) == {
"spam.example",
"second.example",
}
for identifier in (first, same_domain, second, unselected):
assert db.session.get(ReceivedWebmention, identifier) is not None
- from webmentions_ssg.source_policy import source_rejection_reason
-
assert (
- source_rejection_reason("https://child.spam.example/post")
+ rejection_reason("https://child.spam.example/post")
== "Source domain is blocked"
)
- assert source_rejection_reason("https://other.example/post") is None
+ assert rejection_reason("https://other.example/post") is None
# Repeating the action is idempotent.
response = client.post(
@@ -163,7 +156,7 @@ def test_block_selected_source_domains_deduplicates_and_preserves_mentions(
)
assert response.status_code == HTTPStatus.FOUND
with app.app_context():
- assert db.session.scalar(sa.select(sa.func.count(BlockedSource.id))) == 2
+ assert db.session.scalar(sa.select(sa.func.count(BlockedDomain.id))) == 2
def test_bulk_block_rejects_invalid_source_without_partial_changes(
@@ -179,7 +172,7 @@ def test_bulk_block_rejects_invalid_source_without_partial_changes(
)
assert response.status_code == HTTPStatus.BAD_REQUEST
with app.app_context():
- assert db.session.scalar(sa.select(sa.func.count(BlockedSource.id))) == 0
+ assert db.session.scalar(sa.select(sa.func.count(BlockedDomain.id))) == 0
def test_bulk_block_action_enforces_csrf(app: Flask, client: FlaskClient) -> None:
@@ -192,7 +185,7 @@ def test_bulk_block_action_enforces_csrf(app: Flask, client: FlaskClient) -> Non
)
assert response.status_code == HTTPStatus.BAD_REQUEST
with app.app_context():
- assert db.session.scalar(sa.select(sa.func.count(BlockedSource.id))) == 0
+ assert db.session.scalar(sa.select(sa.func.count(BlockedDomain.id))) == 0
def test_endpoint_rejects_blocked_without_queue_or_insert(
@@ -203,7 +196,7 @@ def test_endpoint_rejects_blocked_without_queue_or_insert(
enqueue = Mock()
monkeypatch.setattr(endpoint, "verify_webmention", enqueue)
with app.app_context():
- db.session.add(BlockedSource(domain="source.example"))
+ db.session.add(BlockedDomain(domain="source.example"))
db.session.commit()
response = client.post(
@@ -221,14 +214,14 @@ def test_allowlist_mode_rejects_unlisted_and_allows_listed(
) -> None:
from webmentions_ssg.views import endpoint
- app.config["SOURCE_ALLOWLIST_ENABLED"] = True
+ app.config["ALLOWLIST_ENABLED"] = True
enqueue = Mock()
monkeypatch.setattr(endpoint, "verify_webmention", enqueue)
data = {"source": SOURCE_URL, "target": TARGET_URL}
assert client.post("/endpoint", data=data).status_code == HTTPStatus.FORBIDDEN
with app.app_context():
- db.session.add(AllowedSource(domain="source.example"))
+ db.session.add(AllowedDomain(domain="source.example"))
db.session.commit()
assert client.post("/endpoint", data=data).status_code == HTTPStatus.CREATED
@@ -249,7 +242,7 @@ def test_worker_checks_rules_before_fetch(
fetch = Mock()
monkeypatch.setattr(verification, "source_mentions_target", fetch)
with app.app_context():
- db.session.add(BlockedSource(domain="source.example"))
+ db.session.add(BlockedDomain(domain="source.example"))
db.session.commit()
assert verification.determine_verification_outcome(claim) == (
ReceivedWebmentionStatus.FAILED,
@@ -272,7 +265,7 @@ def test_queued_worker_skips_fetch_after_source_is_blocked(
identifier = mention.uuid
# A user blocks the domain after the submission has already been queued.
- db.session.add(BlockedSource(domain="source.example"))
+ db.session.add(BlockedDomain(domain="source.example"))
db.session.commit()
fetch = Mock()
@@ -291,7 +284,7 @@ def test_admin_actions_enforce_csrf(app: Flask, client: FlaskClient) -> None:
app.config["WTF_CSRF_ENABLED"] = True
assert (
client.post(
- "/source-domains/blocked/add", data={"domain": "spam.example"}
+ "/moderation/blocked/add", data={"domain": "spam.example"}
).status_code
== HTTPStatus.BAD_REQUEST
)
diff --git a/tests/views/test_admin.py b/tests/views/test_received.py
index 175bcfd..ec00e05 100644
--- a/tests/views/test_admin.py
+++ b/tests/views/test_received.py
@@ -17,15 +17,12 @@ from flask import Flask
from flask.testing import FlaskClient
from webmentions_ssg import db
-from webmentions_ssg.models import ReceivedWebmention, SentWebmention, User
+from webmentions_ssg.models.received import ReceivedWebmention
from .helpers import (
SOURCE_URL,
TARGET_URL,
- create_outgoing,
create_received_webmention,
- create_resendable_sent_webmention,
- create_sent_source,
log_in,
mock_admin_action_form,
)
@@ -34,66 +31,11 @@ from .helpers import (
@pytest.fixture
def views_module(app: Flask) -> ModuleType:
_ = app
- from webmentions_ssg.views import admin as module
+ from webmentions_ssg.views import received as module
return module
-@pytest.mark.parametrize("path", ["/sources", f"/sources/{uuid.uuid7()}"])
-def test_sent_views_require_login(client: FlaskClient, path: str) -> None:
- response = client.get(path)
-
- assert response.status_code == HTTPStatus.FOUND
- assert "/login" in response.headers["Location"]
-
-
-def test_sent_lists_sources(app: Flask, client: FlaskClient) -> None:
- log_in(app, client)
- identifier = create_sent_source(app)
-
- response = client.get("/sources")
-
- assert response.status_code == HTTPStatus.OK
-
- html = response.get_data(as_text=True)
-
- assert "blog/example/index.html" in html
- assert "https://dennisfink.me/blog/example/" in html
- assert f"/sources/{identifier}" in html
-
-
-def test_sent_source_lists_webmentions(app: Flask, client: FlaskClient) -> None:
- log_in(app, client)
- identifier = create_sent_source(app)
-
- response = client.get(f"/sources/{identifier}")
-
- assert response.status_code == HTTPStatus.OK
-
- html = response.get_data(as_text=True)
-
- assert "blog/example/index.html" in html
- assert "https://dennisfink.me/blog/example/" in html
-
- first_target = "https://example.com/a"
- second_target = "https://example.com/b"
-
- assert first_target in html
- assert second_target in html
- assert html.index(first_target) < html.index(second_target)
-
- assert "Webmention endpoint returned HTTP 400" in html
- assert "https://example.com/webmention" in html
-
-
-def test_sent_source_returns_404_for_unknown_source(
- app: Flask, client: FlaskClient
-) -> None:
- log_in(app, client)
- response = client.get(f"/sources/{uuid.uuid7()}")
- assert response.status_code == HTTPStatus.NOT_FOUND
-
-
def test_received_requires_login(client: FlaskClient) -> None:
response = client.get("/received")
@@ -471,7 +413,13 @@ def test_received_filters_are_combined(app: Flask, client: FlaskClient) -> None:
)
response = client.get(
- "/received?status=declined&q=match.example&date_from=2026-11-08&date_to=2026-11-14"
+ "/received",
+ query_string={
+ "status": "declined",
+ "q": "match.example",
+ "date_from": "2026-11-08",
+ "date_to": "2026-11-14",
+ },
)
html = response.get_data(as_text=True)
@@ -1113,168 +1061,3 @@ def test_received_bulk_delete_requires_real_csrf_token(
assert valid.status_code == HTTPStatus.FOUND
with app.app_context():
assert db.session.get(ReceivedWebmention, identifier) is None
-
-
-def test_rescan_sent_sources_requires_login(client: FlaskClient) -> None:
- response = client.post("/sources/rescan")
-
- assert response.status_code == HTTPStatus.FOUND
- assert "/login" in response.headers["Location"]
-
-
-@pytest.mark.parametrize("valid", [False, True])
-def test_rescan_sent_sources(
- app: Flask,
- client: FlaskClient,
- views_module: ModuleType,
- monkeypatch: pytest.MonkeyPatch,
- valid: bool,
-) -> None:
- log_in(app, client)
- form = mock_admin_action_form(views_module, monkeypatch, valid=valid)
- manual_scan_sources = Mock()
- monkeypatch.setattr(views_module, "manual_scan_sources", manual_scan_sources)
-
- response = client.post("/sources/rescan")
-
- assert response.status_code == (
- HTTPStatus.FOUND if valid else HTTPStatus.BAD_REQUEST
- )
- form.validate_on_submit.assert_called_once_with()
-
- if valid:
- assert response.headers["Location"].endswith("/sources")
- manual_scan_sources.assert_called_once_with()
- else:
- manual_scan_sources.assert_not_called()
-
-
-def test_resend_sent_webmention_requires_login(client: FlaskClient) -> None:
- response = client.post(f"/sources/{uuid.uuid7()}/{uuid.uuid7()}/resend")
-
- assert response.status_code == HTTPStatus.FOUND
- assert "/login" in response.headers["Location"]
-
-
-def test_resend_sent_webmention_rejects_invalid_form(
- app: Flask,
- client: FlaskClient,
- views_module: ModuleType,
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
- log_in(app, client)
- source_identifier, webmention_identifier = create_resendable_sent_webmention(app)
- mock_admin_action_form(views_module, monkeypatch, valid=False)
-
- response = client.post(
- f"/sources/{source_identifier}/{webmention_identifier}/resend"
- )
-
- assert response.status_code == HTTPStatus.BAD_REQUEST
-
-
-def test_resend_sent_webmention_returns_404_for_unknown_webmention(
- app: Flask,
- client: FlaskClient,
- views_module: ModuleType,
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
- log_in(app, client)
- source_identifier = create_sent_source(app)
- mock_admin_action_form(views_module, monkeypatch, valid=True)
-
- response = client.post(f"/sources/{source_identifier}/{uuid.uuid7()}/resend")
-
- assert response.status_code == HTTPStatus.NOT_FOUND
-
-
-def test_resend_sent_webmention_rejects_wrong_source(
- app: Flask,
- client: FlaskClient,
- views_module: ModuleType,
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
- log_in(app, client)
- _, webmention_identifier = create_resendable_sent_webmention(app)
- wrong_source_identifier = create_sent_source(app)
- mock_admin_action_form(views_module, monkeypatch, valid=True)
-
- response = client.post(
- f"/sources/{wrong_source_identifier}/{webmention_identifier}/resend"
- )
-
- assert response.status_code == HTTPStatus.NOT_FOUND
-
-
-def test_resend_sent_webmention_reopens_current_revision(
- app: Flask,
- client: FlaskClient,
- views_module: ModuleType,
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
- log_in(app, client)
- source_identifier, webmention_identifier = create_resendable_sent_webmention(app)
- mock_admin_action_form(views_module, monkeypatch, valid=True)
- send_webmention = Mock()
- monkeypatch.setattr(views_module, "send_webmention", send_webmention)
-
- with app.app_context():
- webmention = db.session.get(SentWebmention, webmention_identifier)
- assert webmention is not None
- webmention.attempt_count = 6
- webmention.next_retry_at = datetime.now(UTC) + timedelta(hours=24)
- db.session.commit()
-
- response = client.post(
- f"/sources/{source_identifier}/{webmention_identifier}/resend"
- )
-
- assert response.status_code == HTTPStatus.FOUND
- assert response.headers["Location"].endswith(f"/sources/{source_identifier}")
-
- with app.app_context():
- webmention = db.session.get(SentWebmention, webmention_identifier)
- assert webmention is not None
- assert webmention.desired_revision == 3
- assert webmention.processed_revision is None
- assert webmention.attempt_count == 0
- assert webmention.next_retry_at is None
- assert webmention.sent_revision == 2
- assert webmention.status is None
- assert webmention.failure_reason is None
- assert webmention.webmention_endpoint is None
- assert webmention.endpoint_response_status is None
- assert webmention.status_url is None
-
- send_webmention.assert_called_once_with(webmention_identifier)
-
-
-@pytest.mark.parametrize(
- ("status_url", "safe"),
- [
- ("https://example.com/status", True),
- ("http://example.com/status", True),
- ("javascript:alert(1)", False),
- ("data:text/html,evil", False),
- ("ftp://example.com/status", False),
- ],
-)
-def test_admin_only_links_safe_existing_status_urls(
- app: Flask, client: FlaskClient, status_url: str, safe: bool
-) -> None:
- source_identifier, _ = create_outgoing(app, status_url=status_url)
- 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
-
- response = client.get(f"/sources/{source_identifier}")
- assert response.status_code == HTTPStatus.OK
- html = response.get_data(as_text=True)
- assert (f'href="{status_url}"' in html) is safe
- assert status_url in html
diff --git a/tests/views/test_session.py b/tests/views/test_session.py
index a2d3268..7edee71 100644
--- a/tests/views/test_session.py
+++ b/tests/views/test_session.py
@@ -10,7 +10,7 @@ from flask import Flask
from flask.testing import FlaskClient
from webmentions_ssg import db
-from webmentions_ssg.models import User
+from webmentions_ssg.models.user import User
from .helpers import log_in, mock_login_form
diff --git a/tests/views/test_sources.py b/tests/views/test_sources.py
new file mode 100644
index 0000000..012347e
--- /dev/null
+++ b/tests/views/test_sources.py
@@ -0,0 +1,253 @@
+# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+import uuid
+from datetime import UTC, datetime, timedelta
+from http import HTTPStatus
+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.sources import SentWebmention
+from webmentions_ssg.models.user import User
+
+from .helpers import (
+ create_outgoing,
+ create_resendable_sent_webmention,
+ create_sent_source,
+ log_in,
+ mock_admin_action_form,
+)
+
+
+@pytest.fixture
+def sources_module(app: Flask) -> ModuleType:
+ _ = app
+ from webmentions_ssg.views import sources as module
+
+ return module
+
+
+@pytest.mark.parametrize("path", ["/sources", f"/sources/{uuid.uuid7()}"])
+def test_sent_views_require_login(client: FlaskClient, path: str) -> None:
+ response = client.get(path)
+
+ assert response.status_code == HTTPStatus.FOUND
+ assert "/login" in response.headers["Location"]
+
+
+def test_sent_lists_sources(app: Flask, client: FlaskClient) -> None:
+ log_in(app, client)
+ identifier = create_sent_source(app)
+
+ response = client.get("/sources")
+
+ assert response.status_code == HTTPStatus.OK
+
+ html = response.get_data(as_text=True)
+
+ assert "blog/example/index.html" in html
+ assert "https://dennisfink.me/blog/example/" in html
+ assert f"/sources/{identifier}" in html
+
+
+def test_sent_source_lists_webmentions(app: Flask, client: FlaskClient) -> None:
+ log_in(app, client)
+ identifier = create_sent_source(app)
+
+ response = client.get(f"/sources/{identifier}")
+
+ assert response.status_code == HTTPStatus.OK
+
+ html = response.get_data(as_text=True)
+
+ assert "blog/example/index.html" in html
+ assert "https://dennisfink.me/blog/example/" in html
+
+ first_target = "https://example.com/a"
+ second_target = "https://example.com/b"
+
+ assert first_target in html
+ assert second_target in html
+ assert html.index(first_target) < html.index(second_target)
+
+ assert "Webmention endpoint returned HTTP 400" in html
+ assert "https://example.com/webmention" in html
+
+
+def test_sent_source_returns_404_for_unknown_source(
+ app: Flask, client: FlaskClient
+) -> None:
+ log_in(app, client)
+ response = client.get(f"/sources/{uuid.uuid7()}")
+ assert response.status_code == HTTPStatus.NOT_FOUND
+
+
+def test_rescan_sent_sources_requires_login(client: FlaskClient) -> None:
+ response = client.post("/sources/rescan")
+
+ assert response.status_code == HTTPStatus.FOUND
+ assert "/login" in response.headers["Location"]
+
+
+@pytest.mark.parametrize("valid", [False, True])
+def test_rescan_sent_sources(
+ app: Flask,
+ client: FlaskClient,
+ sources_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+ valid: bool,
+) -> None:
+ log_in(app, client)
+ form = mock_admin_action_form(sources_module, monkeypatch, valid=valid)
+ manual_scan_sources = Mock()
+ monkeypatch.setattr(sources_module, "manual_scan_sources", manual_scan_sources)
+
+ response = client.post("/sources/rescan")
+
+ assert response.status_code == (
+ HTTPStatus.FOUND if valid else HTTPStatus.BAD_REQUEST
+ )
+ form.validate_on_submit.assert_called_once_with()
+
+ if valid:
+ assert response.headers["Location"].endswith("/sources")
+ manual_scan_sources.assert_called_once_with()
+ else:
+ manual_scan_sources.assert_not_called()
+
+
+def test_resend_sent_webmention_requires_login(client: FlaskClient) -> None:
+ response = client.post(f"/sources/{uuid.uuid7()}/{uuid.uuid7()}/resend")
+
+ assert response.status_code == HTTPStatus.FOUND
+ assert "/login" in response.headers["Location"]
+
+
+def test_resend_sent_webmention_rejects_invalid_form(
+ app: Flask,
+ client: FlaskClient,
+ sources_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ log_in(app, client)
+ source_identifier, webmention_identifier = create_resendable_sent_webmention(app)
+ mock_admin_action_form(sources_module, monkeypatch, valid=False)
+
+ response = client.post(
+ f"/sources/{source_identifier}/{webmention_identifier}/resend"
+ )
+
+ assert response.status_code == HTTPStatus.BAD_REQUEST
+
+
+def test_resend_sent_webmention_returns_404_for_unknown_webmention(
+ app: Flask,
+ client: FlaskClient,
+ sources_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ log_in(app, client)
+ source_identifier = create_sent_source(app)
+ mock_admin_action_form(sources_module, monkeypatch, valid=True)
+
+ response = client.post(f"/sources/{source_identifier}/{uuid.uuid7()}/resend")
+
+ assert response.status_code == HTTPStatus.NOT_FOUND
+
+
+def test_resend_sent_webmention_rejects_wrong_source(
+ app: Flask,
+ client: FlaskClient,
+ sources_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ log_in(app, client)
+ _, webmention_identifier = create_resendable_sent_webmention(app)
+ wrong_source_identifier = create_sent_source(app)
+ mock_admin_action_form(sources_module, monkeypatch, valid=True)
+
+ response = client.post(
+ f"/sources/{wrong_source_identifier}/{webmention_identifier}/resend"
+ )
+
+ assert response.status_code == HTTPStatus.NOT_FOUND
+
+
+def test_resend_sent_webmention_reopens_current_revision(
+ app: Flask,
+ client: FlaskClient,
+ sources_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ log_in(app, client)
+ source_identifier, webmention_identifier = create_resendable_sent_webmention(app)
+ mock_admin_action_form(sources_module, monkeypatch, valid=True)
+ send_webmention = Mock()
+ monkeypatch.setattr(sources_module, "send_webmention", send_webmention)
+
+ with app.app_context():
+ webmention = db.session.get(SentWebmention, webmention_identifier)
+ assert webmention is not None
+ webmention.attempt_count = 6
+ webmention.next_retry_at = datetime.now(UTC) + timedelta(hours=24)
+ db.session.commit()
+
+ response = client.post(
+ f"/sources/{source_identifier}/{webmention_identifier}/resend"
+ )
+
+ assert response.status_code == HTTPStatus.FOUND
+ assert response.headers["Location"].endswith(f"/sources/{source_identifier}")
+
+ with app.app_context():
+ webmention = db.session.get(SentWebmention, webmention_identifier)
+ assert webmention is not None
+ assert webmention.desired_revision == 3
+ assert webmention.processed_revision is None
+ assert webmention.attempt_count == 0
+ assert webmention.next_retry_at is None
+ assert webmention.sent_revision == 2
+ assert webmention.status is None
+ assert webmention.failure_reason is None
+ assert webmention.webmention_endpoint is None
+ assert webmention.endpoint_response_status is None
+ assert webmention.status_url is None
+
+ send_webmention.assert_called_once_with(webmention_identifier)
+
+
+@pytest.mark.parametrize(
+ ("status_url", "safe"),
+ [
+ ("https://example.com/status", True),
+ ("http://example.com/status", True),
+ ("javascript:alert(1)", False),
+ ("data:text/html,evil", False),
+ ("ftp://example.com/status", False),
+ ],
+)
+def test_admin_only_links_safe_existing_status_urls(
+ app: Flask, client: FlaskClient, status_url: str, safe: bool
+) -> None:
+ source_identifier, _ = create_outgoing(app, status_url=status_url)
+ 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
+
+ response = client.get(f"/sources/{source_identifier}")
+ assert response.status_code == HTTPStatus.OK
+ html = response.get_data(as_text=True)
+ assert (f'href="{status_url}"' in html) is safe
+ assert status_url in html