1212from azure .core .credentials import AccessToken
1313
1414import test_config
15- from azure .cosmos import exceptions
1615from azure .cosmos .aio import CosmosClient , DatabaseProxy , ContainerProxy
1716from azure .core .exceptions import HttpResponseError
1817
18+
19+
1920def _remove_padding (encoded_string ):
2021 while encoded_string .endswith ("=" ):
2122 encoded_string = encoded_string [0 :len (encoded_string ) - 1 ]
@@ -35,7 +36,7 @@ def get_test_item(num):
3536
3637class CosmosEmulatorCredential (object ):
3738 async def get_token (self , * scopes , ** kwargs ):
38- # type: (*str, **Any ) -> AccessToken
39+ # type: (*str, **object ) -> AccessToken
3940 """Request an access token for the emulator. Based on Azure Core's Access Token Credential.
4041
4142 This method is called automatically by Azure SDK clients.
@@ -93,16 +94,11 @@ class TestAADAsync(unittest.IsolatedAsyncioTestCase):
9394 configs = test_config .TestConfig
9495 host = configs .host
9596 masterKey = configs .masterKey
96- credential = CosmosEmulatorCredential () if configs .is_emulator else configs .credential_async
97-
98- @classmethod
99- def setUpClass (cls ):
100- if (cls .credential == '[YOUR_KEY_HERE]' or
101- cls .host == '[YOUR_ENDPOINT_HERE]' ):
102- raise Exception (
103- "You must specify your Azure Cosmos account values for "
104- "'masterKey' and 'host' at the top of this class to run the "
105- "tests." )
97+ credential = CosmosEmulatorCredential ()
98+ _skip_scope_tests_on_non_emulator = pytest .mark .skipif (
99+ not configs .is_emulator ,
100+ reason = "Scope capture tests are emulator-specific (localhost audience)."
101+ )
106102
107103 async def asyncSetUp (self ):
108104 self .client = CosmosClient (self .host , self .credential )
@@ -112,9 +108,8 @@ async def asyncSetUp(self):
112108 async def asyncTearDown (self ):
113109 await self .client .close ()
114110
111+ @_skip_scope_tests_on_non_emulator
115112 async def test_aad_credentials_async (self ):
116- # Do any R/W data operations with your authorized AAD client
117-
118113 print ("Container info: " + str (await self .container .read ()))
119114 await self .container .create_item (get_test_item (0 ))
120115 print ("Point read result: " + str (await self .container .read_item (item = 'Item_0' , partition_key = 'pk' )))
@@ -123,12 +118,6 @@ async def test_aad_credentials_async(self):
123118 print ("Query result: " + str (query_results [0 ]))
124119 await self .container .delete_item (item = 'Item_0' , partition_key = 'pk' )
125120
126- # Attempting to do management operations will return a 403 Forbidden exception
127- try :
128- await self .client .delete_database (self .configs .TEST_DATABASE_ID )
129- except exceptions .CosmosHttpResponseError as e :
130- assert e .status_code == 403
131- print ("403 error assertion success" )
132121
133122 async def _run_with_scope_capture_async (self , credential_cls , action ):
134123 scopes_captured = []
@@ -146,6 +135,7 @@ async def capturing_get_token(self, *scopes, **kwargs):
146135 finally :
147136 credential_cls .get_token = orig_get_token
148137
138+ @_skip_scope_tests_on_non_emulator
149139 async def test_override_scope_no_fallback_async (self ):
150140 """When override scope is provided, only that scope is used and no fallback occurs."""
151141 override_scope = "https://my.custom.scope/.default"
@@ -172,6 +162,7 @@ async def action(scopes_captured):
172162 except Exception :
173163 pass
174164
165+ @_skip_scope_tests_on_non_emulator
175166 async def test_override_scope_no_fallback_on_error_async (self ):
176167 """When override scope is provided and auth fails, no fallback occurs."""
177168 override_scope = "https://my.custom.scope/.default"
@@ -205,6 +196,7 @@ async def action(scopes_captured):
205196 except Exception :
206197 pass
207198
199+ @_skip_scope_tests_on_non_emulator
208200 async def test_account_scope_only_async (self ):
209201 """When account scope is provided, only that scope is used."""
210202 account_scope = "https://localhost/.default"
@@ -230,6 +222,7 @@ async def action(scopes_captured):
230222 except Exception :
231223 pass
232224
225+ @_skip_scope_tests_on_non_emulator
233226 async def test_account_scope_fallback_on_error_async (self ):
234227 """When account scope is provided and auth fails, fallback to default scope occurs."""
235228 account_scope = "https://localhost/.default"
0 commit comments