Skip to content

Commit

Permalink
fixed root folder errors
Browse files Browse the repository at this point in the history
  • Loading branch information
opaduchak committed Dec 16, 2024
1 parent a58fd72 commit 11dd65c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
19 changes: 14 additions & 5 deletions addon_imps/storage/dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from addon_toolkit.interfaces.storage import ItemType


ROOT_ITEM_ID: typing.Final[str] = "/"


class DropboxStorageImp(storage.StorageAddonHttpRequestorImp):
"""storage on dropbox.com
see https://www.dropbox.com/developers/documentation/http/documentation
Expand All @@ -17,7 +20,7 @@ async def list_root_items(self, page_cursor: str = "") -> storage.ItemSampleResu
return storage.ItemSampleResult(
items=[
storage.ItemResult(
item_id="/",
item_id=ROOT_ITEM_ID,
item_name="Root",
item_type=ItemType.FOLDER,
)
Expand All @@ -26,7 +29,7 @@ async def list_root_items(self, page_cursor: str = "") -> storage.ItemSampleResu
)

async def build_wb_config(self) -> dict:
if not self.config.connected_root_id or self.config.connected_root_id == "/":
if self._is_root_id(self.config.connected_root_id):
return {"folder": self.config.connected_root_id}
async with self.network.POST(
"files/get_metadata",
Expand All @@ -40,9 +43,9 @@ async def build_wb_config(self) -> dict:
}

async def get_item_info(self, item_id: str) -> storage.ItemResult:
if not item_id:
if await self._is_root_id(item_id):
return storage.ItemResult(
item_id="",
item_id="/",
item_name="root folder",
item_type=ItemType.FOLDER,
)
Expand All @@ -56,6 +59,9 @@ async def get_item_info(self, item_id: str) -> storage.ItemResult:
_parsed = _DropboxParsedJson(await _response.json_content())
return _parsed.single_item_result()

async def _is_root_id(self, item_id):
return not item_id or item_id == ROOT_ITEM_ID

async def list_child_items(
self,
item_id: str,
Expand All @@ -75,7 +81,10 @@ async def list_child_items(
)
async with self.network.POST(
"files/list_folder",
json={"path": item_id if item_id != "/" else "", "recursive": False},
json={
"path": item_id if item_id != ROOT_ITEM_ID else "",
"recursive": False,
},
) as _response:
_parsed = _DropboxParsedJson(await _response.json_content())
items = list(_parsed.item_results(item_type=item_type))
Expand Down
28 changes: 15 additions & 13 deletions addon_imps/tests/storage/test_dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ async def test_get_external_account_id(self):
self.network.POST.assert_not_called()

async def test_list_root_items(self):
mock_response = {
"entries": [{"id": "123", "name": "root folder", ".tag": "folder"}],
"cursor": "test_cursor",
}
self._patch_post(mock_response)

result = await self.imp.list_root_items()

expected_result = ItemSampleResult(
Expand All @@ -64,7 +58,7 @@ async def test_get_item_info(self):
for item_id, item_name in cases:
with self.subTest(item_id=item_id):
mock_response = {
"id": item_id or "",
"id": item_id or "/",
"name": item_name,
".tag": "folder" if not item_id else "file",
}
Expand All @@ -73,7 +67,7 @@ async def test_get_item_info(self):
result = await self.imp.get_item_info(item_id)

expected_result = ItemResult(
item_id=item_id or "",
item_id=item_id or "/",
item_name=item_name,
item_type=ItemType.FOLDER if not item_id else ItemType.FILE,
)
Expand All @@ -90,6 +84,7 @@ async def test_list_child_items(self):
{"id": "456", "name": "child file", ".tag": "file"},
],
"cursor": "test_cursor",
"has_more": True,
}
self._patch_post(mock_response)

Expand All @@ -112,10 +107,21 @@ async def test_list_child_items(self):
self._assert_post(
"files/list_folder", json={"path": "test_id", "recursive": False}
)
self.network.POST.assert_has_calls(
[
call("files/list_folder", json={"path": "test_id", "recursive": False}),
call().__aenter__(),
call().__aenter__().json_content(),
call().__aexit__(None, None, None),
]
)

async def test_list_child_items_with_cursor(self):

mock_response_continue = {
"entries": [{"id": "789", "name": "another child file", ".tag": "file"}],
"cursor": "next_cursor",
"has_more": False,
}
self._patch_post(mock_response_continue)

Expand All @@ -132,17 +138,13 @@ async def test_list_child_items(self):
)
],
total_count=1,
next_sample_cursor="next_cursor",
next_sample_cursor=None,
)

self.assertEqual(result, expected_result_continue)

self.network.POST.assert_has_calls(
[
call("files/list_folder", json={"path": "test_id", "recursive": False}),
call().__aenter__(),
call().__aenter__().json_content(),
call().__aexit__(None, None, None),
call("files/list_folder/continue", json={"cursor": "test_cursor"}),
call().__aenter__(),
call().__aenter__().json_content(),
Expand Down

0 comments on commit 11dd65c

Please sign in to comment.