Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
test: use real MCP auth and session fixtures (#43159)
Asuka Minato5ab572e0a7df
diff --git a/api/tests/unit_tests/core/mcp/auth/test_auth_flow.py b/api/tests/unit_tests/core/mcp/auth/test_auth_flow.pyindex 32a48500fe9..e5dbd0effe8 100644--- a/api/tests/unit_tests/core/mcp/auth/test_auth_flow.py+++ b/api/tests/unit_tests/core/mcp/auth/test_auth_flow.py@@ -43,6 +43,15 @@ from core.mcp.types import (OAuthTokens,ProtectedResourceMetadata,)+from tests.unit_tests.core.mcp.fixtures import make_provider+++def provider(monkeypatch: pytest.MonkeyPatch) -> MCPProviderEntity:+ """Use real credential parsing with plaintext values at the decryption boundary."""+ monkeypatch.setattr("core.entities.mcp_provider.encrypter.decrypt_token", lambda _tenant_id, token: token)+ monkeypatch.setattr(MCPProviderEntity, "_decrypt_dict", lambda _self, data: data.copy())+ return make_provider(credentials={})class TestPKCEGeneration:@@ -600,22 +609,6 @@ class TestCallbackHandling:class TestAuthOrchestration:"""Test the main auth orchestration function."""- @pytest.fixture- def mock_provider(self):- """Create a mock provider entity."""- provider = Mock(spec=MCPProviderEntity)- provider.id = "provider-id"- provider.tenant_id = "tenant-id"- provider.decrypt_server_url.return_value = "https://api.example.com"- provider.client_metadata = OAuthClientMetadata(- client_name="Dify",- redirect_uris=["https://redirect.example.com"],- )- provider.redirect_url = "https://redirect.example.com"- provider.retrieve_client_information.return_value = None- provider.retrieve_tokens.return_value = None- return provider-@pytest.fixturedef mock_service(self):"""Create a mock MCP service."""@@ -624,7 +617,7 @@ class TestAuthOrchestration:@patch("core.mcp.auth.auth_flow.discover_oauth_metadata")@patch("core.mcp.auth.auth_flow.register_client")@patch("core.mcp.auth.auth_flow.start_authorization")- def test_auth_new_registration(self, mock_start_auth, mock_register, mock_discover, mock_provider, mock_service):+ def test_auth_new_registration(self, mock_start_auth, mock_register, mock_discover, provider, mock_service):"""Test auth flow for new client registration."""# Setupmock_discover.return_value = (@@ -644,7 +637,7 @@ class TestAuthOrchestration:)mock_start_auth.return_value = ("https://auth.example.com/authorize?...", "code-verifier")- result = auth(mock_provider)+ result = auth(provider)# auth() now returns AuthResultassert isinstance(result, AuthResult)@@ -670,7 +663,7 @@ class TestAuthOrchestration:@patch("core.mcp.auth.auth_flow.discover_oauth_metadata")@patch("core.mcp.auth.auth_flow._retrieve_redis_state")@patch("core.mcp.auth.auth_flow.exchange_authorization")- def test_auth_exchange_code(self, mock_exchange, mock_retrieve_state, mock_discover, mock_provider, mock_service):+ def test_auth_exchange_code(self, mock_exchange, mock_retrieve_state, mock_discover, provider, mock_service):"""Test auth flow for exchanging authorization code."""# Setup metadata discoverymock_discover.return_value = (@@ -685,7 +678,7 @@ class TestAuthOrchestration:)# Setup existing client- mock_provider.retrieve_client_information.return_value = OAuthClientInformation(client_id="existing-client")+ provider.credentials["client_information"] = {"client_id": "existing-client"}# Setup state retrievalstate_data = OAuthCallbackState(@@ -703,7 +696,7 @@ class TestAuthOrchestration:tokens = OAuthTokens(access_token="new-token", token_type="Bearer", expires_in=3600)mock_exchange.return_value = tokens- result = auth(mock_provider, authorization_code="auth-code", state_param="state-key")+ result = auth(provider, authorization_code="auth-code", state_param="state-key")# auth() now returns AuthResult, not a dictassert isinstance(result, AuthResult)@@ -717,7 +710,7 @@ class TestAuthOrchestration:assert result.actions[0].tenant_id == "tenant-id"@patch("core.mcp.auth.auth_flow.discover_oauth_metadata")- def test_auth_exchange_code_without_state(self, mock_discover, mock_provider, mock_service):+ def test_auth_exchange_code_without_state(self, mock_discover, provider, mock_service):"""Test auth flow fails when exchanging code without state."""# Setup metadata discoverymock_discover.return_value = (@@ -731,19 +724,19 @@ class TestAuthOrchestration:None,)- mock_provider.retrieve_client_information.return_value = OAuthClientInformation(client_id="existing-client")+ provider.credentials["client_information"] = {"client_id": "existing-client"}with pytest.raises(ValueError) as exc_info:- auth(mock_provider, authorization_code="auth-code")+ auth(provider, authorization_code="auth-code")assert "State parameter is required" in str(exc_info.value)@patch("core.mcp.auth.auth_flow.refresh_authorization")- def test_auth_refresh_token(self, mock_refresh, mock_provider, mock_service):+ def test_auth_refresh_token(self, mock_refresh, provider, mock_service):"""Test auth flow for refreshing tokens."""# Setup existing client and tokens- mock_provider.retrieve_client_information.return_value = OAuthClientInformation(client_id="existing-client")- mock_provider.retrieve_tokens.return_value = OAuthTokens(+ provider.credentials["client_information"] = {"client_id": "existing-client"}+ provider.credentials.update(access_token="old-token",token_type="Bearer",expires_in=0,@@ -771,7 +764,7 @@ class TestAuthOrchestration:None,)- result = auth(mock_provider)+ result = auth(provider)# auth() now returns AuthResultassert isinstance(result, AuthResult)@@ -788,7 +781,7 @@ class TestAuthOrchestration:mock_refresh.assert_called_once()@patch("core.mcp.auth.auth_flow.discover_oauth_metadata")- def test_auth_registration_fails_with_code(self, mock_discover, mock_provider, mock_service):+ def test_auth_registration_fails_with_code(self, mock_discover, provider, mock_service):"""Test auth fails when no client info exists but code is provided."""# Setup metadata discoverymock_discover.return_value = (@@ -802,10 +795,10 @@ class TestAuthOrchestration:None,)- mock_provider.retrieve_client_information.return_value = None+ provider.credentials.pop("client_information", None)with pytest.raises(ValueError) as exc_info:- auth(mock_provider, authorization_code="auth-code")+ auth(provider, authorization_code="auth-code")assert "Existing OAuth client information is required" in str(exc_info.value)@@ -999,15 +992,17 @@ class TestAuthOrchestration:@patch("core.mcp.auth.auth_flow._retrieve_redis_state")@patch("core.mcp.auth.auth_flow.exchange_authorization")def test_handle_callback(self, mock_exchange, mock_retrieve):- state = Mock(spec=OAuthCallbackState)- state.server_url = "https://api"- state.metadata = None- state.client_information = Mock()- state.code_verifier = "cv"- state.redirect_uri = "https://re"+ state = OAuthCallbackState(+ provider_id="provider-id",+ tenant_id="tenant-id",+ server_url="https://api",+ client_information=OAuthClientInformation(client_id="c1"),+ code_verifier="cv",+ redirect_uri="https://re",+ )mock_retrieve.return_value = state- tokens = Mock(spec=OAuthTokens)+ tokens = OAuthTokens(access_token="at", token_type="Bearer")mock_exchange.return_value = tokenss, t = handle_callback("key", "code")@@ -1070,7 +1065,11 @@ class TestAuthOrchestration:mock_prm.return_value = ProtectedResourceMetadata(resource="https://api", authorization_servers=["https://auth"])- mock_asm.return_value = Mock(spec=OAuthMetadata)+ mock_asm.return_value = OAuthMetadata(+ authorization_endpoint="https://auth/authorize",+ token_endpoint="https://auth/token",+ response_types_supported=["code"],+ )asm, prm, hint = discover_oauth_metadata("https://api")assert asm == mock_asm.return_value@@ -1285,11 +1284,8 @@ class TestAuthOrchestration:register_client("https://api", None, client_metadata)@patch("core.mcp.auth.auth_flow.discover_oauth_metadata")- def test_auth_orchestration_failures(self, mock_discover):- provider = Mock(spec=MCPProviderEntity)- provider.decrypt_server_url.return_value = "https://api"- provider.id = "p1"- provider.tenant_id = "t1"+ def test_auth_orchestration_failures(self, mock_discover, provider):+ provider.server_url = "https://api"# Case 1: No server metadatamock_discover.return_value = (None, None, None)@@ -1303,7 +1299,7 @@ class TestAuthOrchestration:response_types_supported=["code"],)mock_discover.return_value = (asm, None, None)- provider.retrieve_client_information.return_value = None+ provider.credentials.pop("client_information", None)with pytest.raises(ValueError, match="Existing OAuth client information is required"):auth(provider, authorization_code="code")@@ -1320,13 +1316,9 @@ class TestAuthOrchestration:auth(provider)@patch("core.mcp.auth.auth_flow.discover_oauth_metadata")- def test_auth_orchestration_client_credentials(self, mock_discover):- provider = Mock(spec=MCPProviderEntity)- provider.decrypt_server_url.return_value = "https://api"- provider.id = "p1"- provider.tenant_id = "t1"- provider.retrieve_client_information.return_value = OAuthClientInformation(client_id="c1", client_secret="s1")- provider.decrypt_credentials.return_value = {"scope": "read"}+ def test_auth_orchestration_client_credentials(self, mock_discover, provider):+ provider.server_url = "https://api"+ provider.credentials = {"client_information": {"client_id": "c1", "client_secret": "s1"}, "scope": "read"}asm = OAuthMetadata(authorization_endpoint="https://auth/auth",@@ -1350,13 +1342,9 @@ class TestAuthOrchestration:auth(provider)@patch("core.mcp.auth.auth_flow.discover_oauth_metadata")- def test_auth_orchestration_authorization_code(self, mock_discover):- provider = Mock(spec=MCPProviderEntity)- provider.decrypt_server_url.return_value = "https://api"- provider.id = "p1"- provider.tenant_id = "t1"- provider.retrieve_client_information.return_value = OAuthClientInformation(client_id="c1")- provider.decrypt_credentials.return_value = {}+ def test_auth_orchestration_authorization_code(self, mock_discover, provider):+ provider.server_url = "https://api"+ provider.credentials = {"client_information": {"client_id": "c1"}}asm = OAuthMetadata(authorization_endpoint="https://auth/auth",@@ -1368,9 +1356,14 @@ class TestAuthOrchestration:# Case 1: Exchange codewith patch("core.mcp.auth.auth_flow._retrieve_redis_state") as mock_retrieve:- state = Mock(spec=OAuthCallbackState)- state.code_verifier = "cv"- state.redirect_uri = "https://re"+ state = OAuthCallbackState(+ provider_id=provider.id,+ tenant_id=provider.tenant_id,+ server_url=provider.server_url,+ client_information=OAuthClientInformation(client_id="c1"),+ code_verifier="cv",+ redirect_uri="https://re",+ )mock_retrieve.return_value = statewith patch("core.mcp.auth.auth_flow.exchange_authorization") as mock_exchange:@@ -1385,7 +1378,7 @@ class TestAuthOrchestration:auth(provider, authorization_code="code")# Missing verifier in state- state.code_verifier = None+ state.code_verifier = ""with pytest.raises(ValueError, match="Missing code_verifier"):auth(provider, authorization_code="code", state_param="sp")@@ -1395,14 +1388,10 @@ class TestAuthOrchestration:auth(provider, authorization_code="code", state_param="sp")@patch("core.mcp.auth.auth_flow.discover_oauth_metadata")- def test_auth_orchestration_refresh_failure(self, mock_discover):- provider = Mock(spec=MCPProviderEntity)- provider.decrypt_server_url.return_value = "https://api"- provider.id = "p1"- provider.tenant_id = "t1"- provider.retrieve_client_information.return_value = OAuthClientInformation(client_id="c1")- provider.decrypt_credentials.return_value = {}- provider.retrieve_tokens.return_value = OAuthTokens(access_token="at", token_type="Bearer", refresh_token="rt")+ def test_auth_orchestration_refresh_failure(self, mock_discover, provider):+ provider.server_url = "https://api"+ provider.credentials = {"client_information": {"client_id": "c1"}}+ provider.credentials.update(access_token="at", token_type="Bearer", refresh_token="rt")asm = OAuthMetadata(authorization_endpoint="https://auth/auth",diff --git a/api/tests/unit_tests/core/mcp/fixtures.py b/api/tests/unit_tests/core/mcp/fixtures.pynew file mode 100644index 00000000000..59cf2329adf--- /dev/null+++ b/api/tests/unit_tests/core/mcp/fixtures.py@@ -0,0 +1,25 @@+"""Real MCP provider fixtures for authentication tests."""++from datetime import datetime++from core.entities.mcp_provider import MCPProviderEntity+++def make_provider(credentials: dict[str, object]) -> MCPProviderEntity:+ return MCPProviderEntity(+ id="provider-id",+ tenant_id="tenant-id",+ user_id="user-id",+ server_identifier="test-server",+ name="Test server",+ server_url="https://api.example.com",+ headers={},+ timeout=30,+ sse_read_timeout=300,+ authed=False,+ credentials=credentials,+ tools=[],+ icon="",+ created_at=datetime(2024, 1, 1),+ updated_at=datetime(2024, 1, 1),+ )diff --git a/api/tests/unit_tests/core/mcp/session/test_base_session.py b/api/tests/unit_tests/core/mcp/session/test_base_session.pyindex 39efe07419a..29bd2208712 100644--- a/api/tests/unit_tests/core/mcp/session/test_base_session.py+++ b/api/tests/unit_tests/core/mcp/session/test_base_session.py@@ -556,17 +556,17 @@ def test_session_exit_timeout(streams):read_stream, write_stream = streamssession = MockSession(read_stream, write_stream, ReceiveRequest, ReceiveNotification)- mock_future = MagicMock(spec=Future)- mock_future.result.side_effect = TimeoutError()- mock_future.done.return_value = False-- session._receiver_future = mock_future- session._executor = MagicMock(spec=ThreadPoolExecutor)-- session.__exit__(None, None, None)-- mock_future.cancel.assert_called_once()- session._executor.shutdown.assert_called_once_with(wait=False)+ future = Future()+ session._receiver_future = future+ with ThreadPoolExecutor(max_workers=1) as executor:+ session._executor = executor+ with patch.object(executor, "shutdown", wraps=executor.shutdown) as shutdown:+ session.__exit__(None, None, None)++ assert future.cancelled()+ shutdown.assert_called_once_with(wait=False)+ with pytest.raises(RuntimeError, match="cannot schedule new futures after shutdown"):+ executor.submit(lambda: None)@pytest.mark.timeout(10)diff --git a/api/tests/unit_tests/core/mcp/session/test_client_session.py b/api/tests/unit_tests/core/mcp/session/test_client_session.pyindex c7b9d3cfa96..0dcccdcf539 100644--- a/api/tests/unit_tests/core/mcp/session/test_client_session.py+++ b/api/tests/unit_tests/core/mcp/session/test_client_session.py@@ -531,16 +531,26 @@ def test_received_notification_logging(streams):assert logging_cb.call_args[0][0].level == "info"-def test_default_message_handler():+def test_default_message_handler(streams):# Exception casewith pytest.raises(ValueError, match="test error"):_default_message_handler(Exception("test error"))# Notification case - should do nothing- _default_message_handler(MagicMock(spec=types.ServerNotification))+ _default_message_handler(types.ServerNotification(root=types.ToolListChangedNotification()))# RequestResponder case - should do nothing- _default_message_handler(MagicMock(spec=RequestResponder))+ read_stream, write_stream = streams+ session = ClientSession(read_stream, write_stream)+ with RequestResponder(+ request_id=1,+ request_meta=None,+ request=types.ServerRequest(root=types.PingRequest()),+ session=session,+ on_complete=lambda _responder: None,+ ) as responder:+ _default_message_handler(responder)+ assert not responder.completeddef test_default_sampling_callback():diff --git a/api/tests/unit_tests/core/mcp/test_mcp_client.py b/api/tests/unit_tests/core/mcp/test_mcp_client.pyindex 4aac002eec0..507ad07f20f 100644--- a/api/tests/unit_tests/core/mcp/test_mcp_client.py+++ b/api/tests/unit_tests/core/mcp/test_mcp_client.py@@ -14,7 +14,8 @@ from core.entities.mcp_provider import MCPProviderEntityfrom core.mcp.auth_client import MCPClientWithAuthRetryfrom core.mcp.error import MCPAuthError, MCPConnectionErrorfrom core.mcp.mcp_client import MCPClient-from core.mcp.types import CallToolResult, ListToolsResult, OAuthTokens, TextContent, Tool, ToolAnnotations+from core.mcp.types import CallToolResult, ListToolsResult, TextContent, Tool, ToolAnnotations+from tests.unit_tests.core.mcp.fixtures import make_providerclass TestMCPClient:@@ -428,23 +429,25 @@ class TestMCPClient:def test_cleanup(self):"""Test cleanup method."""client = MCPClient(server_url="http://test.example.com")- mock_exit_stack = Mock(spec=ExitStack)- client._exit_stack = mock_exit_stack+ cleaned = []+ client._exit_stack.callback(cleaned.append, "closed")client._session = Mock()client._initialized = Trueclient.cleanup()- mock_exit_stack.close.assert_called_once()+ assert cleaned == ["closed"]assert client._session is Noneassert client._initialized is Falsedef test_cleanup_with_error(self):"""Test cleanup method with error."""client = MCPClient(server_url="http://test.example.com")- mock_exit_stack = Mock(spec=ExitStack)- mock_exit_stack.close.side_effect = Exception("Cleanup error")- client._exit_stack = mock_exit_stack++ def fail_cleanup():+ raise RuntimeError("Cleanup error")++ client._exit_stack.callback(fail_cleanup)client._session = Mock()client._initialized = True@@ -530,43 +533,41 @@ class TestMCPClientWithAuthRetry:"""Test suite for MCPClientWithAuthRetry."""@pytest.fixture- def mock_provider(self):- provider = MagicMock(spec=MCPProviderEntity)- provider.id = "test-provider-id"- provider.server_identifier = "test-server-identifier"- provider.tenant_id = "test-tenant-id"- provider.retrieve_tokens.return_value = OAuthTokens(- access_token="new-token",- token_type="Bearer",- expires_in=3600,- refresh_token="refresh-token",+ def provider(self, monkeypatch: pytest.MonkeyPatch):+ monkeypatch.setattr(MCPProviderEntity, "_decrypt_dict", lambda _self, data: data.copy())+ return make_provider(+ credentials={+ "access_token": "new-token",+ "token_type": "Bearer",+ "expires_in": 3600,+ "refresh_token": "refresh-token",+ })- return provider@pytest.fixture- def auth_client(self, mock_provider):+ def auth_client(self, provider):client = MCPClientWithAuthRetry(server_url="http://test.example.com",headers={"Authorization": "Bearer old-token"},- provider_entity=mock_provider,+ provider_entity=provider,authorization_code="test-code",)return client- def test_init(self, mock_provider):+ def test_init(self, provider):"""Test initialization."""client = MCPClientWithAuthRetry(server_url="http://test.example.com",headers={"Authorization": "Bearer test"},timeout=30.0,- provider_entity=mock_provider,+ provider_entity=provider,authorization_code="initial-code",)assert client.server_url == "http://test.example.com"assert client.headers == {"Authorization": "Bearer test"}assert client.timeout == 30.0- assert client.provider_entity == mock_provider+ assert client.provider_entity == providerassert client.authorization_code == "initial-code"assert client._has_retried is False@@ -575,19 +576,20 @@ class TestMCPClientWithAuthRetry:self,mock_service_class,auth_client,- mock_provider,+ provider,monkeypatch: pytest.MonkeyPatch,sqlite_engine: Engine,):monkeypatch.setattr("core.mcp.auth_client.db", SimpleNamespace(engine=sqlite_engine))mock_service = mock_service_class.return_value- new_provider = MagicMock(spec=MCPProviderEntity)- new_provider.retrieve_tokens.return_value = OAuthTokens(- access_token="new-access-token",- token_type="Bearer",- expires_in=3600,- refresh_token="new-refresh-token",+ new_provider = make_provider(+ credentials={+ "access_token": "new-access-token",+ "token_type": "Bearer",+ "expires_in": 3600,+ "refresh_token": "new-refresh-token",+ })mock_service.get_provider_entity_by_server_identifier.return_value = new_provider@@ -603,13 +605,13 @@ class TestMCPClientWithAuthRetry:# Verify service calls - error.resource_metadata_url and error.scope_hint are parsed from headermock_service.auth_with_actions.assert_called_once_with(- mock_provider,+ provider,"test-code",resource_metadata_url="http://meta",scope_hint="read",)mock_service.get_provider_entity_by_server_identifier.assert_called_once_with(- server_identifier=mock_provider.server_identifier, tenant_id=mock_provider.tenant_id+ server_identifier=provider.server_identifier, tenant_id=provider.tenant_id)# Verify client updates@@ -643,7 +645,7 @@ class TestMCPClientWithAuthRetry:self,mock_service_class,auth_client,- mock_provider,+ provider,monkeypatch: pytest.MonkeyPatch,sqlite_engine: Engine,):@@ -651,8 +653,7 @@ class TestMCPClientWithAuthRetry:monkeypatch.setattr("core.mcp.auth_client.db", SimpleNamespace(engine=sqlite_engine))mock_service = mock_service_class.return_value- new_provider = MagicMock(spec=MCPProviderEntity)- new_provider.retrieve_tokens.return_value = None+ new_provider = make_provider(credentials={})mock_service.get_provider_entity_by_server_identifier.return_value = new_providererror = MCPAuthError("Auth failed")