From c906a449fd360e9c9dd02997dc697489c9968729 Mon Sep 17 00:00:00 2001 From: Oleh Paduchak Date: Tue, 25 Feb 2025 19:49:09 +0200 Subject: [PATCH] fixed googledrive 500 --- addon_imps/storage/google_drive.py | 15 +++++++-- addon_imps/tests/storage/test_google_drive.py | 33 ++++++++++++------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/addon_imps/storage/google_drive.py b/addon_imps/storage/google_drive.py index d4c8231a..8c371779 100644 --- a/addon_imps/storage/google_drive.py +++ b/addon_imps/storage/google_drive.py @@ -15,6 +15,15 @@ ) +ROOT_ITEM_ID = "root" + +ROOT_ITEM = ItemResult( + item_name="Root", + item_type=ItemType.FOLDER, + item_id=ROOT_ITEM_ID, +) + + class GoogleDriveStorageImp(storage.StorageAddonHttpRequestorImp): """storage on google drive @@ -25,13 +34,15 @@ async def get_external_account_id(self, _: dict[str, str]) -> str: return "" async def list_root_items(self, page_cursor: str = "") -> storage.ItemSampleResult: - return ItemSampleResult(items=[await self.get_item_info("root")], total_count=1) + return ItemSampleResult(items=[ROOT_ITEM], total_count=1) async def build_wb_config(self) -> dict: return {"folder": {"id": self.config.connected_root_id}} async def get_item_info(self, item_id: str) -> storage.ItemResult: - item_id = item_id or "root" + if item_id == ROOT_ITEM_ID: + return ROOT_ITEM + async with self.network.GET(f"drive/v3/files/{item_id}") as response: if response.http_status == 200: json = await response.json_content() diff --git a/addon_imps/tests/storage/test_google_drive.py b/addon_imps/tests/storage/test_google_drive.py index b8d3d40e..6e563d4c 100644 --- a/addon_imps/tests/storage/test_google_drive.py +++ b/addon_imps/tests/storage/test_google_drive.py @@ -6,6 +6,7 @@ ) from addon_imps.storage.google_drive import ( + ROOT_ITEM, File, GoogleDriveStorageImp, ) @@ -58,12 +59,12 @@ async def test_list_root_items(self): result = await self.imp.list_root_items() expected_result = ItemSampleResult( - items=[mock_response], + items=[ROOT_ITEM], total_count=1, ) self.assertEqual(expected_result, result) - self.imp.get_item_info.assert_awaited_once_with("root") + self.imp.get_item_info.assert_not_called() async def test_list_child_items(self): args = namedtuple( @@ -157,14 +158,24 @@ async def _test_list_collection_items_ordinary( ], ) - async def test_get_item_info(self): - cases = [("", "root"), ("foo", "foo")] - for item_id in cases: - self.network.reset_mock() - with self.subTest(case=f"case: {item_id=}"): - await self._test_item_info(*item_id) + async def test_get_item_info_root(self): + self.network.reset_mock() + self._patch_get( + { + "kind": "drive#file", + "driveId": "123", + "extra_attribute": "dasdasd", + "mimeType": "application/vnd.google-apps.folder", + "name": "foobar", + "id": "1023", + } + ) + result = await self.imp.get_item_info("root") + self.network.GET.assert_not_called() + assert result == ROOT_ITEM - async def _test_item_info(self, item_id: str, url_segment: str): + async def test_get_item_info(self): + self.network.reset_mock() self._patch_get( { "kind": "drive#file", @@ -175,8 +186,8 @@ async def _test_item_info(self, item_id: str, url_segment: str): "id": "1023", } ) - result = await self.imp.get_item_info(item_id) - self._assert_get(f"drive/v3/files/{url_segment}") + result = await self.imp.get_item_info("foo") + self._assert_get("drive/v3/files/foo") assert result == ItemResult( item_id="1023", item_name="foobar", item_type=ItemType.FOLDER )