Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENG-6252] Git hub tests #133

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion addon_imps/storage/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ async def get_item_info(self, item_id: str) -> storage.ItemResult:
if response.http_status == 200:
json = await response.json_content()
if path != "":
return self._parse_github_item(json, full_name=item_id)
if isinstance(json, dict):
return self._parse_github_item(json, full_name=item_id)
else:
return self._parse_github_item(json[0], full_name=item_id)
else:
return self._parse_github_repo(json)
elif response.http_status == 404:
Expand Down Expand Up @@ -84,6 +87,7 @@ async def list_child_items(
items=items, total_count=len(items)
).with_cursor(self._create_offset_cursor(len(items), page_cursor))
elif response.http_status == 404:
print("########## list_child_items 404", await response.json_content())
raise ItemNotFound
else:
raise UnexpectedAddonError
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions addon_imps/tests/_trial_temp/test.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2024-10-10 22:56:41+0300 [-] Log opened.
2024-10-10 22:56:41+0300 [-] /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/unittest/case.py:580: builtins.RuntimeWarning: TestResult has no addDuration method
148 changes: 148 additions & 0 deletions addon_imps/tests/storage/test_github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import unittest
from unittest.mock import AsyncMock

from addon_imps.storage.github import GitHubStorageImp
from addon_toolkit.constrained_network.http import HttpRequestor
from addon_toolkit.interfaces.storage import (
ItemResult,
ItemSampleResult,
ItemType,
StorageConfig,
)


class TestGitHubStorageImp(unittest.IsolatedAsyncioTestCase):
def setUp(self):
self.base_url = "https://api.github.com"
self.config = StorageConfig(external_api_url=self.base_url, max_upload_mb=100)
self.network = AsyncMock(spec_set=HttpRequestor)
self.imp = GitHubStorageImp(config=self.config, network=self.network)

def _patch_get(self, return_value: dict):
mock = self.network.GET.return_value.__aenter__.return_value
mock.json_content = AsyncMock(return_value=return_value)
mock.http_status = 200

def _assert_get(self, url: str, query: dict = None):
extra_params = {"query": query} if query else {}
if url == "repos/testuser/repo1/contents/":
extra_params = {"query": {}}
self.network.GET.assert_called_once_with(url, **extra_params)
self.network.GET.return_value.__aenter__.assert_awaited_once()
self.network.GET.return_value.__aenter__.return_value.json_content.assert_awaited_once()
self.network.GET.return_value.__aexit__.assert_awaited_once_with(
None, None, None
)

async def test_get_external_account_id(self):
mock_response = {"id": "12345"}
self._patch_get(mock_response)
result = await self.imp.get_external_account_id({})
self.assertEqual(result, "12345")
self._assert_get("user")

async def test_list_root_items(self):
mock_response = [
{
"id": 1,
"full_name": "testuser/repo1",
"name": "repo1",
"private": False,
"owner": {
"login": "testuser",
},
},
{
"id": 2,
"full_name": "testuser/repo2",
"name": "repo2",
"private": False,
"owner": {
"login": "testuser",
},
},
]
self._patch_get(mock_response)

result = await self.imp.list_root_items()
expected_items = [
ItemResult(
item_id="testuser/repo1:", item_name="repo1", item_type=ItemType.FOLDER
),
ItemResult(
item_id="testuser/repo2:", item_name="repo2", item_type=ItemType.FOLDER
),
]
expected_result = ItemSampleResult(items=expected_items, total_count=2)
self.assertEqual(result.items, expected_result.items)
self.assertEqual(result.total_count, expected_result.total_count)
self._assert_get("user/repos")

async def test_get_item_info_root(self):
result = await self.imp.get_item_info(".")
expected_result = ItemResult(
item_id="", item_name="GitHub", item_type=ItemType.FOLDER
)
self.assertEqual(result, expected_result)

async def test_get_item_info_repo(self):
mock_response = {
"id": 1,
"full_name": "testuser/repo1",
"name": "repo1",
"private": False,
"owner": {
"login": "testuser",
},
}
self._patch_get(mock_response)
result = await self.imp.get_item_info("testuser/repo1:")
expected_result = ItemResult(
item_id="testuser/repo1:", item_name="repo1", item_type=ItemType.FOLDER
)
self.assertEqual(result, expected_result)
self._assert_get("repos/testuser/repo1")

async def test_get_item_info_file(self):
mock_response = {
"name": "README.md",
"path": "README.md",
"type": "file",
}
self._patch_get(mock_response)
result = await self.imp.get_item_info("testuser/repo1:README.md")

expected_result = ItemResult(
item_id="README.md", item_name="README.md", item_type=ItemType.FILE
)
self.assertEqual(result, expected_result)
self._assert_get("repos/testuser/repo1/contents/README.md")

async def test_list_child_items(self):
mock_response = [
{
"name": "src",
"path": "src",
"type": "dir",
},
{
"name": "README.md",
"path": "README.md",
"type": "file",
},
]
self._patch_get(mock_response)

result = await self.imp.list_child_items("testuser/repo1:")
expected_items = [
ItemResult(
item_id="testuser/repo1:src", item_name="src", item_type=ItemType.FOLDER
),
ItemResult(
item_id="README.md", item_name="README.md", item_type=ItemType.FILE
),
]
expected_result = ItemSampleResult(items=expected_items, total_count=2)
self.assertEqual(result.items, expected_result.items)
self.assertEqual(result.total_count, expected_result.total_count)
self._assert_get("repos/testuser/repo1/contents/")
3 changes: 0 additions & 3 deletions addon_service/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ class ExternalStorageServiceAdmin(GravyvaletModelAdmin):
"int_addon_imp": known_imps.AddonImpNumbers,
"int_credentials_format": CredentialsFormats,
"int_service_type": ServiceTypes,
}

enum_multiple_choice_fields = {
"int_supported_features": SupportedFeatures,
}

Expand Down
3 changes: 1 addition & 2 deletions addon_service/oauth2/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dataclasses
import urllib.parse
from http import HTTPStatus
from secrets import token_urlsafe
from typing import Iterable
Expand Down Expand Up @@ -114,8 +115,6 @@ async def get_refreshed_access_token(
async def _token_request(
token_endpoint_url: str, request_body: dict[str, str]
) -> FreshTokenResult:
import urllib.parse

_client = await get_singleton_client_session()
async with _client.post(token_endpoint_url, data=request_body) as _token_response:
if _token_response.content_type == "application/x-www-form-urlencoded":
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ services:
environment:
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_DB: gravyvalet
ports:
- 5435:5432

rabbitmq:
image: rabbitmq:management
Expand Down