Files
Aegis/backend/tests/test_professional_reports_router.py
T
kitos e951567ba1
Aegis CI / lint-and-test (push) Has been cancelled
Snyk Security Scan / Python vulnerabilities (backend) (push) Has been cancelled
Snyk Security Scan / npm vulnerabilities (frontend) (push) Has been cancelled
Snyk Security Scan / Docker image vulnerabilities (backend) (push) Has been cancelled
fix(scores,reports): fix scores/history 500, disable unfinished report generation cleanly
- /scores/history was annotated -> dict but the service actually returns
  a list, so FastAPI's response validation raised an unhandled exception,
  surfacing as a raw 500 on every call. Fixed the annotation.
- The professional_reports.py endpoints (PDF/DOCX/HTML generation) back
  a Reports feature the frontend already hides entirely (unfinished).
  Hitting them directly fell through to whatever the render pipeline
  raised (e.g. missing weasyprint system deps) as an unhelpful 500.
  Added a REPORTS_ENABLED setting (off by default) so they now return a
  clean 503 instead, without removing the routes or their test coverage.
2026-07-24 15:24:06 +02:00

63 lines
2.3 KiB
Python

"""Professional reports router tests (FASE-2.4)."""
import os
import tempfile
from unittest.mock import patch
from app.models.campaign import Campaign
from app.config import settings
@patch("app.services.report_generation_service.generate_purple_campaign_report")
def test_purple_campaign_pdf_download(mock_gen, client, auth_headers, db):
with tempfile.TemporaryDirectory() as tmpdir:
fake_pdf = os.path.join(tmpdir, "report.pdf")
with open(fake_pdf, "wb") as f:
f.write(b"%PDF-1.4 fake")
mock_gen.return_value = fake_pdf
with patch.object(settings, "REPORT_OUTPUT_DIR", tmpdir), \
patch.object(settings, "REPORTS_ENABLED", True):
campaign = Campaign(name="Export Camp", status="active")
db.add(campaign)
db.commit()
r = client.get(
f"/api/v1/reports/generate/purple-campaign/{campaign.id}",
params={"format": "pdf"},
headers=auth_headers,
)
assert r.status_code == 200
assert r.headers["content-type"] == "application/pdf"
@patch("app.services.report_generation_service.generate_coverage_report")
def test_coverage_summary_html(mock_gen, client, auth_headers):
with tempfile.TemporaryDirectory() as tmpdir:
fake_html = os.path.join(tmpdir, "report.html")
with open(fake_html, "w") as f:
f.write("<html><body>ok</body></html>")
mock_gen.return_value = fake_html
with patch.object(settings, "REPORT_OUTPUT_DIR", tmpdir), \
patch.object(settings, "REPORTS_ENABLED", True):
r = client.get(
"/api/v1/reports/generate/coverage-summary",
params={"format": "html"},
headers=auth_headers,
)
assert r.status_code == 200
assert "text/html" in r.headers["content-type"]
def test_reports_disabled_by_default_returns_503(client, auth_headers):
"""Reports are unfinished — the frontend hides the UI entirely, and the
backend must refuse cleanly rather than let a half-working render
pipeline (e.g. missing weasyprint system deps) surface a raw 500."""
r = client.get(
"/api/v1/reports/generate/coverage-summary",
params={"format": "pdf"},
headers=auth_headers,
)
assert r.status_code == 503