@@ -89,13 +89,25 @@ def get_token(self, *scopes, **kwargs):
8989
9090@pytest .mark .cosmosEmulator
9191@pytest .mark .cosmosAAD
92+ @pytest .mark .skipif (
93+ not test_config .TestConfig .is_emulator
94+ and test_config .TestConfig .data_auth_mode != 'aad' ,
95+ reason = "On a live account, run this file with COSMOS_TEST_DATA_AUTH_MODE=aad "
96+ "so the dual-client factory returns the AAD branch. Otherwise the "
97+ "test would silently use the master key and `delete_database` would "
98+ "succeed instead of returning the asserted 403." ,
99+ )
92100class TestAAD (unittest .TestCase ):
93101 client : cosmos_client .CosmosClient = None
94102 database : DatabaseProxy = None
95103 container : ContainerProxy = None
96104 configs = test_config .TestConfig
97105 host = configs .host
98106 masterKey = configs .masterKey
107+ # Emulator-only: the hand-crafted JWT lets us exercise the AAD code path
108+ # against the local emulator (which has no real AAD endpoint). On a live
109+ # account this attribute is unused; setUpClass routes through the dual-client
110+ # factory below instead.
99111 credential = CosmosEmulatorCredential () if configs .is_emulator else configs .credential
100112 _skip_scope_tests_on_non_emulator = pytest .mark .skipif (
101113 not configs .is_emulator ,
@@ -104,7 +116,23 @@ class TestAAD(unittest.TestCase):
104116
105117 @classmethod
106118 def setUpClass (cls ):
107- cls .client = cosmos_client .CosmosClient (cls .host , cls .credential )
119+ # Two construction paths:
120+ #
121+ # * Emulator runs (`pytest -m cosmosEmulator`): build the client
122+ # directly with `CosmosEmulatorCredential` so the AAD JWT-parsing
123+ # code path is exercised against the local emulator. The dual-client
124+ # factory cannot do this — on the emulator it returns the master-key
125+ # client and bypasses AAD entirely.
126+ #
127+ # * Live runs (`pytest -m cosmosAAD` on the AAD lane, or any live
128+ # run with `COSMOS_TEST_DATA_AUTH_MODE=aad`): go through
129+ # `TestConfig.create_data_client()` so this test exercises the
130+ # same dual-client factory contract every other AAD-tagged test
131+ # uses.
132+ if cls .configs .is_emulator :
133+ cls .client = cosmos_client .CosmosClient (cls .host , cls .credential )
134+ else :
135+ cls .client = test_config .TestConfig .create_data_client ()
108136 cls .database = cls .client .get_database_client (cls .configs .TEST_DATABASE_ID )
109137 cls .container = cls .database .get_container_client (cls .configs .TEST_SINGLE_PARTITION_CONTAINER_ID )
110138
0 commit comments