from __future__ import annotations import base64 import uuid import pytest from httpx import AsyncClient from sqlalchemy.ext.asyncio import AsyncSession from app.db.models import Document, IngestStatus, Report, ReportStatus def _tiny_png_bytes() -> bytes: # 1x1 PNG return base64.b64decode( "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO6q3d8AAAAASUVORK5CYII=" ) @pytest.mark.asyncio async def test_photo_policy_endpoint_returns_sections(async_client: AsyncClient, test_db: AsyncSession) -> None: tenant_id = "tenant_test" doc = Document( id=str(uuid.uuid4()), tenant_id=tenant_id, filename="x.pdf", file_path="C:/tmp/x.pdf", status=IngestStatus.complete, survey_level=3, ) rep = Report( id=str(uuid.uuid4()), tenant_id=tenant_id, document_id=doc.id, status=ReportStatus.pending, survey_level=3, ) test_db.add_all([doc, rep]) await test_db.commit() r = await async_client.get(f"/reports/{rep.id}/photo-policy") assert r.status_code == 200 data = r.json() assert data["report_id"] == rep.id assert data["survey_level"] == 3 assert isinstance(data["sections"], list) assert any(x["code"] == "D" for x in data["sections"]) assert "policy_configuration_required" in data assert isinstance(data["policy_configuration_required"], bool) assert "indexed_upload_count" in data assert isinstance(data["indexed_upload_count"], int) for row in data["sections"]: assert "source" in row assert row["source"] in ( "override", "tenant_library", "tenant_insufficient", "tenant_no_documents", "data_driven_disabled", ) @pytest.mark.asyncio async def test_upload_list_and_get_section_photo_roundtrip(async_client: AsyncClient, test_db: AsyncSession, tmp_path) -> None: tenant_id = "tenant_test" doc = Document( id=str(uuid.uuid4()), tenant_id=tenant_id, filename="x.pdf", file_path="C:/tmp/x.pdf", status=IngestStatus.complete, survey_level=3, ) rep = Report( id=str(uuid.uuid4()), tenant_id=tenant_id, document_id=doc.id, status=ReportStatus.pending, survey_level=3, ) test_db.add_all([doc, rep]) await test_db.commit() from app.config import settings settings.upload_dir = tmp_path / "uploads" settings.upload_dir.mkdir(parents=True, exist_ok=True) png = _tiny_png_bytes() files = [("files", ("p.png", png, "image/png"))] up = await async_client.post(f"/reports/{rep.id}/sections/D/photos", files=files) assert up.status_code == 200 saved = up.json()["saved"] assert saved and saved[0]["photo_id"] lst = await async_client.get(f"/reports/{rep.id}/sections/D/photos") assert lst.status_code == 200 photos = lst.json()["photos"] assert len(photos) == 1 url = photos[0]["url"] img = await async_client.get(url) assert img.status_code == 200 assert img.headers["content-type"].startswith("image/")