4
4
# license information.
5
5
# -------------------------------------------------------------------------
6
6
import asyncio
7
+ import sys
7
8
import time
8
- from unittest .mock import Mock
9
+ from unittest .mock import Mock , patch
9
10
from requests import Response
10
11
11
12
from azure .core .credentials import AccessToken
20
21
)
21
22
from azure .core .pipeline .transport import AsyncHttpTransport , HttpRequest
22
23
import pytest
24
+ import trio
23
25
24
- pytestmark = pytest .mark .asyncio
25
26
from utils import HTTP_REQUESTS
26
27
27
28
29
+ @pytest .mark .asyncio
28
30
@pytest .mark .parametrize ("http_request" , HTTP_REQUESTS )
29
31
async def test_bearer_policy_adds_header (http_request ):
30
32
"""The bearer token policy should add a header containing a token from its credential"""
@@ -54,6 +56,7 @@ async def get_token(*_, **__):
54
56
assert get_token_calls == 1
55
57
56
58
59
+ @pytest .mark .asyncio
57
60
@pytest .mark .parametrize ("http_request" , HTTP_REQUESTS )
58
61
async def test_bearer_policy_send (http_request ):
59
62
"""The bearer token policy should invoke the next policy's send method and return the result"""
@@ -72,6 +75,7 @@ async def verify_request(request):
72
75
assert response is expected_response
73
76
74
77
78
+ @pytest .mark .asyncio
75
79
@pytest .mark .parametrize ("http_request" , HTTP_REQUESTS )
76
80
async def test_bearer_policy_sync_send (http_request ):
77
81
"""The bearer token policy should invoke the next policy's send method and return the result"""
@@ -90,6 +94,7 @@ async def verify_request(request):
90
94
assert response is expected_response
91
95
92
96
97
+ @pytest .mark .asyncio
93
98
@pytest .mark .parametrize ("http_request" , HTTP_REQUESTS )
94
99
async def test_bearer_policy_token_caching (http_request ):
95
100
good_for_one_hour = AccessToken ("token" , time .time () + 3600 )
@@ -130,6 +135,7 @@ async def get_token(*_, **__):
130
135
assert get_token_calls == 2 # token expired -> policy should call get_token
131
136
132
137
138
+ @pytest .mark .asyncio
133
139
@pytest .mark .parametrize ("http_request" , HTTP_REQUESTS )
134
140
async def test_bearer_policy_optionally_enforces_https (http_request ):
135
141
"""HTTPS enforcement should be controlled by a keyword argument, and enabled by default"""
@@ -158,6 +164,7 @@ async def assert_option_popped(request, **kwargs):
158
164
await pipeline .run (http_request ("GET" , "https://secure" ))
159
165
160
166
167
+ @pytest .mark .asyncio
161
168
@pytest .mark .parametrize ("http_request" , HTTP_REQUESTS )
162
169
async def test_bearer_policy_preserves_enforce_https_opt_out (http_request ):
163
170
"""The policy should use request context to preserve an opt out from https enforcement"""
@@ -175,6 +182,7 @@ def on_request(self, request):
175
182
await pipeline .run (http_request ("GET" , "http://not.secure" ), enforce_https = False )
176
183
177
184
185
+ @pytest .mark .asyncio
178
186
@pytest .mark .parametrize ("http_request" , HTTP_REQUESTS )
179
187
async def test_bearer_policy_context_unmodified_by_default (http_request ):
180
188
"""When no options for the policy accompany a request, the policy shouldn't add anything to the request context"""
@@ -192,6 +200,7 @@ def on_request(self, request):
192
200
await pipeline .run (http_request ("GET" , "https://secure" ))
193
201
194
202
203
+ @pytest .mark .asyncio
195
204
@pytest .mark .parametrize ("http_request" , HTTP_REQUESTS )
196
205
async def test_bearer_policy_calls_sansio_methods (http_request ):
197
206
"""AsyncBearerTokenCredentialPolicy should call SansIOHttpPolicy methods as does _SansIOAsyncHTTPPolicyRunner"""
@@ -440,3 +449,24 @@ async def get_token(self, *scopes, **kwargs):
440
449
441
450
cred = TestTokenCredential ()
442
451
await cred .get_token ("scope" )
452
+
453
+
454
+ @pytest .mark .asyncio
455
+ async def test_async_token_credential_asyncio_lock ():
456
+ auth_policy = AsyncBearerTokenCredentialPolicy (Mock (), "scope" )
457
+ assert isinstance (auth_policy ._lock , asyncio .Lock )
458
+
459
+
460
+ @pytest .mark .trio
461
+ async def test_async_token_credential_trio_lock ():
462
+ auth_policy = AsyncBearerTokenCredentialPolicy (Mock (), "scope" )
463
+ assert isinstance (auth_policy ._lock , trio .Lock )
464
+
465
+
466
+ def test_async_token_credential_sync ():
467
+ """Verify that AsyncBearerTokenCredentialPolicy can be constructed in a synchronous context."""
468
+ auth_policy = AsyncBearerTokenCredentialPolicy (Mock (), "scope" )
469
+ with patch .dict ("sys.modules" ):
470
+ # Ensure trio isn't in sys.modules (i.e. imported).
471
+ sys .modules .pop ("trio" , None )
472
+ AsyncBearerTokenCredentialPolicy (Mock (), "scope" )
0 commit comments