mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
test: use real token and permission fixtures
This commit is contained in:
1 parent
841b5e7610
commit
3cfcd8d73a
2 files changed
+16
-17
No files matched your search
@@ -4,8 +4,10 @@ Unit tests for API Token Cache module.
|
||||
|
||||
import json
|
||||
from datetime import datetime
|
||||
from unittest.mock import MagicMock, patch
|
||||
from unittest.mock import patch
|
||||
|
||||
from models.enums import ApiTokenType
|
||||
from models.model import ApiToken
|
||||
from services.api_token_service import (
|
||||
CACHE_KEY_PREFIX,
|
||||
CACHE_NULL_TTL_SECONDS,
|
||||
@@ -20,11 +22,11 @@ class TestApiTokenCache:
|
||||
|
||||
def setup_method(self):
|
||||
"""Setup test fixtures."""
|
||||
self.mock_token = MagicMock()
|
||||
self.mock_token = ApiToken()
|
||||
self.mock_token.id = "test-token-id-123"
|
||||
self.mock_token.app_id = "test-app-id-456"
|
||||
self.mock_token.tenant_id = "test-tenant-id-789"
|
||||
self.mock_token.type = "app"
|
||||
self.mock_token.type = ApiTokenType.APP
|
||||
self.mock_token.token = "test-token-value-abc"
|
||||
self.mock_token.last_used_at = datetime(2026, 2, 3, 10, 0, 0)
|
||||
self.mock_token.created_at = datetime(2026, 1, 1, 0, 0, 0)
|
||||
@@ -54,11 +56,11 @@ class TestApiTokenCache:
|
||||
|
||||
def test_serialize_token_with_nulls(self):
|
||||
"""Test token serialization with None values."""
|
||||
mock_token = MagicMock()
|
||||
mock_token = ApiToken()
|
||||
mock_token.id = "test-id"
|
||||
mock_token.app_id = None
|
||||
mock_token.tenant_id = None
|
||||
mock_token.type = "dataset"
|
||||
mock_token.type = ApiTokenType.DATASET
|
||||
mock_token.token = "test-token"
|
||||
mock_token.last_used_at = None
|
||||
mock_token.created_at = datetime(2026, 1, 1, 0, 0, 0)
|
||||
@@ -213,12 +215,12 @@ class TestApiTokenCacheIntegration:
|
||||
@patch("services.api_token_service.redis_client")
|
||||
def test_full_cache_lifecycle(self, mock_redis):
|
||||
"""Test complete cache lifecycle: set -> get -> delete."""
|
||||
# Setup mock token
|
||||
mock_token = MagicMock()
|
||||
# Use the persisted model's fields for the cache lifecycle.
|
||||
mock_token = ApiToken()
|
||||
mock_token.id = "id-123"
|
||||
mock_token.app_id = "app-456"
|
||||
mock_token.tenant_id = "tenant-789"
|
||||
mock_token.type = "app"
|
||||
mock_token.type = ApiTokenType.APP
|
||||
mock_token.token = "token-abc"
|
||||
mock_token.last_used_at = datetime(2026, 2, 3, 10, 0, 0)
|
||||
mock_token.created_at = datetime(2026, 1, 1, 0, 0, 0)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import logging
|
||||
from unittest.mock import Mock, patch
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from werkzeug.exceptions import Forbidden
|
||||
@@ -9,6 +9,7 @@ from libs.workspace_permission import (
|
||||
check_workspace_member_invite_permission,
|
||||
check_workspace_owner_transfer_permission,
|
||||
)
|
||||
from services.enterprise.enterprise_service import WorkspacePermission
|
||||
|
||||
|
||||
class TestWorkspacePermissionHelper:
|
||||
@@ -37,8 +38,7 @@ class TestWorkspacePermissionHelper:
|
||||
"""Enterprise edition should block invitations when workspace policy is False."""
|
||||
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.ENTERPRISE)
|
||||
|
||||
mock_permission = Mock()
|
||||
mock_permission.allow_member_invite = False
|
||||
mock_permission = WorkspacePermission(workspaceId="test-workspace-id", allowMemberInvite=False)
|
||||
mock_enterprise_service.WorkspacePermissionService.get_permission.return_value = mock_permission
|
||||
|
||||
with pytest.raises(Forbidden, match="Workspace policy prohibits member invitations"):
|
||||
@@ -51,8 +51,7 @@ class TestWorkspacePermissionHelper:
|
||||
"""Enterprise edition should allow invitations when workspace policy is True."""
|
||||
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.ENTERPRISE)
|
||||
|
||||
mock_permission = Mock()
|
||||
mock_permission.allow_member_invite = True
|
||||
mock_permission = WorkspacePermission(workspaceId="test-workspace-id", allowMemberInvite=True)
|
||||
mock_enterprise_service.WorkspacePermissionService.get_permission.return_value = mock_permission
|
||||
|
||||
# Should not raise
|
||||
@@ -74,8 +73,7 @@ class TestWorkspacePermissionHelper:
|
||||
def test_enterprise_blocks_transfer_when_disabled(self, mock_enterprise_service, config_overrides):
|
||||
"""Enterprise edition should block transfer when workspace policy is False."""
|
||||
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.ENTERPRISE)
|
||||
mock_permission = Mock()
|
||||
mock_permission.allow_owner_transfer = False # Workspace policy blocks
|
||||
mock_permission = WorkspacePermission(workspaceId="test-workspace-id", allowOwnerTransfer=False)
|
||||
mock_enterprise_service.WorkspacePermissionService.get_permission.return_value = mock_permission
|
||||
|
||||
with pytest.raises(Forbidden, match="Workspace policy prohibits ownership transfer"):
|
||||
@@ -87,8 +85,7 @@ class TestWorkspacePermissionHelper:
|
||||
def test_enterprise_allows_transfer_when_both_enabled(self, mock_enterprise_service, config_overrides):
|
||||
"""Enterprise edition should allow transfer when both billing and workspace policy allow."""
|
||||
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.ENTERPRISE)
|
||||
mock_permission = Mock()
|
||||
mock_permission.allow_owner_transfer = True # Workspace policy allows
|
||||
mock_permission = WorkspacePermission(workspaceId="test-workspace-id", allowOwnerTransfer=True)
|
||||
mock_enterprise_service.WorkspacePermissionService.get_permission.return_value = mock_permission
|
||||
|
||||
# Should not raise
|
||||
|
||||
Reference in new issue
Block a user