Skip to content

Commit 6eaa715

Browse files
committed
Update tests
1 parent 0add577 commit 6eaa715

2 files changed

Lines changed: 25 additions & 34 deletions

File tree

auth0/test_async/test_async_token_verifier.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from unittest.mock import ANY
44

55
import jwt
6+
import pytest
67
from aioresponses import aioresponses
78
from cryptography.hazmat.primitives import serialization
89
from yarl import URL
@@ -54,17 +55,12 @@ def get_pem_bytes(rsa_public_key):
5455
)
5556

5657

57-
@unittest.skipIf(
58-
not hasattr(unittest, "IsolatedAsyncioTestCase"),
59-
"python 3.7 doesn't have IsolatedAsyncioTestCase",
60-
)
61-
class TestAsyncAsymmetricSignatureVerifier(
62-
getattr(unittest, "IsolatedAsyncioTestCase", object)
63-
):
58+
class TestAsyncAsymmetricSignatureVerifier(unittest.TestCase):
59+
@pytest.mark.asyncio
6460
@aioresponses()
6561
async def test_async_asymmetric_verifier_fetches_key(self, mocked):
6662
callback, mock = get_callback(200, JWKS_RESPONSE_SINGLE_KEY)
67-
mocked.get(JWKS_URI, callback=callback)
63+
await mocked.get(JWKS_URI, callback=callback)
6864

6965
verifier = AsyncAsymmetricSignatureVerifier(JWKS_URI)
7066

@@ -73,11 +69,8 @@ async def test_async_asymmetric_verifier_fetches_key(self, mocked):
7369
self.assertEqual(get_pem_bytes(key), RSA_PUB_KEY_1_PEM)
7470

7571

76-
@unittest.skipIf(
77-
not hasattr(unittest, "IsolatedAsyncioTestCase"),
78-
"python 3.7 doesn't have IsolatedAsyncioTestCase",
79-
)
80-
class TestAsyncJwksFetcher(getattr(unittest, "IsolatedAsyncioTestCase", object)):
72+
class TestAsyncJwksFetcher(unittest.TestCase):
73+
@pytest.mark.asyncio
8174
@aioresponses()
8275
@unittest.mock.patch(
8376
"auth0.authentication.token_verifier.time.time", return_value=0
@@ -88,8 +81,8 @@ async def test_async_get_jwks_json_twice_on_cache_expired(
8881
fetcher = AsyncJwksFetcher(JWKS_URI, cache_ttl=100)
8982

9083
callback, mock = get_callback(200, JWKS_RESPONSE_SINGLE_KEY)
91-
mocked.get(JWKS_URI, callback=callback)
92-
mocked.get(JWKS_URI, callback=callback)
84+
await mocked.get(JWKS_URI, callback=callback)
85+
await mocked.get(JWKS_URI, callback=callback)
9386

9487
key_1 = await fetcher.get_key("test-key-1")
9588
expected_key_1_pem = get_pem_bytes(key_1)
@@ -120,13 +113,14 @@ async def test_async_get_jwks_json_twice_on_cache_expired(
120113
)
121114
self.assertEqual(mock.call_count, 2)
122115

116+
@pytest.mark.asyncio
123117
@aioresponses()
124118
async def test_async_get_jwks_json_once_on_cache_hit(self, mocked):
125119
fetcher = AsyncJwksFetcher(JWKS_URI, cache_ttl=1)
126120

127121
callback, mock = get_callback(200, JWKS_RESPONSE_MULTIPLE_KEYS)
128-
mocked.get(JWKS_URI, callback=callback)
129-
mocked.get(JWKS_URI, callback=callback)
122+
await mocked.get(JWKS_URI, callback=callback)
123+
await mocked.get(JWKS_URI, callback=callback)
130124

131125
key_1 = await fetcher.get_key("test-key-1")
132126
key_2 = await fetcher.get_key("test-key-2")
@@ -144,12 +138,13 @@ async def test_async_get_jwks_json_once_on_cache_hit(self, mocked):
144138
)
145139
self.assertEqual(mock.call_count, 1)
146140

141+
@pytest.mark.asyncio
147142
@aioresponses()
148143
async def test_async_fetches_jwks_json_forced_on_cache_miss(self, mocked):
149144
fetcher = AsyncJwksFetcher(JWKS_URI, cache_ttl=1)
150145

151146
callback, mock = get_callback(200, {"keys": [RSA_PUB_KEY_1_JWK]})
152-
mocked.get(JWKS_URI, callback=callback)
147+
await mocked.get(JWKS_URI, callback=callback)
153148

154149
# Triggers the first call
155150
key_1 = await fetcher.get_key("test-key-1")
@@ -166,7 +161,7 @@ async def test_async_fetches_jwks_json_forced_on_cache_miss(self, mocked):
166161
self.assertEqual(mock.call_count, 1)
167162

168163
callback, mock = get_callback(200, JWKS_RESPONSE_MULTIPLE_KEYS)
169-
mocked.get(JWKS_URI, callback=callback)
164+
await mocked.get(JWKS_URI, callback=callback)
170165

171166
# Triggers the second call
172167
key_2 = await fetcher.get_key("test-key-2")
@@ -182,12 +177,13 @@ async def test_async_fetches_jwks_json_forced_on_cache_miss(self, mocked):
182177
)
183178
self.assertEqual(mock.call_count, 1)
184179

180+
@pytest.mark.asyncio
185181
@aioresponses()
186182
async def test_async_fetches_jwks_json_once_on_cache_miss(self, mocked):
187183
fetcher = AsyncJwksFetcher(JWKS_URI, cache_ttl=1)
188184

189185
callback, mock = get_callback(200, JWKS_RESPONSE_SINGLE_KEY)
190-
mocked.get(JWKS_URI, callback=callback)
186+
await mocked.get(JWKS_URI, callback=callback)
191187

192188
with self.assertRaises(Exception) as err:
193189
await fetcher.get_key("missing-key")
@@ -204,13 +200,14 @@ async def test_async_fetches_jwks_json_once_on_cache_miss(self, mocked):
204200
)
205201
self.assertEqual(mock.call_count, 1)
206202

203+
@pytest.mark.asyncio
207204
@aioresponses()
208205
async def test_async_fails_to_fetch_jwks_json_after_retrying_twice(self, mocked):
209206
fetcher = AsyncJwksFetcher(JWKS_URI, cache_ttl=1)
210207

211208
callback, mock = get_callback(500, {})
212-
mocked.get(JWKS_URI, callback=callback)
213-
mocked.get(JWKS_URI, callback=callback)
209+
await mocked.get(JWKS_URI, callback=callback)
210+
await mocked.get(JWKS_URI, callback=callback)
214211

215212
with self.assertRaises(Exception) as err:
216213
await fetcher.get_key("id1")
@@ -228,15 +225,12 @@ async def test_async_fails_to_fetch_jwks_json_after_retrying_twice(self, mocked)
228225
self.assertEqual(mock.call_count, 2)
229226

230227

231-
@unittest.skipIf(
232-
not hasattr(unittest, "IsolatedAsyncioTestCase"),
233-
"python 3.7 doesn't have IsolatedAsyncioTestCase",
234-
)
235-
class TestAsyncTokenVerifier(getattr(unittest, "IsolatedAsyncioTestCase", object)):
228+
class TestAsyncTokenVerifier(unittest.TestCase):
229+
@pytest.mark.asyncio
236230
@aioresponses()
237231
async def test_RS256_token_signature_passes(self, mocked):
238232
callback, mock = get_callback(200, {"keys": [PUBLIC_KEY]})
239-
mocked.get(JWKS_URI, callback=callback)
233+
await mocked.get(JWKS_URI, callback=callback)
240234

241235
issuer = "https://tokens-test.auth0.com/"
242236
audience = "tokens-test-123"
@@ -261,12 +255,13 @@ async def test_RS256_token_signature_passes(self, mocked):
261255
payload = await tv.verify(token)
262256
self.assertEqual(payload["sub"], "auth0|123456789")
263257

258+
@pytest.mark.asyncio
264259
@aioresponses()
265260
async def test_RS256_token_signature_fails(self, mocked):
266261
callback, mock = get_callback(
267262
200, {"keys": [RSA_PUB_KEY_1_JWK]}
268263
) # different pub key
269-
mocked.get(JWKS_URI, callback=callback)
264+
await mocked.get(JWKS_URI, callback=callback)
270265

271266
issuer = "https://tokens-test.auth0.com/"
272267
audience = "tokens-test-123"

auth0/test_async/test_asyncify.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ def callback(url, **kwargs):
5454
return callback, mock
5555

5656

57-
@unittest.skipIf(
58-
not hasattr(unittest, "IsolatedAsyncioTestCase"),
59-
"python 3.7 doesn't have IsolatedAsyncioTestCase",
60-
)
61-
class TestAsyncify(getattr(unittest, "IsolatedAsyncioTestCase", object)):
57+
class TestAsyncify(unittest.TestCase):
6258
@pytest.mark.asyncio
6359
@aioresponses()
6460
async def test_get(self, mocked):

0 commit comments

Comments
 (0)