feat: enhance production readiness with environment configuration, dependency checks, and sitemap updates
This commit is contained in:
@@ -52,13 +52,36 @@ class TestFileValidator:
|
||||
mock_file.tell = content.tell
|
||||
mock_file.read = content.read
|
||||
|
||||
with patch('app.utils.file_validator.magic') as mock_magic:
|
||||
with patch('app.utils.file_validator.HAS_MAGIC', True), patch(
|
||||
'app.utils.file_validator.magic', create=True
|
||||
) as mock_magic:
|
||||
mock_magic.from_buffer.return_value = 'application/pdf'
|
||||
filename, ext = validate_file(mock_file, allowed_types=["pdf"])
|
||||
|
||||
assert filename == 'document.pdf'
|
||||
assert ext == 'pdf'
|
||||
|
||||
def test_valid_html_passes(self, app):
|
||||
"""Should accept valid HTML file with correct MIME type."""
|
||||
with app.app_context():
|
||||
html_bytes = b'<!doctype html><html><body>Hello</body></html>'
|
||||
content = io.BytesIO(html_bytes)
|
||||
|
||||
mock_file = MagicMock()
|
||||
mock_file.filename = 'page.html'
|
||||
mock_file.seek = content.seek
|
||||
mock_file.tell = content.tell
|
||||
mock_file.read = content.read
|
||||
|
||||
with patch('app.utils.file_validator.HAS_MAGIC', True), patch(
|
||||
'app.utils.file_validator.magic', create=True
|
||||
) as mock_magic:
|
||||
mock_magic.from_buffer.return_value = 'text/html'
|
||||
filename, ext = validate_file(mock_file, allowed_types=["html", "htm"])
|
||||
|
||||
assert filename == 'page.html'
|
||||
assert ext == 'html'
|
||||
|
||||
def test_mime_mismatch_raises(self, app):
|
||||
"""Should raise when MIME type doesn't match extension."""
|
||||
with app.app_context():
|
||||
@@ -70,7 +93,9 @@ class TestFileValidator:
|
||||
mock_file.tell = content.tell
|
||||
mock_file.read = content.read
|
||||
|
||||
with patch('app.utils.file_validator.magic') as mock_magic:
|
||||
with patch('app.utils.file_validator.HAS_MAGIC', True), patch(
|
||||
'app.utils.file_validator.magic', create=True
|
||||
) as mock_magic:
|
||||
mock_magic.from_buffer.return_value = 'text/plain'
|
||||
with pytest.raises(FileValidationError, match="does not match"):
|
||||
validate_file(mock_file, allowed_types=["pdf"])
|
||||
@@ -102,7 +127,9 @@ class TestFileValidator:
|
||||
mock_file.tell = content.tell
|
||||
mock_file.read = content.read
|
||||
|
||||
with patch('app.utils.file_validator.magic') as mock_magic:
|
||||
with patch('app.utils.file_validator.HAS_MAGIC', True), patch(
|
||||
'app.utils.file_validator.magic', create=True
|
||||
) as mock_magic:
|
||||
mock_magic.from_buffer.return_value = 'application/pdf'
|
||||
with pytest.raises(FileValidationError, match="unsafe"):
|
||||
validate_file(mock_file, allowed_types=["pdf"])
|
||||
validate_file(mock_file, allowed_types=["pdf"])
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
import io
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from app.services.html_to_pdf_service import _get_dependency_mismatch_error
|
||||
|
||||
|
||||
class TestHtmlToPdf:
|
||||
def test_no_file(self, client):
|
||||
@@ -41,3 +43,33 @@ class TestHtmlToPdf:
|
||||
assert response.status_code == 202
|
||||
json_data = response.get_json()
|
||||
assert 'task_id' in json_data
|
||||
|
||||
def test_detects_weasyprint_pydyf_version_mismatch(self, monkeypatch):
|
||||
"""Should flag the known WeasyPrint/pydyf incompatibility."""
|
||||
versions = {
|
||||
'weasyprint': '61.2',
|
||||
'pydyf': '0.12.1',
|
||||
}
|
||||
monkeypatch.setattr(
|
||||
'app.services.html_to_pdf_service._get_installed_version',
|
||||
lambda package_name: versions.get(package_name),
|
||||
)
|
||||
|
||||
error = _get_dependency_mismatch_error()
|
||||
|
||||
assert error is not None
|
||||
assert 'WeasyPrint 61.2' in error
|
||||
assert 'pydyf 0.12.1' in error
|
||||
|
||||
def test_allows_compatible_weasyprint_pydyf_versions(self, monkeypatch):
|
||||
"""Should not flag compatible dependency versions."""
|
||||
versions = {
|
||||
'weasyprint': '61.2',
|
||||
'pydyf': '0.10.0',
|
||||
}
|
||||
monkeypatch.setattr(
|
||||
'app.services.html_to_pdf_service._get_installed_version',
|
||||
lambda package_name: versions.get(package_name),
|
||||
)
|
||||
|
||||
assert _get_dependency_mismatch_error() is None
|
||||
|
||||
Reference in New Issue
Block a user