fix(report): eliminate temp file leak in MD fallback; simplify invalid-format test

This commit is contained in:
Ubuntu 2026-04-26 00:11:40 +00:00
parent 8efedb55e5
commit 7daf8566ed
2 changed files with 8 additions and 15 deletions

View File

@ -5,7 +5,6 @@ Provides simulation report generation, retrieval, and chat endpoints
import io
import os
import tempfile
import traceback
import threading
import markdown as md_lib
@ -480,12 +479,12 @@ def download_report(report_id: str):
as_attachment=True,
download_name=f"{report_id}.md"
)
with tempfile.NamedTemporaryFile(mode='w', suffix='.md',
delete=False, encoding='utf-8') as f:
f.write(markdown_content)
temp_path = f.name
return send_file(temp_path, as_attachment=True,
download_name=f"{report_id}.md")
return send_file(
io.BytesIO(markdown_content.encode('utf-8')),
mimetype='text/markdown; charset=utf-8',
as_attachment=True,
download_name=f"{report_id}.md"
)
# fmt == 'pdf'
pdf_bytes = _generate_pdf_bytes(markdown_content)

View File

@ -80,13 +80,7 @@ class TestDownloadPDF:
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."""
mock_report = _make_mock_report()
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')
resp = client.get('/api/report/any_id/download?format=docx')
assert resp.status_code == 400