mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
refactor(api): move app-scoped end users behind application services (#41398)
This commit is contained in:
1 parent
9badde4d65
commit
ce2eaab9f7
41 files changed
+1384
-481
No files matched your search
@@ -26,6 +26,23 @@ class _DatabaseWithEngine:
|
||||
self.engine = engine
|
||||
|
||||
|
||||
class _EndUserProvisioner:
|
||||
def __init__(self) -> None:
|
||||
self.result: EndUser | None = None
|
||||
self.calls: list[tuple[str, str, str | None]] = []
|
||||
|
||||
def get_or_create_end_user(
|
||||
self,
|
||||
tenant_id: str,
|
||||
app_id: str,
|
||||
user_id: str | None = None,
|
||||
) -> EndUser:
|
||||
self.calls.append((tenant_id, app_id, user_id))
|
||||
if self.result is None:
|
||||
raise AssertionError("unexpected end-user provisioning")
|
||||
return self.result
|
||||
|
||||
|
||||
def _app(
|
||||
*,
|
||||
app_id: str = "app-1",
|
||||
@@ -104,6 +121,7 @@ class TestPluginAppBackwardsInvocation:
|
||||
self.session = sqlite_session
|
||||
self.session_factory = sqlite_session_factory
|
||||
self.sqlite_engine = sqlite_engine
|
||||
self.end_users = _EndUserProvisioner()
|
||||
mocker.patch("core.plugin.backwards_invocation.app.create_session", side_effect=sqlite_session_factory)
|
||||
|
||||
def test_fetch_app_info_workflow_path(self, mocker: MockerFixture):
|
||||
@@ -167,6 +185,7 @@ class TestPluginAppBackwardsInvocation:
|
||||
inputs={"x": 1},
|
||||
files=[],
|
||||
session=self.session,
|
||||
end_users=self.end_users,
|
||||
)
|
||||
|
||||
assert result == {"routed": True}
|
||||
@@ -178,10 +197,7 @@ class TestPluginAppBackwardsInvocation:
|
||||
workflow = _workflow()
|
||||
mocker.patch.object(PluginAppBackwardsInvocation, "_get_app", return_value=app)
|
||||
mocker.patch.object(PluginAppBackwardsInvocation, "_get_workflow", return_value=workflow)
|
||||
get_or_create = mocker.patch(
|
||||
"core.plugin.backwards_invocation.app.EndUserService.get_or_create_end_user",
|
||||
return_value=end_user,
|
||||
)
|
||||
self.end_users.result = end_user
|
||||
route = mocker.patch.object(PluginAppBackwardsInvocation, "invoke_workflow_app", return_value={"ok": True})
|
||||
|
||||
result = PluginAppBackwardsInvocation.invoke_app(
|
||||
@@ -194,10 +210,11 @@ class TestPluginAppBackwardsInvocation:
|
||||
inputs={},
|
||||
files=[],
|
||||
session=self.session,
|
||||
end_users=self.end_users,
|
||||
)
|
||||
|
||||
assert result == {"ok": True}
|
||||
get_or_create.assert_called_once_with(app)
|
||||
assert self.end_users.calls == [(app.tenant_id, app.id, None)]
|
||||
assert route.call_args.args[1] is workflow
|
||||
assert route.call_args.args[2] is end_user
|
||||
|
||||
@@ -216,6 +233,7 @@ class TestPluginAppBackwardsInvocation:
|
||||
inputs={},
|
||||
files=[],
|
||||
session=self.session,
|
||||
end_users=self.end_users,
|
||||
)
|
||||
|
||||
def test_invoke_app_unexpected_mode_raises(self, mocker: MockerFixture):
|
||||
@@ -237,6 +255,7 @@ class TestPluginAppBackwardsInvocation:
|
||||
inputs={},
|
||||
files=[],
|
||||
session=self.session,
|
||||
end_users=self.end_users,
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
@@ -374,6 +393,7 @@ class TestPluginAppBackwardsInvocation:
|
||||
inputs={},
|
||||
files=[],
|
||||
session=self.session,
|
||||
end_users=self.end_users,
|
||||
)
|
||||
|
||||
def test_invoke_completion_app(self, mocker: MockerFixture):
|
||||
@@ -510,10 +530,7 @@ class TestPluginAppBackwardsInvocation:
|
||||
mocker.patch.object(PluginAppBackwardsInvocation, "_get_app", return_value=app)
|
||||
mocker.patch.object(PluginAppBackwardsInvocation, "_get_workflow", return_value=workflow)
|
||||
mocker.patch.object(PluginAppBackwardsInvocation, "_get_user", side_effect=ValueError("user not found"))
|
||||
get_or_create = mocker.patch(
|
||||
"core.plugin.backwards_invocation.app.EndUserService.get_or_create_end_user",
|
||||
return_value=end_user,
|
||||
)
|
||||
self.end_users.result = end_user
|
||||
route = mocker.patch.object(PluginAppBackwardsInvocation, "invoke_workflow_app", return_value={"ok": True})
|
||||
|
||||
result = PluginAppBackwardsInvocation.invoke_app(
|
||||
@@ -526,10 +543,11 @@ class TestPluginAppBackwardsInvocation:
|
||||
inputs={},
|
||||
files=[],
|
||||
session=self.session,
|
||||
end_users=self.end_users,
|
||||
)
|
||||
|
||||
assert result == {"ok": True}
|
||||
get_or_create.assert_called_once_with(app, user_id="wecom-sender-1")
|
||||
assert self.end_users.calls == [(app.tenant_id, app.id, "wecom-sender-1")]
|
||||
assert route.call_args.args[2] is end_user
|
||||
|
||||
def test_get_app_returns_app(self):
|
||||
|
||||
Reference in new issue
Block a user