contiai / apps /api /tests /test_api.py
aliabozahra315's picture
Deploy 2bedea9
79707da
Raw History Blame Contribute Delete
16.7 kB
"""API surface.
Covers the things that were wrong or absent in the previous system: that
endpoints refuse unauthenticated callers, that one user cannot reach another
user's work, that CORS headers actually come from the app, and that the
pipeline catalogue the web app builds its UI from is served correctly.
"""
from __future__ import annotations
import uuid
import pytest
import pytest_asyncio
from httpx import ASGITransport, AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from app.core.auth import current_user
from app.db.models import Base, User, UserRole, UserStatus
from app.db.session import get_session
from app.main import app
from app.pipelines.registry import all_pipelines
@pytest_asyncio.fixture
async def api_engine():
eng = create_async_engine("sqlite+aiosqlite:///:memory:", future=True)
async with eng.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield eng
await eng.dispose()
@pytest_asyncio.fixture
async def api_db(api_engine) -> AsyncSession:
factory = async_sessionmaker(api_engine, class_=AsyncSession, expire_on_commit=False)
async with factory() as session:
yield session
@pytest_asyncio.fixture
async def alice(api_db) -> User:
# Approved with every tool. What a grant withholds is covered in
# test_approval.py; these tests are about everything else.
p = User(id=uuid.uuid4(), email="alice@example.com", role=UserRole.user,
status=UserStatus.active,
allowed_task_types=[pl.key for pl in all_pipelines()])
api_db.add(p)
await api_db.commit()
return p
@pytest_asyncio.fixture
async def bob(api_db) -> User:
p = User(id=uuid.uuid4(), email="bob@example.com", role=UserRole.user,
status=UserStatus.active,
allowed_task_types=[pl.key for pl in all_pipelines()])
api_db.add(p)
await api_db.commit()
return p
def _client(api_db, as_user: User | None) -> AsyncClient:
async def _session_override():
yield api_db
app.dependency_overrides[get_session] = _session_override
if as_user is not None:
app.dependency_overrides[current_user] = lambda: as_user
else:
app.dependency_overrides.pop(current_user, None)
return AsyncClient(transport=ASGITransport(app=app), base_url="http://test")
@pytest_asyncio.fixture(autouse=True)
async def _reset_overrides():
yield
app.dependency_overrides.clear()
# ---------------------------------------------------------------------- meta
async def test_health_needs_no_auth(api_db):
async with _client(api_db, None) as c:
r = await c.get("/health")
assert r.status_code == 200
assert r.json()["status"] == "ok"
async def test_cors_headers_come_from_the_app(api_db):
"""The old backend shipped none of these - only the HF proxy added them."""
async with _client(api_db, None) as c:
r = await c.options(
"/v1/pipelines",
headers={
"Origin": "http://localhost:5173",
"Access-Control-Request-Method": "GET",
},
)
assert r.headers.get("access-control-allow-origin") == "http://localhost:5173"
# ----------------------------------------------------------------- pipelines
async def test_pipeline_catalogue_is_public_and_complete(api_db):
async with _client(api_db, None) as c:
r = await c.get("/v1/pipelines")
assert r.status_code == 200
items = r.json()["items"]
assert {p["key"] for p in items} == {
"book", "course", "training_bag", "arabic_training_bag", "arabic_slides", "english_slides",
"instructional_design", "level", "motion_video",
"static_infographic", "interactive_infographic",
"references", "appendix",
"case_study", "training_activities", "applied_tools",
"pre_post_test", "analytical_case_study",
"text_image", "simulation_game", "interactive_activities", "dna_microlearning",
"book_practical", "book_narrative", "novel",
"book_v3",
}
book = next(p for p in items if p["key"] == "book")
assert len(book["steps"]) == 14
# The form comes from the chosen route, not from the step.
routes = {v["key"]: v for v in book["variants"]}
assert set(routes) == {"from_scratch", "from_text", "from_file"}
assert routes["from_scratch"]["input_schema"]["properties"]["topic"]
assert routes["from_file"]["accepts_uploads"] is True
async def test_task_types_carry_the_commercial_fields_the_grid_shows(api_db, alice):
"""The create grid reads /v1/task-types, not /v1/pipelines - it needs the
Arabic name, the category, the outputs and the cost hint as well as the
routes. A pipeline missing a DEFAULTS entry silently lands in 'أخرى'."""
async with _client(api_db, alice) as c:
r = await c.get("/v1/task-types")
assert r.status_code == 200
items = r.json()["items"]
assert {t["id"] for t in items} == {
"book", "course", "training_bag", "arabic_training_bag", "arabic_slides", "english_slides",
"instructional_design", "level", "motion_video",
"static_infographic", "interactive_infographic",
"references", "appendix",
"case_study", "training_activities", "applied_tools",
"pre_post_test", "analytical_case_study",
"text_image", "simulation_game", "interactive_activities", "dna_microlearning",
"book_practical", "book_narrative", "novel",
"book_v3",
}
for item in items:
assert item["nameAr"], f"{item['id']} has no Arabic name"
assert item["category"] != "أخرى", f"{item['id']} is missing a DEFAULTS entry"
assert item["outputs"] and item["estimatedSeconds"] and item["costHint"]
assert item["variants"], f"{item['id']} publishes no entry route"
# The create screen is split by company: كونتيفا the interactive work,
# رؤية the rest, and the sources under both.
brands = {t["id"]: t["brands"] for t in items}
assert brands["references"] == ["contiva", "roya"]
for key in ("case_study", "text_image", "simulation_game", "interactive_activities",
"dna_microlearning", "interactive_infographic", "motion_video",
"static_infographic"):
assert brands[key] == ["contiva"], key
for key in ("book", "course", "arabic_training_bag", "level", "arabic_slides"):
assert brands[key] == ["roya"], key
assert all(set(b) <= {"contiva", "roya"} and b for b in brands.values())
level = next(t for t in items if t["id"] == "level")
assert level["nameAr"] == "مستوى تدريبي"
assert {v["id"] for v in level["variants"]} >= {"units_supplied", "outline_generated"}
supplied = next(v for v in level["variants"] if v["id"] == "units_supplied")
assert supplied["inputSchema"]["properties"]["units"]["type"] == "array"
assert len(level["stages"]) == 7
async def test_task_types_refuses_an_anonymous_caller(api_db):
async with _client(api_db, None) as c:
r = await c.get("/v1/task-types")
assert r.status_code == 401
async def test_unknown_pipeline_is_404(api_db):
async with _client(api_db, None) as c:
r = await c.get("/v1/pipelines/does-not-exist")
assert r.status_code == 404
# ------------------------------------------------------------------ authgate
@pytest.mark.parametrize(
"method,path",
[
("get", "/v1/me"),
("get", "/v1/projects"),
("post", "/v1/projects"),
("get", "/v1/admin/overview"),
("get", "/v1/admin/users"),
],
)
async def test_protected_endpoints_reject_anonymous_callers(api_db, method, path):
async with _client(api_db, None) as c:
r = await c.request(method.upper(), path, json={} if method == "post" else None)
assert r.status_code in (401, 403), f"{path} let an anonymous caller through"
# ------------------------------------------------------------------ projects
async def test_create_and_list_a_project(api_db, alice):
async with _client(api_db, alice) as c:
created = await c.post(
"/v1/projects",
json={"kind": "book", "title": "كتاب القيادة", "description": "تجربة"},
)
assert created.status_code == 201
body = created.json()
assert body["title"] == "كتاب القيادة"
assert body["owner_id"] == str(alice.id)
listed = await c.get("/v1/projects")
assert listed.json()["total"] == 1
async def test_project_kind_must_be_a_known_pipeline(api_db, alice):
async with _client(api_db, alice) as c:
r = await c.post("/v1/projects", json={"kind": "podcast", "title": "x"})
assert r.status_code == 422
assert "not a known project type" in r.json()["detail"]
async def test_one_user_cannot_read_another_users_project(api_db, alice, bob):
async with _client(api_db, alice) as c:
created = await c.post("/v1/projects", json={"kind": "book", "title": "alice's"})
project_id = created.json()["id"]
async with _client(api_db, bob) as c:
r = await c.get(f"/v1/projects/{project_id}")
# 404 rather than 403 - bob should not learn that this id exists
assert r.status_code == 404
async def test_starting_a_run_returns_202_and_queues_it(api_db, alice, monkeypatch):
queued: list[uuid.UUID] = []
async def fake_enqueue(run_id):
queued.append(run_id)
monkeypatch.setattr("app.api.v1.routes.enqueue_run", fake_enqueue)
async with _client(api_db, alice) as c:
project = (await c.post("/v1/projects", json={"kind": "book", "title": "t"})).json()
r = await c.post(
f"/v1/projects/{project['id']}/runs",
json={"params": {"brief": {"book_title": "x", "target_audience": "y"}}},
)
assert r.status_code == 202
assert r.json()["status"] == "queued"
assert len(queued) == 1
async def test_a_project_cannot_have_two_runs_at_once(api_db, alice, monkeypatch):
monkeypatch.setattr("app.api.v1.routes.enqueue_run", lambda run_id: _noop())
async def _noop():
return None
async with _client(api_db, alice) as c:
project = (await c.post("/v1/projects", json={"kind": "book", "title": "t"})).json()
first = await c.post(f"/v1/projects/{project['id']}/runs", json={})
second = await c.post(f"/v1/projects/{project['id']}/runs", json={})
assert first.status_code == 202
assert second.status_code == 409
# --------------------------------------------------------------------- admin
async def test_admin_endpoints_reject_a_normal_user(api_db, alice):
async with _client(api_db, alice) as c:
r = await c.get("/v1/admin/overview")
assert r.status_code == 403
async def test_admin_overview_reports_zeros_on_an_empty_system(api_db):
admin = User(id=uuid.uuid4(), email="admin@test.local", role=UserRole.admin,
status=UserStatus.active)
api_db.add(admin)
await api_db.commit()
async with _client(api_db, admin) as c:
r = await c.get("/v1/admin/overview")
assert r.status_code == 200
body = r.json()
assert body["users_total"] == 1
assert body["cost_all_time_usd"] == 0.0
async def test_admin_cannot_demote_themselves(api_db):
admin = User(id=uuid.uuid4(), email="admin2@test.local", role=UserRole.admin,
status=UserStatus.active)
api_db.add(admin)
await api_db.commit()
async with _client(api_db, admin) as c:
r = await c.patch(f"/v1/admin/users/{admin.id}", json={"role": "user"})
assert r.status_code == 409
async def test_run_detail_does_not_reach_for_a_lazy_relationship(api_db, alice, monkeypatch):
"""RunDetailOut declares `steps`, and Run has a relationship of the same
name. Validating the ORM object against the schema made Pydantic load it
lazily from a thread with no greenlet - a 500 on every run screen."""
from app.db.models import Project, Run, RunStatus, StepRun, StepStatus
project = Project(owner_id=alice.id, kind="book", title="كتاب")
api_db.add(project)
await api_db.commit()
run = Run(project_id=project.id, pipeline_key="book", params={},
created_by=alice.id, status=RunStatus.running, current_step="book.units")
api_db.add(run)
await api_db.flush()
api_db.add_all([
StepRun(run_id=run.id, step_key="book.units", position=0,
status=StepStatus.succeeded, output={"units": []}),
StepRun(run_id=run.id, step_key="book.outline", position=1,
status=StepStatus.pending),
])
await api_db.commit()
async with _client(api_db, alice) as c:
r = await c.get(f"/v1/runs/{run.id}")
assert r.status_code == 200, r.text[:300]
body = r.json()
assert body["status"] == "running"
assert [s["step_key"] for s in body["steps"]] == ["book.units", "book.outline"]
# has_output is computed per step, not read off the relationship.
assert body["steps"][0]["has_output"] is True
assert body["steps"][1]["has_output"] is False
# ------------------------------------------------ the brief is checked at the door
# Every variant carries an input model, the catalogue publishes it as JSON
# Schema, and the web app builds and enforces the form from it. The server was
# not checking it at all - params went straight into the run. A value the form
# would have refused was accepted, the blueprint step ran and was paid for, the
# user approved it, and the run died two steps later on a Pydantic error about
# a field they were never offered a chance to correct.
async def test_a_brief_that_breaks_its_own_form_is_refused_before_anything_runs(
api_db, alice, monkeypatch
):
queued: list[uuid.UUID] = []
async def fake_enqueue(run_id):
queued.append(run_id)
monkeypatch.setattr("app.api.v1.routes.enqueue_run", fake_enqueue)
async with _client(api_db, alice) as c:
project = (
await c.post("/v1/projects", json={"kind": "training_bag", "title": "t"})
).json()
r = await c.post(
f"/v1/projects/{project['id']}/runs",
json={
"params": {
"variant": "from_text",
# The published form declares a minimum length.
"brief": {"blueprint_text": "too short"},
}
},
)
assert r.status_code == 422, r.text
body = r.json()["detail"]
assert "blueprint_text" in str(body), body
assert queued == [], "the run was queued despite an invalid brief"
async def test_an_unknown_route_is_refused(api_db, alice):
"""The entry step branches on this and falls through to a default, so a
typo quietly runs something other than what was asked for."""
async with _client(api_db, alice) as c:
project = (
await c.post("/v1/projects", json={"kind": "training_bag", "title": "t"})
).json()
r = await c.post(
f"/v1/projects/{project['id']}/runs",
json={"params": {"variant": "from_txt", "brief": {}}},
)
assert r.status_code == 422
assert "from_text" in r.json()["detail"], "the message should list the real routes"
async def test_a_valid_brief_still_starts(api_db, alice, monkeypatch):
"""The guard must not become a second, stricter form."""
monkeypatch.setattr("app.api.v1.routes.enqueue_run", lambda run_id: _noop())
async def _noop():
return None
async with _client(api_db, alice) as c:
project = (
await c.post("/v1/projects", json={"kind": "training_bag", "title": "t"})
).json()
r = await c.post(
f"/v1/projects/{project['id']}/runs",
json={
"params": {
"variant": "from_text",
"brief": {
"blueprint_text": "Program Title: X\n" + "y" * 80,
"question_count": 10,
"activities_per_day": 2,
},
}
},
)
assert r.status_code == 202, r.text
def test_health_says_which_commit_it_is_running():
"""«Did the deploy land?» had no answer, so it was guessed at from a
marker that turned out to belong to an earlier commit - and a run was
resumed against code that was still building, failing with the very
error the deploy was meant to fix.
The deploy writes the SHA into `app/BUILD.txt`; this reads it back.
"""
from app.main import _build
got = _build()
assert got and got != "unknown"