Files
hinpdof/test_app.py
Hadley Rich 6c6c837301
Some checks failed
Build and Publish Docker Image / build (push) Failing after 17s
Initial
2024-11-11 10:26:47 +13:00

83 lines
2.7 KiB
Python

import pytest
from fastapi.testclient import TestClient
from app import app
@pytest.fixture
def client():
return TestClient(app)
def test_health_check(client):
response = client.get("/health")
assert response.status_code == 200
assert response.json() == {"status": "ok"}
def test_pdf_generation(client):
request_data = {"html": "<h1>Hello, World!</h1>", "filename": "testfile"}
response = client.post("/pdf", json=request_data)
assert response.status_code == 200
assert response.headers["Content-Type"] == "application/pdf"
assert (
response.headers["Content-Disposition"] == 'attachment; filename="testfile.pdf"'
)
def test_pdf_generation_default_filename(client):
request_data = {"html": "<h1>Hello, World!</h1>", "filename": None}
response = client.post("/pdf", json=request_data)
assert response.status_code == 200
assert response.headers["Content-Type"] == "application/pdf"
assert (
response.headers["Content-Disposition"] == 'attachment; filename="hinpdof.pdf"'
)
def test_pdf_generation_invalid_html(client):
request_data = {"html": "", "filename": "testfile"}
response = client.post("/pdf", json=request_data)
assert response.status_code == 422 # Unprocessable Entity due to invalid input
def test_pdf_generation_missing_html(client):
request_data = {"filename": "testfile"}
response = client.post("/pdf", json=request_data)
assert (
response.status_code == 422
) # Unprocessable Entity due to missing required field
def test_pdf_generation_large_html(client):
large_html = "<h1>" + "Hello, World! " * 1000 + "</h1>"
request_data = {"html": large_html, "filename": "largefile"}
response = client.post("/pdf", json=request_data)
assert response.status_code == 200
assert response.headers["Content-Type"] == "application/pdf"
assert (
response.headers["Content-Disposition"]
== 'attachment; filename="largefile.pdf"'
)
def test_pdf_generation_invalid_filename(client):
request_data = {"html": "<h1>Hello, World!</h1>", "filename": "invalid/filename"}
response = client.post("/pdf", json=request_data)
assert response.status_code == 200
assert response.headers["Content-Type"] == "application/pdf"
assert (
response.headers["Content-Disposition"]
== 'attachment; filename="invalid_filename.pdf"'
)
def test_pdf_generation_missing_filename(client):
request_data = {"html": "<h1>Hello, World!</h1>"}
response = client.post("/pdf", json=request_data)
assert response.status_code == 200
assert response.headers["Content-Type"] == "application/pdf"
assert (
response.headers["Content-Disposition"] == 'attachment; filename="hinpdof.pdf"'
)