Skip to content

Commit

Permalink
[ENG-6601][ENG-6602][ENG-6603][ENG-6604][ENG-6605] fixed onedrive ope…
Browse files Browse the repository at this point in the history
…rations on nested items (#161)
  • Loading branch information
opaduchak authored Nov 25, 2024
1 parent 9c44c11 commit 6ce8667
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
26 changes: 16 additions & 10 deletions addon_imps/storage/onedrive.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from dataclasses import replace
from typing import Final
from urllib.parse import (
parse_qs,
urlparse,
Expand All @@ -12,6 +14,9 @@ def __init__(self, next_link: str):
self.this_cursor_str = next_link


ROOT_ITEM_ID: Final[str] = "root"


class OneDriveStorageImp(storage.StorageAddonHttpRequestorImp):
"""Storage on OneDrive
Expand All @@ -24,23 +29,24 @@ async def get_external_account_id(self, auth_result_extras: dict[str, str]) -> s
return str(_json["id"])

async def list_root_items(self, page_cursor: str = "") -> storage.ItemSampleResult:
root_item = await self.get_item_info("root")
root_item = await self.get_item_info(ROOT_ITEM_ID)
return storage.ItemSampleResult(
items=[root_item],
items=[
# This is required for waterbutler to understand that root is actual root (it checks if id == 'root')
replace(root_item, item_id=ROOT_ITEM_ID),
],
total_count=1,
)

async def build_wb_config(self) -> dict:
async with self.network.GET("me/drive") as _response:
json = await _response.json_content()
return {
"folder": self.config.connected_root_id,
"drive_id": json.get("id"),
}
return {
"folder": self.config.connected_root_id,
"drive_id": self.config.external_account_id,
}

async def get_item_info(self, item_id: str) -> storage.ItemResult:
async with self.network.GET(
f"me/drive/items/{item_id}",
f"drives/{self.config.external_account_id}/items/{item_id}",
query={"select": "id,name,folder,createdDateTime,lastModifiedDateTime"},
) as _response:
_json = await _response.json_content()
Expand All @@ -61,7 +67,7 @@ async def list_child_items(
item_type: storage.ItemType | None = None,
) -> storage.ItemSampleResult:
async with self.network.GET(
f"me/drive/items/{item_id}/children",
f"drives/{self.config.external_account_id}/items/{item_id}/children",
query={
"select": "id,name,folder,createdDateTime,lastModifiedDateTime",
**self._params_from_cursor(page_cursor),
Expand Down
Empty file.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import unittest
from unittest.mock import (
AsyncMock,
Expand All @@ -21,43 +20,48 @@ def __init__(self, next_link: str):
super().__init__(cursor_str=next_link)


class TestOneDriveStorageImp(unittest.TestCase):
class TestOneDriveStorageImp(unittest.IsolatedAsyncioTestCase):

def setUp(self):
self.config = StorageConfig(
external_api_url="https://graph.microsoft.com/v1.0",
connected_root_id=None,
external_account_id=None,
external_account_id="account-id",
max_upload_mb=100,
)
self.network = AsyncMock(spec=HttpRequestor)
self.onedrive_imp = OneDriveStorageImp(config=self.config, network=self.network)

def test_get_external_account_id(self):
async def test_get_external_account_id(self):
mock_response = {"id": "user-id"}
self.onedrive_imp.network.GET.return_value.__aenter__.return_value.json_content = AsyncMock(
return_value=mock_response
)
result = asyncio.run(self.onedrive_imp.get_external_account_id({}))
result = await self.onedrive_imp.get_external_account_id({})
self.assertEqual(result, "user-id")
self.onedrive_imp.network.GET.assert_called_with("me")

def test_list_root_items(self):
async def test_list_root_items(self):
mock_root_item = ItemResult(
item_id="root-id",
item_name="Root",
item_type=ItemType.FOLDER,
)
returned_root_item = ItemResult(
item_id="root",
item_name="Root",
item_type=ItemType.FOLDER,
)
self.onedrive_imp.get_item_info = AsyncMock(return_value=mock_root_item)
result = asyncio.run(self.onedrive_imp.list_root_items())
result = await self.onedrive_imp.list_root_items()
expected_result = ItemSampleResult(
items=[mock_root_item],
items=[returned_root_item],
total_count=1,
)
self.assertEqual(result.items, expected_result.items)
self.onedrive_imp.get_item_info.assert_called_with("root")

def test_get_item_info(self):
async def test_get_item_info(self):
mock_response = {
"id": "item-id",
"name": "Item Name",
Expand All @@ -68,19 +72,19 @@ def test_get_item_info(self):
self.onedrive_imp.network.GET.return_value.__aenter__.return_value.json_content = AsyncMock(
return_value=mock_response
)
result = asyncio.run(self.onedrive_imp.get_item_info("item-id"))
result = await self.onedrive_imp.get_item_info("item-id")
expected_result = ItemResult(
item_id="item-id",
item_name="Item Name",
item_type=ItemType.FOLDER,
)
self.assertEqual(result, expected_result)
self.onedrive_imp.network.GET.assert_called_with(
"me/drive/items/item-id",
"drives/account-id/items/item-id",
query={"select": "id,name,folder,createdDateTime,lastModifiedDateTime"},
)

def test_list_child_items(self):
async def test_list_child_items(self):
mock_response = {
"value": [
{
Expand Down Expand Up @@ -109,7 +113,7 @@ def test_list_child_items(self):
"https://graph.microsoft.com/nextPageLink"
)

result = asyncio.run(self.onedrive_imp.list_child_items("parent-item-id"))
result = await self.onedrive_imp.list_child_items("parent-item-id")

expected_items = [
ItemResult(
Expand All @@ -131,7 +135,7 @@ def test_list_child_items(self):
)

self.onedrive_imp.network.GET.assert_called_with(
"me/drive/items/parent-item-id/children",
"drives/account-id/items/parent-item-id/children",
query={
"select": "id,name,folder,createdDateTime,lastModifiedDateTime",
},
Expand Down

0 comments on commit 6ce8667

Please sign in to comment.