async def test_close(): transport = AsyncMockTransport() credential = ClientSecretCredential("tenant-id", "client-id", "client-secret", transport=transport) await credential.close() assert transport.__aexit__.call_count == 1
async def test_close(): transport = AsyncMockTransport() credential = CertificateCredential("tenant-id", "client-id", PEM_CERT_PATH, transport=transport) await credential.close() assert transport.__aexit__.call_count == 1
async def test_close(): transport = AsyncMockTransport() client = AsyncManagedIdentityClient(lambda *_: None, transport=transport) await client.close() assert transport.__aexit__.call_count == 1
async def test_imds_close(): transport = AsyncMockTransport() credential = ImdsCredential(transport=transport) await credential.close() assert transport.__aexit__.call_count == 1
async def test_close(environ): transport = AsyncMockTransport() with mock.patch.dict(MANAGED_IDENTITY_ENVIRON, environ, clear=True): credential = ManagedIdentityCredential(transport=transport) await credential.close() assert transport.__aexit__.call_count == 1
async def test_imds_context_manager(): transport = AsyncMockTransport() credential = ImdsCredential(transport=transport) async with credential: pass assert transport.__aexit__.call_count == 1
async def test_close(): transport = AsyncMockTransport() credential = SharedTokenCacheCredential(_cache=populated_cache( get_account_event("test@user", "uid", "utid")), transport=transport) await credential.close() assert transport.__aexit__.call_count == 1
async def test_close(): transport = AsyncMockTransport() credential = AuthorizationCodeCredential( "tenant-id", "client-id", "auth-code", "http://localhost", transport=transport ) await credential.close() assert transport.__aexit__.call_count == 1
async def test_context_manager(): transport = AsyncMockTransport() credential = CertificateCredential("tenant-id", "client-id", PEM_CERT_PATH, transport=transport) async with credential: assert transport.__aenter__.call_count == 1 assert transport.__aenter__.call_count == 1 assert transport.__aexit__.call_count == 1
async def test_close(): transport = AsyncMockTransport() with mock.patch("os.environ", {EnvironmentVariables.MSI_ENDPOINT: "https://url"}): credential = MsiCredential(transport=transport) await credential.close() assert transport.__aexit__.call_count == 1
async def test_context_manager(): transport = AsyncMockTransport() credential = ClientSecretCredential("tenant-id", "client-id", "client-secret", transport=transport) async with credential: assert transport.__aenter__.call_count == 1 assert transport.__aenter__.call_count == 1 assert transport.__aexit__.call_count == 1
async def test_context_manager(): transport = AsyncMockTransport() client = AsyncManagedIdentityClient(lambda *_: None, transport=transport) async with client: assert transport.__aenter__.call_count == 1 assert transport.__aexit__.call_count == 0 assert transport.__aenter__.call_count == 1 assert transport.__aexit__.call_count == 1
async def test_context_manager(): transport = AsyncMockTransport() credential = AuthorizationCodeCredential( "tenant-id", "client-id", "auth-code", "http://localhost", transport=transport ) async with credential: assert transport.__aenter__.call_count == 1 assert transport.__aenter__.call_count == 1 assert transport.__aexit__.call_count == 1
async def test_context_manager(environ): transport = AsyncMockTransport() with mock.patch.dict(MANAGED_IDENTITY_ENVIRON, environ, clear=True): credential = ManagedIdentityCredential(transport=transport) async with credential: assert transport.__aenter__.call_count == 1 assert transport.__aexit__.call_count == 0 assert transport.__aenter__.call_count == 1 assert transport.__aexit__.call_count == 1
async def test_context_manager(): transport = AsyncMockTransport() with mock.patch("os.environ", {EnvironmentVariables.MSI_ENDPOINT: "https://url"}): credential = MsiCredential(transport=transport) async with credential: assert transport.__aenter__.call_count == 1 assert transport.__aenter__.call_count == 1 assert transport.__aexit__.call_count == 1
async def test_close(): transport = AsyncMockTransport() credential = OnBehalfOfCredential("tenant-id", "client-id", client_secret="client-secret", user_assertion="assertion", transport=transport) await credential.close() assert transport.__aexit__.call_count == 1
async def test_context_manager(): transport = AsyncMockTransport() credential = SharedTokenCacheCredential(_cache=populated_cache( get_account_event("test@user", "uid", "utid")), transport=transport) async with credential: assert transport.__aenter__.call_count == 1 assert transport.__aenter__.call_count == 1 assert transport.__aexit__.call_count == 1
async def test_close(): transport = AsyncMockTransport() with mock.patch.dict( ENVIRON, {var: "..." for var in EnvironmentVariables.CLIENT_SECRET_VARS}, clear=True): credential = EnvironmentCredential(transport=transport) assert transport.__aexit__.call_count == 0 await credential.close() assert transport.__aexit__.call_count == 1
async def test_context_manager_no_cache(): """the credential shouldn't open/close sessions when instantiated in an environment with no cache""" transport = AsyncMockTransport() with patch.dict("azure.identity._internal.shared_token_cache.os.environ", {}, clear=True): # clearing the environment ensures the credential won't try to load a cache credential = SharedTokenCacheCredential(transport=transport) async with credential: assert transport.__aenter__.call_count == 0 assert transport.__aenter__.call_count == 0 assert transport.__aexit__.call_count == 0
async def test_context_manager_no_cache(): """the credential shouldn't open/close sessions when instantiated in an environment with no cache""" transport = AsyncMockTransport() with patch("azure.identity._internal.shared_token_cache.load_user_cache", Mock(side_effect=NotImplementedError)): credential = SharedTokenCacheCredential(transport=transport) async with credential: assert transport.__aenter__.call_count == 0 assert transport.__aenter__.call_count == 0 assert transport.__aexit__.call_count == 0
async def test_context_manager(): transport = AsyncMockTransport() credential = OnBehalfOfCredential("tenant-id", "client-id", client_secret="client-secret", user_assertion="assertion", transport=transport) async with credential: assert transport.__aenter__.call_count == 1 assert not transport.__aexit__.called assert transport.__aenter__.call_count == 1 assert transport.__aexit__.call_count == 1
async def test_close(): async def send(*_, **__): return mock_response(json_payload=build_aad_response(access_token="**")) transport = AsyncMockTransport(send=send) credential = SharedTokenCacheCredential( _cache=populated_cache(get_account_event("test@user", "uid", "utid")), transport=transport ) # the credential doesn't open a transport session before one is needed, so we send a request await credential.get_token("scope") await credential.close() assert transport.__aexit__.call_count == 1
async def test_context_manager(): async def send(*_, **__): return mock_response(json_payload=build_aad_response(access_token="**")) transport = AsyncMockTransport(send=send) credential = SharedTokenCacheCredential( _cache=populated_cache(get_account_event("test@user", "uid", "utid")), transport=transport ) # async with before initialization: credential should call aexit but not aenter async with credential: await credential.get_token("scope") assert transport.__aenter__.call_count == 0 assert transport.__aexit__.call_count == 1 # async with after initialization: credential should call aenter and aexit async with credential: await credential.get_token("scope") assert transport.__aenter__.call_count == 1 assert transport.__aexit__.call_count == 2