feat: add PDF export generation via WeasyPrint with branded template
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
96
backend/tests/test_pdf_export.py
Normal file
96
backend/tests/test_pdf_export.py
Normal file
@@ -0,0 +1,96 @@
|
||||
"""Tests for PDF export via WeasyPrint."""
|
||||
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestPDFExport:
|
||||
"""Test PDF export endpoint."""
|
||||
|
||||
async def test_export_pdf_returns_pdf_content(
|
||||
self, client: AsyncClient, auth_headers: dict, test_tree: dict
|
||||
):
|
||||
"""Test that PDF export returns application/pdf content starting with %PDF."""
|
||||
# Create a session
|
||||
create_response = await client.post(
|
||||
"/api/v1/sessions",
|
||||
json={"tree_id": test_tree["id"], "ticket_number": "PDF-001"},
|
||||
headers=auth_headers,
|
||||
)
|
||||
assert create_response.status_code in (200, 201)
|
||||
session_id = create_response.json()["id"]
|
||||
|
||||
# Add a decision so there's content
|
||||
await client.put(
|
||||
f"/api/v1/sessions/{session_id}",
|
||||
json={
|
||||
"decisions": [
|
||||
{
|
||||
"node_id": "root",
|
||||
"question": "Is this a test?",
|
||||
"answer": "Yes",
|
||||
"notes": "PDF export test",
|
||||
"timestamp": "2026-03-17T10:00:00Z",
|
||||
}
|
||||
]
|
||||
},
|
||||
headers=auth_headers,
|
||||
)
|
||||
|
||||
# Export as PDF
|
||||
response = await client.post(
|
||||
f"/api/v1/sessions/{session_id}/export",
|
||||
json={"format": "pdf", "include_tree_info": True},
|
||||
headers=auth_headers,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers["content-type"] == "application/pdf"
|
||||
assert "session-export-" in response.headers.get("content-disposition", "")
|
||||
# PDF files start with %PDF
|
||||
assert response.content[:5] == b"%PDF-"
|
||||
|
||||
async def test_export_pdf_with_no_supporting_data(
|
||||
self, client: AsyncClient, auth_headers: dict, test_tree: dict
|
||||
):
|
||||
"""Test PDF export works when session has no supporting data."""
|
||||
create_response = await client.post(
|
||||
"/api/v1/sessions",
|
||||
json={"tree_id": test_tree["id"]},
|
||||
headers=auth_headers,
|
||||
)
|
||||
assert create_response.status_code in (200, 201)
|
||||
session_id = create_response.json()["id"]
|
||||
|
||||
response = await client.post(
|
||||
f"/api/v1/sessions/{session_id}/export",
|
||||
json={"format": "pdf"},
|
||||
headers=auth_headers,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers["content-type"] == "application/pdf"
|
||||
assert response.content[:5] == b"%PDF-"
|
||||
|
||||
async def test_existing_markdown_export_still_works(
|
||||
self, client: AsyncClient, auth_headers: dict, test_tree: dict
|
||||
):
|
||||
"""Verify markdown export is unaffected by PDF addition."""
|
||||
create_response = await client.post(
|
||||
"/api/v1/sessions",
|
||||
json={"tree_id": test_tree["id"], "ticket_number": "MD-001"},
|
||||
headers=auth_headers,
|
||||
)
|
||||
assert create_response.status_code in (200, 201)
|
||||
session_id = create_response.json()["id"]
|
||||
|
||||
response = await client.post(
|
||||
f"/api/v1/sessions/{session_id}/export",
|
||||
json={"format": "markdown", "include_tree_info": True},
|
||||
headers=auth_headers,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert "text/markdown" in response.headers["content-type"]
|
||||
assert "MD-001" in response.text
|
||||
Reference in New Issue
Block a user