aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/cli/test_database.py8
-rw-r--r--tests/forms/test_init.py37
-rw-r--r--tests/tasks/test_receiver.py102
-rw-r--r--tests/test_views.py32
4 files changed, 178 insertions, 1 deletions
diff --git a/tests/cli/test_database.py b/tests/cli/test_database.py
index 7095d51..7d325aa 100644
--- a/tests/cli/test_database.py
+++ b/tests/cli/test_database.py
@@ -85,11 +85,15 @@ def test_create_db_builds_schema_and_records_latest_migration(
columns = {
column["name"] for column in inspector.get_columns("sent_webmentions")
}
+ received_columns = {
+ column["name"] for column in inspector.get_columns("received_webmentions")
+ }
received_indexes = {
index["name"] for index in inspector.get_indexes("received_webmentions")
}
assert {"alembic_version", "users", "received_webmentions"} <= table_names
assert {"attempt_count", "next_retry_at"} <= columns
+ assert "recovery_attempts" in received_columns
assert "ix_received_webmentions_status_updated_at" in received_indexes
@@ -247,6 +251,10 @@ def test_upgrade_backfills_portable_digest_for_existing_received_rows(
)
).one()
assert result.status == "verified"
+ recovery_attempts = connection.execute(
+ sa.text("SELECT recovery_attempts FROM received_webmentions")
+ ).scalar_one()
+ assert recovery_attempts == 0
assert result.url_digest == received_url_digest(
result.source, result.target
)
diff --git a/tests/forms/test_init.py b/tests/forms/test_init.py
index 2a68509..42e6a2c 100644
--- a/tests/forms/test_init.py
+++ b/tests/forms/test_init.py
@@ -144,7 +144,7 @@ def test_endpoint_handles_malformed_ipv6_without_crashing(app: Flask) -> None:
meta={"csrf": False},
)
assert not form.validate()
- assert "Invalid input." in form.errors["target"]
+ assert "Invalid URL." in form.errors["target"]
def test_endpoint_form_applies_configurable_url_byte_limit_to_both_fields(
@@ -168,3 +168,38 @@ def test_endpoint_form_applies_configurable_url_byte_limit_to_both_fields(
assert not form.validate()
assert "target" in form.errors
assert any("bytes" in message for message in form.errors["target"])
+
+
+@pytest.mark.parametrize("field_name", ["source", "target"])
+@pytest.mark.parametrize(
+ "invalid_url",
+ [
+ pytest.param("http://ex☃mple.test/", id="invalid-idn"),
+ pytest.param("https://example.com/a\nb", id="newline"),
+ pytest.param("https://example.com/a\rb", id="carriage-return"),
+ pytest.param("https://example.com/\x00b", id="nul"),
+ pytest.param("https://example.com/a b", id="ascii-space"),
+ pytest.param("https://example.com/a\u00a0b", id="unicode-whitespace"),
+ pytest.param("https://example.com/\x7fb", id="del"),
+ ],
+)
+def test_endpoint_rejects_invalid_httpx_urls_and_whitespace(
+ app: Flask, field_name: str, invalid_url: str
+) -> None:
+ data = {"source": VALID_SOURCE, "target": VALID_TARGET}
+ data[field_name] = invalid_url
+ with app.test_request_context("/endpoint", method="POST"):
+ form = EndpointForm(formdata=MultiDict(data), meta={"csrf": False})
+ assert not form.validate()
+ assert "Invalid URL." in form.errors[field_name]
+
+
+def test_endpoint_accepts_encoded_path_space(app: Flask) -> None:
+ with app.test_request_context("/endpoint", method="POST"):
+ form = EndpointForm(
+ formdata=MultiDict(
+ {"source": "https://source.example/a%20b", "target": VALID_TARGET}
+ ),
+ meta={"csrf": False},
+ )
+ assert form.validate()
diff --git a/tests/tasks/test_receiver.py b/tests/tasks/test_receiver.py
index c795115..44d1cc2 100644
--- a/tests/tasks/test_receiver.py
+++ b/tests/tasks/test_receiver.py
@@ -35,6 +35,7 @@ def create_webmention(
target: str = TARGET_URL,
status: str = "received",
failure_reason: str | None = None,
+ recovery_attempts: int = 0,
) -> uuid.UUID:
identifier = uuid.uuid7()
@@ -45,6 +46,7 @@ def create_webmention(
target=target,
status=status,
failure_reason=failure_reason,
+ recovery_attempts=recovery_attempts,
)
db.session.add(webmention)
@@ -669,6 +671,106 @@ def test_reaper_requeues_and_verifies_integration(
check.assert_called_once_with(SOURCE_URL, TARGET_URL)
+@patch("webmentions_ssg.tasks.receiver.source_mentions_target")
+@pytest.mark.parametrize(
+ "unexpected_error",
+ [
+ pytest.param(httpx.InvalidURL("Invalid IDNA hostname"), id="invalid-url"),
+ pytest.param(
+ RuntimeError("Unexpected HTML parser error"), id="unexpected-error"
+ ),
+ ],
+)
+def test_verify_webmention_records_unexpected_error_as_terminal_failure(
+ source_mentions_target: Mock,
+ app: Flask,
+ receiver: ModuleType,
+ unexpected_error: Exception,
+) -> None:
+ identifier = create_webmention(app, source="http://ex☃mple.test/")
+ source_mentions_target.side_effect = unexpected_error
+
+ receiver.verify_webmention.call_local(identifier)
+
+ assert get_webmention_state(app, identifier) == ("failed", str(unexpected_error))
+ source_mentions_target.assert_called_once()
+ # Failed rows are not eligible for automatic reaper recovery.
+ set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
+ with app.app_context():
+ assert receiver.recover_stale_webmentions() == 0
+
+
+def test_reaper_caps_stale_verification_recoveries(
+ app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ app.config["VERIFICATION_MAX_RECOVERIES"] = 3
+ identifier = create_webmention(app, status="verifying")
+ enqueue = Mock()
+ monkeypatch.setattr(receiver, "verify_webmention", enqueue)
+
+ # Simulate workers that repeatedly crash before completing the task.
+ for attempt in range(3):
+ set_webmention_updated_at(
+ app, identifier, datetime.now(UTC) - timedelta(hours=1)
+ )
+ with app.app_context():
+ assert receiver.recover_stale_webmentions() == 1
+ row = db.session.get(ReceivedWebmention, identifier)
+ assert row is not None
+ assert row.status == "received"
+ assert row.recovery_attempts == attempt + 1
+
+ set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
+ with app.app_context():
+ assert receiver.recover_stale_webmentions() == 0
+ row = db.session.get(ReceivedWebmention, identifier)
+ assert row is not None
+ assert row.status == "failed"
+ assert row.failure_reason == receiver.RECOVERY_EXHAUSTED_REASON
+ assert row.recovery_attempts == 3
+ assert receiver.recover_stale_webmentions() == 0
+ assert enqueue.call_count == 3
+
+
+def test_reaper_caps_received_rows_lost_before_first_task(
+ app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ app.config["VERIFICATION_MAX_RECOVERIES"] = 1
+ identifier = create_webmention(app, status="received", recovery_attempts=1)
+ set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1))
+ enqueue = Mock()
+ monkeypatch.setattr(receiver, "verify_webmention", enqueue)
+
+ with app.app_context():
+ assert receiver.recover_stale_webmentions() == 0
+ assert get_webmention_state(app, identifier) == (
+ "failed",
+ receiver.RECOVERY_EXHAUSTED_REASON,
+ )
+ enqueue.assert_not_called()
+
+
+def test_exhausted_row_cannot_be_revived_by_late_huey_retry(
+ app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ identifier = create_webmention(
+ app,
+ status="failed",
+ failure_reason=receiver.RECOVERY_EXHAUSTED_REASON,
+ recovery_attempts=3,
+ )
+ source_mentions_target = Mock(return_value=True)
+ monkeypatch.setattr(receiver, "source_mentions_target", source_mentions_target)
+
+ receiver.verify_webmention.call_local(identifier)
+
+ assert get_webmention_state(app, identifier) == (
+ "failed",
+ receiver.RECOVERY_EXHAUSTED_REASON,
+ )
+ source_mentions_target.assert_not_called()
+
+
def test_verify_webmention_ignores_unknown_identifier(
receiver: ModuleType, caplog: pytest.LogCaptureFixture
) -> None:
diff --git a/tests/test_views.py b/tests/test_views.py
index f5e301f..2108b38 100644
--- a/tests/test_views.py
+++ b/tests/test_views.py
@@ -576,6 +576,11 @@ def test_reverify_received_webmention(
identifier = create_received_webmention(
app, status="failed", failure_reason="Previous failure"
)
+ with app.app_context():
+ row = db.session.get(ReceivedWebmention, identifier)
+ assert row is not None
+ row.recovery_attempts = 3
+ db.session.commit()
mock_admin_action_form(views_module, monkeypatch, valid=True)
verify_webmention = Mock()
monkeypatch.setattr(views_module, "verify_webmention", verify_webmention)
@@ -590,6 +595,7 @@ def test_reverify_received_webmention(
assert webmention is not None
assert webmention.status == "received"
assert webmention.failure_reason is None
+ assert webmention.recovery_attempts == 0
verify_webmention.assert_called_once_with(identifier)
@@ -911,6 +917,7 @@ def test_endpoint_reverifies_after_cooldown(
target=TARGET_URL,
status="failed",
failure_reason="Previous failure",
+ recovery_attempts=3,
updated_at=datetime.now(UTC) - timedelta(minutes=11),
)
db.session.add(webmention)
@@ -926,6 +933,7 @@ def test_endpoint_reverifies_after_cooldown(
assert stored is not None
assert stored.status == "received"
assert stored.failure_reason is None
+ assert stored.recovery_attempts == 0
verify.assert_called_once_with(identifier)
@@ -1194,3 +1202,27 @@ def test_endpoint_rejects_request_bodies_over_configured_maximum(
"/endpoint", data={"source": SOURCE_URL + "x" * 150, "target": TARGET_URL}
)
assert response.status_code == 413
+
+
+def test_endpoint_rejects_bad_url_before_inserting_row(
+ app: Flask, client: FlaskClient, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ from webmentions_ssg import views
+
+ verify_webmention = Mock()
+ monkeypatch.setattr(views, "verify_webmention", verify_webmention)
+
+ response = client.post(
+ "/endpoint", data={"source": "http://ex☃mple.test/", "target": TARGET_URL}
+ )
+
+ assert response.status_code == 400
+ assert "source" in response.get_json()
+ verify_webmention.assert_not_called()
+ with app.app_context():
+ assert (
+ db.session.scalar(
+ sa.select(sa.func.count()).select_from(ReceivedWebmention)
+ )
+ == 0
+ )