import io import pytest from httpx import AsyncClient @pytest.fixture async def auth_headers(client: AsyncClient): resp = await client.post("/api/v1/auth/register", json={ "email": "docuser@example.com", "username": "docuser", "password": "testpass123", }) assert resp.status_code == 201 return {"Authorization": f"Bearer {resp.json()['access_token']}"} async def test_upload_document(client: AsyncClient, auth_headers: dict): resp = await client.post( "/api/v1/documents/?doc_type=lab_result", headers=auth_headers, files={"file": ("test.pdf", b"%PDF-1.4 test content", "application/pdf")}, ) assert resp.status_code == 201 data = resp.json() assert data["original_filename"] == "test.pdf" assert data["doc_type"] == "lab_result" assert data["processing_status"] == "pending" async def test_upload_invalid_type(client: AsyncClient, auth_headers: dict): resp = await client.post( "/api/v1/documents/", headers=auth_headers, files={"file": ("test.exe", b"MZ...", "application/x-msdownload")}, ) assert resp.status_code == 400 async def test_list_documents(client: AsyncClient, auth_headers: dict): # Upload first await client.post( "/api/v1/documents/", headers=auth_headers, files={"file": ("list_test.pdf", b"%PDF-1.4 content", "application/pdf")}, ) resp = await client.get("/api/v1/documents/", headers=auth_headers) assert resp.status_code == 200 assert len(resp.json()["documents"]) >= 1 async def test_get_document(client: AsyncClient, auth_headers: dict): resp = await client.post( "/api/v1/documents/", headers=auth_headers, files={"file": ("get_test.pdf", b"%PDF-1.4 content", "application/pdf")}, ) doc_id = resp.json()["id"] resp = await client.get(f"/api/v1/documents/{doc_id}", headers=auth_headers) assert resp.status_code == 200 assert resp.json()["id"] == doc_id async def test_delete_document(client: AsyncClient, auth_headers: dict): resp = await client.post( "/api/v1/documents/", headers=auth_headers, files={"file": ("del_test.pdf", b"%PDF-1.4 content", "application/pdf")}, ) doc_id = resp.json()["id"] resp = await client.delete(f"/api/v1/documents/{doc_id}", headers=auth_headers) assert resp.status_code == 204 resp = await client.get(f"/api/v1/documents/{doc_id}", headers=auth_headers) assert resp.status_code == 404 async def test_document_ownership_isolation(client: AsyncClient, auth_headers: dict): resp = await client.post( "/api/v1/documents/", headers=auth_headers, files={"file": ("private.pdf", b"%PDF-1.4 content", "application/pdf")}, ) doc_id = resp.json()["id"] # Register another user resp = await client.post("/api/v1/auth/register", json={ "email": "docother@example.com", "username": "docother", "password": "testpass123", }) other_headers = {"Authorization": f"Bearer {resp.json()['access_token']}"} resp = await client.get(f"/api/v1/documents/{doc_id}", headers=other_headers) assert resp.status_code == 404