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
@@ -1,66 +1,104 @@
|
||||
from datetime import UTC, datetime
|
||||
from inspect import unwrap
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
import pytest
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from controllers.service_api.end_user import end_user as controller_module
|
||||
from controllers.service_api.end_user.end_user import EndUserApi
|
||||
from controllers.service_api.end_user.error import EndUserNotFoundError
|
||||
from models.enums import EndUserType
|
||||
from models.model import App, EndUser
|
||||
from machinery.context import ServiceApiRequestContext
|
||||
from models.model import App
|
||||
from services.app_scoped_end_user_query_service import AppScopedEndUserNotFoundError
|
||||
from services.entities.app_scoped_end_user_entities import AppScopedEndUserRecord
|
||||
|
||||
|
||||
def _request_context(*, tenant_id: str = "workspace-1", app_id: str = "app-1") -> ServiceApiRequestContext:
|
||||
return ServiceApiRequestContext(
|
||||
tenant_id=tenant_id,
|
||||
app_id=app_id,
|
||||
)
|
||||
|
||||
|
||||
class EndUserQueryServiceStub:
|
||||
def __init__(self, result: AppScopedEndUserRecord | Exception) -> None:
|
||||
self._result = result
|
||||
self.calls: list[tuple[ServiceApiRequestContext, str]] = []
|
||||
|
||||
def get_by_id(self, context: ServiceApiRequestContext, end_user_id: str) -> AppScopedEndUserRecord:
|
||||
self.calls.append((context, end_user_id))
|
||||
if isinstance(self._result, Exception):
|
||||
raise self._result
|
||||
return self._result
|
||||
|
||||
|
||||
class ApplicationServicesStub:
|
||||
def __init__(self, end_user_queries: EndUserQueryServiceStub) -> None:
|
||||
self.app_scoped_end_users = AppScopedEndUserServicesStub(end_user_queries)
|
||||
|
||||
|
||||
class AppScopedEndUserServicesStub:
|
||||
def __init__(self, queries: EndUserQueryServiceStub) -> None:
|
||||
self.queries = queries
|
||||
|
||||
|
||||
class TestEndUserApi:
|
||||
@pytest.fixture
|
||||
def resource(self) -> EndUserApi:
|
||||
return EndUserApi()
|
||||
|
||||
@pytest.fixture
|
||||
def app_model(self) -> App:
|
||||
app = App(
|
||||
def test_get_end_user_returns_all_attributes(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
end_user = AppScopedEndUserRecord(
|
||||
id=str(uuid4()),
|
||||
tenant_id=str(uuid4()),
|
||||
)
|
||||
return app
|
||||
|
||||
def test_get_end_user_returns_all_attributes(
|
||||
self, mocker: MockerFixture, resource: EndUserApi, app_model: App
|
||||
) -> None:
|
||||
end_user = EndUser(
|
||||
id=str(uuid4()),
|
||||
tenant_id=app_model.tenant_id,
|
||||
app_id=app_model.id,
|
||||
type=EndUserType.SERVICE_API,
|
||||
app_id=str(uuid4()),
|
||||
type="service-api",
|
||||
external_user_id="external-123",
|
||||
name="Alice",
|
||||
_is_anonymous=True,
|
||||
is_anonymous=True,
|
||||
session_id="session-xyz",
|
||||
created_at=datetime(2024, 1, 1, tzinfo=UTC),
|
||||
updated_at=datetime(2024, 1, 2, tzinfo=UTC),
|
||||
)
|
||||
|
||||
get_end_user_by_id = mocker.patch(
|
||||
"controllers.service_api.end_user.end_user.EndUserService.get_end_user_by_id", return_value=end_user
|
||||
context = _request_context(tenant_id=end_user.tenant_id, app_id=end_user.app_id)
|
||||
service = EndUserQueryServiceStub(end_user)
|
||||
monkeypatch.setattr(
|
||||
controller_module,
|
||||
"application_services",
|
||||
lambda: ApplicationServicesStub(service),
|
||||
)
|
||||
|
||||
result = EndUserApi.get.__wrapped__(resource, app_model=app_model, end_user_id=UUID(end_user.id))
|
||||
|
||||
get_end_user_by_id.assert_called_once_with(
|
||||
tenant_id=app_model.tenant_id, app_id=app_model.id, end_user_id=end_user.id
|
||||
result = unwrap(EndUserApi.get)(
|
||||
EndUserApi(),
|
||||
app_model=App(id=context.app_id, tenant_id=context.tenant_id),
|
||||
end_user_id=UUID(end_user.id),
|
||||
)
|
||||
assert result["id"] == end_user.id
|
||||
assert result["tenant_id"] == end_user.tenant_id
|
||||
assert result["app_id"] == end_user.app_id
|
||||
assert result["type"] == end_user.type
|
||||
assert result["external_user_id"] == end_user.external_user_id
|
||||
assert result["name"] == end_user.name
|
||||
assert result["is_anonymous"] is True
|
||||
assert result["session_id"] == end_user.session_id
|
||||
assert result["created_at"].startswith("2024-01-01T00:00:00")
|
||||
assert result["updated_at"].startswith("2024-01-02T00:00:00")
|
||||
|
||||
def test_get_end_user_not_found(self, mocker: MockerFixture, resource: EndUserApi, app_model: App) -> None:
|
||||
mocker.patch("controllers.service_api.end_user.end_user.EndUserService.get_end_user_by_id", return_value=None)
|
||||
assert service.calls == [(context, end_user.id)]
|
||||
assert result == {
|
||||
"id": end_user.id,
|
||||
"tenant_id": end_user.tenant_id,
|
||||
"app_id": end_user.app_id,
|
||||
"type": end_user.type,
|
||||
"external_user_id": end_user.external_user_id,
|
||||
"name": end_user.name,
|
||||
"is_anonymous": True,
|
||||
"session_id": end_user.session_id,
|
||||
"created_at": "2024-01-01T00:00:00Z",
|
||||
"updated_at": "2024-01-02T00:00:00Z",
|
||||
}
|
||||
|
||||
def test_get_end_user_maps_application_not_found_error(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
context = _request_context()
|
||||
service = EndUserQueryServiceStub(AppScopedEndUserNotFoundError())
|
||||
monkeypatch.setattr(
|
||||
controller_module,
|
||||
"application_services",
|
||||
lambda: ApplicationServicesStub(service),
|
||||
)
|
||||
end_user_id = uuid4()
|
||||
|
||||
with pytest.raises(EndUserNotFoundError):
|
||||
EndUserApi.get.__wrapped__(resource, app_model=app_model, end_user_id=uuid4())
|
||||
unwrap(EndUserApi.get)(
|
||||
EndUserApi(),
|
||||
app_model=App(id=context.app_id, tenant_id=context.tenant_id),
|
||||
end_user_id=end_user_id,
|
||||
)
|
||||
|
||||
assert service.calls == [(context, str(end_user_id))]
|
||||
Reference in new issue
Block a user