mirror of
https://github.com/corazoncp/zoom-sched-poc.git
synced 2026-12-01 04:50:31 +00:00
test(CR-04): mock Zoom API at external HTTP boundary instead of internal zoom module
- Add FakeResponse helper in tests/test_app.py to stub requests.Response
- Replace monkeypatch.setattr(zoom, ...) with zoom.requests.{post,get,delete} mocks so app/zoom.py executes under test
This commit is contained in:
1 parent
51f656100b
commit
fec0013154
1 file changed
+65
-13
+65
-13
@@ -5,6 +5,19 @@ import pytest
|
|||||||
from app import create_app, zoom
|
from app import create_app, zoom
|
||||||
|
|
||||||
|
|
||||||
|
class FakeResponse:
|
||||||
|
def __init__(self, status_code: int = 200, json_data: dict | None = None, text: str = ""):
|
||||||
|
self.status_code = status_code
|
||||||
|
self.ok = 200 <= status_code < 300
|
||||||
|
self._json_data = json_data
|
||||||
|
self.text = text
|
||||||
|
|
||||||
|
def json(self):
|
||||||
|
if isinstance(self._json_data, Exception):
|
||||||
|
raise self._json_data
|
||||||
|
return self._json_data
|
||||||
|
|
||||||
|
|
||||||
SAMPLE_CREDS = {
|
SAMPLE_CREDS = {
|
||||||
"account_id": "acc_test_1234",
|
"account_id": "acc_test_1234",
|
||||||
"client_id": "cli_test_1234",
|
"client_id": "cli_test_1234",
|
||||||
@@ -41,19 +54,34 @@ def test_full_booking_flow_idempotent_and_no_secret_persistence(client, monkeypa
|
|||||||
test_client, db_file = client
|
test_client, db_file = client
|
||||||
create_calls = []
|
create_calls = []
|
||||||
|
|
||||||
monkeypatch.setattr(zoom, "get_access_token", lambda creds: "mock_token")
|
def fake_post(url, **kwargs):
|
||||||
monkeypatch.setattr(zoom, "check_slot_availability", lambda **kwargs: (True, None))
|
if url == "https://zoom.us/oauth/token":
|
||||||
|
return FakeResponse(200, {"access_token": "mock_token"})
|
||||||
def fake_create_meeting(**kwargs):
|
if url == "https://api.zoom.us/v2/users/me/meetings":
|
||||||
create_calls.append(kwargs)
|
create_calls.append(kwargs.get("json"))
|
||||||
return {
|
return FakeResponse(
|
||||||
|
201,
|
||||||
|
{
|
||||||
"id": 987654321,
|
"id": 987654321,
|
||||||
"join_url": "https://zoom.us/j/987654321",
|
"join_url": "https://zoom.us/j/987654321",
|
||||||
"start_url": "https://zoom.us/s/987654321?zak=abc",
|
"start_url": "https://zoom.us/s/987654321?zak=abc",
|
||||||
}
|
},
|
||||||
|
)
|
||||||
|
raise AssertionError(f"Unexpected POST URL: {url}")
|
||||||
|
|
||||||
monkeypatch.setattr(zoom, "create_meeting", fake_create_meeting)
|
def fake_get(url, **kwargs):
|
||||||
monkeypatch.setattr(zoom, "delete_meeting", lambda token, mid: None)
|
if url == "https://api.zoom.us/v2/users/me/meetings":
|
||||||
|
return FakeResponse(200, {"meetings": []})
|
||||||
|
raise AssertionError(f"Unexpected GET URL: {url}")
|
||||||
|
|
||||||
|
def fake_delete(url, **kwargs):
|
||||||
|
if url == "https://api.zoom.us/v2/meetings/987654321":
|
||||||
|
return FakeResponse(204, {})
|
||||||
|
raise AssertionError(f"Unexpected DELETE URL: {url}")
|
||||||
|
|
||||||
|
monkeypatch.setattr(zoom.requests, "post", fake_post)
|
||||||
|
monkeypatch.setattr(zoom.requests, "get", fake_get)
|
||||||
|
monkeypatch.setattr(zoom.requests, "delete", fake_delete)
|
||||||
|
|
||||||
# 1. Verify credentials
|
# 1. Verify credentials
|
||||||
v_resp = test_client.post("/api/zoom/verify-credentials", json=SAMPLE_CREDS)
|
v_resp = test_client.post("/api/zoom/verify-credentials", json=SAMPLE_CREDS)
|
||||||
@@ -117,10 +145,30 @@ def test_full_booking_flow_idempotent_and_no_secret_persistence(client, monkeypa
|
|||||||
|
|
||||||
def test_slot_conflict_returns_409(client, monkeypatch):
|
def test_slot_conflict_returns_409(client, monkeypatch):
|
||||||
test_client, _ = client
|
test_client, _ = client
|
||||||
monkeypatch.setattr(zoom, "get_access_token", lambda creds: "mock_token")
|
|
||||||
monkeypatch.setattr(
|
def fake_post(url, **kwargs):
|
||||||
zoom, "check_slot_availability", lambda **kwargs: (False, "Conflicto con 'Otra sesión'")
|
if url == "https://zoom.us/oauth/token":
|
||||||
|
return FakeResponse(200, {"access_token": "mock_token"})
|
||||||
|
raise AssertionError(f"Unexpected POST URL: {url}")
|
||||||
|
|
||||||
|
def fake_get(url, **kwargs):
|
||||||
|
if url == "https://api.zoom.us/v2/users/me/meetings":
|
||||||
|
return FakeResponse(
|
||||||
|
200,
|
||||||
|
{
|
||||||
|
"meetings": [
|
||||||
|
{
|
||||||
|
"topic": "Otra sesión",
|
||||||
|
"start_time": "2026-11-25T20:00:00Z",
|
||||||
|
"duration": 60,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
raise AssertionError(f"Unexpected GET URL: {url}")
|
||||||
|
|
||||||
|
monkeypatch.setattr(zoom.requests, "post", fake_post)
|
||||||
|
monkeypatch.setattr(zoom.requests, "get", fake_get)
|
||||||
|
|
||||||
resp = test_client.post(
|
resp = test_client.post(
|
||||||
"/api/appointments",
|
"/api/appointments",
|
||||||
@@ -137,7 +185,11 @@ def test_slot_conflict_returns_409(client, monkeypatch):
|
|||||||
def test_rate_limiting_blocks_brute_force(tmp_path: Path, monkeypatch):
|
def test_rate_limiting_blocks_brute_force(tmp_path: Path, monkeypatch):
|
||||||
db_file = tmp_path / "ratelimit.db"
|
db_file = tmp_path / "ratelimit.db"
|
||||||
app = create_app({"TESTING": True, "DB_PATH": str(db_file), "RATELIMIT_ENABLED": True})
|
app = create_app({"TESTING": True, "DB_PATH": str(db_file), "RATELIMIT_ENABLED": True})
|
||||||
monkeypatch.setattr(zoom, "get_access_token", lambda creds: "mock_token")
|
monkeypatch.setattr(
|
||||||
|
zoom.requests,
|
||||||
|
"post",
|
||||||
|
lambda url, **kwargs: FakeResponse(200, {"access_token": "mock_token"}),
|
||||||
|
)
|
||||||
|
|
||||||
with app.test_client() as test_client:
|
with app.test_client() as test_client:
|
||||||
statuses = [
|
statuses = [
|
||||||
|
|||||||
Reference in new issue
Block a user