diff --git a/app/zoom.py b/app/zoom.py index c1c93f6..6de4a37 100644 --- a/app/zoom.py +++ b/app/zoom.py @@ -65,6 +65,7 @@ def check_slot_availability( start_time_local: str, tz_name: str, duration_minutes: int = 60, + max_pages: int = 20, ) -> tuple[bool, str | None]: """Check upcoming Zoom meetings for overlapping time windows.""" try: @@ -81,7 +82,7 @@ def check_slot_availability( req_end = req_start + timedelta(minutes=duration_minutes) next_page_token = "" - while True: + for _ in range(max_pages): params: dict[str, object] = {"type": "upcoming", "page_size": 100} if next_page_token: params["next_page_token"] = next_page_token @@ -100,11 +101,20 @@ def check_slot_availability( raise ZoomAPIError(resp.status_code, _extract_zoom_error(resp)) data = _parse_json_dict(resp, "Meetings API") - for meeting in data.get("meetings", []): - raw_start = meeting.get("start_time") - if not raw_start: + for meeting in data.get("meetings") or []: + if not isinstance(meeting, dict): continue - m_start = datetime.fromisoformat(raw_start.replace("Z", "+00:00")) + raw_start = meeting.get("start_time") + if not isinstance(raw_start, str) or not raw_start: + continue + try: + m_start = datetime.fromisoformat(raw_start.replace("Z", "+00:00")) + except ValueError: + continue + if m_start.tzinfo is None: + m_start = m_start.replace(tzinfo=timezone.utc) + else: + m_start = m_start.astimezone(timezone.utc) duration = meeting.get("duration") or 60 m_end = m_start + timedelta(minutes=duration) if req_start < m_end and req_end > m_start: @@ -113,9 +123,9 @@ def check_slot_availability( next_page_token = data.get("next_page_token") or "" if not next_page_token: - break + return True, None - return True, None + raise ZoomAPIError(502, "Zoom Meetings pagination exceeded maximum page limit") def create_meeting( diff --git a/tests/test_app.py b/tests/test_app.py index c9d5508..0b01400 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -426,6 +426,90 @@ def test_check_slot_availability_paginates_via_next_page_token(client, monkeypat assert seen_params[1].get("next_page_token") == "tok_page_2" +def test_check_slot_availability_handles_null_meetings(client, monkeypatch): + test_client, _ = client + monkeypatch.setattr( + zoom.requests, + "post", + lambda url, **kwargs: FakeResponse(200, {"access_token": "mock_token"}), + ) + monkeypatch.setattr( + zoom.requests, + "get", + lambda url, **kwargs: FakeResponse(200, {"meetings": None}), + ) + resp = test_client.post( + "/api/appointments", + json={ + "credentials": SAMPLE_CREDS, + "client_name": "María Perez", + "start_time_local": "2026-11-25T15:00", + }, + ) + assert resp.status_code == 201 + + +def test_check_slot_availability_handles_naive_and_malformed_start_times(client, monkeypatch): + test_client, _ = client + monkeypatch.setattr( + zoom.requests, + "post", + lambda url, **kwargs: FakeResponse(200, {"access_token": "mock_token"}), + ) + monkeypatch.setattr( + zoom.requests, + "get", + lambda url, **kwargs: FakeResponse( + 200, + { + "meetings": [ + {"topic": "Malformed", "start_time": "not-an-iso-date", "duration": 60}, + {"topic": "Naive", "start_time": "2026-11-24T10:00:00", "duration": 60}, + ] + }, + ), + ) + resp = test_client.post( + "/api/appointments", + json={ + "credentials": SAMPLE_CREDS, + "client_name": "María Perez", + "start_time_local": "2026-11-25T15:00", + }, + ) + assert resp.status_code == 201 + + +def test_check_slot_availability_caps_pagination_at_max_pages(client, monkeypatch): + test_client, _ = client + calls = [] + + monkeypatch.setattr( + zoom.requests, + "post", + lambda url, **kwargs: FakeResponse(200, {"access_token": "mock_token"}), + ) + + def fake_get(url, **kwargs): + calls.append(kwargs.get("params", {})) + if len(calls) > 25: + raise AssertionError("Pagination exceeded safe cap") + return FakeResponse(200, {"meetings": [], "next_page_token": f"tok_{len(calls)}"}) + + monkeypatch.setattr(zoom.requests, "get", fake_get) + + resp = test_client.post( + "/api/appointments", + json={ + "credentials": SAMPLE_CREDS, + "client_name": "María Perez", + "start_time_local": "2026-11-25T15:00", + }, + ) + assert resp.status_code == 502 + assert len(calls) == 20 + + def test_create_meeting_missing_required_keys_returns_502(client, monkeypatch): test_client, _ = client