From 22bbc0c0a8ef04c956acbd69f93bb68fb90e1ec4 Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Sat, 21 Nov 2026 18:21:00 -0500 Subject: [PATCH] fix(r31/CR-01): enforce atomic non-processing predicate in delete_appointment - app/db.py: add AND status != 'payment_processing' predicate to delete_appointment SQL statement - app/__init__.py: return 409 Conflict when db.delete_appointment reports zero rows deleted - tests/test_app.py: add Red-Green concurrency race test proving stale-snapshot DELETE returns 409 and preserves payment_processing row --- app/__init__.py | 3 ++- app/db.py | 5 +++-- tests/test_app.py | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index f19bdc7..602f543 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -195,7 +195,8 @@ def create_app(test_config: dict | None = None) -> Flask: token = zoom.get_access_token(body.credentials) zoom.delete_meeting(token, row["zoom_meeting_id"]) - db.delete_appointment(row["session_id"], db_path=db_path) + if not db.delete_appointment(row["session_id"], db_path=db_path): + return jsonify({"error": "No se puede eliminar la sesión mientras el pago está siendo procesado."}), 409 return jsonify({"deleted": True, "session_id": row["session_id"]}), 200 return app diff --git a/app/db.py b/app/db.py index 9db4695..0dbb582 100644 --- a/app/db.py +++ b/app/db.py @@ -166,9 +166,10 @@ def mark_appointment_confirmed( def delete_appointment(session_id: str, db_path: Path | None = None) -> bool: - """Remove an appointment record from SQLite.""" + """Remove an appointment record from SQLite unless payment is in flight.""" with get_connection(db_path) as conn: cursor = conn.execute( - "DELETE FROM appointments WHERE session_id = ?", (session_id,) + "DELETE FROM appointments WHERE session_id = ? AND status != 'payment_processing'", + (session_id,), ) return cursor.rowcount > 0 diff --git a/tests/test_app.py b/tests/test_app.py index abea9ad..a573101 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1125,3 +1125,42 @@ def test_get_appointment_omits_zoom_start_url_while_pay_returns_it(client, monke fetched = g_resp.get_json() assert fetched["zoom_start_url"] is None assert fetched["zoom_join_url"] == "https://zoom.us/j/987654321" + + +def test_delete_rejects_with_409_when_row_claimed_after_lookup_snapshot(client, monkeypatch): + from app import db + + test_client, db_file = client + _mock_zoom_happy_path(monkeypatch) + + c_resp = test_client.post( + "/api/appointments", + json={ + "credentials": SAMPLE_CREDS, + "client_name": "María Perez", + "start_time_local": "2026-11-25T15:00", + }, + ) + assert c_resp.status_code == 201 + sid = c_resp.get_json()["session_id"] + + real_lookup = db.get_appointment_by_lookup + race_triggered = False + + def racing_lookup(lookup_id, db_path=None): + nonlocal race_triggered + row = real_lookup(lookup_id, db_path=db_path) + if row and not race_triggered and row["status"] == "payment_pending": + race_triggered = True + assert db.claim_appointment_for_payment(row["session_id"], db_path=db_path) is True + return row + + monkeypatch.setattr(db, "get_appointment_by_lookup", racing_lookup) + + d_resp = test_client.delete(f"/api/appointments/{sid}", json={"credentials": SAMPLE_CREDS}) + assert d_resp.status_code == 409 + + monkeypatch.undo() + persisted = real_lookup(sid, db_path=db_file) + assert persisted is not None + assert persisted["status"] == "payment_processing"