chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,35 @@
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from services.tools.api_tools_manage_service import ApiToolManageService
|
||||
|
||||
|
||||
def test_get_api_tool_provider_remote_schema_uses_ssrf_proxy_get(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
schema = """
|
||||
{
|
||||
"openapi": "3.0.0",
|
||||
"info": {"title": "Demo API", "version": "1.0.0"},
|
||||
"servers": [{"url": "https://api.example.com"}],
|
||||
"paths": {}
|
||||
}
|
||||
"""
|
||||
url = "https://example.com/openapi.json"
|
||||
expected_headers = {
|
||||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko)"
|
||||
" Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0",
|
||||
"Accept": "*/*",
|
||||
}
|
||||
mock_get = Mock(return_value=Mock(status_code=200, text=schema))
|
||||
mock_parser = Mock()
|
||||
|
||||
monkeypatch.setattr("services.tools.api_tools_manage_service.ssrf_proxy.get", mock_get)
|
||||
monkeypatch.setattr(ApiToolManageService, "parser_api_schema", mock_parser)
|
||||
|
||||
result = ApiToolManageService.get_api_tool_provider_remote_schema("user-1", "tenant-1", url)
|
||||
|
||||
assert result == {"schema": schema}
|
||||
mock_get.assert_called_once_with(url, headers=expected_headers, timeout=10)
|
||||
mock_parser.assert_called_once_with(schema)
|
||||
+764
@@ -0,0 +1,764 @@
|
||||
"""Unit tests for built-in tool management and its persisted credential state."""
|
||||
|
||||
import json
|
||||
from collections.abc import Callable
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from sqlalchemy import event, select
|
||||
from sqlalchemy.engine import Connection
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from core.entities.provider_entities import ProviderConfig, ProviderConfigType
|
||||
from core.plugin.entities.oauth import OAuthSchema
|
||||
from core.plugin.entities.plugin_daemon import CredentialType
|
||||
from core.plugin.impl import base as client_module
|
||||
from core.plugin.impl.oauth import OAuthHandler
|
||||
from core.tools.entities.common_entities import I18nObject
|
||||
from core.tools.entities.tool_entities import ToolProviderEntityWithPlugin, ToolProviderIdentity
|
||||
from core.tools.plugin_tool.provider import PluginToolProviderController
|
||||
from models.tools import BuiltinToolProvider, ToolOAuthSystemClient, ToolOAuthTenantClient
|
||||
from repositories.credentials.query_repository import CredentialQueryRepository
|
||||
from services.tools import builtin_tools_manage_service as service_module
|
||||
from services.tools.builtin_tools_manage_service import BuiltinToolManageService
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def repository_session(sqlite_session: Session, monkeypatch: pytest.MonkeyPatch) -> Session:
|
||||
"""Bind service-owned sessions to the shared SQLite session's engine."""
|
||||
monkeypatch.setattr(service_module, "db", SimpleNamespace(engine=sqlite_session.get_bind()))
|
||||
return sqlite_session
|
||||
|
||||
|
||||
def _persist_provider(
|
||||
session: Session,
|
||||
*,
|
||||
credential_id: str = "cred-1",
|
||||
tenant_id: str = "tenant-1",
|
||||
user_id: str = "user-1",
|
||||
provider: str = "google",
|
||||
name: str = "Google 1",
|
||||
credentials: dict[str, str] | None = None,
|
||||
is_default: bool = False,
|
||||
) -> BuiltinToolProvider:
|
||||
db_provider = BuiltinToolProvider(
|
||||
tenant_id=tenant_id,
|
||||
user_id=user_id,
|
||||
provider=provider,
|
||||
name=name,
|
||||
encrypted_credentials=json.dumps(credentials or {"key": "encrypted"}),
|
||||
credential_type=CredentialType.API_KEY,
|
||||
is_default=is_default,
|
||||
)
|
||||
db_provider.id = credential_id
|
||||
session.add(db_provider)
|
||||
session.commit()
|
||||
return db_provider
|
||||
|
||||
|
||||
def _persist_tenant_oauth_client(
|
||||
session: Session,
|
||||
*,
|
||||
tenant_id: str = "tenant-1",
|
||||
plugin_id: str = "langgenius/google",
|
||||
provider: str = "google",
|
||||
enabled: bool = True,
|
||||
encrypted_params: str = '{"encrypted": "data"}',
|
||||
) -> ToolOAuthTenantClient:
|
||||
client = ToolOAuthTenantClient(tenant_id=tenant_id, plugin_id=plugin_id, provider=provider)
|
||||
client.enabled = enabled
|
||||
client.encrypted_oauth_params = encrypted_params
|
||||
session.add(client)
|
||||
session.commit()
|
||||
return client
|
||||
|
||||
|
||||
def _persist_system_oauth_client(
|
||||
session: Session,
|
||||
*,
|
||||
plugin_id: str = "langgenius/google",
|
||||
provider: str = "google",
|
||||
) -> ToolOAuthSystemClient:
|
||||
client = ToolOAuthSystemClient(plugin_id=plugin_id, provider=provider, encrypted_oauth_params="enc")
|
||||
session.add(client)
|
||||
session.commit()
|
||||
return client
|
||||
|
||||
|
||||
class TestDeleteCustomOauthClientParams:
|
||||
def test_deletes_matching_tenant_only(self, repository_session: Session) -> None:
|
||||
_persist_tenant_oauth_client(repository_session, tenant_id="tenant-1")
|
||||
_persist_tenant_oauth_client(repository_session, tenant_id="tenant-2")
|
||||
|
||||
result = BuiltinToolManageService.delete_custom_oauth_client_params("tenant-1", "google")
|
||||
|
||||
assert result == {"result": "success"}
|
||||
repository_session.expire_all()
|
||||
clients = repository_session.scalars(select(ToolOAuthTenantClient)).all()
|
||||
assert [client.tenant_id for client in clients] == ["tenant-2"]
|
||||
|
||||
|
||||
class TestListBuiltinToolProviderTools:
|
||||
def test_transforms_each_tool(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
controller = MagicMock()
|
||||
controller.get_tools.return_value = [MagicMock(), MagicMock()]
|
||||
monkeypatch.setattr(service_module.ToolManager, "get_builtin_provider", MagicMock(return_value=controller))
|
||||
convert = MagicMock(return_value=MagicMock())
|
||||
monkeypatch.setattr(service_module.ToolTransformService, "convert_tool_entity_to_api_entity", convert)
|
||||
monkeypatch.setattr(service_module.ToolLabelManager, "get_tool_labels", MagicMock(return_value=[]))
|
||||
|
||||
result = BuiltinToolManageService.list_builtin_tool_provider_tools("tenant-1", "google")
|
||||
|
||||
assert len(result) == 2
|
||||
assert convert.call_count == 2
|
||||
|
||||
def test_empty_tools(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
controller = MagicMock()
|
||||
controller.get_tools.return_value = list[object]()
|
||||
monkeypatch.setattr(service_module.ToolManager, "get_builtin_provider", MagicMock(return_value=controller))
|
||||
|
||||
assert BuiltinToolManageService.list_builtin_tool_provider_tools("t", "p") == []
|
||||
|
||||
|
||||
class TestGetBuiltinToolProviderInfo:
|
||||
def test_raises_when_not_found(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(BuiltinToolManageService, "get_builtin_provider", MagicMock(return_value=None))
|
||||
monkeypatch.setattr(service_module.ToolManager, "get_builtin_provider", MagicMock(return_value=MagicMock()))
|
||||
|
||||
with pytest.raises(ValueError, match="you have not added provider"):
|
||||
BuiltinToolManageService.get_builtin_tool_provider_info("t", "no")
|
||||
|
||||
def test_clears_original_credentials(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(BuiltinToolManageService, "get_builtin_provider", MagicMock(return_value=MagicMock()))
|
||||
monkeypatch.setattr(service_module.ToolManager, "get_builtin_provider", MagicMock(return_value=MagicMock()))
|
||||
entity = MagicMock()
|
||||
monkeypatch.setattr(
|
||||
service_module.ToolTransformService,
|
||||
"builtin_provider_to_user_provider",
|
||||
MagicMock(return_value=entity),
|
||||
)
|
||||
|
||||
result = BuiltinToolManageService.get_builtin_tool_provider_info("t", "google")
|
||||
|
||||
assert result.original_credentials == {}
|
||||
|
||||
|
||||
class TestListBuiltinProviderCredentialsSchema:
|
||||
def test_returns_schema(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
controller = MagicMock()
|
||||
controller.get_credentials_schema_by_type.return_value = [{"f": "k"}]
|
||||
monkeypatch.setattr(service_module.ToolManager, "get_builtin_provider", MagicMock(return_value=controller))
|
||||
|
||||
result = BuiltinToolManageService.list_builtin_provider_credentials_schema("g", CredentialType.API_KEY, "t")
|
||||
|
||||
assert result == [{"f": "k"}]
|
||||
|
||||
|
||||
class TestGetBuiltinToolProviderIcon:
|
||||
def test_returns_bytes_and_mime(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(
|
||||
service_module.ToolManager,
|
||||
"get_hardcoded_provider_icon",
|
||||
MagicMock(return_value=("/icon.svg", "image/svg+xml")),
|
||||
)
|
||||
path = MagicMock()
|
||||
path.return_value.read_bytes.return_value = b"<svg/>"
|
||||
monkeypatch.setattr(service_module, "Path", path)
|
||||
|
||||
icon, mime = BuiltinToolManageService.get_builtin_tool_provider_icon("google")
|
||||
|
||||
assert icon == b"<svg/>"
|
||||
assert mime == "image/svg+xml"
|
||||
|
||||
|
||||
class TestIsOauthSystemClientExists:
|
||||
def test_true_when_exists(self, repository_session: Session) -> None:
|
||||
_persist_system_oauth_client(repository_session)
|
||||
|
||||
assert BuiltinToolManageService.is_oauth_system_client_exists("google") is True
|
||||
|
||||
def test_false_when_missing(self, repository_session: Session) -> None:
|
||||
_persist_system_oauth_client(repository_session, plugin_id="langgenius/slack", provider="slack")
|
||||
|
||||
assert BuiltinToolManageService.is_oauth_system_client_exists("google") is False
|
||||
|
||||
|
||||
class TestIsOauthCustomClientEnabled:
|
||||
def test_true_when_enabled(self, repository_session: Session) -> None:
|
||||
_persist_tenant_oauth_client(repository_session)
|
||||
|
||||
assert BuiltinToolManageService.is_oauth_custom_client_enabled("tenant-1", "google") is True
|
||||
|
||||
def test_false_when_disabled_or_other_tenant(self, repository_session: Session) -> None:
|
||||
_persist_tenant_oauth_client(repository_session, tenant_id="tenant-1", enabled=False)
|
||||
_persist_tenant_oauth_client(repository_session, tenant_id="tenant-2", enabled=True)
|
||||
|
||||
assert BuiltinToolManageService.is_oauth_custom_client_enabled("tenant-1", "google") is False
|
||||
|
||||
|
||||
class _RecordingLock:
|
||||
"""Redis lock stand-in that records when it is held and runs a hook just before release."""
|
||||
|
||||
def __init__(self, events: list[str], on_release: Callable[[], None] | None = None) -> None:
|
||||
self._events = events
|
||||
self._on_release = on_release
|
||||
|
||||
def __enter__(self) -> "_RecordingLock":
|
||||
self._events.append("acquire")
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc_info: object) -> None:
|
||||
if self._on_release is not None:
|
||||
self._on_release()
|
||||
self._events.append("release")
|
||||
|
||||
|
||||
class TestAddBuiltinToolProvider:
|
||||
@pytest.fixture
|
||||
def events(self) -> list[str]:
|
||||
return []
|
||||
|
||||
@pytest.fixture
|
||||
def controller(self, monkeypatch: pytest.MonkeyPatch, events: list[str]) -> PluginToolProviderController:
|
||||
controller = PluginToolProviderController(
|
||||
entity=ToolProviderEntityWithPlugin(
|
||||
identity=ToolProviderIdentity(
|
||||
author="author",
|
||||
name="google",
|
||||
description=I18nObject(en_US="Google"),
|
||||
icon="icon.svg",
|
||||
label=I18nObject(en_US="Google"),
|
||||
),
|
||||
credentials_schema=[ProviderConfig(name="key", type=ProviderConfigType.TEXT_INPUT)],
|
||||
plugin_id="langgenius/google",
|
||||
tools=[],
|
||||
),
|
||||
plugin_id="langgenius/google",
|
||||
plugin_unique_identifier="langgenius/google:0.0.1",
|
||||
tenant_id="tenant-1",
|
||||
)
|
||||
|
||||
def validate_credentials(user_id: str, credentials: dict[str, str]) -> None:
|
||||
assert user_id == "u"
|
||||
assert credentials == {"key": "value"}
|
||||
events.append("validate")
|
||||
|
||||
monkeypatch.setattr(controller, "_validate_credentials", validate_credentials)
|
||||
monkeypatch.setattr(service_module.ToolManager, "get_builtin_provider", lambda *_: controller)
|
||||
return controller
|
||||
|
||||
def _install_lock(
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
events: list[str],
|
||||
on_release: Callable[[], None] | None = None,
|
||||
) -> None:
|
||||
lock = _RecordingLock(events, on_release)
|
||||
monkeypatch.setattr(service_module.redis_client, "lock", lambda *_, **__: lock)
|
||||
|
||||
def _count_providers(self, repository_session: Session) -> int:
|
||||
with Session(repository_session.get_bind()) as session:
|
||||
return len(session.scalars(select(BuiltinToolProvider)).all())
|
||||
|
||||
def test_validates_credentials_before_acquiring_lock(
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
repository_session: Session,
|
||||
controller: PluginToolProviderController,
|
||||
events: list[str],
|
||||
) -> None:
|
||||
self._install_lock(monkeypatch, events)
|
||||
|
||||
result = BuiltinToolManageService.add_builtin_tool_provider(
|
||||
"u", CredentialType.API_KEY, "tenant-1", "google", {"key": "value"}, name="Google 1"
|
||||
)
|
||||
|
||||
assert result["result"] == "success"
|
||||
assert events == ["validate", "acquire", "release"]
|
||||
provider = repository_session.get(BuiltinToolProvider, result["id"])
|
||||
assert provider is not None
|
||||
assert provider.credentials == {"key": "value"}
|
||||
|
||||
def test_commits_credential_before_releasing_lock(
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
repository_session: Session,
|
||||
controller: PluginToolProviderController,
|
||||
events: list[str],
|
||||
) -> None:
|
||||
counts_at_release: list[int] = []
|
||||
self._install_lock(
|
||||
monkeypatch, events, on_release=lambda: counts_at_release.append(self._count_providers(repository_session))
|
||||
)
|
||||
|
||||
BuiltinToolManageService.add_builtin_tool_provider(
|
||||
"u", CredentialType.API_KEY, "tenant-1", "google", {"key": "value"}, name="Google 1"
|
||||
)
|
||||
|
||||
assert counts_at_release == [1]
|
||||
|
||||
def test_does_not_lock_or_save_when_validation_fails(
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
repository_session: Session,
|
||||
controller: PluginToolProviderController,
|
||||
events: list[str],
|
||||
) -> None:
|
||||
self._install_lock(monkeypatch, events)
|
||||
|
||||
def reject_credentials(user_id: str, credentials: dict[str, str]) -> None:
|
||||
raise ValueError("invalid api key")
|
||||
|
||||
monkeypatch.setattr(controller, "_validate_credentials", reject_credentials)
|
||||
|
||||
with pytest.raises(ValueError, match="invalid api key"):
|
||||
BuiltinToolManageService.add_builtin_tool_provider(
|
||||
"u", CredentialType.API_KEY, "tenant-1", "google", {"key": "bad"}
|
||||
)
|
||||
|
||||
assert events == []
|
||||
assert self._count_providers(repository_session) == 0
|
||||
|
||||
def test_checks_count_limit_inside_lock(
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
repository_session: Session,
|
||||
controller: PluginToolProviderController,
|
||||
events: list[str],
|
||||
) -> None:
|
||||
_persist_provider(repository_session, credential_id="cred-1")
|
||||
monkeypatch.setattr(BuiltinToolManageService, "__MAX_BUILTIN_TOOL_PROVIDER_COUNT__", 1)
|
||||
self._install_lock(monkeypatch, events)
|
||||
|
||||
with pytest.raises(ValueError, match="maximum number of providers"):
|
||||
BuiltinToolManageService.add_builtin_tool_provider(
|
||||
"u", CredentialType.API_KEY, "tenant-1", "google", {"key": "value"}
|
||||
)
|
||||
|
||||
assert events == ["validate", "acquire", "release"]
|
||||
assert self._count_providers(repository_session) == 1
|
||||
|
||||
def test_rejects_duplicate_name_inside_lock(
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
repository_session: Session,
|
||||
controller: PluginToolProviderController,
|
||||
events: list[str],
|
||||
) -> None:
|
||||
_persist_provider(repository_session, credential_id="cred-1", name="Google 1")
|
||||
self._install_lock(monkeypatch, events)
|
||||
|
||||
with pytest.raises(ValueError, match="already used"):
|
||||
BuiltinToolManageService.add_builtin_tool_provider(
|
||||
"u", CredentialType.API_KEY, "tenant-1", "google", {"key": "value"}, name="Google 1"
|
||||
)
|
||||
|
||||
assert events == ["validate", "acquire", "release"]
|
||||
assert self._count_providers(repository_session) == 1
|
||||
|
||||
|
||||
class TestDeleteBuiltinToolProvider:
|
||||
def test_raises_when_not_found(self, repository_session: Session) -> None:
|
||||
with pytest.raises(ValueError, match="you have not added provider"):
|
||||
BuiltinToolManageService.delete_builtin_tool_provider("tenant-1", "google", "missing")
|
||||
|
||||
def test_deletes_provider_and_clears_cache(
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
repository_session: Session,
|
||||
) -> None:
|
||||
_persist_provider(repository_session, credential_id="cred-1")
|
||||
_persist_provider(repository_session, credential_id="cred-other", tenant_id="tenant-2")
|
||||
cache = MagicMock()
|
||||
monkeypatch.setattr(service_module.ToolManager, "get_builtin_provider", MagicMock(return_value=MagicMock()))
|
||||
monkeypatch.setattr(
|
||||
BuiltinToolManageService,
|
||||
"create_tool_encrypter",
|
||||
MagicMock(return_value=(MagicMock(), cache)),
|
||||
)
|
||||
|
||||
result = BuiltinToolManageService.delete_builtin_tool_provider("tenant-1", "google", "cred-1")
|
||||
|
||||
assert result == {"result": "success"}
|
||||
cache.delete.assert_called_once()
|
||||
repository_session.expire_all()
|
||||
assert repository_session.get(BuiltinToolProvider, "cred-1") is None
|
||||
assert repository_session.get(BuiltinToolProvider, "cred-other") is not None
|
||||
|
||||
|
||||
class TestSetDefaultProvider:
|
||||
def test_raises_when_not_found(self, repository_session: Session) -> None:
|
||||
with pytest.raises(ValueError, match="provider not found"):
|
||||
BuiltinToolManageService.set_default_provider("tenant-1", "google", "missing")
|
||||
|
||||
def test_sets_target_and_clears_only_same_tenant_defaults(self, repository_session: Session) -> None:
|
||||
_persist_provider(
|
||||
repository_session,
|
||||
credential_id="target",
|
||||
user_id="user-2",
|
||||
name="Google target",
|
||||
)
|
||||
_persist_provider(repository_session, credential_id="old", name="Google old", is_default=True)
|
||||
_persist_provider(
|
||||
repository_session,
|
||||
credential_id="other-tenant",
|
||||
tenant_id="tenant-2",
|
||||
name="Other tenant",
|
||||
is_default=True,
|
||||
)
|
||||
|
||||
result = BuiltinToolManageService.set_default_provider("tenant-1", "google", "target")
|
||||
|
||||
assert result == {"result": "success"}
|
||||
repository_session.expire_all()
|
||||
target = repository_session.get(BuiltinToolProvider, "target")
|
||||
old = repository_session.get(BuiltinToolProvider, "old")
|
||||
other_tenant = repository_session.get(BuiltinToolProvider, "other-tenant")
|
||||
assert target is not None
|
||||
assert target.is_default is True
|
||||
assert old is not None
|
||||
assert old.is_default is False
|
||||
assert other_tenant is not None
|
||||
assert other_tenant.is_default is True
|
||||
|
||||
|
||||
class TestUpdateBuiltinToolProvider:
|
||||
def test_raises_when_provider_not_exists(self, repository_session: Session) -> None:
|
||||
with pytest.raises(ValueError, match="you have not added provider"):
|
||||
BuiltinToolManageService.update_builtin_tool_provider("u", "tenant-1", "google", "missing")
|
||||
|
||||
def test_updates_persisted_credentials_and_clears_cache(
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
repository_session: Session,
|
||||
) -> None:
|
||||
_persist_provider(repository_session, credentials={"key": "old"})
|
||||
controller = MagicMock(need_credentials=True)
|
||||
monkeypatch.setattr(service_module.ToolManager, "get_builtin_provider", MagicMock(return_value=controller))
|
||||
encrypter = MagicMock()
|
||||
encrypter.decrypt.return_value = {"key": "old"}
|
||||
encrypter.encrypt.return_value = {"key": "new"}
|
||||
cache = MagicMock()
|
||||
monkeypatch.setattr(
|
||||
BuiltinToolManageService,
|
||||
"create_tool_encrypter",
|
||||
MagicMock(return_value=(encrypter, cache)),
|
||||
)
|
||||
|
||||
events: list[str] = []
|
||||
|
||||
def committed(_connection: Connection) -> None:
|
||||
events.append("commit")
|
||||
|
||||
engine = repository_session.get_bind()
|
||||
event.listen(engine, "commit", committed)
|
||||
cache.delete.side_effect = lambda: events.append("invalidate")
|
||||
try:
|
||||
result = BuiltinToolManageService.update_builtin_tool_provider(
|
||||
"u", "tenant-1", "google", "cred-1", credentials={"key": "value"}
|
||||
)
|
||||
finally:
|
||||
event.remove(engine, "commit", committed)
|
||||
|
||||
assert result == {"result": "success"}
|
||||
assert events == ["commit", "invalidate"]
|
||||
controller.validate_credentials.assert_called_once_with("u", {"key": "value"})
|
||||
cache.delete.assert_called_once()
|
||||
repository_session.expire_all()
|
||||
provider = repository_session.get(BuiltinToolProvider, "cred-1")
|
||||
assert provider is not None
|
||||
assert provider.credentials == {"key": "new"}
|
||||
|
||||
|
||||
class TestGetOauthClientSchema:
|
||||
def test_returns_schema_dict(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
controller = MagicMock()
|
||||
controller.get_oauth_client_schema.return_value = list[object]()
|
||||
monkeypatch.setattr(service_module.ToolManager, "get_builtin_provider", MagicMock(return_value=controller))
|
||||
monkeypatch.setattr(BuiltinToolManageService, "is_oauth_custom_client_enabled", MagicMock(return_value=True))
|
||||
monkeypatch.setattr(BuiltinToolManageService, "is_oauth_system_client_exists", MagicMock(return_value=False))
|
||||
monkeypatch.setattr(BuiltinToolManageService, "get_custom_oauth_client_params", MagicMock(return_value={}))
|
||||
from tests.helpers.unit.config_override import apply_config_overrides
|
||||
|
||||
apply_config_overrides(monkeypatch, CONSOLE_API_URL="https://api.example.com")
|
||||
|
||||
result = BuiltinToolManageService.get_builtin_tool_provider_oauth_client_schema("t", "google")
|
||||
|
||||
assert "schema" in result
|
||||
assert result["is_oauth_custom_client_enabled"] is True
|
||||
assert "redirect_uri" in result
|
||||
|
||||
|
||||
class TestGetOauthClient:
|
||||
def test_system_client_binds_verified_package_and_reads_rotated_credentials(
|
||||
self, monkeypatch: pytest.MonkeyPatch, repository_session: Session
|
||||
) -> None:
|
||||
identifier = f"langgenius/google:1.0.0@{'a' * 32}"
|
||||
controller = service_module.PluginToolProviderController(
|
||||
entity=ToolProviderEntityWithPlugin(
|
||||
identity=ToolProviderIdentity(
|
||||
author="test",
|
||||
name="google",
|
||||
label=I18nObject(en_US="Google"),
|
||||
description=I18nObject(en_US="Google"),
|
||||
icon="icon.svg",
|
||||
),
|
||||
plugin_id="langgenius/google",
|
||||
credentials_schema=[],
|
||||
tools=[],
|
||||
oauth_schema=OAuthSchema(),
|
||||
),
|
||||
tenant_id="tenant-1",
|
||||
plugin_id="langgenius/google",
|
||||
plugin_unique_identifier=identifier,
|
||||
)
|
||||
monkeypatch.setattr(service_module.ToolManager, "get_plugin_provider", lambda *_args: controller)
|
||||
monkeypatch.setattr(
|
||||
service_module.PluginService, "is_plugin_verified", lambda _tenant, value: value == identifier
|
||||
)
|
||||
monkeypatch.setattr(service_module, "decrypt_system_params", json.loads)
|
||||
row = _persist_system_oauth_client(repository_session)
|
||||
row.encrypted_oauth_params = json.dumps({"client_secret": "first-test-secret"})
|
||||
repository_session.commit()
|
||||
first = BuiltinToolManageService.get_oauth_client("tenant-1", "google")
|
||||
assert first is not None
|
||||
assert first.plugin_unique_identifier == identifier
|
||||
|
||||
row.encrypted_oauth_params = json.dumps({"client_secret": "rotated-test-secret"})
|
||||
repository_session.commit()
|
||||
second = BuiltinToolManageService.get_oauth_client("tenant-1", "google")
|
||||
assert second is not None
|
||||
assert second.credentials == {"client_secret": "rotated-test-secret"}
|
||||
|
||||
replacement_identifier = f"langgenius/google:2.0.0@{'b' * 32}"
|
||||
|
||||
def replace_during_verification(_tenant: str, value: str) -> bool:
|
||||
controller.plugin_unique_identifier = replacement_identifier
|
||||
return value == identifier
|
||||
|
||||
monkeypatch.setattr(service_module.PluginService, "is_plugin_verified", replace_during_verification)
|
||||
third = BuiltinToolManageService.get_oauth_client("tenant-1", "google")
|
||||
assert third is not None
|
||||
assert third.plugin_unique_identifier == identifier
|
||||
assert controller.plugin_unique_identifier == replacement_identifier
|
||||
assert BuiltinToolManageService.get_oauth_client("tenant-1", "google") is None
|
||||
requests: list[httpx.Request] = []
|
||||
|
||||
def handle(request: httpx.Request) -> httpx.Response:
|
||||
requests.append(request)
|
||||
return httpx.Response(
|
||||
200, text='data: {"code":0,"message":"","data":{"authorization_url":"https://example.com"}}'
|
||||
)
|
||||
|
||||
with httpx.Client(transport=httpx.MockTransport(handle)) as client:
|
||||
monkeypatch.setattr(client_module, "_httpx_client", client)
|
||||
for config in (first, second, third):
|
||||
OAuthHandler().get_authorization_url(
|
||||
"tenant-1",
|
||||
"user",
|
||||
"langgenius/google",
|
||||
"google",
|
||||
"https://example.com/callback",
|
||||
config.credentials,
|
||||
plugin_unique_identifier=config.plugin_unique_identifier,
|
||||
)
|
||||
|
||||
assert [request.headers["X-Plugin-Unique-Identifier"] for request in requests] == [
|
||||
identifier,
|
||||
identifier,
|
||||
identifier,
|
||||
]
|
||||
assert [json.loads(request.content)["data"]["system_credentials"] for request in requests] == [
|
||||
{"client_secret": "first-test-secret"},
|
||||
{"client_secret": "rotated-test-secret"},
|
||||
{"client_secret": "rotated-test-secret"},
|
||||
]
|
||||
|
||||
def test_returns_tenant_client_params_when_exists(
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
repository_session: Session,
|
||||
) -> None:
|
||||
_persist_tenant_oauth_client(repository_session)
|
||||
controller = MagicMock()
|
||||
controller.plugin_unique_identifier = "langgenius/google:1.0.0@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
controller.get_oauth_client_schema.return_value = list[object]()
|
||||
monkeypatch.setattr(service_module.ToolManager, "get_plugin_provider", MagicMock(return_value=controller))
|
||||
encrypter = MagicMock()
|
||||
encrypter.decrypt.return_value = {"client_id": "id", "client_secret": "secret"}
|
||||
monkeypatch.setattr(
|
||||
service_module, "create_provider_encrypter", MagicMock(return_value=(encrypter, MagicMock()))
|
||||
)
|
||||
|
||||
result = BuiltinToolManageService.get_oauth_client("tenant-1", "google")
|
||||
|
||||
assert result is not None
|
||||
assert result.credentials == {"client_id": "id", "client_secret": "secret"}
|
||||
encrypter.decrypt.assert_called_once_with({"encrypted": "data"})
|
||||
|
||||
def test_falls_back_to_system_client(
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
repository_session: Session,
|
||||
) -> None:
|
||||
_persist_system_oauth_client(repository_session)
|
||||
controller = MagicMock()
|
||||
controller.plugin_unique_identifier = "langgenius/google:1.0.0@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
controller.get_oauth_client_schema.return_value = list[object]()
|
||||
monkeypatch.setattr(service_module.ToolManager, "get_plugin_provider", MagicMock(return_value=controller))
|
||||
monkeypatch.setattr(
|
||||
service_module, "create_provider_encrypter", MagicMock(return_value=(MagicMock(), MagicMock()))
|
||||
)
|
||||
monkeypatch.setattr(service_module.PluginService, "is_plugin_verified", lambda *_args: True)
|
||||
decrypt = MagicMock(return_value={"sys_key": "sys_val"})
|
||||
monkeypatch.setattr(service_module, "decrypt_system_params", decrypt)
|
||||
|
||||
result = BuiltinToolManageService.get_oauth_client("tenant-1", "google")
|
||||
|
||||
assert result is not None
|
||||
assert result.credentials == {"sys_key": "sys_val"}
|
||||
decrypt.assert_called_once_with("enc")
|
||||
|
||||
|
||||
class TestSaveCustomOauthClientParams:
|
||||
def test_returns_early_when_no_params(self) -> None:
|
||||
result = BuiltinToolManageService.save_custom_oauth_client_params("t", "p")
|
||||
assert result == {"result": "success"}
|
||||
|
||||
def test_raises_when_provider_not_found(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(service_module.ToolManager, "get_builtin_provider", MagicMock(return_value=None))
|
||||
|
||||
with pytest.raises((ValueError, Exception), match="not found|Provider"):
|
||||
BuiltinToolManageService.save_custom_oauth_client_params("t", "p", enable_oauth_custom_client=True)
|
||||
|
||||
|
||||
class TestGetCustomOauthClientParams:
|
||||
def test_returns_empty_when_none(self, repository_session: Session) -> None:
|
||||
_persist_tenant_oauth_client(repository_session, tenant_id="other-tenant")
|
||||
|
||||
result = BuiltinToolManageService.get_custom_oauth_client_params("tenant-1", "google")
|
||||
|
||||
assert result == {}
|
||||
|
||||
|
||||
class TestGetBuiltinToolProviderCredentialInfo:
|
||||
def test_returns_credential_info(
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
repository_session: Session,
|
||||
) -> None:
|
||||
controller = MagicMock()
|
||||
controller.get_supported_credential_types.return_value = ["api-key"]
|
||||
monkeypatch.setattr(service_module.ToolManager, "get_builtin_provider", MagicMock(return_value=controller))
|
||||
monkeypatch.setattr(
|
||||
BuiltinToolManageService,
|
||||
"get_builtin_tool_provider_credentials",
|
||||
MagicMock(return_value=[]),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
BuiltinToolManageService,
|
||||
"is_oauth_custom_client_enabled",
|
||||
MagicMock(return_value=False),
|
||||
)
|
||||
|
||||
result = BuiltinToolManageService.get_builtin_tool_provider_credential_info(
|
||||
"tenant-1",
|
||||
"google",
|
||||
credential_query=CredentialQueryRepository(
|
||||
session_factory=sessionmaker(bind=repository_session.get_bind(), expire_on_commit=False)
|
||||
),
|
||||
)
|
||||
|
||||
assert result.credentials == []
|
||||
assert result.supported_credential_types == ["api-key"]
|
||||
assert result.is_oauth_custom_client_enabled is False
|
||||
|
||||
|
||||
class TestGetBuiltinToolProviderCredentials:
|
||||
def test_returns_empty_when_no_providers(self, repository_session: Session) -> None:
|
||||
_persist_provider(repository_session, credential_id="other", tenant_id="other-tenant")
|
||||
|
||||
result = BuiltinToolManageService.get_builtin_tool_provider_credentials(
|
||||
"tenant-1",
|
||||
"google",
|
||||
credential_query=CredentialQueryRepository(
|
||||
session_factory=sessionmaker(bind=repository_session.get_bind(), expire_on_commit=False)
|
||||
),
|
||||
)
|
||||
|
||||
assert result == []
|
||||
|
||||
def test_returns_tenant_scoped_credential_entities(
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
repository_session: Session,
|
||||
) -> None:
|
||||
_persist_provider(repository_session, is_default=False)
|
||||
_persist_provider(repository_session, credential_id="other", tenant_id="other-tenant")
|
||||
controller = MagicMock()
|
||||
monkeypatch.setattr(service_module.ToolManager, "get_builtin_provider", MagicMock(return_value=controller))
|
||||
encrypter = MagicMock()
|
||||
encrypter.decrypt.return_value = {"key": "decrypted"}
|
||||
encrypter.mask_plugin_credentials.return_value = {"key": "***"}
|
||||
monkeypatch.setattr(
|
||||
BuiltinToolManageService,
|
||||
"create_tool_encrypter",
|
||||
MagicMock(return_value=(encrypter, MagicMock())),
|
||||
)
|
||||
|
||||
result = BuiltinToolManageService.get_builtin_tool_provider_credentials(
|
||||
"tenant-1",
|
||||
"google",
|
||||
credential_query=CredentialQueryRepository(
|
||||
session_factory=sessionmaker(bind=repository_session.get_bind(), expire_on_commit=False)
|
||||
),
|
||||
)
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0].id == "cred-1"
|
||||
assert result[0].credentials == {"key": "***"}
|
||||
assert result[0].is_default is True
|
||||
repository_session.expire_all()
|
||||
stored = repository_session.get(BuiltinToolProvider, "cred-1")
|
||||
assert stored is not None
|
||||
assert stored.is_default is False
|
||||
|
||||
|
||||
class TestGetBuiltinProvider:
|
||||
def test_returns_none_when_not_found(self, repository_session: Session) -> None:
|
||||
assert BuiltinToolManageService.get_builtin_provider("google", "tenant-1") is None
|
||||
|
||||
def test_returns_langgenius_provider_for_matching_tenant(self, repository_session: Session) -> None:
|
||||
_persist_provider(repository_session, tenant_id="tenant-1", provider="google")
|
||||
_persist_provider(repository_session, credential_id="other", tenant_id="tenant-2", provider="google")
|
||||
|
||||
result = BuiltinToolManageService.get_builtin_provider("google", "tenant-1")
|
||||
|
||||
assert result is not None
|
||||
assert result.id == "cred-1"
|
||||
assert result.provider == "langgenius/google/google"
|
||||
|
||||
def test_returns_non_langgenius_provider(self, repository_session: Session) -> None:
|
||||
full_provider = "third-party/custom/custom-tool"
|
||||
_persist_provider(repository_session, provider=full_provider)
|
||||
|
||||
result = BuiltinToolManageService.get_builtin_provider(full_provider, "tenant-1")
|
||||
|
||||
assert result is not None
|
||||
assert result.id == "cred-1"
|
||||
assert result.provider == full_provider
|
||||
|
||||
def test_falls_back_on_provider_id_parse_exception(
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
repository_session: Session,
|
||||
) -> None:
|
||||
_persist_provider(repository_session, provider="old-provider")
|
||||
monkeypatch.setattr(service_module, "ToolProviderID", MagicMock(side_effect=Exception("parse error")))
|
||||
|
||||
result = BuiltinToolManageService.get_builtin_provider("old-provider", "tenant-1")
|
||||
|
||||
assert result is not None
|
||||
assert result.id == "cred-1"
|
||||
@@ -0,0 +1,160 @@
|
||||
"""Exercise tenant-scoped MCP provider lookups against persisted rows.
|
||||
|
||||
Primary keys and server identifiers are distinct, even when an identifier looks
|
||||
like a UUID or collides with another provider's primary key.
|
||||
"""
|
||||
|
||||
from typing import Literal
|
||||
|
||||
import pytest
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from models.tools import MCPToolProvider
|
||||
from services.tools.mcp_tools_manage_service import MCPToolManageService
|
||||
|
||||
PROVIDER_UUID = "0b2bd1e4-3a7d-4a0b-9f2a-9b2d6f3c1e55"
|
||||
OTHER_PROVIDER_UUID = "00000000-0000-4000-8000-000000000002"
|
||||
SERVER_IDENTIFIER = "monday_mcp"
|
||||
TENANT_ID = "3f0a1c4e-1f3d-4a4e-9a1f-2a6f8c5d0b11"
|
||||
OTHER_TENANT_ID = "00000000-0000-4000-8000-000000000003"
|
||||
|
||||
|
||||
def _provider(
|
||||
*, provider_id: str = PROVIDER_UUID, server_identifier: str = SERVER_IDENTIFIER, tenant_id: str = TENANT_ID
|
||||
) -> MCPToolProvider:
|
||||
provider = MCPToolProvider(
|
||||
name=f"MCP {provider_id}",
|
||||
server_identifier=server_identifier,
|
||||
server_url=f"https://example.com/mcp/{provider_id}",
|
||||
server_url_hash=provider_id,
|
||||
icon="icon",
|
||||
tenant_id=tenant_id,
|
||||
user_id="user-id",
|
||||
)
|
||||
provider.id = provider_id
|
||||
return provider
|
||||
|
||||
|
||||
def _persist(session: Session, *providers: MCPToolProvider) -> None:
|
||||
session.add_all(providers)
|
||||
session.commit()
|
||||
session.expire_all()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def service(sqlite_session: Session) -> MCPToolManageService:
|
||||
return MCPToolManageService(session=sqlite_session)
|
||||
|
||||
|
||||
def test_get_provider_by_id_queries_primary_key(service: MCPToolManageService, sqlite_session: Session) -> None:
|
||||
provider = _provider()
|
||||
identifier_collision = _provider(provider_id=OTHER_PROVIDER_UUID, server_identifier=PROVIDER_UUID)
|
||||
_persist(sqlite_session, identifier_collision, provider)
|
||||
|
||||
assert service.get_provider_by_id(provider_id=PROVIDER_UUID, tenant_id=TENANT_ID).id == provider.id
|
||||
|
||||
|
||||
def test_get_provider_by_id_rejects_server_identifier(unbound_session: Session) -> None:
|
||||
"""Invalid UUIDs must be rejected before any database access is attempted."""
|
||||
service = MCPToolManageService(session=unbound_session)
|
||||
with pytest.raises(ValueError, match="expected a valid UUID"):
|
||||
service.get_provider_by_id(provider_id=SERVER_IDENTIFIER, tenant_id=TENANT_ID)
|
||||
|
||||
|
||||
def test_get_provider_by_id_not_found(service: MCPToolManageService) -> None:
|
||||
with pytest.raises(ValueError, match="MCP tool not found"):
|
||||
service.get_provider_by_id(provider_id=PROVIDER_UUID, tenant_id=TENANT_ID)
|
||||
|
||||
|
||||
def test_get_provider_by_server_identifier_queries_identifier(
|
||||
service: MCPToolManageService, sqlite_session: Session
|
||||
) -> None:
|
||||
primary_key_collision = _provider()
|
||||
provider = _provider(provider_id=OTHER_PROVIDER_UUID, server_identifier=PROVIDER_UUID)
|
||||
_persist(sqlite_session, primary_key_collision, provider)
|
||||
|
||||
result = service.get_provider_by_server_identifier(server_identifier=PROVIDER_UUID, tenant_id=TENANT_ID)
|
||||
|
||||
assert result.id == provider.id
|
||||
|
||||
|
||||
def test_get_provider_by_server_identifier_not_found(service: MCPToolManageService) -> None:
|
||||
with pytest.raises(ValueError, match="MCP tool not found"):
|
||||
service.get_provider_by_server_identifier(server_identifier=SERVER_IDENTIFIER, tenant_id=TENANT_ID)
|
||||
|
||||
|
||||
def test_persisted_reference_non_uuid_queries_identifier_only(
|
||||
service: MCPToolManageService, sqlite_session: Session
|
||||
) -> None:
|
||||
provider = _provider()
|
||||
_persist(sqlite_session, provider)
|
||||
|
||||
assert (
|
||||
service.get_provider_by_persisted_reference(id_or_server_identifier=SERVER_IDENTIFIER, tenant_id=TENANT_ID).id
|
||||
== provider.id
|
||||
)
|
||||
|
||||
|
||||
def test_persisted_reference_uuid_prefers_primary_key(service: MCPToolManageService, sqlite_session: Session) -> None:
|
||||
"""Legacy graph references prefer the primary key when both meanings resolve."""
|
||||
provider = _provider()
|
||||
identifier_collision = _provider(provider_id=OTHER_PROVIDER_UUID, server_identifier=PROVIDER_UUID)
|
||||
_persist(sqlite_session, identifier_collision, provider)
|
||||
|
||||
assert (
|
||||
service.get_provider_by_persisted_reference(id_or_server_identifier=PROVIDER_UUID, tenant_id=TENANT_ID).id
|
||||
== provider.id
|
||||
)
|
||||
|
||||
|
||||
def test_persisted_reference_uuid_falls_back_to_identifier(
|
||||
service: MCPToolManageService, sqlite_session: Session
|
||||
) -> None:
|
||||
"""A foreign primary-key match must not hide the tenant's UUID-shaped identifier."""
|
||||
foreign_primary_key = _provider(tenant_id=OTHER_TENANT_ID)
|
||||
provider = _provider(provider_id=OTHER_PROVIDER_UUID, server_identifier=PROVIDER_UUID)
|
||||
_persist(sqlite_session, foreign_primary_key, provider)
|
||||
|
||||
assert (
|
||||
service.get_provider_by_persisted_reference(id_or_server_identifier=PROVIDER_UUID, tenant_id=TENANT_ID).id
|
||||
== provider.id
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("reference", [SERVER_IDENTIFIER, PROVIDER_UUID])
|
||||
def test_persisted_reference_not_found(service: MCPToolManageService, reference: str) -> None:
|
||||
with pytest.raises(ValueError, match="MCP tool not found"):
|
||||
service.get_provider_by_persisted_reference(id_or_server_identifier=reference, tenant_id=TENANT_ID)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("lookup", ["id", "server_identifier", "persisted_id", "persisted_identifier"])
|
||||
def test_provider_lookups_reject_foreign_tenants(
|
||||
service: MCPToolManageService,
|
||||
sqlite_session: Session,
|
||||
lookup: Literal["id", "server_identifier", "persisted_id", "persisted_identifier"],
|
||||
) -> None:
|
||||
_persist(sqlite_session, _provider(tenant_id=OTHER_TENANT_ID))
|
||||
|
||||
if lookup == "id":
|
||||
with pytest.raises(ValueError, match="MCP tool not found"):
|
||||
service.get_provider_by_id(provider_id=PROVIDER_UUID, tenant_id=TENANT_ID)
|
||||
elif lookup == "server_identifier":
|
||||
with pytest.raises(ValueError, match="MCP tool not found"):
|
||||
service.get_provider_by_server_identifier(server_identifier=SERVER_IDENTIFIER, tenant_id=TENANT_ID)
|
||||
else:
|
||||
with pytest.raises(ValueError, match="MCP tool not found"):
|
||||
service.get_provider_by_persisted_reference(
|
||||
id_or_server_identifier=PROVIDER_UUID if lookup == "persisted_id" else SERVER_IDENTIFIER,
|
||||
tenant_id=TENANT_ID,
|
||||
)
|
||||
|
||||
|
||||
def test_server_identifier_can_be_reused_across_tenants(service: MCPToolManageService, sqlite_session: Session) -> None:
|
||||
foreign = _provider(tenant_id=OTHER_TENANT_ID)
|
||||
provider = _provider(provider_id=OTHER_PROVIDER_UUID)
|
||||
_persist(sqlite_session, foreign, provider)
|
||||
|
||||
assert (
|
||||
service.get_provider_by_server_identifier(server_identifier=SERVER_IDENTIFIER, tenant_id=TENANT_ID).id
|
||||
== provider.id
|
||||
)
|
||||
@@ -0,0 +1,21 @@
|
||||
from services.tools.tool_labels_service import ToolLabelsService
|
||||
|
||||
|
||||
def test_list_tool_labels_returns_default_labels() -> None:
|
||||
result = ToolLabelsService.list_tool_labels()
|
||||
assert isinstance(result, list)
|
||||
assert len(result) > 0
|
||||
|
||||
|
||||
def test_list_tool_labels_items_are_tool_labels() -> None:
|
||||
from core.tools.entities.tool_entities import ToolLabel
|
||||
|
||||
result = ToolLabelsService.list_tool_labels()
|
||||
for label in result:
|
||||
assert isinstance(label, ToolLabel)
|
||||
|
||||
|
||||
def test_list_tool_labels_matches_default_values() -> None:
|
||||
from core.tools.entities.values import default_tool_labels
|
||||
|
||||
assert ToolLabelsService.list_tool_labels() is default_tool_labels
|
||||
@@ -0,0 +1,44 @@
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from services.tools.tools_manage_service import ToolCommonService
|
||||
|
||||
|
||||
class TestToolCommonService:
|
||||
@patch("services.tools.tools_manage_service.ToolTransformService")
|
||||
@patch("services.tools.tools_manage_service.ToolManager")
|
||||
def test_list_tool_providers_transforms_and_returns(
|
||||
self, mock_manager: MagicMock, mock_transform: MagicMock
|
||||
) -> None:
|
||||
mock_provider1 = MagicMock()
|
||||
mock_provider1.to_dict.return_value = {"name": "provider1"}
|
||||
mock_provider2 = MagicMock()
|
||||
mock_provider2.to_dict.return_value = {"name": "provider2"}
|
||||
mock_manager.list_providers_from_api.return_value = [mock_provider1, mock_provider2]
|
||||
|
||||
result = ToolCommonService.list_tool_providers("user-1", "tenant-1")
|
||||
|
||||
mock_manager.list_providers_from_api.assert_called_once_with("user-1", "tenant-1", None)
|
||||
assert mock_transform.repack_provider.call_count == 2
|
||||
assert result == [{"name": "provider1"}, {"name": "provider2"}]
|
||||
|
||||
@patch("services.tools.tools_manage_service.ToolTransformService")
|
||||
@patch("services.tools.tools_manage_service.ToolManager")
|
||||
def test_list_tool_providers_with_type_filter(self, mock_manager: MagicMock, mock_transform: MagicMock) -> None:
|
||||
providers: list[object] = []
|
||||
mock_manager.list_providers_from_api.return_value = providers
|
||||
|
||||
result = ToolCommonService.list_tool_providers("user-1", "tenant-1", typ="builtin")
|
||||
|
||||
mock_manager.list_providers_from_api.assert_called_once_with("user-1", "tenant-1", "builtin")
|
||||
assert result == []
|
||||
|
||||
@patch("services.tools.tools_manage_service.ToolTransformService")
|
||||
@patch("services.tools.tools_manage_service.ToolManager")
|
||||
def test_list_tool_providers_empty(self, mock_manager: MagicMock, mock_transform: MagicMock) -> None:
|
||||
providers: list[object] = []
|
||||
mock_manager.list_providers_from_api.return_value = providers
|
||||
|
||||
result = ToolCommonService.list_tool_providers("u", "t")
|
||||
|
||||
assert result == []
|
||||
mock_transform.repack_provider.assert_not_called()
|
||||
@@ -0,0 +1,995 @@
|
||||
import json
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
from pytest_mock import MockerFixture
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from core.mcp.types import Tool as MCPTool
|
||||
from core.tools.entities.api_entities import ToolApiEntity, ToolProviderApiEntity
|
||||
from core.tools.entities.common_entities import I18nObject
|
||||
from core.tools.entities.tool_entities import (
|
||||
ApiProviderSchemaType,
|
||||
ToolParameter,
|
||||
ToolProviderEntity,
|
||||
ToolProviderIdentity,
|
||||
ToolProviderType,
|
||||
)
|
||||
from models.account import Account
|
||||
from models.tools import ApiToolProvider, MCPToolProvider
|
||||
from services.tools.tools_transform_service import ToolTransformService
|
||||
from tests.tool_fixtures import make_runtime_tool
|
||||
|
||||
"""Test cases for MCP tool transformation functionality."""
|
||||
|
||||
|
||||
class TestMcpToolsTransformCases:
|
||||
@staticmethod
|
||||
@pytest.fixture
|
||||
def mock_user():
|
||||
"""Provides a real mapped account returned by the provider lookup."""
|
||||
return Account(name="Test User", email="user@example.com")
|
||||
|
||||
@staticmethod
|
||||
@pytest.fixture
|
||||
def mock_provider(mock_user, mocker: MockerFixture):
|
||||
"""Provides a mock MCPToolProvider with a loaded user."""
|
||||
provider = MCPToolProvider(
|
||||
name="Test Provider",
|
||||
server_identifier="test-provider",
|
||||
server_url="https://example.com",
|
||||
server_url_hash="hash",
|
||||
icon="icon",
|
||||
tenant_id="tenant-id",
|
||||
user_id="user-id",
|
||||
)
|
||||
mocker.patch.object(provider, "load_user", return_value=mock_user)
|
||||
return provider
|
||||
|
||||
@staticmethod
|
||||
@pytest.fixture
|
||||
def mock_provider_no_user(mocker: MockerFixture):
|
||||
"""Provides a mock MCPToolProvider with no user."""
|
||||
provider = MCPToolProvider(
|
||||
name="Test Provider",
|
||||
server_identifier="test-provider",
|
||||
server_url="https://example.com",
|
||||
server_url_hash="hash",
|
||||
icon="icon",
|
||||
tenant_id="tenant-id",
|
||||
user_id="user-id",
|
||||
)
|
||||
mocker.patch.object(provider, "load_user", return_value=None)
|
||||
return provider
|
||||
|
||||
@staticmethod
|
||||
@pytest.fixture
|
||||
def mock_provider_full(mock_user, mocker: MockerFixture):
|
||||
"""Provides a fully configured mock MCPToolProvider for detailed tests."""
|
||||
provider = MCPToolProvider(
|
||||
name="Test MCP Provider",
|
||||
server_identifier="server-identifier-456",
|
||||
server_url="https://example.com",
|
||||
server_url_hash="hash",
|
||||
icon="icon",
|
||||
tenant_id="tenant-id",
|
||||
user_id="user-id",
|
||||
authed=True,
|
||||
timeout=30,
|
||||
sse_read_timeout=300,
|
||||
)
|
||||
provider.id = "provider-id-123"
|
||||
provider.provider_icon = "icon.png"
|
||||
provider.masked_server_url = "https://*****.com/mcp"
|
||||
provider.masked_headers = {"Authorization": "Bearer *****"}
|
||||
provider.decrypted_headers = {"Authorization": "Bearer secret-token"}
|
||||
|
||||
# Mock timestamp
|
||||
mock_updated_at = Mock()
|
||||
mock_updated_at.timestamp.return_value = 1234567890
|
||||
provider.updated_at = mock_updated_at
|
||||
|
||||
mocker.patch.object(provider, "load_user", return_value=mock_user)
|
||||
return provider
|
||||
|
||||
@staticmethod
|
||||
@pytest.fixture
|
||||
def sample_mcp_tools():
|
||||
"""Provides sample MCP tools for testing."""
|
||||
return {
|
||||
"simple": MCPTool(
|
||||
name="simple_tool", description="A simple test tool", inputSchema={"type": "object", "properties": {}}
|
||||
),
|
||||
"none_desc": MCPTool(
|
||||
name="tool_none_desc", description=None, inputSchema={"type": "object", "properties": {}}
|
||||
),
|
||||
"complex": MCPTool(
|
||||
name="complex_tool",
|
||||
description="A tool with complex parameters",
|
||||
inputSchema={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"text": {"type": "string", "description": "Input text"},
|
||||
"count": {"type": "integer", "description": "Number of items", "minimum": 1, "maximum": 100},
|
||||
"options": {"type": "array", "items": {"type": "string"}, "description": "List of options"},
|
||||
},
|
||||
"required": ["text"],
|
||||
},
|
||||
),
|
||||
}
|
||||
|
||||
class TestMCPToolTransform:
|
||||
"""Test cases for MCP tool transformation methods."""
|
||||
|
||||
def test_mcp_tool_to_user_tool_with_none_description(self, mock_provider):
|
||||
"""Test that mcp_tool_to_user_tool handles None description correctly."""
|
||||
# Create MCP tools with None description
|
||||
tools = [
|
||||
MCPTool(
|
||||
name="tool1",
|
||||
description=None, # This is the case that caused the error
|
||||
inputSchema={"type": "object", "properties": {}},
|
||||
),
|
||||
MCPTool(
|
||||
name="tool2",
|
||||
description=None,
|
||||
inputSchema={
|
||||
"type": "object",
|
||||
"properties": {"param1": {"type": "string", "description": "A parameter"}},
|
||||
},
|
||||
),
|
||||
]
|
||||
|
||||
# Call the method
|
||||
result = ToolTransformService.mcp_tool_to_user_tool(mock_provider, tools)
|
||||
|
||||
# Verify the result
|
||||
assert len(result) == 2
|
||||
assert all(isinstance(tool, ToolApiEntity) for tool in result)
|
||||
|
||||
# Check first tool
|
||||
assert result[0].name == "tool1"
|
||||
assert result[0].author == "Test User"
|
||||
assert isinstance(result[0].label, I18nObject)
|
||||
assert result[0].label.en_US == "tool1"
|
||||
assert isinstance(result[0].description, I18nObject)
|
||||
assert result[0].description.en_US == "" # Should be empty string, not None
|
||||
assert result[0].description.zh_Hans == ""
|
||||
|
||||
# Check second tool
|
||||
assert result[1].name == "tool2"
|
||||
assert result[1].description.en_US == ""
|
||||
assert result[1].description.zh_Hans == ""
|
||||
|
||||
def test_mcp_tool_to_user_tool_with_description(self, mock_provider):
|
||||
"""Test that mcp_tool_to_user_tool handles normal description correctly."""
|
||||
# Create MCP tools with description
|
||||
tools = [
|
||||
MCPTool(
|
||||
name="tool_with_desc",
|
||||
description="This is a test tool that does something useful",
|
||||
inputSchema={"type": "object", "properties": {}},
|
||||
)
|
||||
]
|
||||
|
||||
# Call the method
|
||||
result = ToolTransformService.mcp_tool_to_user_tool(mock_provider, tools)
|
||||
|
||||
# Verify the result
|
||||
assert len(result) == 1
|
||||
assert isinstance(result[0], ToolApiEntity)
|
||||
assert result[0].name == "tool_with_desc"
|
||||
assert result[0].description.en_US == "This is a test tool that does something useful"
|
||||
assert result[0].description.zh_Hans == "This is a test tool that does something useful"
|
||||
|
||||
def test_mcp_tool_to_user_tool_with_no_user(self, mock_provider_no_user):
|
||||
"""Test that mcp_tool_to_user_tool handles None user correctly."""
|
||||
# Create MCP tool
|
||||
tools = [MCPTool(name="tool1", description="Test tool", inputSchema={"type": "object", "properties": {}})]
|
||||
|
||||
# Call the method
|
||||
result = ToolTransformService.mcp_tool_to_user_tool(mock_provider_no_user, tools)
|
||||
|
||||
# Verify the result
|
||||
assert len(result) == 1
|
||||
assert result[0].author == "Anonymous"
|
||||
|
||||
def test_mcp_tool_to_user_tool_with_complex_schema(self, mock_provider, sample_mcp_tools):
|
||||
"""Test that mcp_tool_to_user_tool correctly converts complex input schemas."""
|
||||
# Use complex tool from fixtures
|
||||
tools = [sample_mcp_tools["complex"]]
|
||||
|
||||
# Call the method
|
||||
result = ToolTransformService.mcp_tool_to_user_tool(mock_provider, tools)
|
||||
|
||||
# Verify the result
|
||||
assert len(result) == 1
|
||||
assert result[0].name == "complex_tool"
|
||||
assert result[0].parameters is not None
|
||||
# The actual parameter conversion is handled by convert_mcp_schema_to_parameter
|
||||
# which should be tested separately
|
||||
|
||||
def test_convert_mcp_schema_to_parameter_preserves_anyof_object_type(self):
|
||||
"""Nullable object schemas should keep the object parameter type."""
|
||||
schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"retrieval_model": {
|
||||
"anyOf": [{"type": "object"}, {"type": "null"}],
|
||||
"description": "检索模型配置",
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
result = ToolTransformService.convert_mcp_schema_to_parameter(schema)
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0].name == "retrieval_model"
|
||||
assert result[0].type == ToolParameter.ToolParameterType.OBJECT
|
||||
assert result[0].input_schema == schema["properties"]["retrieval_model"]
|
||||
|
||||
def test_convert_mcp_schema_to_parameter_preserves_oneof_object_type(self):
|
||||
"""Nullable oneOf object schemas should keep the object parameter type."""
|
||||
schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"retrieval_model": {
|
||||
"oneOf": [{"type": "object"}, {"type": "null"}],
|
||||
"description": "检索模型配置",
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
result = ToolTransformService.convert_mcp_schema_to_parameter(schema)
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0].name == "retrieval_model"
|
||||
assert result[0].type == ToolParameter.ToolParameterType.OBJECT
|
||||
assert result[0].input_schema == schema["properties"]["retrieval_model"]
|
||||
|
||||
def test_convert_mcp_schema_to_parameter_handles_null_type(self):
|
||||
"""Schemas with only a null type should fall back to string."""
|
||||
schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"null_prop_str": {"type": "null"},
|
||||
"null_prop_list": {"type": ["null"]},
|
||||
},
|
||||
}
|
||||
|
||||
result = ToolTransformService.convert_mcp_schema_to_parameter(schema)
|
||||
|
||||
assert len(result) == 2
|
||||
param_map = {parameter.name: parameter for parameter in result}
|
||||
assert "null_prop_str" in param_map
|
||||
assert param_map["null_prop_str"].type == ToolParameter.ToolParameterType.STRING
|
||||
assert "null_prop_list" in param_map
|
||||
assert param_map["null_prop_list"].type == ToolParameter.ToolParameterType.STRING
|
||||
|
||||
def test_convert_mcp_schema_to_parameter_preserves_allof_object_type_with_multiple_object_items(self):
|
||||
"""Property-level allOf with multiple object items should still resolve to object."""
|
||||
schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"config": {
|
||||
"allOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"enabled": {"type": "boolean"},
|
||||
},
|
||||
"required": ["enabled"],
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"priority": {"type": "integer", "minimum": 1, "maximum": 10},
|
||||
},
|
||||
"required": ["priority"],
|
||||
},
|
||||
],
|
||||
"description": "Config must match all schemas (allOf)",
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
result = ToolTransformService.convert_mcp_schema_to_parameter(schema)
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0].name == "config"
|
||||
assert result[0].type == ToolParameter.ToolParameterType.OBJECT
|
||||
assert result[0].input_schema == schema["properties"]["config"]
|
||||
|
||||
def test_convert_mcp_schema_to_parameter_preserves_allof_object_type(self):
|
||||
"""Composed property schemas should keep the object parameter type."""
|
||||
schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"retrieval_model": {
|
||||
"allOf": [
|
||||
{"type": "object"},
|
||||
{"properties": {"top_k": {"type": "integer"}}},
|
||||
],
|
||||
"description": "检索模型配置",
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
result = ToolTransformService.convert_mcp_schema_to_parameter(schema)
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0].name == "retrieval_model"
|
||||
assert result[0].type == ToolParameter.ToolParameterType.OBJECT
|
||||
assert result[0].input_schema == schema["properties"]["retrieval_model"]
|
||||
|
||||
def test_convert_mcp_schema_to_parameter_limits_recursive_schema_depth(self):
|
||||
"""Self-referential composed schemas should stop resolving after the configured max depth."""
|
||||
recursive_property: dict[str, object] = {"description": "Recursive schema"}
|
||||
recursive_property["anyOf"] = [recursive_property]
|
||||
schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"recursive_config": recursive_property,
|
||||
},
|
||||
}
|
||||
|
||||
result = ToolTransformService.convert_mcp_schema_to_parameter(schema)
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0].name == "recursive_config"
|
||||
assert result[0].type == ToolParameter.ToolParameterType.STRING
|
||||
assert result[0].input_schema is None
|
||||
|
||||
def test_mcp_provider_to_user_provider_minimal_response(self, mock_provider_full, mocker: MockerFixture):
|
||||
"""Test mcp_provider_to_user_provider with a response that omits optional fields."""
|
||||
# Set tools data with null description
|
||||
mock_provider_full.tools = '[{"name": "tool1", "description": null, "inputSchema": {}}]'
|
||||
|
||||
# Mock the to_entity and to_api_response methods
|
||||
mock_entity = Mock()
|
||||
mock_entity.to_api_response.return_value = {
|
||||
"name": "Test MCP Provider",
|
||||
"type": ToolProviderType.MCP,
|
||||
"is_team_authorization": True,
|
||||
"server_url": "https://*****.com/mcp",
|
||||
"provider_icon": "icon.png",
|
||||
"masked_headers": {"Authorization": "Bearer *****"},
|
||||
"updated_at": 1234567890,
|
||||
"labels": [],
|
||||
"author": "Test User",
|
||||
"description": I18nObject(
|
||||
en_US="Test MCP Provider Description", zh_Hans="Test MCP Provider Description"
|
||||
),
|
||||
"icon": "icon.png",
|
||||
"label": I18nObject(en_US="Test MCP Provider", zh_Hans="Test MCP Provider"),
|
||||
"masked_credentials": {},
|
||||
}
|
||||
mocker.patch.object(mock_provider_full, "to_entity", return_value=mock_entity)
|
||||
|
||||
result = ToolTransformService.mcp_provider_to_user_provider(mock_provider_full)
|
||||
|
||||
# Verify the result
|
||||
assert isinstance(result, ToolProviderApiEntity)
|
||||
assert result.id == "provider-id-123"
|
||||
assert result.name == "Test MCP Provider"
|
||||
assert result.type == ToolProviderType.MCP
|
||||
assert result.is_team_authorization is True
|
||||
assert result.server_url == "https://*****.com/mcp"
|
||||
assert len(result.tools) == 1
|
||||
assert result.tools[0].description.en_US == "" # Should handle None description
|
||||
|
||||
def test_mcp_provider_to_user_provider_full_response(self, mock_provider_full, mocker: MockerFixture):
|
||||
"""Test mcp_provider_to_user_provider with configuration and sensitive fields."""
|
||||
# Set tools data with description
|
||||
mock_provider_full.tools = '[{"name": "tool1", "description": "Tool description", "inputSchema": {}}]'
|
||||
|
||||
# Mock the to_entity and to_api_response methods
|
||||
mock_entity = Mock()
|
||||
mock_entity.to_api_response.return_value = {
|
||||
"name": "Test MCP Provider",
|
||||
"type": ToolProviderType.MCP,
|
||||
"is_team_authorization": True,
|
||||
"server_url": "https://*****.com/mcp",
|
||||
"provider_icon": "icon.png",
|
||||
"masked_headers": {"Authorization": "Bearer *****"},
|
||||
"updated_at": 1234567890,
|
||||
"labels": [],
|
||||
"configuration": {"timeout": "30", "sse_read_timeout": "300"},
|
||||
"original_headers": {"Authorization": "Bearer secret-token"},
|
||||
"author": "Test User",
|
||||
"description": I18nObject(
|
||||
en_US="Test MCP Provider Description", zh_Hans="Test MCP Provider Description"
|
||||
),
|
||||
"icon": "icon.png",
|
||||
"label": I18nObject(en_US="Test MCP Provider", zh_Hans="Test MCP Provider"),
|
||||
"masked_credentials": {},
|
||||
}
|
||||
mocker.patch.object(mock_provider_full, "to_entity", return_value=mock_entity)
|
||||
|
||||
result = ToolTransformService.mcp_provider_to_user_provider(mock_provider_full)
|
||||
|
||||
# Verify the result
|
||||
assert isinstance(result, ToolProviderApiEntity)
|
||||
assert result.id == "provider-id-123"
|
||||
assert result.server_identifier == "server-identifier-456"
|
||||
assert result.configuration is not None
|
||||
assert result.configuration.timeout == 30
|
||||
assert result.configuration.sse_read_timeout == 300
|
||||
assert result.original_headers == {"Authorization": "Bearer secret-token"}
|
||||
assert len(result.tools) == 1
|
||||
assert result.tools[0].description.en_US == "Tool description"
|
||||
|
||||
def test_mcp_tool_to_user_tool_falls_back_to_name_when_title_is_null(self, mock_provider):
|
||||
"""Tools where the server returned ``title: null`` should fall back to the tool name.
|
||||
|
||||
Regression test for langgenius/dify#42453: Exa's MCP server returns
|
||||
``title: null`` for some tools, breaking tool conversion when the
|
||||
server-provided title is required.
|
||||
"""
|
||||
tools = [
|
||||
MCPTool(
|
||||
name="search_web",
|
||||
title=None,
|
||||
description="Search the web",
|
||||
inputSchema={"type": "object", "properties": {}},
|
||||
),
|
||||
MCPTool(
|
||||
name="get_content",
|
||||
description="Fetch page content",
|
||||
inputSchema={"type": "object", "properties": {}},
|
||||
),
|
||||
]
|
||||
|
||||
result = ToolTransformService.mcp_tool_to_user_tool(mock_provider, tools)
|
||||
|
||||
assert len(result) == 2
|
||||
# title is explicitly null -> must fall back to ``name``
|
||||
assert result[0].name == "search_web"
|
||||
assert result[0].label.en_US == "search_web"
|
||||
assert result[0].label.zh_Hans == "search_web"
|
||||
# title key is absent entirely -> must also fall back to ``name``
|
||||
assert result[1].name == "get_content"
|
||||
assert result[1].label.en_US == "get_content"
|
||||
assert result[1].label.zh_Hans == "get_content"
|
||||
|
||||
def test_mcp_tool_to_user_tool_uses_title_when_provided(self, mock_provider):
|
||||
"""When the server provides a non-null title, it should be used for the label."""
|
||||
tools = [
|
||||
MCPTool(
|
||||
name="search_web",
|
||||
title="Search the Web",
|
||||
description="Search the web",
|
||||
inputSchema={"type": "object", "properties": {}},
|
||||
)
|
||||
]
|
||||
|
||||
result = ToolTransformService.mcp_tool_to_user_tool(mock_provider, tools)
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0].name == "search_web"
|
||||
assert result[0].label.en_US == "Search the Web"
|
||||
assert result[0].label.zh_Hans == "Search the Web"
|
||||
|
||||
|
||||
MODULE = "services.tools.tools_transform_service"
|
||||
|
||||
|
||||
def _parameter(
|
||||
name: str,
|
||||
label: str,
|
||||
form: ToolParameter.ToolParameterForm = ToolParameter.ToolParameterForm.FORM,
|
||||
) -> ToolParameter:
|
||||
return ToolParameter(
|
||||
name=name,
|
||||
label=I18nObject(en_US=label),
|
||||
human_description=I18nObject(en_US=label),
|
||||
type=ToolParameter.ToolParameterType.STRING,
|
||||
form=form,
|
||||
)
|
||||
|
||||
|
||||
def _api_provider(session: Session, auth_type: str) -> ApiToolProvider:
|
||||
user = Account(name="Test User", email="user@example.com")
|
||||
session.add(user)
|
||||
session.flush()
|
||||
provider = ApiToolProvider(
|
||||
name="Test API Provider",
|
||||
icon='{"background": "#000", "content": "A"}',
|
||||
schema="{}",
|
||||
schema_type_str=ApiProviderSchemaType.OPENAPI,
|
||||
user_id=user.id,
|
||||
tenant_id="00000000-0000-0000-0000-000000000002",
|
||||
description="Test API tool provider",
|
||||
tools_str="[]",
|
||||
credentials_str=json.dumps({"auth_type": auth_type}),
|
||||
)
|
||||
session.add(provider)
|
||||
session.commit()
|
||||
return provider
|
||||
|
||||
|
||||
class TestToolsTransformServiceCases:
|
||||
class TestToolTransformService:
|
||||
"""Test cases for ToolTransformService.convert_tool_entity_to_api_entity method"""
|
||||
|
||||
def test_convert_tool_with_parameter_override(self):
|
||||
"""Test that runtime parameters correctly override base parameters"""
|
||||
base_param1 = _parameter("param1", "Base Param 1")
|
||||
base_param2 = _parameter("param2", "Base Param 2")
|
||||
|
||||
runtime_param1 = _parameter("param1", "Runtime Param 1")
|
||||
|
||||
tool = make_runtime_tool(base_params=[base_param1, base_param2], runtime_params=[runtime_param1])
|
||||
|
||||
# Call the method
|
||||
result = ToolTransformService.convert_tool_entity_to_api_entity(tool, "test_tenant", None)
|
||||
|
||||
# Verify the result
|
||||
assert isinstance(result, ToolApiEntity)
|
||||
assert result.author == "test_author"
|
||||
assert result.name == "test_tool"
|
||||
assert result.parameters is not None
|
||||
assert len(result.parameters) == 2
|
||||
|
||||
# Find the overridden parameter
|
||||
overridden_param = next((p for p in result.parameters if p.name == "param1"), None)
|
||||
assert overridden_param is not None
|
||||
assert overridden_param.label.en_US == "Runtime Param 1" # Should be runtime version
|
||||
|
||||
# Find the non-overridden parameter
|
||||
original_param = next((p for p in result.parameters if p.name == "param2"), None)
|
||||
assert original_param is not None
|
||||
assert original_param.label.en_US == "Base Param 2" # Should be base version
|
||||
|
||||
def test_convert_tool_with_additional_runtime_parameters(self):
|
||||
"""Test that additional runtime parameters are added to the final list"""
|
||||
base_param1 = _parameter("param1", "Base Param 1")
|
||||
|
||||
runtime_param1 = _parameter("param1", "Runtime Param 1")
|
||||
runtime_param2 = _parameter("runtime_only", "Runtime Only Param")
|
||||
|
||||
tool = make_runtime_tool(base_params=[base_param1], runtime_params=[runtime_param1, runtime_param2])
|
||||
|
||||
# Call the method
|
||||
result = ToolTransformService.convert_tool_entity_to_api_entity(tool, "test_tenant", None)
|
||||
|
||||
# Verify the result
|
||||
assert isinstance(result, ToolApiEntity)
|
||||
assert result.parameters is not None
|
||||
assert len(result.parameters) == 2
|
||||
|
||||
# Check that both parameters are present
|
||||
param_names = [p.name for p in result.parameters]
|
||||
assert "param1" in param_names
|
||||
assert "runtime_only" in param_names
|
||||
|
||||
# Verify the overridden parameter has runtime version
|
||||
overridden_param = next((p for p in result.parameters if p.name == "param1"), None)
|
||||
assert overridden_param is not None
|
||||
assert overridden_param.label.en_US == "Runtime Param 1"
|
||||
|
||||
# Verify the new runtime parameter is included
|
||||
new_param = next((p for p in result.parameters if p.name == "runtime_only"), None)
|
||||
assert new_param is not None
|
||||
assert new_param.label.en_US == "Runtime Only Param"
|
||||
|
||||
def test_convert_tool_with_non_form_runtime_parameters(self):
|
||||
"""Test that non-FORM runtime parameters are not added as new parameters"""
|
||||
base_param1 = _parameter("param1", "Base Param 1")
|
||||
|
||||
runtime_param1 = _parameter("param1", "Runtime Param 1")
|
||||
runtime_param2 = _parameter("llm_param", "LLM Param", ToolParameter.ToolParameterForm.LLM)
|
||||
|
||||
tool = make_runtime_tool(base_params=[base_param1], runtime_params=[runtime_param1, runtime_param2])
|
||||
|
||||
# Call the method
|
||||
result = ToolTransformService.convert_tool_entity_to_api_entity(tool, "test_tenant", None)
|
||||
|
||||
# Verify the result
|
||||
assert isinstance(result, ToolApiEntity)
|
||||
assert result.parameters is not None
|
||||
assert len(result.parameters) == 1 # Only the FORM parameter should be present
|
||||
|
||||
# Check that only the FORM parameter is present
|
||||
param_names = [p.name for p in result.parameters]
|
||||
assert "param1" in param_names
|
||||
assert "llm_param" not in param_names
|
||||
|
||||
def test_convert_tool_with_empty_parameters(self):
|
||||
"""Test conversion with empty base and runtime parameters"""
|
||||
tool = make_runtime_tool(base_params=[], runtime_params=[])
|
||||
|
||||
# Call the method
|
||||
result = ToolTransformService.convert_tool_entity_to_api_entity(tool, "test_tenant", None)
|
||||
|
||||
# Verify the result
|
||||
assert isinstance(result, ToolApiEntity)
|
||||
assert result.parameters is not None
|
||||
assert len(result.parameters) == 0
|
||||
|
||||
def test_convert_tool_with_none_parameters(self):
|
||||
"""Test conversion when base parameters is None"""
|
||||
tool = make_runtime_tool(base_params=None, runtime_params=[])
|
||||
|
||||
# Call the method
|
||||
result = ToolTransformService.convert_tool_entity_to_api_entity(tool, "test_tenant", None)
|
||||
|
||||
# Verify the result
|
||||
assert isinstance(result, ToolApiEntity)
|
||||
assert result.parameters is not None
|
||||
assert len(result.parameters) == 0
|
||||
|
||||
def test_convert_tool_parameter_order_preserved(self):
|
||||
"""Test that parameter order is preserved correctly"""
|
||||
base_param1 = _parameter("param1", "Base Param 1")
|
||||
base_param2 = _parameter("param2", "Base Param 2")
|
||||
base_param3 = _parameter("param3", "Base Param 3")
|
||||
|
||||
# Create runtime parameter that overrides middle parameter
|
||||
runtime_param2 = _parameter("param2", "Runtime Param 2")
|
||||
|
||||
# Create new runtime parameter
|
||||
runtime_param4 = _parameter("param4", "Runtime Param 4")
|
||||
|
||||
tool = make_runtime_tool(
|
||||
base_params=[base_param1, base_param2, base_param3], runtime_params=[runtime_param2, runtime_param4]
|
||||
)
|
||||
|
||||
# Call the method
|
||||
result = ToolTransformService.convert_tool_entity_to_api_entity(tool, "test_tenant", None)
|
||||
|
||||
# Verify the result
|
||||
assert isinstance(result, ToolApiEntity)
|
||||
assert result.parameters is not None
|
||||
assert len(result.parameters) == 4
|
||||
|
||||
# Check that order is maintained: base parameters first, then new runtime parameters
|
||||
param_names = [p.name for p in result.parameters]
|
||||
assert param_names == ["param1", "param2", "param3", "param4"]
|
||||
|
||||
# Verify that param2 was overridden with runtime version
|
||||
param2 = result.parameters[1]
|
||||
assert param2.name == "param2"
|
||||
assert param2.label.en_US == "Runtime Param 2"
|
||||
|
||||
class TestWorkflowProviderToUserProvider:
|
||||
"""Test cases for ToolTransformService.workflow_provider_to_user_provider method"""
|
||||
|
||||
def test_workflow_provider_to_user_provider_with_workflow_app_id(self):
|
||||
"""Test that workflow_provider_to_user_provider correctly sets workflow_app_id."""
|
||||
from core.tools.workflow_as_tool.provider import WorkflowToolProviderController
|
||||
|
||||
workflow_app_id = "app_123"
|
||||
provider_id = "provider_123"
|
||||
controller = WorkflowToolProviderController(
|
||||
provider_id=provider_id,
|
||||
entity=ToolProviderEntity(
|
||||
# Retain legacy emoji mappings accepted by the API transformation.
|
||||
identity=ToolProviderIdentity.model_construct(
|
||||
author="test_author",
|
||||
name="test_workflow_tool",
|
||||
description=I18nObject(en_US="Test description"),
|
||||
icon={"type": "emoji", "content": "🔧"},
|
||||
icon_dark=None,
|
||||
label=I18nObject(en_US="Test Workflow Tool"),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
# Call the method
|
||||
result = ToolTransformService.workflow_provider_to_user_provider(
|
||||
provider_controller=controller,
|
||||
labels=["label1", "label2"],
|
||||
workflow_app_id=workflow_app_id,
|
||||
)
|
||||
|
||||
# Verify the result
|
||||
assert isinstance(result, ToolProviderApiEntity)
|
||||
assert result.id == provider_id
|
||||
assert result.author == "test_author"
|
||||
assert result.name == "test_workflow_tool"
|
||||
assert result.type == ToolProviderType.WORKFLOW
|
||||
assert result.workflow_app_id == workflow_app_id
|
||||
assert result.labels == ["label1", "label2"]
|
||||
assert result.is_team_authorization is True
|
||||
assert result.plugin_id is None
|
||||
assert result.plugin_unique_identifier is None
|
||||
assert result.tools == []
|
||||
|
||||
def test_workflow_provider_to_user_provider_without_workflow_app_id(self):
|
||||
"""Test that workflow_provider_to_user_provider works when workflow_app_id is not provided."""
|
||||
from core.tools.workflow_as_tool.provider import WorkflowToolProviderController
|
||||
|
||||
provider_id = "provider_123"
|
||||
controller = WorkflowToolProviderController(
|
||||
provider_id=provider_id,
|
||||
entity=ToolProviderEntity(
|
||||
# Retain legacy emoji mappings accepted by the API transformation.
|
||||
identity=ToolProviderIdentity.model_construct(
|
||||
author="test_author",
|
||||
name="test_workflow_tool",
|
||||
description=I18nObject(en_US="Test description"),
|
||||
icon={"type": "emoji", "content": "🔧"},
|
||||
icon_dark=None,
|
||||
label=I18nObject(en_US="Test Workflow Tool"),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
# Call the method without workflow_app_id
|
||||
result = ToolTransformService.workflow_provider_to_user_provider(
|
||||
provider_controller=controller,
|
||||
labels=["label1"],
|
||||
)
|
||||
|
||||
# Verify the result
|
||||
assert isinstance(result, ToolProviderApiEntity)
|
||||
assert result.id == provider_id
|
||||
assert result.workflow_app_id is None
|
||||
assert result.labels == ["label1"]
|
||||
|
||||
def test_workflow_provider_to_user_provider_workflow_app_id_none(self):
|
||||
"""Test that workflow_provider_to_user_provider handles None workflow_app_id explicitly."""
|
||||
from core.tools.workflow_as_tool.provider import WorkflowToolProviderController
|
||||
|
||||
provider_id = "provider_123"
|
||||
controller = WorkflowToolProviderController(
|
||||
provider_id=provider_id,
|
||||
entity=ToolProviderEntity(
|
||||
# Retain legacy emoji mappings accepted by the API transformation.
|
||||
identity=ToolProviderIdentity.model_construct(
|
||||
author="test_author",
|
||||
name="test_workflow_tool",
|
||||
description=I18nObject(en_US="Test description"),
|
||||
icon={"type": "emoji", "content": "🔧"},
|
||||
icon_dark=None,
|
||||
label=I18nObject(en_US="Test Workflow Tool"),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
# Call the method with explicit None values
|
||||
result = ToolTransformService.workflow_provider_to_user_provider(
|
||||
provider_controller=controller,
|
||||
labels=None,
|
||||
workflow_app_id=None,
|
||||
)
|
||||
|
||||
# Verify the result
|
||||
assert isinstance(result, ToolProviderApiEntity)
|
||||
assert result.id == provider_id
|
||||
assert result.workflow_app_id is None
|
||||
assert result.labels == []
|
||||
|
||||
def test_workflow_provider_to_user_provider_preserves_other_fields(self):
|
||||
"""Test that workflow_provider_to_user_provider preserves all other entity fields."""
|
||||
from core.tools.workflow_as_tool.provider import WorkflowToolProviderController
|
||||
|
||||
workflow_app_id = "app_456"
|
||||
provider_id = "provider_456"
|
||||
controller = WorkflowToolProviderController(
|
||||
provider_id=provider_id,
|
||||
entity=ToolProviderEntity(
|
||||
# Retain legacy emoji mappings accepted by the API transformation.
|
||||
identity=ToolProviderIdentity.model_construct(
|
||||
author="another_author",
|
||||
name="another_workflow_tool",
|
||||
description=I18nObject(en_US="Another description", zh_Hans="Another description"),
|
||||
icon={"type": "emoji", "content": "⚙️"},
|
||||
icon_dark={"type": "emoji", "content": "🔧"},
|
||||
label=I18nObject(en_US="Another Workflow Tool", zh_Hans="Another Workflow Tool"),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
# Call the method
|
||||
result = ToolTransformService.workflow_provider_to_user_provider(
|
||||
provider_controller=controller,
|
||||
labels=["automation", "workflow"],
|
||||
workflow_app_id=workflow_app_id,
|
||||
)
|
||||
|
||||
# Verify all fields are preserved correctly
|
||||
assert isinstance(result, ToolProviderApiEntity)
|
||||
assert result.id == provider_id
|
||||
assert result.author == "another_author"
|
||||
assert result.name == "another_workflow_tool"
|
||||
assert result.description.en_US == "Another description"
|
||||
assert result.description.zh_Hans == "Another description"
|
||||
assert result.icon == {"type": "emoji", "content": "⚙️"}
|
||||
assert result.icon_dark == {"type": "emoji", "content": "🔧"}
|
||||
assert result.label.en_US == "Another Workflow Tool"
|
||||
assert result.label.zh_Hans == "Another Workflow Tool"
|
||||
assert result.type == ToolProviderType.WORKFLOW
|
||||
assert result.workflow_app_id == workflow_app_id
|
||||
assert result.labels == ["automation", "workflow"]
|
||||
assert result.masked_credentials == {}
|
||||
assert result.is_team_authorization is True
|
||||
assert result.allow_delete is True
|
||||
assert result.plugin_id is None
|
||||
assert result.plugin_unique_identifier is None
|
||||
assert result.tools == []
|
||||
|
||||
class TestGetToolProviderIconUrl:
|
||||
def test_builtin_provider_returns_console_url(self, config_overrides):
|
||||
config_overrides(CONSOLE_API_URL="https://app.dify.ai")
|
||||
url = ToolTransformService.get_tool_provider_icon_url("builtin", "google", "icon.png")
|
||||
|
||||
assert "/builtin/google/icon" in url
|
||||
assert url.startswith("https://app.dify.ai/console/api/workspaces/current/tool-provider")
|
||||
|
||||
def test_builtin_provider_with_no_console_url(self, config_overrides):
|
||||
config_overrides(CONSOLE_API_URL=None)
|
||||
url = ToolTransformService.get_tool_provider_icon_url("builtin", "slack", "icon.png")
|
||||
|
||||
assert "/builtin/slack/icon" in url
|
||||
|
||||
def test_api_provider_parses_json_icon(self):
|
||||
icon_json = '{"background": "#fff", "content": "A"}'
|
||||
result = ToolTransformService.get_tool_provider_icon_url("api", "my-api", icon_json)
|
||||
assert result == {"background": "#fff", "content": "A"}
|
||||
|
||||
def test_api_provider_returns_dict_icon_directly(self):
|
||||
icon = {"background": "#000", "content": "B"}
|
||||
result = ToolTransformService.get_tool_provider_icon_url("api", "my-api", icon)
|
||||
assert result == icon
|
||||
|
||||
def test_api_provider_returns_fallback_on_invalid_json(self):
|
||||
result = ToolTransformService.get_tool_provider_icon_url("api", "my-api", "not-json")
|
||||
assert result == {"background": "#252525", "content": "\ud83d\ude01"}
|
||||
|
||||
def test_workflow_provider_behaves_like_api(self):
|
||||
icon = {"background": "#123", "content": "W"}
|
||||
assert ToolTransformService.get_tool_provider_icon_url("workflow", "wf", icon) == icon
|
||||
|
||||
def test_mcp_returns_icon_as_is(self):
|
||||
assert ToolTransformService.get_tool_provider_icon_url("mcp", "srv", "icon-value") == "icon-value"
|
||||
|
||||
def test_unknown_type_returns_empty(self):
|
||||
assert ToolTransformService.get_tool_provider_icon_url("unknown", "x", "i") == ""
|
||||
|
||||
class TestRepackProvider:
|
||||
def test_repacks_dict_provider_icon(self):
|
||||
provider = {"type": "builtin", "name": "google", "icon": "old"}
|
||||
with patch.object(ToolTransformService, "get_tool_provider_icon_url", return_value="/new-url") as mock_fn:
|
||||
ToolTransformService.repack_provider("t1", provider)
|
||||
|
||||
assert provider["icon"] == "/new-url"
|
||||
mock_fn.assert_called_once_with(provider_type="builtin", provider_name="google", icon="old")
|
||||
|
||||
def test_repacks_tool_provider_api_entity_without_plugin(self):
|
||||
entity = ToolProviderApiEntity(
|
||||
id="slack",
|
||||
author="test_author",
|
||||
name="slack",
|
||||
description=I18nObject(en_US="Slack"),
|
||||
label=I18nObject(en_US="Slack"),
|
||||
type=ToolProviderType.BUILT_IN,
|
||||
plugin_id=None,
|
||||
icon="icon.svg",
|
||||
icon_dark="dark.svg",
|
||||
)
|
||||
|
||||
with patch.object(ToolTransformService, "get_tool_provider_icon_url", return_value="/url"):
|
||||
ToolTransformService.repack_provider("t1", entity)
|
||||
|
||||
assert entity.icon == "/url"
|
||||
assert entity.icon_dark == "/url"
|
||||
|
||||
class TestConvertMcpSchemaToParameter:
|
||||
def test_simple_object_schema(self):
|
||||
schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"query": {"type": "string", "description": "Search query"},
|
||||
"count": {"type": "integer", "description": "Result count"},
|
||||
},
|
||||
"required": ["query"],
|
||||
}
|
||||
|
||||
params = ToolTransformService.convert_mcp_schema_to_parameter(schema)
|
||||
|
||||
assert len(params) == 2
|
||||
query_param = next(p for p in params if p.name == "query")
|
||||
count_param = next(p for p in params if p.name == "count")
|
||||
assert query_param.required is True
|
||||
assert count_param.required is False
|
||||
assert count_param.type.value == "number"
|
||||
|
||||
def test_float_maps_to_number(self):
|
||||
schema = {"type": "object", "properties": {"rate": {"type": "float"}}, "required": []}
|
||||
assert ToolTransformService.convert_mcp_schema_to_parameter(schema)[0].type.value == "number"
|
||||
|
||||
def test_array_type_attaches_input_schema(self):
|
||||
prop = {"type": "array", "description": "Items", "items": {"type": "string"}}
|
||||
schema = {"type": "object", "properties": {"items": prop}, "required": []}
|
||||
param = ToolTransformService.convert_mcp_schema_to_parameter(schema)[0]
|
||||
assert param.input_schema is not None
|
||||
|
||||
def test_non_object_schema_returns_empty(self):
|
||||
assert ToolTransformService.convert_mcp_schema_to_parameter({"type": "string"}) == []
|
||||
|
||||
def test_missing_properties_returns_empty(self):
|
||||
assert ToolTransformService.convert_mcp_schema_to_parameter({"type": "object"}) == []
|
||||
|
||||
def test_list_type_uses_first_element(self):
|
||||
schema = {"type": "object", "properties": {"f": {"type": ["string", "null"]}}, "required": []}
|
||||
assert ToolTransformService.convert_mcp_schema_to_parameter(schema)[0].type.value == "string"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("prop", "expected_type"),
|
||||
[
|
||||
({"anyOf": [{"type": "string"}, {"type": "array", "items": {"type": "string"}}]}, "string"),
|
||||
({"anyOf": [{"type": "array", "items": {"type": "string"}}, {"type": "string"}]}, "array"),
|
||||
({"oneOf": [{"type": "string"}, {"type": "array", "items": {"type": "string"}}]}, "string"),
|
||||
({"oneOf": [{"type": "array", "items": {"type": "string"}}, {"type": "string"}]}, "array"),
|
||||
({"type": ["string", "array"], "items": {"type": "string"}}, "string"),
|
||||
({"type": ["array", "string"], "items": {"type": "string"}}, "array"),
|
||||
({"type": ["string", "array", "null"]}, "string"),
|
||||
],
|
||||
)
|
||||
def test_mixed_type_union_attaches_input_schema(self, prop, expected_type):
|
||||
schema = {"type": "object", "properties": {"domain": prop}, "required": []}
|
||||
param = ToolTransformService.convert_mcp_schema_to_parameter(schema)[0]
|
||||
assert param.type.value == expected_type
|
||||
assert param.input_schema == prop
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"prop",
|
||||
[
|
||||
{"type": ["string", "null"]},
|
||||
{"anyOf": [{"type": "string"}, {"type": "null"}]},
|
||||
{"oneOf": [{"type": "null"}, {"type": "string"}]},
|
||||
{"oneOf": [{"type": "integer"}, {"type": "number"}]},
|
||||
],
|
||||
)
|
||||
def test_single_type_union_has_no_input_schema(self, prop):
|
||||
schema = {"type": "object", "properties": {"f": prop}, "required": []}
|
||||
assert ToolTransformService.convert_mcp_schema_to_parameter(schema)[0].input_schema is None
|
||||
|
||||
def test_missing_description_defaults_empty(self):
|
||||
schema = {"type": "object", "properties": {"f": {"type": "string"}}, "required": []}
|
||||
assert ToolTransformService.convert_mcp_schema_to_parameter(schema)[0].llm_description == ""
|
||||
|
||||
class TestApiProviderToController:
|
||||
def test_api_key_header_auth(self, sqlite_session: Session) -> None:
|
||||
db_provider = _api_provider(sqlite_session, "api_key_header")
|
||||
with patch(f"{MODULE}.db.session", return_value=sqlite_session):
|
||||
controller = ToolTransformService.api_provider_to_controller(db_provider)
|
||||
assert controller.entity.identity.author == "Test User"
|
||||
assert [c.name for c in controller.entity.credentials_schema] == [
|
||||
"auth_type",
|
||||
"api_key_header",
|
||||
"api_key_value",
|
||||
"api_key_header_prefix",
|
||||
]
|
||||
|
||||
def test_api_key_query_auth(self, sqlite_session: Session) -> None:
|
||||
db_provider = _api_provider(sqlite_session, "api_key_query")
|
||||
with patch(f"{MODULE}.db.session", return_value=sqlite_session):
|
||||
controller = ToolTransformService.api_provider_to_controller(db_provider)
|
||||
assert controller.entity.identity.author == "Test User"
|
||||
assert [c.name for c in controller.entity.credentials_schema] == [
|
||||
"auth_type",
|
||||
"api_key_query_param",
|
||||
"api_key_value",
|
||||
]
|
||||
|
||||
def test_legacy_api_key_maps_to_header(self, sqlite_session: Session) -> None:
|
||||
db_provider = _api_provider(sqlite_session, "api_key")
|
||||
with patch(f"{MODULE}.db.session", return_value=sqlite_session):
|
||||
controller = ToolTransformService.api_provider_to_controller(db_provider)
|
||||
assert controller.entity.identity.author == "Test User"
|
||||
assert [c.name for c in controller.entity.credentials_schema] == [
|
||||
"auth_type",
|
||||
"api_key_header",
|
||||
"api_key_value",
|
||||
"api_key_header_prefix",
|
||||
]
|
||||
|
||||
def test_unknown_auth_defaults_to_none(self, sqlite_session: Session) -> None:
|
||||
db_provider = _api_provider(sqlite_session, "something_else")
|
||||
with patch(f"{MODULE}.db.session", return_value=sqlite_session):
|
||||
controller = ToolTransformService.api_provider_to_controller(db_provider)
|
||||
assert controller.entity.identity.author == "Test User"
|
||||
assert [c.name for c in controller.entity.credentials_schema] == ["auth_type"]
|
||||
Reference in new issue
Block a user