@@ -37,23 +37,34 @@ class MockInteractiveBrowserCredential:
3737 def get_token (self , scope ):
3838 return MockToken ()
3939
40+ # Mock ClientAuthenticationError
41+ class MockClientAuthenticationError (Exception ):
42+ pass
43+
4044 class MockIdentity :
4145 DefaultAzureCredential = MockDefaultAzureCredential
4246 DeviceCodeCredential = MockDeviceCodeCredential
4347 InteractiveBrowserCredential = MockInteractiveBrowserCredential
4448
49+ class MockCore :
50+ class exceptions :
51+ ClientAuthenticationError = MockClientAuthenticationError
52+
4553 # Create mock azure module if it doesn't exist
4654 if 'azure' not in sys .modules :
4755 sys .modules ['azure' ] = type ('MockAzure' , (), {})()
4856
49- # Add identity module to azure
57+ # Add identity and core modules to azure
5058 sys .modules ['azure.identity' ] = MockIdentity ()
59+ sys .modules ['azure.core' ] = MockCore ()
60+ sys .modules ['azure.core.exceptions' ] = MockCore .exceptions ()
5161
5262 yield
5363
5464 # Cleanup
55- if 'azure.identity' in sys .modules :
56- del sys .modules ['azure.identity' ]
65+ for module in ['azure.identity' , 'azure.core' , 'azure.core.exceptions' ]:
66+ if module in sys .modules :
67+ del sys .modules [module ]
5768
5869class TestAuthType :
5970 def test_auth_type_constants (self ):
@@ -67,18 +78,55 @@ def test_get_token_struct(self):
6778 assert isinstance (token_struct , bytes )
6879 assert len (token_struct ) > 4
6980
70- def test_get_default_token (self ):
71- token_struct = AADAuth .get_default_token ( )
81+ def test_get_token_default (self ):
82+ token_struct = AADAuth .get_token ( "default" )
7283 assert isinstance (token_struct , bytes )
7384
74- def test_get_device_code_token (self ):
75- token_struct = AADAuth .get_device_code_token ( )
85+ def test_get_token_device_code (self ):
86+ token_struct = AADAuth .get_token ( "devicecode" )
7687 assert isinstance (token_struct , bytes )
7788
78- def test_get_interactive_token (self ):
79- token_struct = AADAuth .get_interactive_token ( )
89+ def test_get_token_interactive (self ):
90+ token_struct = AADAuth .get_token ( "interactive" )
8091 assert isinstance (token_struct , bytes )
8192
93+ def test_get_token_credential_mapping (self ):
94+ # Test that all supported auth types work
95+ supported_types = ["default" , "devicecode" , "interactive" ]
96+ for auth_type in supported_types :
97+ token_struct = AADAuth .get_token (auth_type )
98+ assert isinstance (token_struct , bytes )
99+ assert len (token_struct ) > 4
100+
101+ def test_get_token_client_authentication_error (self ):
102+ """Test that ClientAuthenticationError is properly handled"""
103+ from azure .core .exceptions import ClientAuthenticationError
104+
105+ # Create a mock credential that raises ClientAuthenticationError
106+ class MockFailingCredential :
107+ def get_token (self , scope ):
108+ raise ClientAuthenticationError ("Mock authentication failed" )
109+
110+ # Use monkeypatch to mock the credential creation
111+ def mock_get_token_failing (auth_type ):
112+ from azure .core .exceptions import ClientAuthenticationError
113+ if auth_type == "default" :
114+ try :
115+ credential = MockFailingCredential ()
116+ token = credential .get_token ("https://database.windows.net/.default" ).token
117+ return AADAuth .get_token_struct (token )
118+ except ClientAuthenticationError as e :
119+ raise RuntimeError (
120+ f"Azure AD authentication failed for MockFailingCredential: { e } . "
121+ f"This could be due to invalid credentials, missing environment variables, "
122+ f"user cancellation, network issues, or unsupported configuration."
123+ ) from e
124+ else :
125+ return AADAuth .get_token (auth_type )
126+
127+ with pytest .raises (RuntimeError , match = "Azure AD authentication failed" ):
128+ mock_get_token_failing ("default" )
129+
82130class TestProcessAuthParameters :
83131 def test_empty_parameters (self ):
84132 modified_params , auth_type = process_auth_parameters ([])
0 commit comments