From d4cb0cd5028dd5f8f14aa97c1c2f44d7edb7f487 Mon Sep 17 00:00:00 2001 From: romasku Date: Tue, 18 Aug 2020 20:41:35 +0300 Subject: [PATCH] Fix no error for non-recursive iterrmove of folder (#237) Co-authored-by: Roman Skurikhin --- platform_storage_api/api.py | 5 ++++- tests/integration/test_api.py | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/platform_storage_api/api.py b/platform_storage_api/api.py index 8cd08e94..5a0365e9 100644 --- a/platform_storage_api/api.py +++ b/platform_storage_api/api.py @@ -557,11 +557,14 @@ async def _handle_iterdelete( recursive = request.query.get("recursive", "true") == "true" response = web.StreamResponse() response.headers["Content-Type"] = "application/x-ndjson" - await response.prepare(request) + request_prepared = False try: async for remove_listing in await self._storage.iterremove( storage_path, recursive=recursive ): + if not request_prepared: + await response.prepare(request) + request_prepared = True listing_dict = { "path": str(remove_listing.path), "is_dir": remove_listing.is_dir, diff --git a/tests/integration/test_api.py b/tests/integration/test_api.py index 5198cc2f..476e44b2 100644 --- a/tests/integration/test_api.py +++ b/tests/integration/test_api.py @@ -685,8 +685,10 @@ async def test_iterdelete_dir_with_file( } @pytest.mark.asyncio + @pytest.mark.parametrize("use_stream_response", [False, True]) async def test_cant_delete_folder_without_non_recursive( self, + use_stream_response: bool, server_url: str, client: aiohttp.ClientSession, regular_user_factory: Callable[[], User], @@ -696,12 +698,15 @@ async def test_cant_delete_folder_without_non_recursive( headers = {"Authorization": "Bearer " + user.token} params_mkdir = {"op": "MKDIRS"} params_delete = {"recursive": "false"} - path_str = f"/{user.name}/new/nested/{uuid.uuid4()}" + path_str = f"/{user.name}/new/nested/foobar222/{uuid.uuid4()}" url = f"{server_url}{path_str}" async with client.put(url, headers=headers, params=params_mkdir) as response: assert response.status == aiohttp.web.HTTPCreated.status_code + if use_stream_response: + headers["Accept"] = "application/x-ndjson" + async with client.delete( url, headers=headers, params=params_delete ) as response: