File size: 2,005 Bytes
939c0c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Unit tests for Document API and Ingestion Pipeline.
"""
from __future__ import annotations

import io
import pytest
from httpx import AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession


@pytest.mark.asyncio
async def test_upload_document_flow(client: AsyncClient, db_session: AsyncSession):
    # 1. Register & login
    reg = await client.post("/api/v1/auth/register", json={
        "email": "doc_test@company.com",
        "password": "Password123!",
        "full_name": "Doc Tester",
        "tenant_name": "Doc Corp"
    })
    token = reg.json()["tokens"]["access_token"]
    headers = {"Authorization": f"Bearer {token}"}

    # 2. Upload text file
    file_content = b"This is an enterprise policy document regarding remote work and annual leaves."
    files = {"file": ("policy.txt", io.BytesIO(file_content), "text/plain")}
    data = {"doc_type": "hr"}

    response = await client.post("/api/v1/documents/upload", headers=headers, files=files, data=data)
    assert response.status_code == 202, response.text
    res_data = response.json()
    assert res_data["original_name"] == "policy.txt"
    assert res_data["doc_type"] == "hr"
    doc_id = res_data["id"]

    # 3. Get document details
    response = await client.get(f"/api/v1/documents/{doc_id}", headers=headers)
    assert response.status_code == 200
    assert response.json()["id"] == doc_id

    # 4. List documents
    response = await client.get("/api/v1/documents/", headers=headers)
    assert response.status_code == 200
    docs = response.json()["documents"]
    assert len(docs) >= 1
    assert docs[0]["id"] == doc_id

    # 5. Check status endpoint
    response = await client.get(f"/api/v1/documents/{doc_id}/status", headers=headers)
    assert response.status_code == 200
    assert "status" in response.json()

    # 6. Delete document
    response = await client.delete(f"/api/v1/documents/{doc_id}", headers=headers)
    assert response.status_code == 200
    assert response.json()["id"] == doc_id