156 lines
4.8 KiB
Python
156 lines
4.8 KiB
Python
import os
|
|
import uuid
|
|
from datetime import date
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
os.environ.setdefault("SECRET_KEY", "x" * 32)
|
|
os.environ.setdefault("DATABASE_URL", "sqlite:////tmp/delphi_test.sqlite")
|
|
|
|
from app.main import app # noqa: E402
|
|
from app.auth.security import get_current_user # noqa: E402
|
|
from tests.helpers import assert_validation_error, assert_http_error # noqa: E402
|
|
|
|
|
|
class _User:
|
|
def __init__(self):
|
|
self.id = 1
|
|
self.username = "tester"
|
|
self.is_admin = True
|
|
self.is_active = True
|
|
|
|
|
|
@pytest.fixture()
|
|
def client():
|
|
app.dependency_overrides[get_current_user] = lambda: _User()
|
|
try:
|
|
yield TestClient(app)
|
|
finally:
|
|
app.dependency_overrides.pop(get_current_user, None)
|
|
|
|
|
|
def _create_customer_and_file(client: TestClient):
|
|
cust_id = f"DOC-{uuid.uuid4().hex[:8]}"
|
|
resp = client.post("/api/customers/", json={"id": cust_id, "last": "Doc", "email": "d@example.com"})
|
|
assert resp.status_code == 200
|
|
file_no = f"D-{uuid.uuid4().hex[:6]}"
|
|
payload = {
|
|
"file_no": file_no,
|
|
"id": cust_id,
|
|
"regarding": "Doc matter",
|
|
"empl_num": "E01",
|
|
"file_type": "CIVIL",
|
|
"opened": date.today().isoformat(),
|
|
"status": "ACTIVE",
|
|
"rate_per_hour": 100.0,
|
|
}
|
|
resp = client.post("/api/files/", json=payload)
|
|
assert resp.status_code == 200
|
|
return cust_id, file_no
|
|
|
|
|
|
def test_qdro_schema_validation_and_404s(client: TestClient):
|
|
# Missing required: file_no
|
|
resp = client.post("/api/documents/qdros/", json={"version": "01"})
|
|
assert_validation_error(resp, "file_no")
|
|
|
|
# Bad dates type
|
|
resp = client.post(
|
|
"/api/documents/qdros/",
|
|
json={
|
|
"file_no": "NOFILE-1",
|
|
"created_date": "not-a-date",
|
|
"approved_date": "also-bad",
|
|
},
|
|
)
|
|
assert_validation_error(resp, "created_date")
|
|
|
|
# 404 get/update/delete for missing
|
|
resp = client.get("/api/documents/qdros/NOFILE-1/999999")
|
|
assert_http_error(resp, 404, "QDRO not found")
|
|
resp = client.put("/api/documents/qdros/NOFILE-1/999999", json={"status": "APPROVED"})
|
|
assert_http_error(resp, 404, "QDRO not found")
|
|
resp = client.delete("/api/documents/qdros/NOFILE-1/999999")
|
|
assert_http_error(resp, 404, "QDRO not found")
|
|
|
|
|
|
def test_qdro_end_to_end_crud(client: TestClient):
|
|
_, file_no = _create_customer_and_file(client)
|
|
|
|
# Create
|
|
create_payload = {
|
|
"file_no": file_no,
|
|
"version": "01",
|
|
"status": "DRAFT",
|
|
"created_date": date.today().isoformat(),
|
|
"plan_name": "Plan X",
|
|
"plan_administrator": "Admin",
|
|
}
|
|
resp = client.post("/api/documents/qdros/", json=create_payload)
|
|
assert resp.status_code == 200
|
|
qdro = resp.json()
|
|
qid = qdro["id"]
|
|
assert qdro["file_no"] == file_no
|
|
|
|
# List by file
|
|
resp = client.get(f"/api/documents/qdros/{file_no}")
|
|
assert resp.status_code == 200
|
|
assert any(item["id"] == qid for item in resp.json())
|
|
|
|
# Get by composite path
|
|
resp = client.get(f"/api/documents/qdros/{file_no}/{qid}")
|
|
assert resp.status_code == 200
|
|
assert resp.json()["id"] == qid
|
|
|
|
# Update
|
|
resp = client.put(f"/api/documents/qdros/{file_no}/{qid}", json={"status": "APPROVED"})
|
|
assert resp.status_code == 200
|
|
assert resp.json()["status"] == "APPROVED"
|
|
|
|
# Delete
|
|
resp = client.delete(f"/api/documents/qdros/{file_no}/{qid}")
|
|
assert resp.status_code == 200
|
|
|
|
|
|
def test_template_crud_and_generate_requires_file_and_shapes(client: TestClient):
|
|
# Create a template
|
|
tid = f"TMP-{uuid.uuid4().hex[:6]}"
|
|
tpl_payload = {
|
|
"form_id": tid,
|
|
"form_name": "Letter",
|
|
"category": "GENERAL",
|
|
"content": "Hello {{CLIENT_FULL}} on ^TODAY for file ^FILE_NO",
|
|
}
|
|
resp = client.post("/api/documents/templates/", json=tpl_payload)
|
|
assert resp.status_code == 200
|
|
|
|
# Get template
|
|
resp = client.get(f"/api/documents/templates/{tid}")
|
|
assert resp.status_code == 200
|
|
assert resp.json()["form_id"] == tid
|
|
|
|
# Update template
|
|
resp = client.put(f"/api/documents/templates/{tid}", json={"content": "Updated"})
|
|
assert resp.status_code == 200
|
|
assert "content" in resp.json()
|
|
|
|
# Generate requires an existing file
|
|
_, file_no = _create_customer_and_file(client)
|
|
resp = client.post(
|
|
f"/api/documents/generate/{tid}",
|
|
json={"template_id": tid, "file_no": file_no, "output_format": "HTML"},
|
|
)
|
|
assert resp.status_code == 200
|
|
gen = resp.json()
|
|
assert {"document_id", "file_name", "file_path", "size", "created_at"} <= set(gen.keys())
|
|
|
|
# 404 when file missing
|
|
resp = client.post(
|
|
f"/api/documents/generate/{tid}",
|
|
json={"template_id": tid, "file_no": "X-NOFILE", "output_format": "PDF"},
|
|
)
|
|
assert_http_error(resp, 404, "File not found")
|
|
|
|
|