11import os
2+ from pathlib import Path
23import pytest
34from fastapi .testclient import TestClient
45from app .main import app
@@ -40,4 +41,43 @@ def test_report_pdf_endpoint():
4041 }
4142 response = client .post ("/api/report/report-pdf" , json = report_data )
4243 assert response .status_code == 200
43- assert response .headers ["content-type" ] == "application/pdf"
44+ assert response .headers ["content-type" ] == "application/pdf"
45+
46+
47+ def test_report_pdf_endpoint_removes_temp_file (tmp_path , monkeypatch ):
48+ temp_pdf = tmp_path / "report.pdf"
49+
50+ class DummyTempFile :
51+ def __init__ (self , path : Path ):
52+ self .name = str (path )
53+ self ._file = path .open ("wb" )
54+
55+ def __enter__ (self ):
56+ return self
57+
58+ def __exit__ (self , exc_type , exc , tb ):
59+ self ._file .close ()
60+
61+ class FakeHTML :
62+ def __init__ (self , string : str ):
63+ self .string = string
64+
65+ def write_pdf (self , path : str ):
66+ Path (path ).write_bytes (b"%PDF-1.4 fake" )
67+
68+ monkeypatch .setattr ("app.routers.reports.tempfile.NamedTemporaryFile" , lambda ** kwargs : DummyTempFile (temp_pdf ))
69+ monkeypatch .setattr ("app.routers.reports.HTML" , FakeHTML )
70+
71+ report_data = {
72+ "report_data" : {
73+ "asl_parameters" : [("param1" , "value1" )],
74+ "missing_parameters" : [],
75+ "basic_report" : "Basic report" ,
76+ "extended_report" : "Extended report" ,
77+ }
78+ }
79+
80+ response = client .post ("/api/report/report-pdf" , json = report_data )
81+
82+ assert response .status_code == 200
83+ assert not temp_pdf .exists ()
0 commit comments