Skip to content

Commit 4b5051c

Browse files
committed
Add more tests
1 parent 1e0f07b commit 4b5051c

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

tests/aio/test_aio_client.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,3 +917,84 @@ async def test_attribute_truncation(aio_client: Client, method, mock_method, cal
917917
assert "attributes" in kwargs["json"]
918918
assert kwargs["json"]["attributes"]
919919
assert len(kwargs["json"]["attributes"][0]["value"]) == 128
920+
921+
922+
@pytest.mark.asyncio
923+
async def test_api_key_authorization_header():
924+
"""Test that API key authentication sets Authorization header correctly."""
925+
api_key = "test_api_key_12345"
926+
client = Client(endpoint=ENDPOINT, project=PROJECT, api_key=api_key)
927+
928+
# Get the session (which is ClientSession wrapper)
929+
session = await client.session()
930+
931+
# Mock the underlying aiohttp.ClientSession within ClientSession
932+
# noinspection PyProtectedMember
933+
underlying_session_mock = mock.AsyncMock()
934+
935+
# Mock response with status attribute
936+
response_mock = mock.Mock()
937+
response_mock.status = 200
938+
response_mock.json = mock.AsyncMock(return_value=RETURN_GET_JSON)
939+
underlying_session_mock.get.return_value = response_mock
940+
941+
# noinspection PyProtectedMember
942+
session._client = underlying_session_mock
943+
client._skip_analytics = "1"
944+
945+
# Make a request
946+
await client.get_project_settings()
947+
948+
# Verify the underlying session.get was called
949+
underlying_session_mock.get.assert_called_once()
950+
call_kwargs = underlying_session_mock.get.call_args_list[0][1]
951+
952+
# Verify Authorization header is set correctly
953+
assert "headers" in call_kwargs
954+
assert "Authorization" in call_kwargs["headers"]
955+
assert call_kwargs["headers"]["Authorization"] == f"Bearer {api_key}"
956+
957+
958+
@pytest.mark.asyncio
959+
async def test_oauth_authorization_header():
960+
"""Test that OAuth authentication sets Authorization header correctly."""
961+
client = Client(
962+
endpoint=ENDPOINT,
963+
project=PROJECT,
964+
oauth_oauth_uri="https://example.com/oauth/token",
965+
oauth_username="test_user",
966+
oauth_password="test_password",
967+
oauth_client_id="test_client_id",
968+
)
969+
970+
# Get the session (which is ClientSession wrapper)
971+
session = await client.session()
972+
973+
# Mock the underlying aiohttp.ClientSession within ClientSession
974+
# noinspection PyProtectedMember
975+
underlying_session_mock = mock.AsyncMock()
976+
977+
# Mock response with status attribute
978+
response_mock = mock.Mock()
979+
response_mock.status = 200
980+
response_mock.json = mock.AsyncMock(return_value=RETURN_GET_JSON)
981+
underlying_session_mock.get.return_value = response_mock
982+
983+
# noinspection PyProtectedMember
984+
session._client = underlying_session_mock
985+
client._skip_analytics = "1"
986+
987+
# Mock the Auth.get() method to return a test token
988+
test_token = "Bearer test_oauth_token_xyz"
989+
with mock.patch.object(client.auth, "get", return_value=test_token):
990+
# Make a request
991+
await client.get_project_settings()
992+
993+
# Verify the underlying session.get was called
994+
underlying_session_mock.get.assert_called_once()
995+
call_kwargs = underlying_session_mock.get.call_args_list[0][1]
996+
997+
# Verify Authorization header is set correctly
998+
assert "headers" in call_kwargs
999+
assert "Authorization" in call_kwargs["headers"]
1000+
assert call_kwargs["headers"]["Authorization"] == test_token

tests/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424

2525
class DummyResponse:
26+
def __init__(self):
27+
self.status_code = 200
28+
2629
# noinspection PyMethodMayBeStatic
2730
def json(self):
2831
return {

tests/test_client.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,68 @@ def test_clone_with_oauth():
434434
and cloned.mode == kwargs["mode"]
435435
)
436436
assert cloned._item_stack.qsize() == 1 and client.current_item() == cloned.current_item()
437+
438+
439+
def test_api_key_authorization_header():
440+
"""Test that API key authentication sets Authorization header correctly."""
441+
api_key = "test_api_key_12345"
442+
client = RPClient(endpoint="http://endpoint", project="project", api_key=api_key)
443+
444+
# Mock the underlying requests.Session within ClientSession
445+
# noinspection PyProtectedMember
446+
underlying_session_mock = mock.Mock()
447+
underlying_session_mock.get.return_value = DummyResponse()
448+
underlying_session_mock.post.return_value = DummyResponse()
449+
underlying_session_mock.put.return_value = DummyResponse()
450+
# noinspection PyProtectedMember
451+
client.session._client = underlying_session_mock
452+
client._skip_analytics = "1"
453+
454+
# Make a request
455+
client.get_project_settings()
456+
457+
# Verify the underlying session.get was called
458+
underlying_session_mock.get.assert_called_once()
459+
call_kwargs = underlying_session_mock.get.call_args_list[0][1]
460+
461+
# Verify Authorization header is set correctly
462+
assert "headers" in call_kwargs
463+
assert "Authorization" in call_kwargs["headers"]
464+
assert call_kwargs["headers"]["Authorization"] == f"Bearer {api_key}"
465+
466+
467+
def test_oauth_authorization_header():
468+
"""Test that OAuth authentication sets Authorization header correctly."""
469+
client = RPClient(
470+
endpoint="http://endpoint",
471+
project="project",
472+
oauth_oauth_uri="https://example.com/oauth/token",
473+
oauth_username="test_user",
474+
oauth_password="test_password",
475+
oauth_client_id="test_client_id",
476+
)
477+
478+
# Mock the underlying requests.Session within ClientSession
479+
# noinspection PyProtectedMember
480+
underlying_session_mock = mock.Mock()
481+
underlying_session_mock.get.return_value = DummyResponse()
482+
underlying_session_mock.post.return_value = DummyResponse()
483+
underlying_session_mock.put.return_value = DummyResponse()
484+
# noinspection PyProtectedMember
485+
client.session._client = underlying_session_mock
486+
client._skip_analytics = "1"
487+
488+
# Mock the Auth.get() method to return a test token
489+
test_token = "Bearer test_oauth_token_xyz"
490+
with mock.patch.object(client.auth, "get", return_value=test_token):
491+
# Make a request
492+
client.get_project_settings()
493+
494+
# Verify the underlying session.get was called
495+
underlying_session_mock.get.assert_called_once()
496+
call_kwargs = underlying_session_mock.get.call_args_list[0][1]
497+
498+
# Verify Authorization header is set correctly
499+
assert "headers" in call_kwargs
500+
assert "Authorization" in call_kwargs["headers"]
501+
assert call_kwargs["headers"]["Authorization"] == test_token

0 commit comments

Comments
 (0)