mirror of
https://github.com/langgenius/dify.git
synced 2026-05-04 09:28:04 +08:00
test(api): adjust tests for _resolve_human_input_email_delivery_enabled
This commit is contained in:
@ -14,7 +14,6 @@ from controllers.web.error import NotFoundError
|
||||
from controllers.web.site import serialize_app_site_payload
|
||||
from extensions.ext_database import db
|
||||
from models.account import TenantStatus
|
||||
from models.human_input import RecipientType
|
||||
from models.model import App, Site
|
||||
from services.human_input_service import Form, FormNotFoundError, HumanInputService
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
from unittest.mock import MagicMock
|
||||
from dataclasses import dataclass
|
||||
|
||||
import pytest
|
||||
|
||||
@ -7,70 +7,98 @@ from services import feature_service as feature_service_module
|
||||
from services.feature_service import FeatureModel, FeatureService
|
||||
|
||||
|
||||
def _make_features(plan: str) -> FeatureModel:
|
||||
@dataclass(frozen=True)
|
||||
class HumanInputEmailDeliveryCase:
|
||||
name: str
|
||||
enterprise_enabled: bool
|
||||
billing_enabled: bool
|
||||
tenant_id: str | None
|
||||
billing_feature_enabled: bool
|
||||
plan: str
|
||||
expected: bool
|
||||
|
||||
|
||||
CASES = [
|
||||
HumanInputEmailDeliveryCase(
|
||||
name="enterprise_enabled",
|
||||
enterprise_enabled=True,
|
||||
billing_enabled=True,
|
||||
tenant_id=None,
|
||||
billing_feature_enabled=False,
|
||||
plan=CloudPlan.SANDBOX,
|
||||
expected=True,
|
||||
),
|
||||
HumanInputEmailDeliveryCase(
|
||||
name="billing_disabled",
|
||||
enterprise_enabled=False,
|
||||
billing_enabled=False,
|
||||
tenant_id=None,
|
||||
billing_feature_enabled=False,
|
||||
plan=CloudPlan.SANDBOX,
|
||||
expected=True,
|
||||
),
|
||||
HumanInputEmailDeliveryCase(
|
||||
name="billing_enabled_requires_tenant",
|
||||
enterprise_enabled=False,
|
||||
billing_enabled=True,
|
||||
tenant_id=None,
|
||||
billing_feature_enabled=True,
|
||||
plan=CloudPlan.PROFESSIONAL,
|
||||
expected=False,
|
||||
),
|
||||
HumanInputEmailDeliveryCase(
|
||||
name="billing_feature_off",
|
||||
enterprise_enabled=False,
|
||||
billing_enabled=True,
|
||||
tenant_id="tenant-1",
|
||||
billing_feature_enabled=False,
|
||||
plan=CloudPlan.PROFESSIONAL,
|
||||
expected=False,
|
||||
),
|
||||
HumanInputEmailDeliveryCase(
|
||||
name="professional_plan",
|
||||
enterprise_enabled=False,
|
||||
billing_enabled=True,
|
||||
tenant_id="tenant-1",
|
||||
billing_feature_enabled=True,
|
||||
plan=CloudPlan.PROFESSIONAL,
|
||||
expected=True,
|
||||
),
|
||||
HumanInputEmailDeliveryCase(
|
||||
name="team_plan",
|
||||
enterprise_enabled=False,
|
||||
billing_enabled=True,
|
||||
tenant_id="tenant-1",
|
||||
billing_feature_enabled=True,
|
||||
plan=CloudPlan.TEAM,
|
||||
expected=True,
|
||||
),
|
||||
HumanInputEmailDeliveryCase(
|
||||
name="sandbox_plan",
|
||||
enterprise_enabled=False,
|
||||
billing_enabled=True,
|
||||
tenant_id="tenant-1",
|
||||
billing_feature_enabled=True,
|
||||
plan=CloudPlan.SANDBOX,
|
||||
expected=False,
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("case", CASES, ids=lambda case: case.name)
|
||||
def test_resolve_human_input_email_delivery_enabled_matrix(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
case: HumanInputEmailDeliveryCase,
|
||||
):
|
||||
monkeypatch.setattr(feature_service_module.dify_config, "ENTERPRISE_ENABLED", case.enterprise_enabled)
|
||||
monkeypatch.setattr(feature_service_module.dify_config, "BILLING_ENABLED", case.billing_enabled)
|
||||
features = FeatureModel()
|
||||
features.billing.enabled = True
|
||||
features.billing.subscription.plan = plan
|
||||
return features
|
||||
features.billing.enabled = case.billing_feature_enabled
|
||||
features.billing.subscription.plan = case.plan
|
||||
|
||||
result = FeatureService._resolve_human_input_email_delivery_enabled(
|
||||
features=features,
|
||||
tenant_id=case.tenant_id,
|
||||
)
|
||||
|
||||
def test_human_input_email_delivery_available_for_enterprise(monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(feature_service_module.dify_config, "ENTERPRISE_ENABLED", True)
|
||||
monkeypatch.setattr(feature_service_module.dify_config, "BILLING_ENABLED", True)
|
||||
mock_fulfill = MagicMock()
|
||||
monkeypatch.setattr(FeatureService, "_fulfill_params_from_billing_api", mock_fulfill)
|
||||
mock_workspace = MagicMock()
|
||||
monkeypatch.setattr(FeatureService, "_fulfill_params_from_workspace_info", mock_workspace)
|
||||
|
||||
features = FeatureService.get_features("tenant-1")
|
||||
|
||||
assert features.human_input_email_delivery_enabled is True
|
||||
mock_fulfill.assert_called_once()
|
||||
|
||||
|
||||
def test_human_input_email_delivery_available_when_billing_disabled(monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(feature_service_module.dify_config, "ENTERPRISE_ENABLED", False)
|
||||
monkeypatch.setattr(feature_service_module.dify_config, "BILLING_ENABLED", False)
|
||||
|
||||
features = FeatureService.get_features("tenant-1")
|
||||
|
||||
assert features.human_input_email_delivery_enabled is True
|
||||
|
||||
|
||||
def test_human_input_email_delivery_requires_tenant_id_when_billing_enabled(monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(feature_service_module.dify_config, "ENTERPRISE_ENABLED", False)
|
||||
monkeypatch.setattr(feature_service_module.dify_config, "BILLING_ENABLED", True)
|
||||
mock_fulfill = MagicMock()
|
||||
monkeypatch.setattr(FeatureService, "_fulfill_params_from_billing_api", mock_fulfill)
|
||||
|
||||
features = FeatureService.get_features("")
|
||||
|
||||
assert features.human_input_email_delivery_enabled is False
|
||||
mock_fulfill.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("plan", "expected"),
|
||||
[
|
||||
(CloudPlan.PROFESSIONAL, True),
|
||||
(CloudPlan.SANDBOX, False),
|
||||
(CloudPlan.TEAM, False),
|
||||
],
|
||||
)
|
||||
def test_human_input_email_delivery_checks_plan(monkeypatch: pytest.MonkeyPatch, plan: str, expected: bool):
|
||||
monkeypatch.setattr(feature_service_module.dify_config, "ENTERPRISE_ENABLED", False)
|
||||
monkeypatch.setattr(feature_service_module.dify_config, "BILLING_ENABLED", True)
|
||||
features = _make_features(plan)
|
||||
mock_fulfill = MagicMock()
|
||||
|
||||
def _apply_fulfill(target: FeatureModel, _tenant_id: str) -> None:
|
||||
target.billing.enabled = features.billing.enabled
|
||||
target.billing.subscription.plan = features.billing.subscription.plan
|
||||
|
||||
mock_fulfill.side_effect = _apply_fulfill
|
||||
monkeypatch.setattr(FeatureService, "_fulfill_params_from_billing_api", mock_fulfill)
|
||||
|
||||
result = FeatureService.get_features("tenant-1")
|
||||
|
||||
assert result.human_input_email_delivery_enabled is expected
|
||||
mock_fulfill.assert_called_once()
|
||||
assert result is case.expected
|
||||
|
||||
Reference in New Issue
Block a user