mirror of
https://github.com/langgenius/dify.git
synced 2026-05-31 06:06:20 +08:00
Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: EvanYao826 <155432245+EvanYao826@users.noreply.github.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: 盐粒 Yanli <yanli@dify.ai> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Tianle <40735546+Tianlel@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai> Co-authored-by: zyssyz123 <916125788@qq.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: chariri <w@chariri.moe> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Nian <11332799+Lillian68@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Carmen Fernández Ruiz <279459669+zeus1959@users.noreply.github.com> Co-authored-by: wangxiaolei <fatelei@gmail.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: L1nSn0w <l1nsn0w@qq.com> Co-authored-by: Evan <2869018789@qq.com> Co-authored-by: Escape0707 <tothesong@gmail.com> Co-authored-by: Jingyi <jingyi.qi@dify.ai> Co-authored-by: Amr Sherif <140330826+amr-sheriff@users.noreply.github.com> Co-authored-by: ZHOU ZHICHEN <118870511+zhuiguangzhe2003@users.noreply.github.com> Co-authored-by: unknown <EI05187@apwx.com> Co-authored-by: JzoNg <jzongcode@gmail.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com>
97 lines
3.0 KiB
Python
97 lines
3.0 KiB
Python
from pytest_mock import MockerFixture
|
|
from werkzeug.exceptions import Unauthorized
|
|
|
|
|
|
def unwrap(func):
|
|
"""
|
|
Recursively unwrap decorated functions.
|
|
"""
|
|
while hasattr(func, "__wrapped__"):
|
|
func = func.__wrapped__
|
|
return func
|
|
|
|
|
|
class TestFeatureApi:
|
|
def test_get_tenant_features_success(self, mocker: MockerFixture):
|
|
from controllers.console.feature import FeatureApi
|
|
|
|
get_features = mocker.patch("controllers.console.feature.FeatureService.get_features")
|
|
get_features.return_value.model_dump.return_value = {
|
|
"features": {"feature_a": True},
|
|
"vector_space": {"size": 1, "limit": 2},
|
|
}
|
|
|
|
api = FeatureApi()
|
|
|
|
raw_get = unwrap(FeatureApi.get)
|
|
result = raw_get(api, "tenant_123")
|
|
|
|
assert result == {"features": {"feature_a": True}}
|
|
get_features.assert_called_once_with("tenant_123", exclude_vector_space=True)
|
|
|
|
|
|
class TestFeatureVectorSpaceApi:
|
|
def test_get_vector_space_success(self, mocker: MockerFixture):
|
|
from controllers.console.feature import FeatureVectorSpaceApi
|
|
|
|
get_vector_space = mocker.patch("controllers.console.feature.FeatureService.get_vector_space")
|
|
get_vector_space.return_value.model_dump.return_value = {"size": 5120, "limit": 20480}
|
|
|
|
api = FeatureVectorSpaceApi()
|
|
|
|
raw_get = unwrap(FeatureVectorSpaceApi.get)
|
|
result = raw_get(api, "tenant_123")
|
|
|
|
assert result == {"size": 5120, "limit": 20480}
|
|
get_vector_space.assert_called_once_with("tenant_123")
|
|
|
|
|
|
class TestSystemFeatureApi:
|
|
def test_get_system_features_authenticated(self, mocker: MockerFixture):
|
|
"""
|
|
current_user.is_authenticated == True
|
|
"""
|
|
|
|
from controllers.console.feature import SystemFeatureApi
|
|
|
|
fake_user = mocker.Mock()
|
|
fake_user.is_authenticated = True
|
|
|
|
mocker.patch(
|
|
"controllers.console.feature.current_user",
|
|
fake_user,
|
|
)
|
|
|
|
mocker.patch(
|
|
"controllers.console.feature.FeatureService.get_system_features"
|
|
).return_value.model_dump.return_value = {"features": {"sys_feature": True}}
|
|
|
|
api = SystemFeatureApi()
|
|
result = api.get()
|
|
|
|
assert result == {"features": {"sys_feature": True}}
|
|
|
|
def test_get_system_features_unauthenticated(self, mocker: MockerFixture):
|
|
"""
|
|
current_user.is_authenticated raises Unauthorized
|
|
"""
|
|
|
|
from controllers.console.feature import SystemFeatureApi
|
|
|
|
fake_user = mocker.Mock()
|
|
type(fake_user).is_authenticated = mocker.PropertyMock(side_effect=Unauthorized())
|
|
|
|
mocker.patch(
|
|
"controllers.console.feature.current_user",
|
|
fake_user,
|
|
)
|
|
|
|
mocker.patch(
|
|
"controllers.console.feature.FeatureService.get_system_features"
|
|
).return_value.model_dump.return_value = {"features": {"sys_feature": False}}
|
|
|
|
api = SystemFeatureApi()
|
|
result = api.get()
|
|
|
|
assert result == {"features": {"sys_feature": False}}
|