You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
simple-agent/tests/test_main_http_integration.py

154 lines
4.4 KiB
Python

from __future__ import annotations
from dataclasses import replace
from types import SimpleNamespace
from fastapi.testclient import TestClient
import app.main as main_module
from app.config import get_settings
from app.security.auth import AuthBackend
class _DummyCoreService:
def scan_mailbox(self, max_results: int) -> SimpleNamespace:
return SimpleNamespace(
scanned=max_results,
linkedin=1,
advertising=2,
veille_techno=0,
skipped=3,
failed=0,
)
def check_availability(
self,
start: str,
end: str,
calendar_ids: list[str] | None,
) -> SimpleNamespace:
return SimpleNamespace(
start=start,
end=end,
available=False,
busy_slots=[
{
"calendar_id": "primary",
"start": start,
"end": end,
}
],
checked_calendars=calendar_ids or ["primary"],
)
def available_meeting_intervals(
self,
start: str,
end: str,
calendar_ids: list[str] | None,
) -> SimpleNamespace:
return SimpleNamespace(
start=start,
end=end,
timezone="Europe/Paris",
meeting_intervals=[
SimpleNamespace(
start="2026-03-10T08:30:00+01:00",
end="2026-03-10T09:30:00+01:00",
)
],
checked_calendars=calendar_ids or ["primary"],
)
async def _noop_task() -> None:
return None
def _setup_main_test_context(monkeypatch) -> None:
auth_settings = replace(
get_settings(),
auth_mode="api_key",
agent_api_key="integration-key",
auth_jwt_secret="",
)
monkeypatch.setattr(main_module, "auth_backend", AuthBackend(auth_settings))
monkeypatch.setattr(main_module, "core_service", _DummyCoreService())
# Prevent scheduler jobs from executing real background work during lifespan startup.
monkeypatch.setattr(main_module, "_scheduled_scan", _noop_task)
monkeypatch.setattr(main_module, "_scheduled_unsubscribe_digest", _noop_task)
monkeypatch.setattr(main_module, "_scheduled_unsubscribe_auto", _noop_task)
def test_main_scan_endpoint_with_api_key(monkeypatch) -> None:
_setup_main_test_context(monkeypatch)
with TestClient(main_module.app) as client:
response = client.post(
"/scan?max_results=15",
headers={"X-API-Key": "integration-key"},
)
assert response.status_code == 200
payload = response.json()
assert payload["scanned"] == 15
assert payload["linkedin"] == 1
assert payload["advertising"] == 2
def test_main_availability_endpoint_rejects_missing_key(monkeypatch) -> None:
_setup_main_test_context(monkeypatch)
with TestClient(main_module.app) as client:
response = client.post(
"/availability",
json={
"start": "2026-03-10T09:00:00+01:00",
"end": "2026-03-10T10:00:00+01:00",
"calendar_ids": ["primary"],
},
)
assert response.status_code == 401
def test_main_meeting_intervals_endpoint_with_api_key(monkeypatch) -> None:
_setup_main_test_context(monkeypatch)
with TestClient(main_module.app) as client:
response = client.post(
"/meeting-intervals",
headers={"X-API-Key": "integration-key"},
json={
"start": "2026-03-10T08:00:00+01:00",
"end": "2026-03-10T10:00:00+01:00",
"calendar_ids": ["primary"],
},
)
assert response.status_code == 200
payload = response.json()
assert payload["timezone"] == "Europe/Paris"
assert payload["meeting_intervals"] == [
{
"start": "2026-03-10T08:30:00+01:00",
"end": "2026-03-10T09:30:00+01:00",
}
]
def test_main_meeting_intervals_endpoint_rejects_missing_key(monkeypatch) -> None:
_setup_main_test_context(monkeypatch)
with TestClient(main_module.app) as client:
response = client.post(
"/meeting-intervals",
json={
"start": "2026-03-10T08:00:00+01:00",
"end": "2026-03-10T10:00:00+01:00",
"calendar_ids": ["primary"],
},
)
assert response.status_code == 401