diff --git a/backend/endpoints/roms/upload.py b/backend/endpoints/roms/upload.py index b69d93ae0..f12d2971a 100644 --- a/backend/endpoints/roms/upload.py +++ b/backend/endpoints/roms/upload.py @@ -279,11 +279,11 @@ async def start_chunked_upload( ) -> dict: """Initiate a chunked ROM upload session.""" - # An empty file takes no chunks and goes straight to /complete. - if total_size > 0 and total_chunks == 0: + # Only an empty file takes no chunks, and it goes straight to /complete. + if (total_size == 0) != (total_chunks == 0): raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, - detail="A non-empty file needs at least one chunk", + detail="Chunk count does not match the file size", ) db_platform = db_platform_handler.get_platform(platform_id) diff --git a/backend/tests/endpoints/roms/test_upload.py b/backend/tests/endpoints/roms/test_upload.py index 3e76c69ce..ae2cdb54b 100644 --- a/backend/tests/endpoints/roms/test_upload.py +++ b/backend/tests/endpoints/roms/test_upload.py @@ -162,18 +162,25 @@ def test_upload_empty_file_without_chunks( assert final_file.read_bytes() == b"" -def test_start_non_empty_file_without_chunks_returns_400( +@pytest.mark.parametrize(("total_size", "total_chunks"), [(11, 0), (0, 2)]) +def test_start_with_chunks_that_do_not_match_the_size_returns_400( client: TestClient, access_token: str, platform: Platform, upload_fs: dict, + total_size: int, + total_chunks: int, ): response = _start_upload( - client, access_token, platform.id, total_size=11, total_chunks=0 + client, + access_token, + platform.id, + total_size=total_size, + total_chunks=total_chunks, ) assert response.status_code == status.HTTP_400_BAD_REQUEST - assert response.json()["detail"] == "A non-empty file needs at least one chunk" + assert response.json()["detail"] == "Chunk count does not match the file size" def test_upload_chunk_invalid_upload_id(client: TestClient, access_token: str):