Skip to content

Commit aaddd31

Browse files
committed
Add some authclient tests
1 parent e24ee17 commit aaddd31

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/auth0_fastapi/server/routes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ async def connect_account(
144144
Endpoint to initiate the connect account flow for linking a third-party account to the user's profile.
145145
Redirects the user to the Auth0 connect account URL.
146146
"""
147-
authorization_params = {k: v for k, v in request.query_params.items() if k not in [
148-
"connection", "returnTo"]}
147+
authorization_params = {
148+
k: v for k, v in request.query_params.items() if k not in ["connection", "returnTo"]}
149149

150150
return_to = request.query_params.get("returnTo")
151151
connect_account_url = await auth_client.start_connect_account(

src/auth0_fastapi/test/test_auth_client.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from unittest.mock import AsyncMock, Mock, patch
33

44
import pytest
5+
from auth0_server_python.auth_types import CompleteConnectAccountResponse, ConnectAccountOptions
56
from fastapi import HTTPException, Request, Response
67

78
from auth0_fastapi.auth.auth_client import AuthClient
@@ -392,3 +393,48 @@ async def test_store_options_validation(self, auth_client):
392393
await auth_client.start_login(store_options=valid_options)
393394

394395
mock_start.assert_called()
396+
397+
398+
class TestConnectedAccountFlow:
399+
"""Test connected account functionality."""
400+
401+
@pytest.mark.asyncio
402+
async def test_start_connect_account(self, auth_client):
403+
"""Test initiating user account linking."""
404+
mock_connect_url = "https://test.auth0.com/connected-accounts/connect?ticket"
405+
406+
with patch.object(auth_client.client, 'start_connect_account', new_callable=AsyncMock) as mock_start_connect:
407+
mock_start_connect.return_value = mock_connect_url
408+
409+
result = await auth_client.start_connect_account(
410+
connection="google-oauth2",
411+
app_state={"returnTo": "/profile"},
412+
authorization_params={"prompt": "consent"},
413+
)
414+
415+
assert result == mock_connect_url
416+
mock_start_connect.assert_called_once_with(
417+
options=ConnectAccountOptions(
418+
connection="google-oauth2",
419+
app_state={"returnTo": "/profile"},
420+
authorization_params={"prompt": "consent"},
421+
), store_options=None)
422+
423+
@pytest.mark.asyncio
424+
async def test_complete_connect_account(self, auth_client):
425+
"""Test initiating user account linking."""
426+
mock_callback_url = "https://test.auth0.com/connected-accounts/connect?ticket"
427+
mock_result = CompleteConnectAccountResponse(
428+
id="id_12345",
429+
connection="google-oauth2",
430+
access_type="offline",
431+
scopes=["read:foo"],
432+
created_at="1970-01-01T00:00:00Z"
433+
)
434+
with patch.object(auth_client.client, 'complete_connect_account', new_callable=AsyncMock) as mock_complete:
435+
mock_complete.return_value = mock_result
436+
437+
result = await auth_client.complete_connect_account(mock_callback_url)
438+
439+
assert result == mock_result
440+
mock_complete.assert_called_once_with(mock_callback_url, store_options=None)

0 commit comments

Comments
 (0)