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.

refactor(api): enable strict typing for enterprise service tests (#43766)

Eddy ZHANG1ca631c47bc2
diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml
index 27607988998..3539b3c7cd1 100644
--- a/api/tests/unit_tests/pyrefly.toml
+++ b/api/tests/unit_tests/pyrefly.toml
@@ -735,7 +735,6 @@ project-excludes = [
"services/document_service_validation.py",
"services/enterprise/test_account_deletion_sync.py",
"services/enterprise/test_app_permitted_service.py",
- "services/enterprise/test_enterprise_service.py",
"services/enterprise/test_plugin_manager_service.py",
"services/enterprise/test_rbac_service.py",
"services/enterprise/test_traceparent_propagation.py",
diff --git a/api/tests/unit_tests/services/enterprise/test_enterprise_service.py b/api/tests/unit_tests/services/enterprise/test_enterprise_service.py
index e37ffc8d425..2f478249589 100644
--- a/api/tests/unit_tests/services/enterprise/test_enterprise_service.py
+++ b/api/tests/unit_tests/services/enterprise/test_enterprise_service.py
@@ -5,6 +5,7 @@ Covers:
- License status caching (get_cached_license_status)
"""
+from collections.abc import Callable
from datetime import datetime
from unittest.mock import patch
@@ -34,7 +35,7 @@ MODULE = "services.enterprise.enterprise_service"
class TestEnterpriseServiceInfo:
- def test_get_info_delegates(self):
+ def test_get_info_delegates(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = {"version": "1.0"}
result = EnterpriseService.get_info()
@@ -42,7 +43,7 @@ class TestEnterpriseServiceInfo:
req.send_request.assert_called_once_with("GET", "/info")
assert result == {"version": "1.0"}
- def test_get_workspace_info_delegates(self):
+ def test_get_workspace_info_delegates(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = {"name": "ws"}
result = EnterpriseService.get_workspace_info("tenant-1")
@@ -52,7 +53,7 @@ class TestEnterpriseServiceInfo:
class TestSsoSettingsLastUpdateTime:
- def test_app_sso_parses_valid_timestamp(self):
+ def test_app_sso_parses_valid_timestamp(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = "2025-01-15T10:30:00+00:00"
result = EnterpriseService.get_app_sso_settings_last_update_time()
@@ -60,26 +61,26 @@ class TestSsoSettingsLastUpdateTime:
assert isinstance(result, datetime)
assert result.year == 2025
- def test_app_sso_raises_on_empty(self):
+ def test_app_sso_raises_on_empty(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = ""
with pytest.raises(ValueError, match="No data found"):
EnterpriseService.get_app_sso_settings_last_update_time()
- def test_app_sso_raises_on_invalid_format(self):
+ def test_app_sso_raises_on_invalid_format(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = "not-a-date"
with pytest.raises(ValueError, match="Invalid date format"):
EnterpriseService.get_app_sso_settings_last_update_time()
- def test_workspace_sso_parses_valid_timestamp(self):
+ def test_workspace_sso_parses_valid_timestamp(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = "2025-06-01T00:00:00+00:00"
result = EnterpriseService.get_workspace_sso_settings_last_update_time()
assert isinstance(result, datetime)
- def test_workspace_sso_raises_on_empty(self):
+ def test_workspace_sso_raises_on_empty(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = None
with pytest.raises(ValueError, match="No data found"):
@@ -87,23 +88,23 @@ class TestSsoSettingsLastUpdateTime:
class TestWorkspacePermissionService:
- def test_raises_on_empty_workspace_id(self):
+ def test_raises_on_empty_workspace_id(self) -> None:
with pytest.raises(ValueError, match="workspace_id must be provided"):
EnterpriseService.WorkspacePermissionService.get_permission("")
- def test_raises_on_missing_data(self):
+ def test_raises_on_missing_data(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = None
with pytest.raises(ValueError, match="No data found"):
EnterpriseService.WorkspacePermissionService.get_permission("ws-1")
- def test_raises_on_missing_permission_key(self):
+ def test_raises_on_missing_permission_key(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = {"other": "data"}
with pytest.raises(ValueError, match="No data found"):
EnterpriseService.WorkspacePermissionService.get_permission("ws-1")
- def test_returns_parsed_permission(self):
+ def test_returns_parsed_permission(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = {
"permission": {
@@ -121,30 +122,30 @@ class TestWorkspacePermissionService:
class TestWebAppAuth:
- def test_is_user_allowed_returns_result_field(self):
+ def test_is_user_allowed_returns_result_field(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = {"result": True}
assert EnterpriseService.WebAppAuth.is_user_allowed_to_access_webapp("u1", "a1") is True
- def test_is_user_allowed_defaults_false(self):
+ def test_is_user_allowed_defaults_false(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
- req.send_request.return_value = {}
+ req.send_request.return_value = dict[str, object]()
assert EnterpriseService.WebAppAuth.is_user_allowed_to_access_webapp("u1", "a1") is False
- def test_batch_is_user_allowed_returns_empty_for_no_apps(self):
+ def test_batch_is_user_allowed_returns_empty_for_no_apps(self) -> None:
assert EnterpriseService.WebAppAuth.batch_is_user_allowed_to_access_webapps("u1", []) == {}
- def test_batch_is_user_allowed_raises_on_empty_response(self):
+ def test_batch_is_user_allowed_raises_on_empty_response(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = None
with pytest.raises(ValueError, match="No data found"):
EnterpriseService.WebAppAuth.batch_is_user_allowed_to_access_webapps("u1", ["a1"])
- def test_get_app_access_mode_raises_on_empty_app_id(self):
+ def test_get_app_access_mode_raises_on_empty_app_id(self) -> None:
with pytest.raises(ValueError, match="app_id must be provided"):
EnterpriseService.WebAppAuth.get_app_access_mode_by_id("")
- def test_get_app_access_mode_returns_settings(self):
+ def test_get_app_access_mode_returns_settings(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = {"accessMode": "public"}
result = EnterpriseService.WebAppAuth.get_app_access_mode_by_id("a1")
@@ -152,16 +153,16 @@ class TestWebAppAuth:
assert isinstance(result, WebAppSettings)
assert result.access_mode == "public"
- def test_get_app_access_mode_raises_service_error_on_empty_response(self):
+ def test_get_app_access_mode_raises_service_error_on_empty_response(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = None
with pytest.raises(EnterpriseServiceError, match="No data found"):
EnterpriseService.WebAppAuth.get_app_access_mode_by_id("a1")
- def test_batch_get_returns_empty_for_no_apps(self):
+ def test_batch_get_returns_empty_for_no_apps(self) -> None:
assert EnterpriseService.WebAppAuth.batch_get_app_access_mode_by_id([]) == {}
- def test_batch_get_maps_access_modes(self):
+ def test_batch_get_maps_access_modes(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = {"accessModes": {"a1": "public", "a2": "private"}}
result = EnterpriseService.WebAppAuth.batch_get_app_access_mode_by_id(["a1", "a2"])
@@ -169,21 +170,21 @@ class TestWebAppAuth:
assert result["a1"].access_mode == "public"
assert result["a2"].access_mode == "private"
- def test_batch_get_raises_on_invalid_format(self):
+ def test_batch_get_raises_on_invalid_format(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = {"accessModes": "not-a-dict"}
with pytest.raises(ValueError, match="Invalid data format"):
EnterpriseService.WebAppAuth.batch_get_app_access_mode_by_id(["a1"])
- def test_update_access_mode_raises_on_empty_app_id(self):
+ def test_update_access_mode_raises_on_empty_app_id(self) -> None:
with pytest.raises(ValueError, match="app_id must be provided"):
EnterpriseService.WebAppAuth.update_app_access_mode("", "public")
- def test_update_access_mode_raises_on_invalid_mode(self):
+ def test_update_access_mode_raises_on_invalid_mode(self) -> None:
with pytest.raises(ValueError, match="access_mode must be"):
EnterpriseService.WebAppAuth.update_app_access_mode("a1", "invalid")
- def test_update_access_mode_delegates_and_returns(self):
+ def test_update_access_mode_delegates_and_returns(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = {"result": True}
result = EnterpriseService.WebAppAuth.update_app_access_mode("a1", "public")
@@ -193,19 +194,19 @@ class TestWebAppAuth:
"POST", "/webapp/access-mode", json={"appId": "a1", "accessMode": "public"}
)
- def test_cleanup_webapp_raises_on_empty_app_id(self):
+ def test_cleanup_webapp_raises_on_empty_app_id(self) -> None:
with pytest.raises(ValueError, match="app_id must be provided"):
EnterpriseService.WebAppAuth.cleanup_webapp("")
- def test_cleanup_webapp_delegates(self):
+ def test_cleanup_webapp_delegates(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
EnterpriseService.WebAppAuth.cleanup_webapp("a1")
req.send_request.assert_called_once_with("DELETE", "/webapp/clean", params={"appId": "a1"})
- def test_list_externally_accessible_apps_minimal_call(self):
+ def test_list_externally_accessible_apps_minimal_call(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
- req.send_request.return_value = {"data": [], "total": 0, "hasMore": False}
+ req.send_request.return_value = {"data": list[object](), "total": 0, "hasMore": False}
result = EnterpriseService.WebAppAuth.list_externally_accessible_apps(page=1, limit=10)
assert result == {"data": [], "total": 0, "hasMore": False}
@@ -216,9 +217,9 @@ class TestWebAppAuth:
timeout=5.0,
)
- def test_list_externally_accessible_apps_with_filters(self):
+ def test_list_externally_accessible_apps_with_filters(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
- req.send_request.return_value = {"data": [], "total": 0, "hasMore": False}
+ req.send_request.return_value = {"data": list[object](), "total": 0, "hasMore": False}
EnterpriseService.WebAppAuth.list_externally_accessible_apps(page=2, limit=5, mode="workflow", name="alpha")
req.send_request.assert_called_once_with(
@@ -230,7 +231,7 @@ class TestWebAppAuth:
class TestJoinDefaultWorkspace:
- def test_join_default_workspace_success(self):
+ def test_join_default_workspace_success(self) -> None:
account_id = "11111111-1111-1111-1111-111111111111"
response = {"workspace_id": "22222222-2222-2222-2222-222222222222", "joined": True, "message": "ok"}
@@ -251,7 +252,7 @@ class TestJoinDefaultWorkspace:
timeout=1.0,
)
- def test_join_default_workspace_invalid_response_format_raises(self):
+ def test_join_default_workspace_invalid_response_format_raises(self) -> None:
account_id = "11111111-1111-1111-1111-111111111111"
with patch("services.enterprise.enterprise_service.EnterpriseRequest.send_request") as mock_send_request:
@@ -260,11 +261,11 @@ class TestJoinDefaultWorkspace:
with pytest.raises(ValueError, match="Invalid response format"):
EnterpriseService.join_default_workspace(account_id=account_id)
- def test_join_default_workspace_invalid_account_id_raises(self):
+ def test_join_default_workspace_invalid_account_id_raises(self) -> None:
with pytest.raises(ValueError):
EnterpriseService.join_default_workspace(account_id="not-a-uuid")
- def test_join_default_workspace_missing_required_fields_raises(self):
+ def test_join_default_workspace_missing_required_fields_raises(self) -> None:
account_id = "11111111-1111-1111-1111-111111111111"
response = {"workspace_id": "", "message": "ok"} # missing "joined"
@@ -274,24 +275,26 @@ class TestJoinDefaultWorkspace:
with pytest.raises(ValueError, match="Invalid response payload"):
EnterpriseService.join_default_workspace(account_id=account_id)
- def test_join_default_workspace_joined_without_workspace_id_raises(self):
+ def test_join_default_workspace_joined_without_workspace_id_raises(self) -> None:
with pytest.raises(ValueError, match="workspace_id must be non-empty when joined is True"):
DefaultWorkspaceJoinResult(workspace_id="", joined=True, message="ok")
class TestTryJoinDefaultWorkspace:
@pytest.fixture(autouse=True)
- def _enterprise_edition(self, config_overrides) -> None:
+ def _enterprise_edition(self, config_overrides: Callable[..., None]) -> None:
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.ENTERPRISE)
- def test_try_join_default_workspace_non_enterprise_edition_noop(self, config_overrides):
+ def test_try_join_default_workspace_non_enterprise_edition_noop(
+ self, config_overrides: Callable[..., None]
+ ) -> None:
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY)
with patch("services.enterprise.enterprise_service.EnterpriseService.join_default_workspace") as mock_join:
try_join_default_workspace("11111111-1111-1111-1111-111111111111")
mock_join.assert_not_called()
- def test_try_join_default_workspace_successful_join_does_not_raise(self):
+ def test_try_join_default_workspace_successful_join_does_not_raise(self) -> None:
account_id = "11111111-1111-1111-1111-111111111111"
with patch("services.enterprise.enterprise_service.EnterpriseService.join_default_workspace") as mock_join:
@@ -306,7 +309,7 @@ class TestTryJoinDefaultWorkspace:
mock_join.assert_called_once_with(account_id=account_id)
- def test_try_join_default_workspace_skipped_join_does_not_raise(self):
+ def test_try_join_default_workspace_skipped_join_does_not_raise(self) -> None:
account_id = "11111111-1111-1111-1111-111111111111"
with patch("services.enterprise.enterprise_service.EnterpriseService.join_default_workspace") as mock_join:
@@ -321,7 +324,7 @@ class TestTryJoinDefaultWorkspace:
mock_join.assert_called_once_with(account_id=account_id)
- def test_try_join_default_workspace_api_failure_soft_fails(self):
+ def test_try_join_default_workspace_api_failure_soft_fails(self) -> None:
account_id = "11111111-1111-1111-1111-111111111111"
with patch("services.enterprise.enterprise_service.EnterpriseService.join_default_workspace") as mock_join:
@@ -332,7 +335,7 @@ class TestTryJoinDefaultWorkspace:
mock_join.assert_called_once_with(account_id=account_id)
- def test_try_join_default_workspace_invalid_account_id_soft_fails(self):
+ def test_try_join_default_workspace_invalid_account_id_soft_fails(self) -> None:
# Should not raise even though UUID parsing fails inside join_default_workspace
try_join_default_workspace("not-a-uuid")
@@ -348,14 +351,14 @@ class TestGetCachedLicenseStatus:
"""Tests for EnterpriseService.get_cached_license_status."""
@pytest.fixture(autouse=True)
- def _enterprise_edition(self, config_overrides) -> None:
+ def _enterprise_edition(self, config_overrides: Callable[..., None]) -> None:
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.ENTERPRISE)
- def test_returns_none_outside_enterprise_edition(self, config_overrides):
+ def test_returns_none_outside_enterprise_edition(self, config_overrides: Callable[..., None]) -> None:
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY)
assert EnterpriseService.get_cached_license_status() is None
- def test_cache_hit_returns_license_status_enum(self):
+ def test_cache_hit_returns_license_status_enum(self) -> None:
with (
patch(f"{_EE_SVC}.redis_client") as mock_redis,
patch.object(EnterpriseService, "get_info") as mock_get_info,
@@ -368,7 +371,7 @@ class TestGetCachedLicenseStatus:
assert isinstance(result, LicenseStatus)
mock_get_info.assert_not_called()
- def test_cache_miss_fetches_api_and_caches_valid_status(self):
+ def test_cache_miss_fetches_api_and_caches_valid_status(self) -> None:
with (
patch(f"{_EE_SVC}.redis_client") as mock_redis,
patch.object(EnterpriseService, "get_info") as mock_get_info,
@@ -383,7 +386,7 @@ class TestGetCachedLicenseStatus:
LICENSE_STATUS_CACHE_KEY, VALID_LICENSE_CACHE_TTL, LicenseStatus.ACTIVE
)
- def test_cache_miss_fetches_api_and_caches_invalid_status_with_short_ttl(self):
+ def test_cache_miss_fetches_api_and_caches_invalid_status_with_short_ttl(self) -> None:
with (
patch(f"{_EE_SVC}.redis_client") as mock_redis,
patch.object(EnterpriseService, "get_info") as mock_get_info,
@@ -398,7 +401,7 @@ class TestGetCachedLicenseStatus:
LICENSE_STATUS_CACHE_KEY, INVALID_LICENSE_CACHE_TTL, LicenseStatus.EXPIRED
)
- def test_redis_read_failure_falls_through_to_api(self):
+ def test_redis_read_failure_falls_through_to_api(self) -> None:
with (
patch(f"{_EE_SVC}.redis_client") as mock_redis,
patch.object(EnterpriseService, "get_info") as mock_get_info,
@@ -411,7 +414,7 @@ class TestGetCachedLicenseStatus:
assert result == LicenseStatus.ACTIVE
mock_get_info.assert_called_once()
- def test_redis_write_failure_still_returns_status(self):
+ def test_redis_write_failure_still_returns_status(self) -> None:
with (
patch(f"{_EE_SVC}.redis_client") as mock_redis,
patch.object(EnterpriseService, "get_info") as mock_get_info,
@@ -424,7 +427,7 @@ class TestGetCachedLicenseStatus:
assert result == LicenseStatus.EXPIRING
- def test_api_failure_returns_none(self):
+ def test_api_failure_returns_none(self) -> None:
with (
patch(f"{_EE_SVC}.redis_client") as mock_redis,
patch.object(EnterpriseService, "get_info") as mock_get_info,
@@ -434,13 +437,13 @@ class TestGetCachedLicenseStatus:
assert EnterpriseService.get_cached_license_status() is None
- def test_api_returns_no_license_info(self):
+ def test_api_returns_no_license_info(self) -> None:
with (
patch(f"{_EE_SVC}.redis_client") as mock_redis,
patch.object(EnterpriseService, "get_info") as mock_get_info,
):
mock_redis.get.return_value = None
- mock_get_info.return_value = {} # no "License" key
+ mock_get_info.return_value = dict[str, object]() # no "License" key
assert EnterpriseService.get_cached_license_status() is None
mock_redis.setex.assert_not_called()
@@ -455,7 +458,7 @@ class TestIssueMCPToken:
"""
@staticmethod
- def _call():
+ def _call() -> tuple[str, int]:
return EnterpriseService.issue_mcp_token(
user_id="user-uuid",
tenant_id="tenant-uuid",
@@ -463,7 +466,7 @@ class TestIssueMCPToken:
audience="https://mcp.example.com/mcp/",
)
- def test_happy_path_returns_token_and_expiry(self):
+ def test_happy_path_returns_token_and_expiry(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = {"token": "abc.def.ghi", "expires_at": 1900000000}
token, exp = self._call()
@@ -482,7 +485,7 @@ class TestIssueMCPToken:
},
)
- def test_end_user_type_is_forwarded(self):
+ def test_end_user_type_is_forwarded(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = {"token": "t", "expires_at": 1900000000}
EnterpriseService.issue_mcp_token(
@@ -496,13 +499,13 @@ class TestIssueMCPToken:
assert body["user_type"] == "end_user"
assert body["app_id"] == "app-uuid"
- def test_401_maps_to_identity_refresh_error(self):
+ def test_401_maps_to_identity_refresh_error(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.side_effect = EnterpriseAPIUnauthorizedError("refresh rejected by IdP")
with pytest.raises(MCPIdentityRefreshError, match="refresh rejected"):
self._call()
- def test_428_maps_to_no_refresh_token_error(self):
+ def test_428_maps_to_no_refresh_token_error(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
# 428 PreconditionRequired is what EE returns when there's no
# stored SSO refresh token for the user.
@@ -510,31 +513,31 @@ class TestIssueMCPToken:
with pytest.raises(MCPNoRefreshTokenError, match="SSO"):
self._call()
- def test_403_maps_to_identity_refresh_error_for_license(self):
+ def test_403_maps_to_identity_refresh_error_for_license(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.side_effect = EnterpriseAPIForbiddenError("not licensed for MCP forwarding")
with pytest.raises(MCPIdentityRefreshError, match="not licensed"):
self._call()
- def test_other_status_maps_to_generic_token_error(self):
+ def test_other_status_maps_to_generic_token_error(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.side_effect = EnterpriseAPIError("upstream 502", status_code=502)
with pytest.raises(MCPTokenError, match="status=502"):
self._call()
- def test_malformed_response_shape_raises_token_error(self):
+ def test_malformed_response_shape_raises_token_error(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = "not-a-dict"
with pytest.raises(MCPTokenError, match="invalid response shape"):
self._call()
- def test_missing_token_field_raises_token_error(self):
+ def test_missing_token_field_raises_token_error(self) -> None:
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = {"expires_at": 1700000000} # no token
with pytest.raises(MCPTokenError, match="missing or non-string token"):
self._call()
- def test_float_expires_at_is_accepted(self):
+ def test_float_expires_at_is_accepted(self) -> None:
"""expires_at may arrive as float (time.time()) — must be coerced."""
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = {"token": "t", "expires_at": 1900000000.5}
@@ -543,7 +546,7 @@ class TestIssueMCPToken:
assert exp == 1900000000
assert isinstance(exp, int)
- def test_bool_expires_at_is_rejected(self):
+ def test_bool_expires_at_is_rejected(self) -> None:
"""bool is a subclass of int — must NOT be accepted as expires_at."""
with patch(f"{MODULE}.EnterpriseRequest") as req:
req.send_request.return_value = {"token": "t", "expires_at": True}