refactor(api): move app-scoped end users behind application services (#41398)

This commit is contained in:
Byron.wang authored and GitHub committed 2026-09-22 11:20:45 +00:00
1 parent 9badde4d65
commit ce2eaab9f7
41 files changed
+1384 -481

No files matched your search

@@ -1,3 +1,4 @@
from collections.abc import Mapping
from unittest.mock import MagicMock, patch
import pytest
@@ -42,6 +43,22 @@ def _end_user() -> EndUser:
)
class _EndUserServiceStub:
def __init__(self) -> None:
self.result: Mapping[str, EndUser] = {}
self.calls: list[tuple[EndUserType, str, list[str], str]] = []
def create_end_user_batch(
self,
type: EndUserType,
tenant_id: str,
app_ids: list[str],
user_id: str,
) -> Mapping[str, EndUser]:
self.calls.append((type, tenant_id, app_ids, user_id))
return self.result
class TestDispatchTriggeredWorkflow:
"""Unit tests covering branch behaviours of ``dispatch_triggered_workflow``.
@@ -97,7 +114,7 @@ class TestDispatchTriggeredWorkflow:
Defaults are configured so the code flow can reach the final async
trigger block (line ~385); each test overrides specific handles
(``get_workflows``, ``reserve``, ``create_end_user_batch``, ...) to
(``get_workflows``, ``reserve``, ``end_users``, ...) to
drive the path it targets.
"""
invoke_response = MagicMock()
@@ -105,6 +122,7 @@ class TestDispatchTriggeredWorkflow:
invoke_response.variables = {}
quota_charge = MagicMock()
end_users = _EndUserServiceStub()
with (
patch.object(
@@ -141,11 +159,6 @@ class TestDispatchTriggeredWorkflow:
trigger_processing_tasks_module,
"_get_published_workflows_by_app_ids",
) as get_workflows,
patch.object(
trigger_processing_tasks_module.EndUserService,
"create_end_user_batch",
return_value={},
) as create_end_user_batch,
patch.object(
trigger_processing_tasks_module.QuotaService,
"reserve",
@@ -167,7 +180,7 @@ class TestDispatchTriggeredWorkflow:
"mark_rate_limited": mark_rate_limited,
"invoke_trigger_event": invoke_trigger_event,
"invoke_response": invoke_response,
"create_end_user_batch": create_end_user_batch,
"end_users": end_users,
"trigger_workflow_async": trigger_workflow_async,
}
@@ -180,6 +193,7 @@ class TestDispatchTriggeredWorkflow:
subscription=subscription,
event_name="test_event",
request_id="request-123",
end_users=dispatch_mocks["end_users"],
)
assert dispatched == 0
@@ -200,6 +214,7 @@ class TestDispatchTriggeredWorkflow:
subscription=subscription,
event_name="test_event",
request_id="request-123",
end_users=dispatch_mocks["end_users"],
)
assert dispatched == 0
@@ -215,13 +230,14 @@ class TestDispatchTriggeredWorkflow:
dispatch_mocks["get_workflows"].return_value = {plugin_trigger.app_id: workflow}
end_user = _end_user()
dispatch_mocks["create_end_user_batch"].return_value = {plugin_trigger.app_id: end_user}
dispatch_mocks["end_users"].result = {plugin_trigger.app_id: end_user}
dispatched = dispatch_triggered_workflow(
user_id="user-123",
subscription=subscription,
event_name="test_event",
request_id="request-123",
end_users=dispatch_mocks["end_users"],
)
assert dispatched == 1