@@ -189,3 +189,74 @@ def test_load_azure_tenant_id_happy_path(requests_mock, monkeypatch):
189189 cfg = Config (host = "https://abc123.azuredatabricks.net" )
190190 assert cfg .azure_tenant_id == "tenant-id"
191191 assert mock .called_once
192+
193+
194+ def test_oauth_token_with_pat_auth ():
195+ """Test that oauth_token() raises an error for PAT authentication."""
196+ config = Config (host = "https://test.databricks.com" , token = "dapi1234567890abcdef" )
197+
198+ with pytest .raises (ValueError ) as exc_info :
199+ config .oauth_token ()
200+
201+ assert "OAuth tokens are not available for pat authentication" in str (exc_info .value )
202+
203+
204+ def test_oauth_token_with_basic_auth ():
205+ """Test that oauth_token() raises an error for basic authentication."""
206+ config = Config (host = "https://test.databricks.com" , username = "testuser" , password = "testpass" )
207+
208+ with pytest .raises (ValueError ) as exc_info :
209+ config .oauth_token ()
210+
211+ assert "OAuth tokens are not available for basic authentication" in str (exc_info .value )
212+
213+
214+ def test_oauth_token_with_oauth_provider (mocker ):
215+ """Test that oauth_token() works correctly for OAuth authentication."""
216+ from databricks .sdk .credentials_provider import OAuthCredentialsProvider
217+ from databricks .sdk .oauth import Token
218+
219+ # Create a mock OAuth token
220+ mock_token = Token (access_token = "mock_access_token" , token_type = "Bearer" , refresh_token = "mock_refresh_token" )
221+
222+ # Create a mock OAuth provider
223+ mock_oauth_provider = mocker .Mock (spec = OAuthCredentialsProvider )
224+ mock_oauth_provider .oauth_token .return_value = mock_token
225+
226+ # Create config with mocked header factory
227+ config = Config (host = "https://test.databricks.com" , client_id = "test-client-id" , client_secret = "test-client-secret" )
228+
229+ # Replace the header factory with our mock
230+ config ._header_factory = mock_oauth_provider
231+
232+ # Test that oauth_token() works and returns the expected token
233+ token = config .oauth_token ()
234+ assert token == mock_token
235+ mock_oauth_provider .oauth_token .assert_called_once ()
236+
237+
238+ def test_oauth_token_reuses_existing_provider (mocker ):
239+ """Test that oauth_token() reuses the existing OAuthCredentialsProvider."""
240+ from databricks .sdk .credentials_provider import OAuthCredentialsProvider
241+ from databricks .sdk .oauth import Token
242+
243+ # Create a mock OAuth token
244+ mock_token = Token (access_token = "mock_access_token" , token_type = "Bearer" , refresh_token = "mock_refresh_token" )
245+
246+ # Create a mock OAuth provider
247+ mock_oauth_provider = mocker .Mock (spec = OAuthCredentialsProvider )
248+ mock_oauth_provider .oauth_token .return_value = mock_token
249+
250+ # Create config with mocked header factory
251+ config = Config (host = "https://test.databricks.com" , client_id = "test-client-id" , client_secret = "test-client-secret" )
252+
253+ # Replace the header factory with our mock
254+ config ._header_factory = mock_oauth_provider
255+
256+ # Call oauth_token() multiple times to verify reuse
257+ token1 = config .oauth_token ()
258+ token2 = config .oauth_token ()
259+
260+ # Both calls should work and use the same provider instance
261+ assert token1 == token2 == mock_token
262+ assert mock_oauth_provider .oauth_token .call_count == 2
0 commit comments