homelab_automation/tests/test_help_downloads.py

79 lines
2.1 KiB
Python

"""Tests for help documentation download endpoints helpers.
We keep this lightweight by validating that the help endpoints are registered
and that the PDF generator returns a non-empty PDF payload.
"""
from __future__ import annotations
import sys
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
# Ensure project root on path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
@pytest.mark.asyncio
async def test_help_markdown_builder_exists_and_non_empty():
from app import create_app
app = create_app()
client = TestClient(app)
resp = client.get(
"/api/help/documentation.md",
headers={"X-API-Key": "dev-key-1234567890"},
)
assert resp.status_code == 200
md = resp.text
assert isinstance(md, str)
assert len(md) > 100
assert "Guide d'Utilisation" in md or "Démarrage Rapide" in md
@pytest.mark.asyncio
async def test_help_pdf_generator_returns_pdf_bytes():
pytest.importorskip("reportlab")
pytest.importorskip("PIL")
from app import create_app
app = create_app()
client = TestClient(app)
resp = client.get(
"/api/help/documentation.pdf",
headers={"X-API-Key": "dev-key-1234567890"},
)
assert resp.status_code == 200
pdf_bytes = resp.content
assert isinstance(pdf_bytes, (bytes, bytearray))
assert len(pdf_bytes) > 1000
assert bytes(pdf_bytes[:4]) == b"%PDF"
def test_help_routes_registered():
from app import create_app
app = create_app()
paths = {r.path for r in app.routes}
assert "/api/help/content" in paths
assert "/api/help/documentation.md" in paths
assert "/api/help/documentation.pdf" in paths
def test_help_content_endpoint_returns_html_and_toc():
from app import create_app
app = create_app()
client = TestClient(app)
resp = client.get(
"/api/help/content",
headers={"X-API-Key": "dev-key-1234567890"},
)
assert resp.status_code == 200
data = resp.json()
assert isinstance(data.get("content"), str)
assert isinstance(data.get("toc"), str)
assert "help-quickstart" in data["toc"]