mirror of
https://github.com/langgenius/dify.git
synced 2026-05-31 22:26:19 +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>
118 lines
3.1 KiB
Python
118 lines
3.1 KiB
Python
import uuid
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from controllers.openapi.auth.data import (
|
|
AuthData,
|
|
Edition,
|
|
ExternalIdentity,
|
|
RequestContext,
|
|
current_edition,
|
|
)
|
|
from libs.oauth_bearer import Scope, TokenType
|
|
|
|
|
|
def test_current_edition_saas():
|
|
with patch("controllers.openapi.auth.data.dify_config") as cfg:
|
|
cfg.EDITION = "CLOUD"
|
|
cfg.ENTERPRISE_ENABLED = True
|
|
assert current_edition() == Edition.SAAS
|
|
|
|
|
|
def test_current_edition_ee():
|
|
with patch("controllers.openapi.auth.data.dify_config") as cfg:
|
|
cfg.EDITION = "SELF_HOSTED"
|
|
cfg.ENTERPRISE_ENABLED = True
|
|
assert current_edition() == Edition.EE
|
|
|
|
|
|
def test_current_edition_ce():
|
|
with patch("controllers.openapi.auth.data.dify_config") as cfg:
|
|
cfg.EDITION = "SELF_HOSTED"
|
|
cfg.ENTERPRISE_ENABLED = False
|
|
assert current_edition() == Edition.CE
|
|
|
|
|
|
def test_external_identity_frozen():
|
|
ei = ExternalIdentity(email="a@b.com", issuer="idp")
|
|
with pytest.raises(ValidationError):
|
|
ei.email = "other@b.com" # type: ignore[misc]
|
|
|
|
|
|
def test_external_identity_issuer_optional():
|
|
ei = ExternalIdentity(email="a@b.com")
|
|
assert ei.issuer is None
|
|
|
|
|
|
def test_request_context_frozen():
|
|
ctx = RequestContext(
|
|
token_type=TokenType.OAUTH_ACCOUNT,
|
|
path_params={"app_id": "123"},
|
|
)
|
|
with pytest.raises(ValidationError):
|
|
ctx.token_type = TokenType.OAUTH_EXTERNAL_SSO # type: ignore[misc]
|
|
|
|
|
|
def test_request_context_scope_optional():
|
|
ctx = RequestContext(token_type=TokenType.OAUTH_ACCOUNT, path_params={})
|
|
assert ctx.scope is None
|
|
|
|
|
|
def test_auth_data_is_mutable():
|
|
data = AuthData(
|
|
token_type=TokenType.OAUTH_ACCOUNT,
|
|
token_hash="abc",
|
|
scopes=frozenset({Scope.FULL}),
|
|
)
|
|
data.token_type = TokenType.OAUTH_EXTERNAL_SSO
|
|
assert data.token_type == TokenType.OAUTH_EXTERNAL_SSO
|
|
|
|
|
|
def test_auth_data_path_params_defaults_empty():
|
|
data = AuthData(
|
|
token_type=TokenType.OAUTH_ACCOUNT,
|
|
token_hash="abc",
|
|
scopes=frozenset(),
|
|
)
|
|
assert data.path_params == {}
|
|
|
|
|
|
def test_auth_data_account_id_optional():
|
|
data = AuthData(
|
|
token_type=TokenType.OAUTH_EXTERNAL_SSO,
|
|
token_hash="abc",
|
|
scopes=frozenset({Scope.APPS_RUN}),
|
|
external_identity=ExternalIdentity(email="u@sso.com"),
|
|
)
|
|
assert data.account_id is None
|
|
|
|
|
|
def test_auth_data_external_identity_none_for_account():
|
|
data = AuthData(
|
|
token_type=TokenType.OAUTH_ACCOUNT,
|
|
account_id=uuid.uuid4(),
|
|
token_hash="abc",
|
|
scopes=frozenset({Scope.FULL}),
|
|
)
|
|
assert data.external_identity is None
|
|
|
|
|
|
def test_auth_data_tenants_default_empty():
|
|
data = AuthData(
|
|
token_type=TokenType.OAUTH_ACCOUNT,
|
|
token_hash="abc",
|
|
scopes=frozenset(),
|
|
)
|
|
assert data.tenants == {}
|
|
|
|
|
|
def test_auth_data_token_id_optional():
|
|
data = AuthData(
|
|
token_type=TokenType.OAUTH_ACCOUNT,
|
|
token_hash="abc",
|
|
scopes=frozenset(),
|
|
)
|
|
assert data.token_id is None
|