|
4 | 4 | from pathlib import Path |
5 | 5 | from tempfile import NamedTemporaryFile |
6 | 6 | from urllib.parse import urlparse |
7 | | -from unittest.mock import patch |
| 7 | +from unittest.mock import patch, MagicMock |
8 | 8 |
|
9 | 9 | import pytest |
10 | 10 | from moto import mock_aws |
@@ -301,6 +301,50 @@ def test_transformation_job_created_in_database( |
301 | 301 | # Check that start_job was called with the right arguments |
302 | 302 | assert "transformer_name" in kwargs or len(args) >= 4 |
303 | 303 |
|
| 304 | + def test_upload_file_within_size_limit( |
| 305 | + self, |
| 306 | + db: Session, |
| 307 | + route: Route, |
| 308 | + scratch: Path, |
| 309 | + uploader: WebUploader, |
| 310 | + ) -> None: |
| 311 | + """Test that a file within the size limit uploads successfully.""" |
| 312 | + aws = AmazonCloudStorageClient() |
| 313 | + aws.create() |
| 314 | + |
| 315 | + # Mock calculate_file_size to return a value just under the 25 MB limit (in KB) |
| 316 | + with patch( |
| 317 | + "app.api.routes.documents.calculate_file_size", |
| 318 | + return_value=25 * 1024 - 1, # 25 MB - 1 KB |
| 319 | + ): |
| 320 | + response = uploader.put(route, scratch) |
| 321 | + |
| 322 | + assert response.status_code == 200 |
| 323 | + |
| 324 | + def test_upload_file_exceeds_size_limit( |
| 325 | + self, |
| 326 | + db: Session, |
| 327 | + route: Route, |
| 328 | + scratch: Path, |
| 329 | + uploader: WebUploader, |
| 330 | + ) -> None: |
| 331 | + """Test that a file exceeding 25 MB returns a 413 error.""" |
| 332 | + aws = AmazonCloudStorageClient() |
| 333 | + aws.create() |
| 334 | + |
| 335 | + # Mock calculate_file_size to return a value over the 25 MB limit (in KB) |
| 336 | + with patch( |
| 337 | + "app.api.routes.documents.calculate_file_size", |
| 338 | + return_value=25 * 1024 + 1, # 25 MB + 1 KB |
| 339 | + ): |
| 340 | + response = uploader.put(route, scratch) |
| 341 | + |
| 342 | + assert response.status_code == 413 |
| 343 | + print("response =", response.json()) |
| 344 | + error_detail = response.json()["error"] |
| 345 | + assert "exceeds the maximum allowed size" in error_detail |
| 346 | + assert "25" in error_detail |
| 347 | + |
304 | 348 | def test_upload_response_structure_without_transformation( |
305 | 349 | self, |
306 | 350 | db: Session, |
|
0 commit comments