feat(backend): PDF deviz + portal client + SMS + invoice service
- PDF generation with WeasyPrint: deviz and factura templates (A4, branding)
- GET /orders/{id}/pdf/deviz returns PDF with order lines and totals
- Client portal (public, no auth): GET /p/{token}, POST /p/{token}/accept|reject
- SMS service (SMSAPI.ro) - skips in dev when no token configured
- Invoice service: create from validated order, auto-number (F-YYYY-NNNN)
- GET /invoices/{id}/pdf returns factura PDF
- Order status_client field for client accept/reject tracking
- Alembic migration for status_client
- 19 passing tests (auth + sync + orders + pdf + portal + invoices)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
142
backend/tests/test_pdf.py
Normal file
142
backend/tests/test_pdf.py
Normal file
@@ -0,0 +1,142 @@
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
|
||||
from app.main import app
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_pdf_deviz_returns_pdf(client, auth_headers):
|
||||
me = await client.get("/api/auth/me", headers=auth_headers)
|
||||
tenant_id = me.json()["tenant_id"]
|
||||
|
||||
vid = str(uuid.uuid4())
|
||||
now = datetime.now(UTC).isoformat()
|
||||
await client.post(
|
||||
"/api/sync/push",
|
||||
headers=auth_headers,
|
||||
json={
|
||||
"operations": [
|
||||
{
|
||||
"table": "vehicles",
|
||||
"id": vid,
|
||||
"operation": "INSERT",
|
||||
"data": {
|
||||
"id": vid,
|
||||
"tenant_id": tenant_id,
|
||||
"nr_inmatriculare": "CT99PDF",
|
||||
"client_nume": "PDF Test",
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
},
|
||||
"timestamp": now,
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
r = await client.post(
|
||||
"/api/orders",
|
||||
headers=auth_headers,
|
||||
json={"vehicle_id": vid},
|
||||
)
|
||||
order_id = r.json()["id"]
|
||||
|
||||
await client.post(
|
||||
f"/api/orders/{order_id}/lines",
|
||||
headers=auth_headers,
|
||||
json={
|
||||
"tip": "manopera",
|
||||
"descriere": "Diagnosticare",
|
||||
"ore": 0.5,
|
||||
"pret_ora": 100,
|
||||
},
|
||||
)
|
||||
|
||||
r = await client.get(
|
||||
f"/api/orders/{order_id}/pdf/deviz",
|
||||
headers=auth_headers,
|
||||
)
|
||||
assert r.status_code == 200
|
||||
assert r.headers["content-type"] == "application/pdf"
|
||||
assert r.content[:4] == b"%PDF"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_invoice_workflow(client, auth_headers):
|
||||
me = await client.get("/api/auth/me", headers=auth_headers)
|
||||
tenant_id = me.json()["tenant_id"]
|
||||
|
||||
vid = str(uuid.uuid4())
|
||||
now = datetime.now(UTC).isoformat()
|
||||
await client.post(
|
||||
"/api/sync/push",
|
||||
headers=auth_headers,
|
||||
json={
|
||||
"operations": [
|
||||
{
|
||||
"table": "vehicles",
|
||||
"id": vid,
|
||||
"operation": "INSERT",
|
||||
"data": {
|
||||
"id": vid,
|
||||
"tenant_id": tenant_id,
|
||||
"nr_inmatriculare": "B01INV",
|
||||
"client_nume": "Factura Test",
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
},
|
||||
"timestamp": now,
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
# Create order + line + validate
|
||||
r = await client.post(
|
||||
"/api/orders",
|
||||
headers=auth_headers,
|
||||
json={"vehicle_id": vid},
|
||||
)
|
||||
order_id = r.json()["id"]
|
||||
|
||||
await client.post(
|
||||
f"/api/orders/{order_id}/lines",
|
||||
headers=auth_headers,
|
||||
json={
|
||||
"tip": "material",
|
||||
"descriere": "Filtru aer",
|
||||
"cantitate": 1,
|
||||
"pret_unitar": 80,
|
||||
"um": "buc",
|
||||
},
|
||||
)
|
||||
|
||||
await client.post(
|
||||
f"/api/orders/{order_id}/validate",
|
||||
headers=auth_headers,
|
||||
)
|
||||
|
||||
# Create invoice
|
||||
r = await client.post(
|
||||
"/api/invoices",
|
||||
headers=auth_headers,
|
||||
json={"order_id": order_id},
|
||||
)
|
||||
assert r.status_code == 200
|
||||
data = r.json()
|
||||
assert "id" in data
|
||||
assert "nr_factura" in data
|
||||
assert data["nr_factura"].startswith("F-")
|
||||
|
||||
# Get invoice PDF
|
||||
invoice_id = data["id"]
|
||||
r = await client.get(
|
||||
f"/api/invoices/{invoice_id}/pdf",
|
||||
headers=auth_headers,
|
||||
)
|
||||
assert r.status_code == 200
|
||||
assert r.headers["content-type"] == "application/pdf"
|
||||
assert r.content[:4] == b"%PDF"
|
||||
Reference in New Issue
Block a user