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.

71.5 KB
Raw Blame
from unittest.mock import patch

import pytest
from faker import Faker
from sqlalchemy.orm import Session

from enums import CloudPlan, DeploymentEdition
from services.billing_client import billing
from services.entities.feature_entities import (
    FeatureModel,
    KnowledgeRateLimitModel,
    LicenseModel,
    LicenseStatus,
    SSOProtocol,
    SystemFeatureModel,
)
from services.feature_service import FeatureService
from services.system_feature_service import SystemFeatureService
from tests.helpers import billing_builders as builders


class TestFeatureService:
    """Integration tests for FeatureService using testcontainers."""

    @pytest.fixture
    def mock_external_service_dependencies(self):
        """Mock setup for external service dependencies."""
        with (
            patch("services.feature_service.BillingService") as mock_billing_service,
            patch("services.feature_service.EnterpriseService") as mock_enterprise_service,
            patch("services.system_feature_service.EnterpriseService", new=mock_enterprise_service),
        ):
            # Setup default mock returns for BillingService
            mock_billing_service.get_info.return_value = builders.subscription_info(
                subscription=builders.subscription_plan("professional", interval="month", education=True),
                members=billing.SubscriptionQuota(size=5, limit=10),
                apps=billing.SubscriptionQuota(size=3, limit=20),
                vector_space=builders.vector_space(size=2, limit=10),
                documents_upload_quota=billing.SubscriptionQuota(size=15, limit=100),
                annotation_quota_limit=billing.SubscriptionQuota(size=8, limit=50),
                can_replace_logo=True,
                model_load_balancing_enabled=True,
            )

            mock_billing_service.get_knowledge_rate_limit.return_value = billing.GetKnowledgeRateLimitReply(
                subscription_plan="professional", limit=100
            )

            # Setup default mock returns for EnterpriseService
            mock_enterprise_service.get_workspace_info.return_value = {
                "WorkspaceMembers": {"used": 5, "limit": 10, "enabled": True}
            }

            mock_enterprise_service.get_info.return_value = {
                "SSOEnforcedForSignin": True,
                "SSOEnforcedForSigninProtocol": "saml",
                "EnableEmailCodeLogin": True,
                "EnableEmailPasswordLogin": False,
                "IsAllowRegister": False,
                "IsAllowCreateWorkspace": False,
                "Branding": {
                    "applicationTitle": "Test Enterprise",
                    "loginPageLogo": "https://example.com/logo.png",
                    "workspaceLogo": "https://example.com/workspace.png",
                    "favicon": "https://example.com/favicon.ico",
                },
                "WebAppAuth": {"allowSso": True, "allowEmailCodeLogin": True, "allowEmailPasswordLogin": False},
                "SSOEnforcedForWebProtocol": "oidc",
                "EnableAppDeploy": True,
                "License": {
                    "status": "active",
                    "expiredAt": "2025-12-31",
                    "workspaces": {"enabled": True, "limit": 5, "used": 2},
                },
                "PluginInstallationPermission": {
                    "pluginInstallationScope": "official_only",
                    "restrictToMarketplaceOnly": True,
                },
            }

            yield {
                "billing_service": mock_billing_service,
                "enterprise_service": mock_enterprise_service,
            }

    def _create_test_tenant_id(self):
        """Helper method to create a test tenant ID."""
        fake = Faker()
        return fake.uuid4()

    def test_get_features_success(self, db_session_with_containers: Session, mock_external_service_dependencies):
        """
        Test successful feature retrieval for the Cloud edition.

        This test verifies:
        - Proper feature model creation with all required fields
        - Correct integration with billing service
        - Enterprise workspace information remains isolated from Cloud
        - Return value correctness and structure
        """
        # Arrange: Setup test data with proper config mocking
        tenant_id = self._create_test_tenant_id()

        with patch("services.feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.CLOUD
            mock_config.CAN_REPLACE_LOGO = True
            mock_config.MODEL_LB_ENABLED = True
            mock_config.EDUCATION_ENABLED = True

            # Act: Execute the method under test
            result = FeatureService.get_features(tenant_id)

            # Assert: Verify the expected outcomes
            assert result is not None
            assert isinstance(result, FeatureModel)

            # Verify billing features
            assert result.billing.subscription.plan == CloudPlan.PROFESSIONAL
            assert result.billing.subscription.interval == "month"
            assert result.education.activated is True

            # Verify member limitations
            assert result.members.size == 5
            assert result.members.limit == 10

            # Verify app limitations
            assert result.apps.size == 3
            assert result.apps.limit == 20

            # Verify vector space limitations
            assert result.vector_space.size == 2
            assert result.vector_space.limit == 10

            # Verify document upload quota
            assert result.documents_upload_quota.size == 15
            assert result.documents_upload_quota.limit == 100

            # Verify annotation quota
            assert result.annotation_quota_limit.size == 8
            assert result.annotation_quota_limit.limit == 50

            # Verify other features
            assert result.can_replace_logo is True
            assert result.model_load_balancing_enabled is True

            # Enterprise workspace features are not loaded in Cloud.
            assert result.workspace_members.enabled is False
            assert result.workspace_members.size == 0
            assert result.workspace_members.limit == 0

            # Verify webapp copyright is enabled for non-sandbox plans
            assert result.webapp_copyright_enabled is True
            assert result.is_allow_transfer_workspace is True

            # Verify mock interactions
            mock_external_service_dependencies["billing_service"].get_info.assert_called_once_with(tenant_id)
            mock_external_service_dependencies["enterprise_service"].get_workspace_info.assert_not_called()

    def test_get_features_sandbox_plan(self, db_session_with_containers: Session, mock_external_service_dependencies):
        """
        Test feature retrieval for sandbox plan with specific limitations.

        This test verifies:
        - Proper handling of sandbox plan limitations
        - Correct webapp copyright settings for sandbox
        - Transfer workspace restrictions for sandbox plans
        - Proper billing service integration
        """
        # Arrange: Setup sandbox plan mock with proper config
        tenant_id = self._create_test_tenant_id()

        with patch("services.feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.CLOUD
            mock_config.CAN_REPLACE_LOGO = False
            mock_config.MODEL_LB_ENABLED = False
            mock_config.EDUCATION_ENABLED = False

            # Set mock return value inside the patch context
            mock_external_service_dependencies["billing_service"].get_info.return_value = builders.subscription_info(
                subscription=builders.subscription_plan("sandbox", interval="month", education=False),
                members=billing.SubscriptionQuota(size=1, limit=3),
                apps=billing.SubscriptionQuota(size=1, limit=5),
                vector_space=builders.vector_space(size=1, limit=2),
                documents_upload_quota=billing.SubscriptionQuota(size=5, limit=20),
                annotation_quota_limit=billing.SubscriptionQuota(size=2, limit=10),
                can_replace_logo=False,
                model_load_balancing_enabled=False,
            )

            # Act: Execute the method under test
            result = FeatureService.get_features(tenant_id)

        # Assert: Verify sandbox-specific limitations
        assert result.billing.subscription.plan == CloudPlan.SANDBOX
        assert result.education.activated is False

        # Verify sandbox limitations
        assert result.members.size == 1
        assert result.members.limit == 3
        assert result.apps.size == 1
        assert result.apps.limit == 5
        assert result.vector_space.size == 1
        assert result.vector_space.limit == 2
        assert result.documents_upload_quota.size == 5
        assert result.documents_upload_quota.limit == 20
        assert result.annotation_quota_limit.size == 2
        assert result.annotation_quota_limit.limit == 10

        # Verify sandbox-specific restrictions
        assert result.webapp_copyright_enabled is False
        assert result.is_allow_transfer_workspace is False
        assert result.can_replace_logo is False
        assert result.model_load_balancing_enabled is False

        # Verify mock interactions
        mock_external_service_dependencies["billing_service"].get_info.assert_called_once_with(tenant_id)

    def test_get_knowledge_rate_limit_success(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test successful knowledge rate limit retrieval in the Cloud edition.

        This test verifies:
        - Proper knowledge rate limit model creation
        - Correct integration with billing service
        - Proper rate limit configuration
        - Return value correctness and structure
        """
        # Arrange: Setup test data with proper config
        tenant_id = self._create_test_tenant_id()

        with patch("services.feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.CLOUD

            # Act: Execute the method under test
            result = FeatureService.get_knowledge_rate_limit(tenant_id)

            # Assert: Verify the expected outcomes
            assert result is not None
            assert isinstance(result, KnowledgeRateLimitModel)

            # Verify rate limit configuration
            assert result.enabled is True
            assert result.limit == 100
            assert result.subscription_plan == CloudPlan.PROFESSIONAL

            # Verify mock interactions
            mock_external_service_dependencies["billing_service"].get_knowledge_rate_limit.assert_called_once_with(
                tenant_id
            )

    def test_get_system_features_success(self, db_session_with_containers: Session, mock_external_service_dependencies):
        """
        Test successful system features retrieval with enterprise and marketplace enabled.

        This test verifies:
        - Proper system feature model creation
        - Correct integration with enterprise service
        - Proper marketplace configuration
        - Return value correctness and structure
        """
        # Arrange: Setup test data with proper config
        self._create_test_tenant_id()

        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE
            mock_config.MARKETPLACE_ENABLED = True
            mock_config.ENABLE_EMAIL_CODE_LOGIN = True
            mock_config.ENABLE_EMAIL_PASSWORD_LOGIN = True
            mock_config.ENABLE_SOCIAL_OAUTH_LOGIN = False
            mock_config.ALLOW_REGISTER = False
            mock_config.ALLOW_CREATE_WORKSPACE = False
            mock_config.MAIL_TYPE = "smtp"

            # Act: Execute the method under test
            result = SystemFeatureService.get_public_system_features()

        # Assert: Verify the expected outcomes
        assert result is not None
        assert isinstance(result, SystemFeatureModel)

        # Verify enterprise features
        assert result.enable_app_deploy is True
        assert result.branding.enabled is True
        assert result.webapp_auth.enabled is True
        assert result.enable_change_email is False

        # Verify SSO configuration
        assert result.sso_enforced_for_signin is True
        assert result.sso_enforced_for_signin_protocol is SSOProtocol.SAML

        # Verify authentication settings
        assert result.enable_email_code_login is True
        assert result.enable_email_password_login is False
        assert result.is_allow_register is False

        # Verify branding configuration
        assert result.branding.application_title == "Test Enterprise"
        assert result.branding.login_page_logo == "https://example.com/logo.png"
        assert result.branding.workspace_logo == "https://example.com/workspace.png"
        assert result.branding.favicon == "https://example.com/favicon.ico"

        # Verify webapp auth configuration
        assert result.webapp_auth.allow_sso is True
        assert result.webapp_auth.allow_email_code_login is True
        assert result.webapp_auth.allow_email_password_login is False
        assert result.webapp_auth.sso_config.protocol == "oidc"

        # Verify license status (public system-features exposes status only; detail lives on get_license)
        assert result.license.status.value == "active"
        assert not hasattr(result.license, "expired_at")

        # Verify plugin installation permission
        assert result.plugin_installation_permission.plugin_installation_scope == "official_only"
        assert result.plugin_installation_permission.restrict_to_marketplace_only is True

        # Verify marketplace configuration
        assert result.enable_marketplace is True

        # Verify mock interactions
        mock_external_service_dependencies["enterprise_service"].get_info.assert_called_once()

    def test_get_system_features_unauthenticated(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test system features retrieval for an unauthenticated user.

        This test verifies that:
        - The response payload is minimized (e.g., verbose license details are excluded).
        - Essential UI configuration (Branding, SSO, Marketplace) remains available.
        - The response structure adheres to the public schema for unauthenticated clients.
        """
        # Arrange: Setup test data with exact same config as success test
        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE
            mock_config.MARKETPLACE_ENABLED = True
            mock_config.ENABLE_EMAIL_CODE_LOGIN = True
            mock_config.ENABLE_EMAIL_PASSWORD_LOGIN = True
            mock_config.ENABLE_SOCIAL_OAUTH_LOGIN = False
            mock_config.ALLOW_REGISTER = False
            mock_config.ALLOW_CREATE_WORKSPACE = False
            mock_config.MAIL_TYPE = "smtp"
            mock_config.PLUGIN_MAX_PACKAGE_SIZE = 100

            # Act: Execute the public (unauthenticated) system-features call
            result = SystemFeatureService.get_public_system_features()

        # Assert: Basic structure
        assert result is not None
        assert isinstance(result, SystemFeatureModel)

        # --- 1. Verify only license *status* is exposed to unauthenticated clients ---
        # Detailed license info (expiry, workspaces) is not part of the public model.
        assert result.license.status == LicenseStatus.ACTIVE
        assert not hasattr(result.license, "expired_at")
        assert not hasattr(result.license, "workspaces")

        # --- 2. Verify Public UI Configuration Availability ---
        # Ensure that data required for frontend rendering remains accessible.

        # Branding should match the mock data
        assert result.enable_app_deploy is True
        assert result.branding.enabled is True
        assert result.branding.application_title == "Test Enterprise"
        assert result.branding.login_page_logo == "https://example.com/logo.png"

        # SSO settings should be visible for login page rendering
        assert result.sso_enforced_for_signin is True
        assert result.sso_enforced_for_signin_protocol is SSOProtocol.SAML

        # General auth settings should be visible
        assert result.enable_email_code_login is True

        # Marketplace should be visible
        assert result.enable_marketplace is True

    def test_get_license_returns_full_detail(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test the authenticated license accessor.

        This test verifies that:
        - get_license() returns the full license payload (status, expiry, workspace usage).
        - Detail withheld from the public system-features model is present here.
        """
        # Arrange
        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE

            # Act
            result = SystemFeatureService.get_license()

        # Assert: full license detail is populated
        assert isinstance(result, LicenseModel)
        assert result.status == LicenseStatus.ACTIVE
        assert result.expired_at == "2025-12-31"
        assert result.workspaces.enabled is True
        assert result.workspaces.limit == 5
        assert result.workspaces.size == 2
        mock_external_service_dependencies["enterprise_service"].get_info.assert_called_once()

    def test_get_license_non_enterprise_is_unconstrained(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """Non-enterprise deployments have no license, so limits are unconstrained."""
        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.COMMUNITY

            result = SystemFeatureService.get_license()

        assert isinstance(result, LicenseModel)
        assert result.status == LicenseStatus.NONE
        assert result.workspaces.is_available() is True
        assert result.seats.is_available() is True
        mock_external_service_dependencies["enterprise_service"].get_info.assert_not_called()

    def test_get_system_features_basic_config(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test system features retrieval with basic configuration (no enterprise).

        This test verifies:
        - Proper system feature model creation without enterprise
        - Correct environment variable handling
        - Default configuration values
        - Return value correctness and structure
        """
        # Arrange: Setup basic config mock (no enterprise)
        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.COMMUNITY
            mock_config.MARKETPLACE_ENABLED = False
            mock_config.ENABLE_EMAIL_CODE_LOGIN = True
            mock_config.ENABLE_EMAIL_PASSWORD_LOGIN = True
            mock_config.ENABLE_SOCIAL_OAUTH_LOGIN = False
            mock_config.ENABLE_CHANGE_EMAIL = True
            mock_config.ALLOW_REGISTER = True
            mock_config.ALLOW_CREATE_WORKSPACE = True
            mock_config.MAIL_TYPE = "smtp"
            mock_config.PLUGIN_MAX_PACKAGE_SIZE = 100

            # Act: Execute the method under test
            result = SystemFeatureService.get_public_system_features()

            # Assert: Verify the expected outcomes
            assert result is not None
            assert isinstance(result, SystemFeatureModel)

            # Verify basic configuration
            assert result.enable_app_deploy is False
            assert result.branding.enabled is False
            assert result.webapp_auth.enabled is False
            assert result.enable_change_email is True

            # Verify authentication settings from config
            assert result.enable_email_code_login is True
            assert result.enable_email_password_login is True
            assert result.enable_social_oauth_login is False
            assert result.is_allow_register is True
            assert result.is_email_setup is True

            # Verify marketplace configuration
            assert result.enable_marketplace is False

    def test_get_features_community_edition(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test feature retrieval for the Community edition.

        This test verifies:
        - Proper feature model creation without billing
        - Correct environment variable handling
        - Default configuration values
        - Return value correctness and structure
        """
        # Arrange: Use the Community edition.
        with patch("services.feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.COMMUNITY
            mock_config.CAN_REPLACE_LOGO = True
            mock_config.MODEL_LB_ENABLED = True
            mock_config.EDUCATION_ENABLED = True

            tenant_id = self._create_test_tenant_id()

            # Act: Execute the method under test
            result = FeatureService.get_features(tenant_id)

            # Assert: Verify the expected outcomes
            assert result is not None
            assert isinstance(result, FeatureModel)

            # Verify environment-based features
            assert result.can_replace_logo is True
            assert result.model_load_balancing_enabled is True
            # CLOUD-only: the Console education endpoints 404 on every other edition.
            assert result.education.enabled is False

            # Verify default limitations
            assert result.members.size == 0
            assert result.members.limit == 1
            assert result.apps.size == 0
            assert result.apps.limit == 10
            assert result.vector_space.size == 0
            assert result.vector_space.limit == 5
            assert result.documents_upload_quota.size == 0
            assert result.documents_upload_quota.limit == 50
            assert result.annotation_quota_limit.size == 0
            assert result.annotation_quota_limit.limit == 10

            # Verify no enterprise features
            assert result.workspace_members.enabled is False
            assert result.webapp_copyright_enabled is False

    def test_get_knowledge_rate_limit_community_edition(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test knowledge rate limit retrieval for the Community edition.

        This test verifies:
        - Proper knowledge rate limit model creation without billing
        - Default rate limit configuration
        - Return value correctness and structure
        """
        # Arrange: Use the Community edition.
        with patch("services.feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.COMMUNITY

            tenant_id = self._create_test_tenant_id()

            # Act: Execute the method under test
            result = FeatureService.get_knowledge_rate_limit(tenant_id)

            # Assert: Verify the expected outcomes
            assert result is not None
            assert isinstance(result, KnowledgeRateLimitModel)

            # Verify default configuration
            assert result.enabled is False
            assert result.limit == 10
            assert result.subscription_plan == ""

            # Verify no billing service calls
            mock_external_service_dependencies["billing_service"].get_knowledge_rate_limit.assert_not_called()

    def test_get_features_enterprise_only(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test feature retrieval for the Enterprise edition.

        This test verifies:
        - Proper feature model creation with enterprise only
        - Correct enterprise service integration
        - Proper workspace member handling
        - Return value correctness and structure
        """
        # Arrange: Setup enterprise only mock
        with patch("services.feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE
            mock_config.CAN_REPLACE_LOGO = False
            mock_config.MODEL_LB_ENABLED = False
            mock_config.EDUCATION_ENABLED = False

            tenant_id = self._create_test_tenant_id()

            # Act: Execute the method under test
            result = FeatureService.get_features(tenant_id)

            # Assert: Verify the expected outcomes
            assert result is not None
            assert isinstance(result, FeatureModel)

            # Verify enterprise features
            assert result.webapp_copyright_enabled is True

            # Verify workspace members from enterprise
            assert result.workspace_members.enabled is True
            assert result.workspace_members.size == 5
            assert result.workspace_members.limit == 10

            # Verify environment-based features
            assert result.can_replace_logo is False
            assert result.model_load_balancing_enabled is False
            assert result.education.enabled is False

            # Verify default limitations
            assert result.members.size == 0
            assert result.members.limit == 1
            assert result.apps.size == 0
            assert result.apps.limit == 10
            assert result.vector_space.size == 0
            assert result.vector_space.limit == 5

            # Verify mock interactions
            mock_external_service_dependencies["enterprise_service"].get_workspace_info.assert_called_once_with(
                tenant_id
            )
            mock_external_service_dependencies["billing_service"].get_info.assert_not_called()

    def test_get_system_features_community_edition(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test system features retrieval for the Community edition.

        This test verifies:
        - Proper system feature model creation without enterprise
        - Correct environment variable handling
        - Default configuration values
        - Return value correctness and structure
        """
        # Arrange: Use the Community edition.
        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.COMMUNITY
            mock_config.MARKETPLACE_ENABLED = True
            mock_config.ENABLE_EMAIL_CODE_LOGIN = False
            mock_config.ENABLE_EMAIL_PASSWORD_LOGIN = True
            mock_config.ENABLE_SOCIAL_OAUTH_LOGIN = True
            mock_config.ENABLE_CHANGE_EMAIL = True
            mock_config.ALLOW_REGISTER = False
            mock_config.ALLOW_CREATE_WORKSPACE = False
            mock_config.MAIL_TYPE = None
            mock_config.PLUGIN_MAX_PACKAGE_SIZE = 50

            # Act: Execute the method under test
            result = SystemFeatureService.get_public_system_features()

            # Assert: Verify the expected outcomes
            assert result is not None
            assert isinstance(result, SystemFeatureModel)

            # Verify enterprise features are disabled
            assert result.enable_app_deploy is False
            assert result.branding.enabled is False
            assert result.webapp_auth.enabled is False
            assert result.enable_change_email is True

            # Verify authentication settings from config
            assert result.enable_email_code_login is False
            assert result.enable_email_password_login is True
            assert result.enable_social_oauth_login is True
            assert result.is_allow_register is False
            assert result.is_email_setup is False

            # Verify marketplace configuration
            assert result.enable_marketplace is True

            # Verify default license status
            assert result.license.status == "none"
            assert not hasattr(result.license, "expired_at")
            assert not hasattr(result.license, "workspaces")

            # Verify no enterprise service calls
            mock_external_service_dependencies["enterprise_service"].get_info.assert_not_called()

    def test_get_features_no_tenant_id(self, db_session_with_containers: Session, mock_external_service_dependencies):
        """
        Test Cloud feature retrieval without a tenant ID.

        This test verifies:
        - Proper feature model creation without tenant ID
        - Billing data is not loaded without a tenant ID
        - Default configuration values
        - Return value correctness and structure
        """
        # Arrange: Setup no tenant ID scenario
        with patch("services.feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.CLOUD
            mock_config.CAN_REPLACE_LOGO = True
            mock_config.MODEL_LB_ENABLED = False
            mock_config.EDUCATION_ENABLED = False

            # Act: Execute the method under test
            result = FeatureService.get_features("")

            # Assert: Verify the expected outcomes
            assert result is not None
            assert isinstance(result, FeatureModel)

            # Verify environment-based features
            assert result.can_replace_logo is True
            assert result.model_load_balancing_enabled is False
            assert result.education.enabled is False

            # Verify default limitations
            assert result.members.size == 0
            assert result.members.limit == 1
            assert result.apps.size == 0
            assert result.apps.limit == 10
            assert result.vector_space.size == 0
            assert result.vector_space.limit == 5

            # Verify no billing service calls
            mock_external_service_dependencies["billing_service"].get_info.assert_not_called()

    def test_get_features_edge_case_vector_space(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test feature retrieval with edge case vector space configuration.

        This test verifies:
        - Proper handling of vector space quota limits
        - Correct integration with billing service
        - Proper fallback to default values
        - Return value correctness and structure
        """
        # Arrange: Setup edge case vector space mock with proper config
        tenant_id = self._create_test_tenant_id()

        with patch("services.feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.CLOUD
            mock_config.CAN_REPLACE_LOGO = True
            mock_config.MODEL_LB_ENABLED = False
            mock_config.EDUCATION_ENABLED = False

            mock_external_service_dependencies["billing_service"].get_info.return_value = builders.subscription_info(
                subscription=builders.subscription_plan("professional", interval="month"),
                vector_space=builders.vector_space(size=0, limit=0),
                apps=billing.SubscriptionQuota(size=5, limit=10),
            )

            # Act: Execute the method under test
            result = FeatureService.get_features(tenant_id)

        # Assert: Verify the expected outcomes
        assert result is not None
        assert isinstance(result, FeatureModel)

        # Verify vector space configuration
        assert result.vector_space.size == 0
        assert result.vector_space.limit == 0

        # Verify apps configuration
        assert result.apps.size == 5
        assert result.apps.limit == 10

        # Verify paid plan behavior.
        assert result.webapp_copyright_enabled is True
        assert result.is_allow_transfer_workspace is True

        # Verify default values for missing billing info
        assert result.members.size == 0
        assert result.members.limit == 1
        assert result.documents_upload_quota.size == 0
        assert result.documents_upload_quota.limit == 50
        assert result.annotation_quota_limit.size == 0
        assert result.annotation_quota_limit.limit == 10

        # Verify mock interactions
        mock_external_service_dependencies["billing_service"].get_info.assert_called_once_with(tenant_id)

    def test_get_system_features_edge_case_webapp_auth(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test system features retrieval with edge case webapp auth configuration.

        This test verifies:
        - Proper handling of webapp auth configuration
        - Correct enterprise service integration
        - Proper fallback to default values
        - Return value correctness and structure
        """
        # Arrange: Setup edge case webapp auth mock with proper config
        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE
            mock_config.MARKETPLACE_ENABLED = False
            mock_config.ENABLE_EMAIL_CODE_LOGIN = False
            mock_config.ENABLE_EMAIL_PASSWORD_LOGIN = True
            mock_config.ENABLE_SOCIAL_OAUTH_LOGIN = False
            mock_config.ALLOW_REGISTER = False
            mock_config.ALLOW_CREATE_WORKSPACE = False
            mock_config.MAIL_TYPE = "smtp"
            mock_config.PLUGIN_MAX_PACKAGE_SIZE = 100

            mock_external_service_dependencies["enterprise_service"].get_info.return_value = {
                "WebAppAuth": {"allowSso": False, "allowEmailCodeLogin": True, "allowEmailPasswordLogin": False}
            }

            # Act: Execute the method under test
            result = SystemFeatureService.get_public_system_features()

        # Assert: Verify the expected outcomes
        assert result is not None
        assert isinstance(result, SystemFeatureModel)

        # Verify webapp auth configuration
        assert result.webapp_auth.allow_sso is False
        assert result.webapp_auth.allow_email_code_login is True
        assert result.webapp_auth.allow_email_password_login is False
        assert result.webapp_auth.sso_config.protocol is None

        # Verify enterprise features
        assert result.branding.enabled is True
        assert result.webapp_auth.enabled is True
        assert result.enable_change_email is False

        # Verify default values for missing enterprise info
        assert result.sso_enforced_for_signin is False
        assert result.sso_enforced_for_signin_protocol is None
        assert result.enable_email_code_login is False
        assert result.enable_email_password_login is True
        assert result.is_allow_register is False

        # Verify mock interactions
        mock_external_service_dependencies["enterprise_service"].get_info.assert_called_once()

    def test_get_features_edge_case_members_quota(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test feature retrieval with edge case members quota configuration.

        This test verifies:
        - Proper handling of members quota limits
        - Correct integration with billing service
        - Proper fallback to default values
        - Return value correctness and structure
        """
        # Arrange: Setup edge case members quota mock with proper config
        tenant_id = self._create_test_tenant_id()

        with patch("services.feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.CLOUD
            mock_config.CAN_REPLACE_LOGO = True
            mock_config.MODEL_LB_ENABLED = False
            mock_config.EDUCATION_ENABLED = False

            mock_external_service_dependencies["billing_service"].get_info.return_value = builders.subscription_info(
                subscription=builders.subscription_plan("professional", interval="year"),
                members=billing.SubscriptionQuota(size=10, limit=10),
                vector_space=builders.vector_space(size=3, limit=5),
            )

            # Act: Execute the method under test
            result = FeatureService.get_features(tenant_id)

        # Assert: Verify the expected outcomes
        assert result is not None
        assert isinstance(result, FeatureModel)

        # Verify members configuration
        assert result.members.size == 10
        assert result.members.limit == 10

        # Verify vector space configuration
        assert result.vector_space.size == 3
        assert result.vector_space.limit == 5

        # Verify paid plan behavior.
        assert result.webapp_copyright_enabled is True
        assert result.is_allow_transfer_workspace is True

        # Verify default values for missing billing info
        assert result.apps.size == 0
        assert result.apps.limit == 10
        assert result.documents_upload_quota.size == 0
        assert result.documents_upload_quota.limit == 50
        assert result.annotation_quota_limit.size == 0
        assert result.annotation_quota_limit.limit == 10

        # Verify mock interactions
        mock_external_service_dependencies["billing_service"].get_info.assert_called_once_with(tenant_id)

    def test_plugin_installation_permission_scopes(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test system features retrieval with different plugin installation permission scopes.

        This test verifies:
        - Proper handling of different plugin installation scopes
        - Correct enterprise service integration
        - Proper permission configuration
        - Return value correctness and structure
        """

        # Test case 1: Official only scope
        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE
            mock_config.MARKETPLACE_ENABLED = False
            mock_config.ENABLE_EMAIL_CODE_LOGIN = False
            mock_config.ENABLE_EMAIL_PASSWORD_LOGIN = True
            mock_config.ENABLE_SOCIAL_OAUTH_LOGIN = False
            mock_config.ALLOW_REGISTER = False
            mock_config.ALLOW_CREATE_WORKSPACE = False
            mock_config.MAIL_TYPE = "smtp"
            mock_config.PLUGIN_MAX_PACKAGE_SIZE = 100

            mock_external_service_dependencies["enterprise_service"].get_info.return_value = {
                "PluginInstallationPermission": {
                    "pluginInstallationScope": "official_only",
                    "restrictToMarketplaceOnly": True,
                }
            }

            result = SystemFeatureService.get_public_system_features()
            assert result.plugin_installation_permission.plugin_installation_scope == "official_only"
            assert result.plugin_installation_permission.restrict_to_marketplace_only is True

        # Test case 2: All plugins scope
        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE
            mock_config.MARKETPLACE_ENABLED = False
            mock_config.ENABLE_EMAIL_CODE_LOGIN = False
            mock_config.ENABLE_EMAIL_PASSWORD_LOGIN = True
            mock_config.ENABLE_SOCIAL_OAUTH_LOGIN = False
            mock_config.ALLOW_REGISTER = False
            mock_config.ALLOW_CREATE_WORKSPACE = False
            mock_config.MAIL_TYPE = "smtp"
            mock_config.PLUGIN_MAX_PACKAGE_SIZE = 100

            mock_external_service_dependencies["enterprise_service"].get_info.return_value = {
                "PluginInstallationPermission": {"pluginInstallationScope": "all", "restrictToMarketplaceOnly": False}
            }

            result = SystemFeatureService.get_public_system_features()
            assert result.plugin_installation_permission.plugin_installation_scope == "all"
            assert result.plugin_installation_permission.restrict_to_marketplace_only is False

        # Test case 3: Specific partners scope
        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE
            mock_config.MARKETPLACE_ENABLED = False
            mock_config.ENABLE_EMAIL_CODE_LOGIN = False
            mock_config.ENABLE_EMAIL_PASSWORD_LOGIN = True
            mock_config.ENABLE_SOCIAL_OAUTH_LOGIN = False
            mock_config.ALLOW_REGISTER = False
            mock_config.ALLOW_CREATE_WORKSPACE = False
            mock_config.MAIL_TYPE = "smtp"
            mock_config.PLUGIN_MAX_PACKAGE_SIZE = 100

            mock_external_service_dependencies["enterprise_service"].get_info.return_value = {
                "PluginInstallationPermission": {
                    "pluginInstallationScope": "official_and_specific_partners",
                    "restrictToMarketplaceOnly": False,
                }
            }

            result = SystemFeatureService.get_public_system_features()
            assert result.plugin_installation_permission.plugin_installation_scope == "official_and_specific_partners"
            assert result.plugin_installation_permission.restrict_to_marketplace_only is False

        # Test case 4: None scope
        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE
            mock_config.MARKETPLACE_ENABLED = False
            mock_config.ENABLE_EMAIL_CODE_LOGIN = False
            mock_config.ENABLE_EMAIL_PASSWORD_LOGIN = True
            mock_config.ENABLE_SOCIAL_OAUTH_LOGIN = False
            mock_config.ALLOW_REGISTER = False
            mock_config.ALLOW_CREATE_WORKSPACE = False
            mock_config.MAIL_TYPE = "smtp"
            mock_config.PLUGIN_MAX_PACKAGE_SIZE = 100

            mock_external_service_dependencies["enterprise_service"].get_info.return_value = {
                "PluginInstallationPermission": {"pluginInstallationScope": "none", "restrictToMarketplaceOnly": True}
            }

            result = SystemFeatureService.get_public_system_features()
            assert result.plugin_installation_permission.plugin_installation_scope == "none"
            assert result.plugin_installation_permission.restrict_to_marketplace_only is True

    def test_get_features_workspace_members_missing(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test feature retrieval when workspace members info is missing from enterprise.

        This test verifies:
        - Proper handling of missing workspace members data
        - Correct enterprise service integration
        - Proper fallback to default values
        - Return value correctness and structure
        """
        # Arrange: Setup missing workspace members mock
        tenant_id = self._create_test_tenant_id()
        mock_external_service_dependencies["enterprise_service"].get_workspace_info.return_value = {
            # Missing WorkspaceMembers key
        }

        with patch("services.feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE

            # Act: Execute the method under test
            result = FeatureService.get_features(tenant_id)

            # Assert: Verify the expected outcomes
            assert result is not None
            assert isinstance(result, FeatureModel)

            # Verify workspace members use default values
            assert result.workspace_members.enabled is False
            assert result.workspace_members.size == 0
            assert result.workspace_members.limit == 0

            # Verify enterprise features
            assert result.webapp_copyright_enabled is True

            # Verify mock interactions
            mock_external_service_dependencies["enterprise_service"].get_workspace_info.assert_called_once_with(
                tenant_id
            )

    def test_get_system_features_license_inactive(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test system features retrieval with inactive license.

        This test verifies:
        - Proper handling of inactive license status
        - Correct enterprise service integration
        - Proper license status handling
        - Return value correctness and structure
        """
        # Arrange: Setup inactive license mock with proper config
        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE
            mock_config.MARKETPLACE_ENABLED = False
            mock_config.ENABLE_EMAIL_CODE_LOGIN = False
            mock_config.ENABLE_EMAIL_PASSWORD_LOGIN = True
            mock_config.ENABLE_SOCIAL_OAUTH_LOGIN = False
            mock_config.ALLOW_REGISTER = False
            mock_config.ALLOW_CREATE_WORKSPACE = False
            mock_config.MAIL_TYPE = "smtp"
            mock_config.PLUGIN_MAX_PACKAGE_SIZE = 100

            mock_external_service_dependencies["enterprise_service"].get_info.return_value = {
                "License": {
                    "status": "inactive",
                    "expiredAt": "",
                    "workspaces": {"enabled": False, "limit": 0, "used": 0},
                }
            }

            # Act: Execute the authenticated license accessor
            result = SystemFeatureService.get_license()

        # Assert: Verify the expected outcomes
        assert result is not None
        assert isinstance(result, LicenseModel)

        # Verify license status and detail
        assert result.status == "inactive"
        assert result.expired_at == ""
        assert result.workspaces.enabled is False
        assert result.workspaces.size == 0
        assert result.workspaces.limit == 0

        # Verify mock interactions
        mock_external_service_dependencies["enterprise_service"].get_info.assert_called_once()

    def test_get_system_features_partial_enterprise_info(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test system features retrieval with partial enterprise information.

        This test verifies:
        - Proper handling of partial enterprise data
        - Correct fallback to default values
        - Proper enterprise service integration
        - Return value correctness and structure
        """
        # Arrange: Setup partial enterprise info mock with proper config
        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE
            mock_config.MARKETPLACE_ENABLED = False
            mock_config.ENABLE_EMAIL_CODE_LOGIN = False
            mock_config.ENABLE_EMAIL_PASSWORD_LOGIN = True
            mock_config.ENABLE_SOCIAL_OAUTH_LOGIN = False
            mock_config.ALLOW_REGISTER = False
            mock_config.ALLOW_CREATE_WORKSPACE = False
            mock_config.MAIL_TYPE = "smtp"
            mock_config.PLUGIN_MAX_PACKAGE_SIZE = 100

            mock_external_service_dependencies["enterprise_service"].get_info.return_value = {
                "SSOEnforcedForSignin": True,
                "Branding": {"applicationTitle": "Partial Enterprise"},
                # Missing WebAppAuth, License, PluginInstallationPermission, etc.
            }

            # Act: Execute the method under test
            result = SystemFeatureService.get_public_system_features()

        # Assert: Verify the expected outcomes
        assert result is not None
        assert isinstance(result, SystemFeatureModel)

        # Verify enterprise features
        assert result.branding.enabled is True
        assert result.webapp_auth.enabled is True
        assert result.enable_change_email is False

        # Verify SSO configuration
        assert result.sso_enforced_for_signin is True
        assert result.sso_enforced_for_signin_protocol is None

        # Verify branding configuration (partial)
        assert result.branding.application_title == "Partial Enterprise"
        assert result.branding.login_page_logo == ""
        assert result.branding.workspace_logo == ""
        assert result.branding.favicon == ""

        # Verify default values for missing enterprise info
        assert result.webapp_auth.allow_sso is False
        assert result.webapp_auth.allow_email_code_login is False
        assert result.webapp_auth.allow_email_password_login is False
        assert result.webapp_auth.sso_config.protocol is None

        # Verify default license status
        assert result.license.status == "none"
        assert not hasattr(result.license, "expired_at")
        assert not hasattr(result.license, "workspaces")

        # Verify default plugin installation permission
        assert result.plugin_installation_permission.plugin_installation_scope == "all"
        assert result.plugin_installation_permission.restrict_to_marketplace_only is False

        # Verify mock interactions
        mock_external_service_dependencies["enterprise_service"].get_info.assert_called_once()

    def test_get_features_edge_case_limits(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test feature retrieval with edge case limit values.

        This test verifies:
        - Proper handling of zero and negative limits
        - Correct handling of very large limits
        - Proper fallback to default values
        - Return value correctness and structure
        """
        # Arrange: Setup edge case limits mock with proper config
        tenant_id = self._create_test_tenant_id()

        with patch("services.feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.CLOUD
            mock_config.CAN_REPLACE_LOGO = True
            mock_config.MODEL_LB_ENABLED = False
            mock_config.EDUCATION_ENABLED = False

            mock_external_service_dependencies["billing_service"].get_info.return_value = builders.subscription_info(
                subscription=builders.subscription_plan("team", interval="year"),
                members=billing.SubscriptionQuota(size=0, limit=0),
                apps=billing.SubscriptionQuota(size=0, limit=-1),
                vector_space=builders.vector_space(size=0, limit=999999),
                documents_upload_quota=billing.SubscriptionQuota(size=0, limit=0),
                annotation_quota_limit=billing.SubscriptionQuota(size=0, limit=1),
            )

            # Act: Execute the method under test
            result = FeatureService.get_features(tenant_id)

        # Assert: Verify the expected outcomes
        assert result is not None
        assert isinstance(result, FeatureModel)

        # Verify edge case limits
        assert result.members.size == 0
        assert result.members.limit == 0
        assert result.apps.size == 0
        assert result.apps.limit == -1
        assert result.vector_space.size == 0
        assert result.vector_space.limit == 999999
        assert result.documents_upload_quota.size == 0
        assert result.documents_upload_quota.limit == 0
        assert result.annotation_quota_limit.size == 0
        assert result.annotation_quota_limit.limit == 1

        # Verify paid plan behavior.
        assert result.webapp_copyright_enabled is True
        assert result.is_allow_transfer_workspace is True

        # Verify mock interactions
        mock_external_service_dependencies["billing_service"].get_info.assert_called_once_with(tenant_id)

    def test_get_system_features_edge_case_protocols(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test system features retrieval with edge case protocol values.

        This test verifies:
        - Proper handling of empty protocol strings
        - Correct handling of special protocol values
        - Proper fallback to default values
        - Return value correctness and structure
        """
        # Arrange: Setup edge case protocols mock with proper config
        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE
            mock_config.MARKETPLACE_ENABLED = False
            mock_config.ENABLE_EMAIL_CODE_LOGIN = False
            mock_config.ENABLE_EMAIL_PASSWORD_LOGIN = True
            mock_config.ENABLE_SOCIAL_OAUTH_LOGIN = False
            mock_config.ALLOW_REGISTER = False
            mock_config.ALLOW_CREATE_WORKSPACE = False
            mock_config.MAIL_TYPE = "smtp"
            mock_config.PLUGIN_MAX_PACKAGE_SIZE = 100

            mock_external_service_dependencies["enterprise_service"].get_info.return_value = {
                "SSOEnforcedForSigninProtocol": "",
                "SSOEnforcedForWebProtocol": "   ",
                "WebAppAuth": {"allowSso": True, "allowEmailCodeLogin": False, "allowEmailPasswordLogin": True},
            }

            # Act: Execute the method under test
            result = SystemFeatureService.get_public_system_features()

        # Assert: Verify the expected outcomes
        assert result is not None
        assert isinstance(result, SystemFeatureModel)

        # Verify edge case protocols
        assert result.sso_enforced_for_signin_protocol is None
        assert result.webapp_auth.sso_config.protocol is None

        # Verify webapp auth configuration
        assert result.webapp_auth.allow_sso is True
        assert result.webapp_auth.allow_email_code_login is False
        assert result.webapp_auth.allow_email_password_login is True

        # Verify enterprise features
        assert result.branding.enabled is True
        assert result.webapp_auth.enabled is True
        assert result.enable_change_email is False

        # Verify mock interactions
        mock_external_service_dependencies["enterprise_service"].get_info.assert_called_once()

    def test_get_features_edge_case_education(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test feature retrieval with edge case education configuration.

        This test verifies:
        - Proper handling of education feature flags
        - Correct integration with billing service
        - Proper fallback to default values
        - Return value correctness and structure
        """
        # Arrange: Setup edge case education mock
        tenant_id = self._create_test_tenant_id()
        mock_external_service_dependencies["billing_service"].get_info.return_value = builders.subscription_info(
            subscription=builders.subscription_plan("professional", interval="year", education=True),
            members=billing.SubscriptionQuota(size=100, limit=200),
            apps=billing.SubscriptionQuota(size=50, limit=100),
            vector_space=builders.vector_space(size=20, limit=50),
            documents_upload_quota=billing.SubscriptionQuota(size=500, limit=1000),
            annotation_quota_limit=billing.SubscriptionQuota(size=200, limit=500),
        )

        with patch("services.feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.CLOUD
            mock_config.EDUCATION_ENABLED = True

            # Act: Execute the method under test
            result = FeatureService.get_features(tenant_id)

            # Assert: Verify the expected outcomes
            assert result is not None
            assert isinstance(result, FeatureModel)

            # Verify education features
            assert result.education.enabled is True
            assert result.education.activated is True

            # Verify education subscription limits.
            assert result.members.size == 100
            assert result.members.limit == 200
            assert result.apps.size == 50
            assert result.apps.limit == 100
            assert result.vector_space.size == 20
            assert result.vector_space.limit == 50
            assert result.documents_upload_quota.size == 500
            assert result.documents_upload_quota.limit == 1000
            assert result.annotation_quota_limit.size == 200
            assert result.annotation_quota_limit.limit == 500

            # Verify paid plan behavior.
            assert result.webapp_copyright_enabled is True
            assert result.is_allow_transfer_workspace is True

            # Verify mock interactions
            mock_external_service_dependencies["billing_service"].get_info.assert_called_once_with(tenant_id)

    def test_get_features_workspace_members_disabled(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test feature retrieval when workspace members are disabled in enterprise.

        This test verifies:
        - Proper handling of disabled workspace members
        - Correct enterprise service integration
        - Proper fallback to default values
        - Return value correctness and structure
        """
        # Arrange: Setup workspace members disabled mock
        tenant_id = self._create_test_tenant_id()
        mock_external_service_dependencies["enterprise_service"].get_workspace_info.return_value = {
            "WorkspaceMembers": {"used": 0, "limit": 0, "enabled": False}
        }

        with patch("services.feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE

            # Act: Execute the method under test
            result = FeatureService.get_features(tenant_id)

            # Assert: Verify the expected outcomes
        assert result is not None
        assert isinstance(result, FeatureModel)

        # Verify workspace members are disabled
        assert result.workspace_members.enabled is False
        assert result.workspace_members.size == 0
        assert result.workspace_members.limit == 0

        # Verify enterprise features
        assert result.webapp_copyright_enabled is True

        # Verify mock interactions
        mock_external_service_dependencies["enterprise_service"].get_workspace_info.assert_called_once_with(tenant_id)

    def test_get_system_features_license_expired(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test system features retrieval with expired license.

        This test verifies:
        - Proper handling of expired license status
        - Correct enterprise service integration
        - Proper license status handling
        - Return value correctness and structure
        """
        # Arrange: Setup expired license mock with proper config
        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE
            mock_config.MARKETPLACE_ENABLED = False
            mock_config.ENABLE_EMAIL_CODE_LOGIN = False
            mock_config.ENABLE_EMAIL_PASSWORD_LOGIN = True
            mock_config.ENABLE_SOCIAL_OAUTH_LOGIN = False
            mock_config.ALLOW_REGISTER = False
            mock_config.ALLOW_CREATE_WORKSPACE = False
            mock_config.MAIL_TYPE = "smtp"
            mock_config.PLUGIN_MAX_PACKAGE_SIZE = 100

            mock_external_service_dependencies["enterprise_service"].get_info.return_value = {
                "License": {
                    "status": "expired",
                    "expiredAt": "2023-12-31",
                    "workspaces": {"enabled": False, "limit": 0, "used": 0},
                }
            }

            # Act: Execute the authenticated license accessor
            result = SystemFeatureService.get_license()

        # Assert: Verify the expected outcomes
        assert result is not None
        assert isinstance(result, LicenseModel)

        # Verify license status and detail
        assert result.status == "expired"
        assert result.expired_at == "2023-12-31"
        assert result.workspaces.enabled is False
        assert result.workspaces.size == 0
        assert result.workspaces.limit == 0

        # Verify mock interactions
        mock_external_service_dependencies["enterprise_service"].get_info.assert_called_once()

    def test_get_system_features_edge_case_branding(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test system features retrieval with edge case branding configuration.

        This test verifies:
        - Proper handling of partial branding information
        - Correct enterprise service integration
        - Proper fallback to default values
        - Return value correctness and structure
        """
        # Arrange: Setup edge case branding mock with proper config
        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE
            mock_config.MARKETPLACE_ENABLED = False
            mock_config.ENABLE_EMAIL_CODE_LOGIN = False
            mock_config.ENABLE_EMAIL_PASSWORD_LOGIN = True
            mock_config.ENABLE_SOCIAL_OAUTH_LOGIN = False
            mock_config.ALLOW_REGISTER = False
            mock_config.ALLOW_CREATE_WORKSPACE = False
            mock_config.MAIL_TYPE = "smtp"
            mock_config.PLUGIN_MAX_PACKAGE_SIZE = 100

            mock_external_service_dependencies["enterprise_service"].get_info.return_value = {
                "Branding": {
                    "applicationTitle": "Edge Case App",
                    "loginPageLogo": None,
                    "workspaceLogo": "",
                    "favicon": "https://example.com/favicon.ico",
                }
            }

            # Act: Execute the method under test
            result = SystemFeatureService.get_public_system_features()

        # Assert: Verify the expected outcomes
        assert result is not None
        assert isinstance(result, SystemFeatureModel)

        # Verify branding configuration (edge cases)
        assert result.branding.application_title == "Edge Case App"
        assert result.branding.login_page_logo is None  # None value from mock
        assert result.branding.workspace_logo == ""
        assert result.branding.favicon == "https://example.com/favicon.ico"

        # Verify enterprise features
        assert result.branding.enabled is True
        assert result.webapp_auth.enabled is True
        assert result.enable_change_email is False

        # Verify default values for missing enterprise info
        assert result.sso_enforced_for_signin is False
        assert result.sso_enforced_for_signin_protocol is None
        assert result.enable_email_code_login is False
        assert result.enable_email_password_login is True
        assert result.is_allow_register is False

        # Verify mock interactions
        mock_external_service_dependencies["enterprise_service"].get_info.assert_called_once()

    def test_get_features_edge_case_annotation_quota(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test feature retrieval with edge case annotation quota configuration.

        This test verifies:
        - Proper handling of annotation quota limits
        - Correct integration with billing service
        - Proper fallback to default values
        - Return value correctness and structure
        """
        # Arrange: Setup edge case annotation quota mock with proper config
        tenant_id = self._create_test_tenant_id()

        with patch("services.feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.CLOUD
            mock_config.CAN_REPLACE_LOGO = True
            mock_config.MODEL_LB_ENABLED = False
            mock_config.EDUCATION_ENABLED = False

            mock_external_service_dependencies["billing_service"].get_info.return_value = builders.subscription_info(
                subscription=builders.subscription_plan("team", interval="year"),
                annotation_quota_limit=billing.SubscriptionQuota(size=999, limit=1000),
            )

            # Act: Execute the method under test
            result = FeatureService.get_features(tenant_id)

        # Assert: Verify the expected outcomes
        assert result is not None
        assert isinstance(result, FeatureModel)

        # Verify annotation quota configuration
        assert result.annotation_quota_limit.size == 999
        assert result.annotation_quota_limit.limit == 1000

        # Verify paid plan behavior.
        assert result.webapp_copyright_enabled is True
        assert result.is_allow_transfer_workspace is True

        # Verify default values for missing billing info
        assert result.members.size == 0
        assert result.members.limit == 1
        assert result.apps.size == 0
        assert result.apps.limit == 10
        assert result.vector_space.size == 0
        assert result.vector_space.limit == 5
        assert result.documents_upload_quota.size == 0
        assert result.documents_upload_quota.limit == 50

        # Verify mock interactions
        mock_external_service_dependencies["billing_service"].get_info.assert_called_once_with(tenant_id)

    def test_get_features_edge_case_documents_upload(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test feature retrieval with edge case documents upload settings.

        This test verifies:
        - Proper handling of edge case documents upload configuration
        - Correct integration with billing service
        - Proper fallback to default values
        - Return value correctness and structure
        """
        # Arrange: Setup edge case documents upload mock with proper config
        tenant_id = self._create_test_tenant_id()

        with patch("services.feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.CLOUD
            mock_config.CAN_REPLACE_LOGO = True
            mock_config.MODEL_LB_ENABLED = False
            mock_config.EDUCATION_ENABLED = False

            mock_external_service_dependencies["billing_service"].get_info.return_value = builders.subscription_info(
                subscription=builders.subscription_plan("professional", interval="month"),
                documents_upload_quota=billing.SubscriptionQuota(size=0, limit=0),
            )

            # Act: Execute the method under test
            result = FeatureService.get_features(tenant_id)

        # Assert: Verify the expected outcomes
        assert result is not None
        assert isinstance(result, FeatureModel)

        # Verify documents upload quota configuration (edge cases)
        assert result.documents_upload_quota.size == 0
        assert result.documents_upload_quota.limit == 0

        # Verify paid plan behavior.
        assert result.webapp_copyright_enabled is True
        assert result.is_allow_transfer_workspace is True

        # Verify default values for missing billing info
        assert result.members.size == 0
        assert result.members.limit == 1
        assert result.apps.size == 0
        assert result.apps.limit == 10
        assert result.vector_space.size == 0
        assert result.vector_space.limit == 5
        assert result.annotation_quota_limit.size == 0
        assert result.annotation_quota_limit.limit == 10  # Default value when not provided

        # Verify mock interactions
        mock_external_service_dependencies["billing_service"].get_info.assert_called_once_with(tenant_id)

    def test_get_system_features_edge_case_license_lost(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test system features with lost license status.

        This test verifies:
        - Proper handling of lost license status
        - Correct enterprise service integration
        - Proper fallback to default values
        - Return value correctness and structure
        """
        # Arrange: Setup lost license mock with proper config
        with patch("services.system_feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.ENTERPRISE
            mock_config.MARKETPLACE_ENABLED = False
            mock_config.ENABLE_EMAIL_CODE_LOGIN = False
            mock_config.ENABLE_EMAIL_PASSWORD_LOGIN = True
            mock_config.ENABLE_SOCIAL_OAUTH_LOGIN = False
            mock_config.ALLOW_REGISTER = False
            mock_config.ALLOW_CREATE_WORKSPACE = False
            mock_config.MAIL_TYPE = "smtp"
            mock_config.PLUGIN_MAX_PACKAGE_SIZE = 100

            mock_external_service_dependencies["enterprise_service"].get_info.return_value = {
                "License": {"status": "lost"}
            }

            # Act: Execute the method under test
            result = SystemFeatureService.get_public_system_features()

        # Assert: Verify the expected outcomes
        assert result is not None
        assert isinstance(result, SystemFeatureModel)

        # Verify enterprise features
        assert result.branding.enabled is True
        assert result.webapp_auth.enabled is True
        assert result.enable_change_email is False

        # Verify default values for missing enterprise info
        assert result.sso_enforced_for_signin is False
        assert result.sso_enforced_for_signin_protocol is None
        assert result.enable_email_code_login is False
        assert result.enable_email_password_login is True
        assert result.is_allow_register is False
        assert result.license.status == LicenseStatus.LOST

        # Verify mock interactions
        mock_external_service_dependencies["enterprise_service"].get_info.assert_called_once()

    def test_get_features_edge_case_education_disabled(
        self, db_session_with_containers: Session, mock_external_service_dependencies
    ):
        """
        Test feature retrieval with education feature disabled.

        This test verifies:
        - Proper handling of disabled education features
        - Correct integration with billing service
        - Proper fallback to default values
        - Return value correctness and structure
        """
        # Arrange: Setup education disabled mock with proper config
        tenant_id = self._create_test_tenant_id()

        with patch("services.feature_service.dify_config") as mock_config:
            mock_config.DEPLOYMENT_EDITION = DeploymentEdition.CLOUD
            mock_config.CAN_REPLACE_LOGO = True
            mock_config.MODEL_LB_ENABLED = False
            mock_config.EDUCATION_ENABLED = False

            mock_external_service_dependencies["billing_service"].get_info.return_value = builders.subscription_info(
                subscription=builders.subscription_plan("professional", interval="month", education=False)
            )

            # Act: Execute the method under test
            result = FeatureService.get_features(tenant_id)

        # Assert: Verify the expected outcomes
        assert result is not None
        assert isinstance(result, FeatureModel)

        # Verify education configuration
        assert result.education.activated is False

        # Verify paid plan behavior.
        assert result.webapp_copyright_enabled is True
        assert result.is_allow_transfer_workspace is True

        # Verify default values for missing billing info
        assert result.members.size == 0
        assert result.members.limit == 1
        assert result.apps.size == 0
        assert result.apps.limit == 10
        assert result.vector_space.size == 0
        assert result.vector_space.limit == 5
        assert result.documents_upload_quota.size == 0
        assert result.documents_upload_quota.limit == 50
        assert result.annotation_quota_limit.size == 0
        assert result.annotation_quota_limit.limit == 10  # Default value when not provided

        # Verify mock interactions
        mock_external_service_dependencies["billing_service"].get_info.assert_called_once_with(tenant_id)