Skip to content

Commit e672073

Browse files
committed
Add parameter validation to new my_account_api functions
1 parent 6f46801 commit e672073

File tree

4 files changed

+140
-5
lines changed

4 files changed

+140
-5
lines changed

src/auth0_server_python/auth_server/my_account_client.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
)
1414
from auth0_server_python.error import (
1515
ApiError,
16+
InvalidArgumentError,
17+
MissingRequiredArgumentError,
1618
MyAccountApiError,
1719
)
1820

@@ -104,6 +106,12 @@ async def list_connected_accounts(
104106
from_param: Optional[str] = None,
105107
take: Optional[int] = None
106108
) -> ListConnectedAccountResponse:
109+
if access_token is None:
110+
raise MissingRequiredArgumentError("access_token")
111+
112+
if take is not None and (not isinstance(take, int) or take < 1):
113+
raise InvalidArgumentError("take", "The 'take' parameter must be a positive integer.")
114+
107115
try:
108116
async with httpx.AsyncClient() as client:
109117
params = {}
@@ -149,6 +157,13 @@ async def delete_connected_account(
149157
access_token: str,
150158
connected_account_id: str
151159
) -> None:
160+
161+
if access_token is None:
162+
raise MissingRequiredArgumentError("access_token")
163+
164+
if connected_account_id is None:
165+
raise MissingRequiredArgumentError("connected_account_id")
166+
152167
try:
153168
async with httpx.AsyncClient() as client:
154169
response = await client.delete(
@@ -181,6 +196,12 @@ async def list_connected_account_connections(
181196
from_param: Optional[str] = None,
182197
take: Optional[int] = None
183198
) -> ListConnectedAccountConnectionsResponse:
199+
if access_token is None:
200+
raise MissingRequiredArgumentError("access_token")
201+
202+
if take is not None and (not isinstance(take, int) or take < 1):
203+
raise InvalidArgumentError("take", "The 'take' parameter must be a positive integer.")
204+
184205
try:
185206
async with httpx.AsyncClient() as client:
186207
params = {}

src/auth0_server_python/auth_server/server_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,7 @@ async def list_connected_accounts(
15001500
"""
15011501
if take is not None and (not isinstance(take, int) or take < 1):
15021502
raise InvalidArgumentError("take", "The 'take' parameter must be a positive integer.")
1503-
1503+
15041504
access_token = await self.get_access_token(
15051505
audience=self._my_account_client.audience,
15061506
scope="read:me:connected_accounts",
@@ -1527,7 +1527,7 @@ async def delete_connected_account(
15271527
"""
15281528
if not connected_account_id:
15291529
raise MissingRequiredArgumentError("connected_account_id")
1530-
1530+
15311531
access_token = await self.get_access_token(
15321532
audience=self._my_account_client.audience,
15331533
scope="delete:me:connected_accounts",
@@ -1559,7 +1559,7 @@ async def list_connected_account_connections(
15591559
"""
15601560
if take is not None and (not isinstance(take, int) or take < 1):
15611561
raise InvalidArgumentError("take", "The 'take' parameter must be a positive integer.")
1562-
1562+
15631563
access_token = await self.get_access_token(
15641564
audience=self._my_account_client.audience,
15651565
scope="read:me:connected_accounts",

src/auth0_server_python/error/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ class InvalidArgumentError(Auth0Error):
108108
code = "invalid_argument"
109109

110110
def __init__(self, argument: str, message: str):
111-
message = message
112111
super().__init__(message)
113112
self.name = "InvalidArgumentError"
114113
self.argument = argument

src/auth0_server_python/tests/test_my_account_client.py

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
ListConnectedAccountConnectionsResponse,
1414
ListConnectedAccountResponse,
1515
)
16-
from auth0_server_python.error import MyAccountApiError
16+
from auth0_server_python.error import (
17+
InvalidArgumentError,
18+
MissingRequiredArgumentError,
19+
MyAccountApiError,
20+
)
1721

1822

1923
@pytest.mark.asyncio
@@ -228,6 +232,45 @@ async def test_list_connected_accounts_success(mocker):
228232
next="<next_token>"
229233
)
230234

235+
@pytest.mark.asyncio
236+
async def test_list_connected_accounts_missing_access_token(mocker):
237+
# Arrange
238+
client = MyAccountClient(domain="auth0.local")
239+
mock_get = mocker.patch("httpx.AsyncClient.get", new_callable=AsyncMock)
240+
241+
# Act
242+
with pytest.raises(MissingRequiredArgumentError) as exc:
243+
await client.list_connected_accounts(
244+
access_token=None,
245+
connection="<connection>",
246+
from_param="<from_param>",
247+
take=2
248+
)
249+
250+
# Assert
251+
mock_get.assert_not_awaited()
252+
assert "access_token" in str(exc.value)
253+
254+
@pytest.mark.asyncio
255+
@pytest.mark.parametrize("take", ["not_an_integer", 21.3, -5, 0])
256+
async def test_list_connected_accounts_invalid_take_param(mocker, take):
257+
# Arrange
258+
client = MyAccountClient(domain="auth0.local")
259+
mock_get = mocker.patch("httpx.AsyncClient.get", new_callable=AsyncMock)
260+
261+
# Act
262+
with pytest.raises(InvalidArgumentError) as exc:
263+
await client.list_connected_accounts(
264+
access_token="<access_token>",
265+
connection="<connection>",
266+
from_param="<from_param>",
267+
take=take
268+
)
269+
270+
# Assert
271+
mock_get.assert_not_awaited()
272+
assert "The 'take' parameter must be a positive integer." in str(exc.value)
273+
231274
@pytest.mark.asyncio
232275
async def test_list_connected_accounts_api_response_failure(mocker):
233276
# Arrange
@@ -277,6 +320,40 @@ async def test_delete_connected_account_success(mocker):
277320
auth=ANY
278321
)
279322

323+
@pytest.mark.asyncio
324+
async def test_delete_connected_account_missing_access_token(mocker):
325+
# Arrange
326+
client = MyAccountClient(domain="auth0.local")
327+
mock_delete = mocker.patch("httpx.AsyncClient.delete", new_callable=AsyncMock)
328+
329+
# Act
330+
with pytest.raises(MissingRequiredArgumentError) as exc:
331+
await client.delete_connected_account(
332+
access_token=None,
333+
connected_account_id="<id_1>"
334+
)
335+
336+
# Assert
337+
mock_delete.assert_not_awaited()
338+
assert "access_token" in str(exc.value)
339+
340+
@pytest.mark.asyncio
341+
async def test_delete_connected_account_missing_connected_account_id(mocker):
342+
# Arrange
343+
client = MyAccountClient(domain="auth0.local")
344+
mock_delete = mocker.patch("httpx.AsyncClient.delete", new_callable=AsyncMock)
345+
346+
# Act
347+
with pytest.raises(MissingRequiredArgumentError) as exc:
348+
await client.delete_connected_account(
349+
access_token="<access_token>",
350+
connected_account_id=None
351+
)
352+
353+
# Assert
354+
mock_delete.assert_not_awaited()
355+
assert "connected_account_id" in str(exc.value)
356+
280357
@pytest.mark.asyncio
281358
async def test_delete_connected_account_api_response_failure(mocker):
282359
# Arrange
@@ -359,6 +436,44 @@ async def test_list_connected_account_connections_success(mocker):
359436
next="<next_token>"
360437
)
361438

439+
@pytest.mark.asyncio
440+
async def test_list_connected_account_connections_missing_access_token(mocker):
441+
# Arrange
442+
client = MyAccountClient(domain="auth0.local")
443+
mock_get = mocker.patch("httpx.AsyncClient.get", new_callable=AsyncMock)
444+
445+
# Act
446+
with pytest.raises(MissingRequiredArgumentError) as exc:
447+
await client.list_connected_account_connections(
448+
access_token=None,
449+
from_param="<from_param>",
450+
take=2
451+
)
452+
453+
# Assert
454+
mock_get.assert_not_awaited()
455+
assert "access_token" in str(exc.value)
456+
457+
@pytest.mark.asyncio
458+
@pytest.mark.parametrize("take", ["not_an_integer", 21.3, -5, 0])
459+
async def test_list_connected_account_connections_invalid_take_param(mocker, take):
460+
# Arrange
461+
client = MyAccountClient(domain="auth0.local")
462+
mock_get = mocker.patch("httpx.AsyncClient.get", new_callable=AsyncMock)
463+
464+
# Act
465+
with pytest.raises(InvalidArgumentError) as exc:
466+
await client.list_connected_account_connections(
467+
access_token="<access_token>",
468+
from_param="<from_param>",
469+
take=take
470+
)
471+
472+
# Assert
473+
mock_get.assert_not_awaited()
474+
assert "The 'take' parameter must be a positive integer." in str(exc.value)
475+
476+
362477
@pytest.mark.asyncio
363478
async def test_list_connected_account_connections_api_response_failure(mocker):
364479
# Arrange

0 commit comments

Comments
 (0)