Skip to content

armgit / dify

Public Imported from GitHub
0

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.

test: migrate residual user ORM stand-ins to real models (#40625)

Asuka Minato582c73bc3b47
diff --git a/api/tests/unit_tests/controllers/console/workspace/test_plugin.py b/api/tests/unit_tests/controllers/console/workspace/test_plugin.py
index 7e81596d443..88e9487f5dc 100644
--- a/api/tests/unit_tests/controllers/console/workspace/test_plugin.py
+++ b/api/tests/unit_tests/controllers/console/workspace/test_plugin.py
@@ -77,6 +77,7 @@ from models.account import (
TenantAccountRole,
TenantPluginAutoUpgradeCategory,
TenantPluginAutoUpgradeMode,
+ TenantPluginAutoUpgradeStrategy,
TenantPluginAutoUpgradeStrategySetting,
TenantPluginDebugPermission,
TenantPluginInstallPermission,
@@ -1428,7 +1429,7 @@ class TestPluginChangeAutoUpgradeApi:
api = PluginChangeAutoUpgradeApi()
method = unwrap(api.post)
- user = MagicMock(is_admin_or_owner=True)
+ user = _account()
payload = {
"category": TenantPluginAutoUpgradeCategory.TOOL.value,
@@ -1455,7 +1456,8 @@ class TestPluginFetchAutoUpgradeApi:
api = PluginFetchAutoUpgradeApi()
method = unwrap(api.get)
- auto_upgrade = MagicMock(
+ auto_upgrade = TenantPluginAutoUpgradeStrategy(
+ tenant_id="t1",
category=TenantPluginAutoUpgradeCategory.TOOL,
strategy_setting=TenantPluginAutoUpgradeStrategySetting.FIX_ONLY,
upgrade_time_of_day=1,
diff --git a/api/tests/unit_tests/core/datasource/test_file_upload.py b/api/tests/unit_tests/core/datasource/test_file_upload.py
index c6d6dd58085..4319cc3e60f 100644
--- a/api/tests/unit_tests/core/datasource/test_file_upload.py
+++ b/api/tests/unit_tests/core/datasource/test_file_upload.py
@@ -120,7 +120,6 @@ Run with coverage: pytest api/tests/unit_tests/core/datasource/test_file_upload.
import hashlib # For SHA3-256 hashing of file content
import os # For file path operations
import uuid # For generating unique identifiers
-from unittest.mock import Mock # For mocking dependencies
# Third-party imports
import pytest # Testing framework
@@ -686,12 +685,11 @@ class TestUserRoleHandling:
def test_creator_role_detection_account(self):
"""Test creator role detection for Account user."""
# Arrange
- user = Mock()
- user.__class__.__name__ = "Account"
-
- # Act
from models import Account
+ user = Account(name="Test User", email="[email protected]")
+
+ # Act
is_account = isinstance(user, Account) or user.__class__.__name__ == "Account"
role = CreatorUserRole.ACCOUNT if is_account else CreatorUserRole.END_USER
@@ -701,12 +699,17 @@ class TestUserRoleHandling:
def test_creator_role_detection_end_user(self):
"""Test creator role detection for EndUser."""
# Arrange
- user = Mock()
- user.__class__.__name__ = "EndUser"
+ from models import Account, EndUser
+ from models.model import EndUserType
- # Act
- from models import Account
+ user = EndUser(
+ tenant_id="tenant-1",
+ app_id="app-1",
+ type=EndUserType.SERVICE_API,
+ session_id="session-1",
+ )
+ # Act
is_account = isinstance(user, Account) or user.__class__.__name__ == "Account"
role = CreatorUserRole.ACCOUNT if is_account else CreatorUserRole.END_USER
diff --git a/api/tests/unit_tests/services/test_datasource_provider_service.py b/api/tests/unit_tests/services/test_datasource_provider_service.py
index 4db672f393c..6668d9d31c0 100644
--- a/api/tests/unit_tests/services/test_datasource_provider_service.py
+++ b/api/tests/unit_tests/services/test_datasource_provider_service.py
@@ -143,9 +143,9 @@ class TestDatasourceProviderService:
@pytest.fixture
def mock_user(self):
- u = MagicMock()
- u.id = "uid-1"
- return u
+ user = Account(name="Test User", email="[email protected]")
+ user.id = "uid-1"
+ return user
# -----------------------------------------------------------------------
# get_current_user (lines 27-40)
@@ -153,24 +153,26 @@ class TestDatasourceProviderService:
def test_should_return_proxy_when_current_object_is_account(self):
with patch("libs.login.current_user", new_callable=MagicMock) as proxy:
- user_obj = MagicMock()
- user_obj.__class__ = Account
+ user_obj = Account(name="Test User", email="[email protected]")
proxy._get_current_object.return_value = user_obj
assert get_current_user() is proxy
def test_should_return_proxy_when_current_object_is_enduser(self):
with patch("libs.login.current_user", new_callable=MagicMock) as proxy:
- user_obj = MagicMock()
- user_obj.__class__ = EndUser
+ user_obj = EndUser(
+ tenant_id="tenant-1",
+ app_id="app-1",
+ type="service-api",
+ session_id="session-1",
+ )
proxy._get_current_object.return_value = user_obj
assert get_current_user() is proxy
def test_should_return_proxy_when_get_current_object_raises_attribute_error(self):
"""AttributeError from LocalProxy falls back to the proxy itself."""
- with patch("libs.login.current_user", new_callable=MagicMock) as proxy:
- proxy._get_current_object.side_effect = AttributeError("no attr")
- proxy.__class__ = Account # make the proxy itself satisfy isinstance
- assert get_current_user() is proxy
+ account = Account(name="Test User", email="[email protected]")
+ with patch("libs.login.current_user", account):
+ assert get_current_user() is account
def test_should_raise_type_error_when_user_is_not_account_or_enduser(self):
with patch("libs.login.current_user", new_callable=MagicMock) as proxy:
diff --git a/api/tests/unit_tests/services/test_workflow_collaboration_service.py b/api/tests/unit_tests/services/test_workflow_collaboration_service.py
index 33c11f4f193..77e55b545c3 100644
--- a/api/tests/unit_tests/services/test_workflow_collaboration_service.py
+++ b/api/tests/unit_tests/services/test_workflow_collaboration_service.py
@@ -8,6 +8,7 @@ from socketio.exceptions import TimeoutError as SocketIOTimeoutError
from sqlalchemy import Engine
from sqlalchemy.orm import Session
+from models.account import Account, Tenant
from models.base import TypeBase
from models.model import App, AppMode, IconType
from repositories.workflow_collaboration_repository import WorkflowCollaborationRepository
@@ -130,11 +131,12 @@ class TestWorkflowCollaborationService:
def test_repr_and_save_socket_identity(self, service: tuple[WorkflowCollaborationService, Mock, Mock]) -> None:
collaboration_service, _repository, socketio = service
- user = Mock()
+ user = Account(name="Jane", email="[email protected]")
user.id = "u-1"
- user.name = "Jane"
user.avatar = "avatar.png"
- user.current_tenant_id = "t-1"
+ tenant = Tenant(name="Tenant")
+ tenant.id = "t-1"
+ user._current_tenant = tenant
assert "WorkflowCollaborationService" in repr(collaboration_service)
diff --git a/api/tests/unit_tests/services/tools/test_mcp_tools_transform.py b/api/tests/unit_tests/services/tools/test_mcp_tools_transform.py
index aa5f28c0535..2379a9fa92b 100644
--- a/api/tests/unit_tests/services/tools/test_mcp_tools_transform.py
+++ b/api/tests/unit_tests/services/tools/test_mcp_tools_transform.py
@@ -9,16 +9,15 @@ 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 ToolParameter, ToolProviderType
+from models.account import Account
from models.tools import MCPToolProvider
from services.tools.tools_transform_service import ToolTransformService
@pytest.fixture
def mock_user():
- """Provides a mock user object."""
- user = Mock()
- user.name = "Test User"
- return user
+ """Provides a real mapped account returned by the provider lookup."""
+ return Account(name="Test User", email="[email protected]")
@pytest.fixture