fix(report): eliminate temp file leak in MD fallback; simplify invalid-format test
This commit is contained in:
parent
8efedb55e5
commit
7daf8566ed
|
|
@ -5,7 +5,6 @@ Provides simulation report generation, retrieval, and chat endpoints
|
||||||
|
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
import tempfile
|
|
||||||
import traceback
|
import traceback
|
||||||
import threading
|
import threading
|
||||||
import markdown as md_lib
|
import markdown as md_lib
|
||||||
|
|
@ -480,12 +479,12 @@ def download_report(report_id: str):
|
||||||
as_attachment=True,
|
as_attachment=True,
|
||||||
download_name=f"{report_id}.md"
|
download_name=f"{report_id}.md"
|
||||||
)
|
)
|
||||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.md',
|
return send_file(
|
||||||
delete=False, encoding='utf-8') as f:
|
io.BytesIO(markdown_content.encode('utf-8')),
|
||||||
f.write(markdown_content)
|
mimetype='text/markdown; charset=utf-8',
|
||||||
temp_path = f.name
|
as_attachment=True,
|
||||||
return send_file(temp_path, as_attachment=True,
|
download_name=f"{report_id}.md"
|
||||||
download_name=f"{report_id}.md")
|
)
|
||||||
|
|
||||||
# fmt == 'pdf'
|
# fmt == 'pdf'
|
||||||
pdf_bytes = _generate_pdf_bytes(markdown_content)
|
pdf_bytes = _generate_pdf_bytes(markdown_content)
|
||||||
|
|
|
||||||
|
|
@ -80,13 +80,7 @@ class TestDownloadPDF:
|
||||||
|
|
||||||
assert resp.status_code == 404
|
assert resp.status_code == 404
|
||||||
|
|
||||||
def test_download_pdf_invalid_format(self, client, tmp_path):
|
def test_download_pdf_invalid_format(self, client):
|
||||||
"""Returns 400 for unknown format parameter."""
|
"""Returns 400 for unknown format parameter."""
|
||||||
mock_report = _make_mock_report()
|
resp = client.get('/api/report/any_id/download?format=docx')
|
||||||
md_path = _make_md_file(tmp_path, mock_report.report_id, mock_report.markdown_content)
|
|
||||||
|
|
||||||
with patch('app.api.report.ReportManager.get_report', return_value=mock_report), \
|
|
||||||
patch('app.api.report.ReportManager._get_report_markdown_path', return_value=md_path):
|
|
||||||
resp = client.get(f'/api/report/{mock_report.report_id}/download?format=docx')
|
|
||||||
|
|
||||||
assert resp.status_code == 400
|
assert resp.status_code == 400
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue