Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c5131b6

Browse files
authored
Fix asyncify for users client where token is not required (auth0#506)
2 parents d2ab498 + 6e3b2a9 commit c5131b6

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

auth0/asyncify.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import aiohttp
22

3+
from auth0.authentication import Users
34
from auth0.authentication.base import AuthenticationBase
45
from auth0.rest import RestClientOptions
56
from auth0.rest_async import AsyncRestClient
@@ -21,6 +22,17 @@ def asyncify(cls):
2122
if callable(getattr(cls, func)) and not func.startswith("_")
2223
]
2324

25+
class UsersAsyncClient(cls):
26+
def __init__(
27+
self,
28+
domain,
29+
telemetry=True,
30+
timeout=5.0,
31+
protocol="https",
32+
):
33+
super().__init__(domain, telemetry, timeout, protocol)
34+
self.client = AsyncRestClient(None, telemetry=telemetry, timeout=timeout)
35+
2436
class AsyncManagementClient(cls):
2537
def __init__(
2638
self,
@@ -68,7 +80,9 @@ def __init__(
6880
class Wrapper(cls):
6981
def __init__(self, *args, **kwargs):
7082
super().__init__(*args, **kwargs)
71-
if AuthenticationBase in cls.__bases__:
83+
if cls == Users:
84+
self._async_client = UsersAsyncClient(*args, **kwargs)
85+
elif AuthenticationBase in cls.__bases__:
7286
self._async_client = AsyncAuthenticationClient(*args, **kwargs)
7387
else:
7488
self._async_client = AsyncManagementClient(*args, **kwargs)

auth0/authentication/users.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ def userinfo(self, access_token: str) -> dict[str, Any]:
4646
Returns:
4747
The user profile.
4848
"""
49-
5049
data: dict[str, Any] = self.client.get(
5150
url=f"{self.protocol}://{self.domain}/userinfo",
5251
headers={"Authorization": f"Bearer {access_token}"},

auth0/test_async/test_asyncify.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
from callee import Attrs
1313

1414
from auth0.asyncify import asyncify
15-
from auth0.authentication import GetToken
15+
from auth0.authentication import GetToken, Users
1616
from auth0.management import Clients, Guardian, Jobs
1717

1818
clients = re.compile(r"^https://example\.com/api/v2/clients.*")
1919
token = re.compile(r"^https://example\.com/oauth/token.*")
20+
user_info = re.compile(r"^https://example\.com/userinfo.*")
2021
factors = re.compile(r"^https://example\.com/api/v2/guardian/factors.*")
2122
users_imports = re.compile(r"^https://example\.com/api/v2/jobs/users-imports.*")
2223
payload = {"foo": "bar"}
@@ -111,6 +112,22 @@ async def test_post_auth(self, mocked):
111112
timeout=ANY,
112113
)
113114

115+
@aioresponses()
116+
async def test_user_info(self, mocked):
117+
callback, mock = get_callback()
118+
mocked.get(user_info, callback=callback)
119+
c = asyncify(Users)(domain="example.com")
120+
self.assertEqual(
121+
await c.userinfo_async(access_token="access-token-example"), payload
122+
)
123+
mock.assert_called_with(
124+
Attrs(path="/userinfo"),
125+
headers={**headers, "Authorization": "Bearer access-token-example"},
126+
timeout=ANY,
127+
allow_redirects=True,
128+
params=None,
129+
)
130+
114131
@aioresponses()
115132
async def test_file_post(self, mocked):
116133
callback, mock = get_callback()

0 commit comments

Comments
 (0)