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
This commit is contained in:
kiskaadee committed 2026-11-21 18:21:00 -05:00
1 parent 93356a6871
commit 22bbc0c0a8
3 files changed
+44 -3

No files matched your search

+2 -1
View File
@@ -195,7 +195,8 @@ def create_app(test_config: dict | None = None) -> Flask:
token = zoom.get_access_token(body.credentials) token = zoom.get_access_token(body.credentials)
zoom.delete_meeting(token, row["zoom_meeting_id"]) 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 jsonify({"deleted": True, "session_id": row["session_id"]}), 200
return app return app
+3 -2
View File
@@ -166,9 +166,10 @@ def mark_appointment_confirmed(
def delete_appointment(session_id: str, db_path: Path | None = None) -> bool: 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: with get_connection(db_path) as conn:
cursor = conn.execute( 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 return cursor.rowcount > 0
+39
View File
@@ -1125,3 +1125,42 @@ def test_get_appointment_omits_zoom_start_url_while_pay_returns_it(client, monke
fetched = g_resp.get_json() fetched = g_resp.get_json()
assert fetched["zoom_start_url"] is None assert fetched["zoom_start_url"] is None
assert fetched["zoom_join_url"] == "https://zoom.us/j/987654321" 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"