From 202fcc14ee85643d36280f5463af8e0602414114 Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Sat, 21 Nov 2026 17:48:00 -0500 Subject: [PATCH] fix(r30/CR-01): require Zoom credential verification on confirmed pay - Call zoom.get_access_token(body.credentials) before returning zoom_start_url on confirmed branches in pay_appointment - Add test_pay_confirmed_appointment_rejects_invalid_credentials verifying 401 on invalid credentials and idempotent 200 on valid credentials --- app/__init__.py | 2 ++ tests/test_app.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/app/__init__.py b/app/__init__.py index 9483e8a..338f04a 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -139,6 +139,7 @@ def create_app(test_config: dict | None = None) -> Flask: # Idempotent: return existing confirmed session without creating a duplicate meeting if row["status"] == "confirmed": + zoom.get_access_token(body.credentials) return jsonify(AppointmentResponse.model_validate(row).model_dump()), 200 if row["status"] == "payment_processing": return jsonify({"error": "El pago ya está siendo procesado."}), 409 @@ -146,6 +147,7 @@ def create_app(test_config: dict | None = None) -> Flask: if not db.claim_appointment_for_payment(row["session_id"], db_path=db_path): latest = db.get_appointment_by_lookup(row["session_id"], db_path=db_path) if latest and latest["status"] == "confirmed": + zoom.get_access_token(body.credentials) return jsonify(AppointmentResponse.model_validate(latest).model_dump()), 200 return jsonify({"error": "El pago ya está siendo procesado."}), 409 diff --git a/tests/test_app.py b/tests/test_app.py index 0486202..20befc8 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -160,6 +160,65 @@ def test_pay_appointment_is_idempotent(client, monkeypatch): assert len(create_calls) == 1 +def test_pay_confirmed_appointment_rejects_invalid_credentials(client, monkeypatch): + test_client, _ = client + create_calls = [] + _mock_zoom_happy_path(monkeypatch, create_calls=create_calls) + + 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"] + + first = test_client.post( + f"/api/appointments/{sid}/pay", + json={"credentials": SAMPLE_CREDS}, + ) + assert first.status_code == 200 + assert first.get_json()["zoom_start_url"] == "https://zoom.us/s/987654321?zak=abc" + assert len(create_calls) == 1 + + def fake_post_invalid_on_bad_creds(url, **kwargs): + if url == "https://zoom.us/oauth/token": + if kwargs.get("params", {}).get("account_id") != SAMPLE_CREDS["account_id"]: + return FakeResponse(401, {"reason": "Invalid client_id or client_secret"}) + return FakeResponse(200, {"access_token": "mock_token"}) + if url == "https://api.zoom.us/v2/users/me/meetings": + create_calls.append(kwargs.get("json")) + raise AssertionError("create_meeting must not be called for already-confirmed appointment") + raise AssertionError(f"Unexpected POST URL: {url}") + + monkeypatch.setattr(zoom.requests, "post", fake_post_invalid_on_bad_creds) + + bad_resp = test_client.post( + f"/api/appointments/{sid}/pay", + json={ + "credentials": { + "account_id": "wrong_acc", + "client_id": "wrong_id", + "client_secret": "wrong_secret", + } + }, + ) + assert bad_resp.status_code == 401 + assert "zoom_start_url" not in bad_resp.get_json() + assert len(create_calls) == 1 + + good_resp = test_client.post( + f"/api/appointments/{sid}/pay", + json={"credentials": SAMPLE_CREDS}, + ) + assert good_resp.status_code == 200 + assert good_resp.get_json()["zoom_start_url"] == "https://zoom.us/s/987654321?zak=abc" + assert len(create_calls) == 1 + + def test_booking_flow_never_persists_zoom_credentials(client, monkeypatch): test_client, db_file = client _mock_zoom_happy_path(monkeypatch)