From f801d8acef788a7ce676adc099ebcb5618db94b2 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Fri, 13 Nov 2026 17:14:29 +0100 Subject: fix(receiver): reopen raced submissions after cooldown Recover a concurrently inserted Webmention through the same cooldown path as an existing submission so stale failures can be queued for verification. Extract digest lookup, insertion, and reopening helpers while preserving explicit SHA-256 collision handling. --- tests/views/test_endpoint.py | 54 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 7 deletions(-) (limited to 'tests/views') diff --git a/tests/views/test_endpoint.py b/tests/views/test_endpoint.py index 72efa47..4b81d70 100644 --- a/tests/views/test_endpoint.py +++ b/tests/views/test_endpoint.py @@ -173,8 +173,13 @@ def test_status_displays_webmention(app: Flask, client: FlaskClient) -> None: assert "Verification failed" in html +@pytest.mark.parametrize(("age_minutes", "reopened"), [(0, False), (11, True)]) def test_endpoint_recovers_from_concurrent_insert( - client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + age_minutes: int, + reopened: bool, ) -> None: mock_endpoint_form(views_module, monkeypatch, valid=True) @@ -185,6 +190,7 @@ def test_endpoint_recovers_from_concurrent_insert( target=TARGET_URL, status="failed", failure_reason="Competing request failed", + updated_at=datetime.now(UTC) - timedelta(minutes=age_minutes), ) initial_result = Mock() @@ -192,7 +198,8 @@ def test_endpoint_recovers_from_concurrent_insert( competing_result = Mock() competing_result.scalar_one_or_none.return_value = competing_webmention - execute = Mock(side_effect=[initial_result, competing_result]) + update_result = Mock(rowcount=int(reopened)) + execute = Mock(side_effect=[initial_result, competing_result, update_result]) add = Mock() rollback = Mock() commit = Mock( @@ -218,14 +225,47 @@ def test_endpoint_recovers_from_concurrent_insert( assert response.status_code == 201 assert response.headers["Location"].endswith(f"/status/{competing_identifier}") - assert execute.call_count == 2 + assert execute.call_count == 3 add.assert_called_once_with(ANY) rollback.assert_called_once_with() - assert commit.call_count == 1 + assert commit.call_count == 2 - assert competing_webmention.status == "failed" - assert competing_webmention.failure_reason == "Competing request failed" - verify_webmention.assert_not_called() + if reopened: + verify_webmention.assert_called_once_with(competing_identifier) + else: + verify_webmention.assert_not_called() + + +def test_endpoint_rejects_digest_collision( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + form = mock_endpoint_form(views_module, monkeypatch, valid=True) + verify = Mock() + monkeypatch.setattr(views_module, "verify_webmention", verify) + + with app.app_context(): + existing = ReceivedWebmention( + uuid=uuid.uuid7(), source=SOURCE_URL, target=TARGET_URL + ) + db.session.add(existing) + db.session.commit() + digest = existing.url_digest + + form.source.data = "https://source.example/different" + monkeypatch.setattr( + views_module, "received_url_digest", lambda source, target: digest + ) + + response = client.post("/endpoint") + + assert response.status_code == 409 + assert response.get_json() == {"error": "Webmention URL fingerprint conflict"} + verify.assert_not_called() + with app.app_context(): + assert db.session.scalar(sa.select(sa.func.count(ReceivedWebmention.uuid))) == 1 def test_endpoint_rate_limits_requests_per_ip( -- cgit v1.3.1