1111 UC_FUNCTIONS_MCP ,
1212 VECTOR_SEARCH_MCP ,
1313 DatabricksMCPClient ,
14+ _is_databricks_apps_url ,
15+ _is_oauth_auth ,
1416)
1517
1618
@@ -131,6 +133,7 @@ async def test_get_tools_async(self):
131133 with (
132134 patch ("databricks_mcp.mcp.streamablehttp_client" ) as mock_client ,
133135 patch ("databricks_mcp.mcp.ClientSession" ) as mock_session_class ,
136+ patch ("databricks_mcp.mcp.DatabricksOAuthClientProvider" ),
134137 ):
135138 mock_client .return_value .__aenter__ .return_value = (AsyncMock (), AsyncMock (), None )
136139 mock_session_class .return_value .__aenter__ .return_value = mock_session
@@ -156,6 +159,7 @@ async def test_call_tools_async(self):
156159 with (
157160 patch ("databricks_mcp.mcp.streamablehttp_client" ) as mock_client ,
158161 patch ("databricks_mcp.mcp.ClientSession" ) as mock_session_class ,
162+ patch ("databricks_mcp.mcp.DatabricksOAuthClientProvider" ),
159163 ):
160164 mock_client .return_value .__aenter__ .return_value = (AsyncMock (), AsyncMock (), None )
161165 mock_session_class .return_value .__aenter__ .return_value = mock_session
@@ -434,6 +438,7 @@ def test_error_decorator_managed_server_reraises_original(self):
434438 client , "_get_databricks_managed_mcp_url_type" , return_value = UC_FUNCTIONS_MCP
435439 ),
436440 patch ("databricks_mcp.mcp.streamablehttp_client" ) as mock_client ,
441+ patch ("databricks_mcp.mcp.DatabricksOAuthClientProvider" ),
437442 patch ("requests.request" ) as mock_request ,
438443 ):
439444 mock_client .side_effect = original_error
@@ -442,3 +447,57 @@ def test_error_decorator_managed_server_reraises_original(self):
442447 client .list_tools ()
443448
444449 mock_request .assert_not_called ()
450+
451+
452+ class TestIsDatabricksAppsUrl :
453+ """Test cases for _is_databricks_apps_url helper function."""
454+
455+ @pytest .mark .parametrize (
456+ "url,expected" ,
457+ [
458+ ("https://my-app.staging.aws.databricksapps.com/mcp" , True ),
459+ ("https://my-app.prod.azure.databricksapps.com/mcp" , True ),
460+ ("https://my-app.databricksapps.com" , True ),
461+ ("https://test.cloud.databricks.com/api/2.0/mcp/functions/a/b" , False ),
462+ ("https://custom-server.example.com/mcp" , False ),
463+ ("https://databricksapps.com.evil.com/mcp" , False ),
464+ ("https://notdatabricksapps.com/mcp" , False ),
465+ ],
466+ )
467+ def test_is_databricks_apps_url (self , url , expected ):
468+ assert _is_databricks_apps_url (url ) == expected
469+
470+
471+ class TestIsOauthAuth :
472+ @pytest .mark .parametrize (
473+ "side_effect,expected" ,
474+ [
475+ (None , True ), # oauth_token succeeds
476+ (ValueError ("not available" ), False ), # oauth_token raises
477+ ],
478+ )
479+ def test_is_oauth_auth (self , side_effect , expected ):
480+ mock_client = MagicMock (spec = WorkspaceClient )
481+ if side_effect :
482+ mock_client .config .oauth_token .side_effect = side_effect
483+ assert _is_oauth_auth (mock_client ) is expected
484+
485+
486+ class TestDatabricksMCPClientOAuthValidation :
487+ @pytest .mark .parametrize ("auth_type" , ["pat" , "runtime" ])
488+ def test_raises_error_for_non_oauth_with_databricks_apps (self , auth_type ):
489+ mock_client = MagicMock (spec = WorkspaceClient )
490+ mock_client .config .oauth_token .side_effect = ValueError (f"not available for { auth_type } " )
491+ with pytest .raises (ValueError , match = "OAuth authentication is required" ):
492+ DatabricksMCPClient ("https://my-app.databricksapps.com/mcp" , mock_client )
493+
494+ def test_allows_oauth_with_databricks_apps (self ):
495+ mock_client = MagicMock (spec = WorkspaceClient )
496+ client = DatabricksMCPClient ("https://my-app.databricksapps.com/mcp" , mock_client )
497+ assert client .server_url == "https://my-app.databricksapps.com/mcp"
498+
499+ def test_allows_non_oauth_with_non_databricks_apps (self ):
500+ mock_client = MagicMock (spec = WorkspaceClient )
501+ mock_client .config .oauth_token .side_effect = ValueError ("not available" )
502+ client = DatabricksMCPClient ("https://test.com/api/2.0/mcp/functions/a/b" , mock_client )
503+ assert client .server_url == "https://test.com/api/2.0/mcp/functions/a/b"
0 commit comments