Skip to content

Commit 6f46801

Browse files
committed
Add argument validation to new server_client methods
1 parent 8fb55b1 commit 6f46801

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

src/auth0_server_python/auth_server/server_client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
AccessTokenForConnectionErrorCode,
3535
ApiError,
3636
BackchannelLogoutError,
37+
InvalidArgumentError,
3738
MissingRequiredArgumentError,
3839
MissingTransactionError,
3940
PollingApiError,
@@ -1497,6 +1498,9 @@ async def list_connected_accounts(
14971498
Auth0Error: If there is an error retrieving the access token.
14981499
MyAccountApiError: If the My Account API returns an error response.
14991500
"""
1501+
if take is not None and (not isinstance(take, int) or take < 1):
1502+
raise InvalidArgumentError("take", "The 'take' parameter must be a positive integer.")
1503+
15001504
access_token = await self.get_access_token(
15011505
audience=self._my_account_client.audience,
15021506
scope="read:me:connected_accounts",
@@ -1521,6 +1525,9 @@ async def delete_connected_account(
15211525
Auth0Error: If there is an error retrieving the access token.
15221526
MyAccountApiError: If the My Account API returns an error response.
15231527
"""
1528+
if not connected_account_id:
1529+
raise MissingRequiredArgumentError("connected_account_id")
1530+
15241531
access_token = await self.get_access_token(
15251532
audience=self._my_account_client.audience,
15261533
scope="delete:me:connected_accounts",
@@ -1550,6 +1557,9 @@ async def list_connected_account_connections(
15501557
Auth0Error: If there is an error retrieving the access token.
15511558
MyAccountApiError: If the My Account API returns an error response.
15521559
"""
1560+
if take is not None and (not isinstance(take, int) or take < 1):
1561+
raise InvalidArgumentError("take", "The 'take' parameter must be a positive integer.")
1562+
15531563
access_token = await self.get_access_token(
15541564
audience=self._my_account_client.audience,
15551565
scope="read:me:connected_accounts",

src/auth0_server_python/error/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ def __init__(self, argument: str):
101101
self.argument = argument
102102

103103

104+
class InvalidArgumentError(Auth0Error):
105+
"""
106+
Error raised when a given argument is an invalid value.
107+
"""
108+
code = "invalid_argument"
109+
110+
def __init__(self, argument: str, message: str):
111+
message = message
112+
super().__init__(message)
113+
self.name = "InvalidArgumentError"
114+
self.argument = argument
115+
116+
104117
class BackchannelLogoutError(Auth0Error):
105118
"""
106119
Error raised during backchannel logout processing.

src/auth0_server_python/tests/test_server_client.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
AccessTokenForConnectionError,
2424
ApiError,
2525
BackchannelLogoutError,
26+
InvalidArgumentError,
2627
MissingRequiredArgumentError,
2728
MissingTransactionError,
2829
PollingApiError,
@@ -1937,6 +1938,31 @@ async def test_complete_connect_account_no_transactions(mocker):
19371938
assert "transaction" in str(exc.value)
19381939
mock_my_account_client.complete_connect_account.assert_not_awaited()
19391940

1941+
@pytest.mark.asyncio
1942+
@pytest.mark.parametrize("take", ["not_an_integer", 21.3, -5, 0])
1943+
async def test_list_connected_accounts__with_invalid_take_param(mocker, take):
1944+
# Setup
1945+
client = ServerClient(
1946+
domain="auth0.local",
1947+
client_id="<client_id>",
1948+
client_secret="<client_secret>",
1949+
secret="some-secret"
1950+
)
1951+
mock_my_account_client = AsyncMock(MyAccountClient)
1952+
mocker.patch.object(client, "_my_account_client", mock_my_account_client)
1953+
1954+
# Act
1955+
with pytest.raises(InvalidArgumentError) as exc:
1956+
await client.list_connected_accounts(
1957+
connection="<connection>",
1958+
from_param="<from_param>",
1959+
take=take
1960+
)
1961+
1962+
# Assert
1963+
assert "The 'take' parameter must be a positive integer." in str(exc.value)
1964+
mock_my_account_client.list_connected_accounts.assert_not_awaited()
1965+
19401966
@pytest.mark.asyncio
19411967
async def test_list_connected_accounts_gets_access_token_and_calls_my_account(mocker):
19421968
# Setup
@@ -2022,6 +2048,26 @@ async def test_delete_connected_account_gets_access_token_and_calls_my_account(m
20222048
connected_account_id="<id>"
20232049
)
20242050

2051+
@pytest.mark.asyncio
2052+
async def test_delete_connected_account_with_empty_connected_account_id(mocker):
2053+
# Setup
2054+
client = ServerClient(
2055+
domain="auth0.local",
2056+
client_id="<client_id>",
2057+
client_secret="<client_secret>",
2058+
secret="some-secret"
2059+
)
2060+
mock_my_account_client = AsyncMock(MyAccountClient)
2061+
mocker.patch.object(client, "_my_account_client", mock_my_account_client)
2062+
2063+
# Act
2064+
with pytest.raises(MissingRequiredArgumentError) as exc:
2065+
await client.delete_connected_account(connected_account_id=None)
2066+
2067+
# Assert
2068+
assert "connected_account_id" in str(exc.value)
2069+
mock_my_account_client.delete_connected_account.assert_not_awaited()
2070+
20252071
@pytest.mark.asyncio
20262072
async def test_list_connected_account_connections_gets_access_token_and_calls_my_account(mocker):
20272073
# Setup
@@ -2069,3 +2115,27 @@ async def test_list_connected_account_connections_gets_access_token_and_calls_my
20692115
from_param="<from_param>",
20702116
take=2
20712117
)
2118+
2119+
@pytest.mark.asyncio
2120+
@pytest.mark.parametrize("take", ["not_an_integer", 21.3, -5, 0])
2121+
async def test_list_connected_account_connections_with_invalid_take_param(mocker, take):
2122+
# Setup
2123+
client = ServerClient(
2124+
domain="auth0.local",
2125+
client_id="<client_id>",
2126+
client_secret="<client_secret>",
2127+
secret="some-secret"
2128+
)
2129+
mock_my_account_client = AsyncMock(MyAccountClient)
2130+
mocker.patch.object(client, "_my_account_client", mock_my_account_client)
2131+
2132+
# Act
2133+
with pytest.raises(InvalidArgumentError) as exc:
2134+
await client.list_connected_account_connections(
2135+
from_param="<from_param>",
2136+
take=take
2137+
)
2138+
2139+
# Assert
2140+
assert "The 'take' parameter must be a positive integer." in str(exc.value)
2141+
mock_my_account_client.list_connected_account_connections.assert_not_awaited()

0 commit comments

Comments
 (0)