Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

test: use real account service composition (#43563)

Asuka Minato6bb84c389273
diff --git a/api/tests/unit_tests/controllers/console/workspace/test_accounts.py b/api/tests/unit_tests/controllers/console/workspace/test_accounts.py
index e469fa5555d..d8ff65f993f 100644
--- a/api/tests/unit_tests/controllers/console/workspace/test_accounts.py
+++ b/api/tests/unit_tests/controllers/console/workspace/test_accounts.py
@@ -1,6 +1,6 @@
import inspect
+from collections.abc import Iterator
from datetime import datetime
-from types import SimpleNamespace
from unittest.mock import MagicMock, PropertyMock, patch
from uuid import NAMESPACE_URL, uuid5
@@ -56,6 +56,9 @@ from controllers.console.workspace.error import (
InvalidAccountDeletionCodeError,
MissingInvitationCodeRequestError,
)
+from extensions.application_services.account import AccountServices
+from extensions.ext_application_services import ApplicationServices
+from libs.login import AccountWithTenant
from machinery.context import RequestContext
from models import Account, Tenant, TenantAccountJoin
from models.account import AccountStatus, TenantAccountRole
@@ -110,8 +113,20 @@ def persist_account_with_tenant(
return account, tenant
+def account_services(
+ account_application_services: ApplicationServices,
+) -> Iterator[AccountServices]:
+ """Use the real application-service composition root in controller tests."""
+ with patch(
+ "controllers.console.workspace.account.application_services",
+ return_value=account_application_services,
+ ):
+ yield account_application_services.accounts
+
+
class TestAccountInitApi:
- def test_init_success(self, app: Flask):
+ def test_init_success(self, app: Flask, account_services: AccountServices):
api = AccountInitApi()
method = inspect.unwrap(api.post)
request_context = RequestContext(
@@ -120,7 +135,17 @@ class TestAccountInitApi:
account_id="account-1",
active_workspace_id="workspace-1",
)
- initialization = MagicMock()
+ initialize_calls: list[tuple[RequestContext, str, str, str | None]] = []
+
+ def initialize(
+ context: RequestContext,
+ *,
+ interface_language: str,
+ timezone: str,
+ invitation_code: str | None,
+ ) -> None:
+ initialize_calls.append((context, interface_language, timezone, invitation_code))
+
payload = {
"interface_language": "en-US",
"timezone": "UTC",
@@ -129,22 +154,14 @@ class TestAccountInitApi:
with (
app.test_request_context("/account/init", json=payload),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(initialization=initialization)),
- ),
+ patch.object(account_services.initialization, "initialize", initialize),
):
resp = method(api, AccountInitPayload.model_validate(payload), request_context)
assert resp["result"] == "success"
- initialization.initialize.assert_called_once_with(
- request_context,
- interface_language="en-US",
- timezone="UTC",
- invitation_code="code123",
- )
+ assert initialize_calls == [(request_context, "en-US", "UTC", "code123")]
- def test_init_already_initialized(self, app: Flask):
+ def test_init_already_initialized(self, app: Flask, account_services: AccountServices):
api = AccountInitApi()
method = inspect.unwrap(api.post)
@@ -154,21 +171,20 @@ class TestAccountInitApi:
account_id="account-1",
active_workspace_id="workspace-1",
)
- initialization = MagicMock()
- initialization.initialize.side_effect = AccountAlreadyInitializedError
+
+ def initialize(*_args: object, **_kwargs: object) -> None:
+ raise AccountAlreadyInitializedError
+
payload = {"interface_language": "en-US", "timezone": "UTC"}
with (
app.test_request_context("/account/init", json=payload),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(initialization=initialization)),
- ),
+ patch.object(account_services.initialization, "initialize", initialize),
):
with pytest.raises(AccountAlreadyInitedError):
method(api, AccountInitPayload.model_validate(payload), request_context)
- def test_init_missing_invitation_code_is_mapped(self, app: Flask):
+ def test_init_missing_invitation_code_is_mapped(self, app: Flask, account_services: AccountServices):
api = AccountInitApi()
method = inspect.unwrap(api.post)
request_context = RequestContext(
@@ -177,16 +193,15 @@ class TestAccountInitApi:
account_id="account-1",
active_workspace_id="workspace-1",
)
- initialization = MagicMock()
- initialization.initialize.side_effect = MissingInvitationCodeError("invitation_code is required")
+
+ def initialize(*_args: object, **_kwargs: object) -> None:
+ raise MissingInvitationCodeError("invitation_code is required")
+
payload = {"interface_language": "en-US", "timezone": "UTC"}
with (
app.test_request_context("/account/init", json=payload),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(initialization=initialization)),
- ),
+ patch.object(account_services.initialization, "initialize", initialize),
):
with pytest.raises(MissingInvitationCodeRequestError) as exc_info:
method(api, AccountInitPayload.model_validate(payload), request_context)
@@ -199,7 +214,7 @@ class TestAccountInitApi:
class TestAccountProfileApi:
- def test_get_profile_success(self, app: Flask):
+ def test_get_profile_success(self, app: Flask, account_services: AccountServices):
api = AccountProfileApi()
method = inspect.unwrap(api.get)
user = make_account()
@@ -209,20 +224,16 @@ class TestAccountProfileApi:
account_id=user.id,
active_workspace_id="workspace-1",
)
- profile = MagicMock()
- profile.get.return_value = user
+ get_profile = MagicMock(return_value=user)
with (
app.test_request_context("/account/profile"),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(profile=profile)),
- ),
+ patch.object(account_services.profile, "get", get_profile),
):
result = method(api, request_context)
assert result["id"] == user.id
- profile.get.assert_called_once_with(request_context)
+ get_profile.assert_called_once_with(request_context)
class TestAccountUpdateApis:
@@ -247,7 +258,13 @@ class TestAccountUpdateApis:
],
)
def test_deprecated_update_routes_delegate_to_profile_service(
- self, app: Flask, api_cls, payload_model, payload, expected_changes: AccountProfileChanges
+ self,
+ app: Flask,
+ account_services: AccountServices,
+ api_cls,
+ payload_model,
+ payload,
+ expected_changes: AccountProfileChanges,
):
api = api_cls()
method = inspect.unwrap(api.post)
@@ -258,20 +275,16 @@ class TestAccountUpdateApis:
account_id=user.id,
active_workspace_id="workspace-1",
)
- profile = MagicMock()
- profile.update.return_value = user
+ update_profile = MagicMock(return_value=user)
with (
app.test_request_context("/", json=payload),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(profile=profile)),
- ),
+ patch.object(account_services.profile, "update", update_profile),
):
result = method(api, payload_model.model_validate(payload), request_context)
assert result["id"] == user.id
- profile.update.assert_called_once_with(request_context, expected_changes)
+ update_profile.assert_called_once_with(request_context, expected_changes)
def test_deprecated_update_routes_are_marked_deprecated(self):
for api_cls in (
@@ -309,7 +322,7 @@ class TestAccountProfilePatchApi:
):
assert list(validator.iter_errors(payload))
- def test_updates_multiple_profile_fields(self, app: Flask):
+ def test_updates_multiple_profile_fields(self, app: Flask, account_services: AccountServices):
api = AccountProfileApi()
method = inspect.unwrap(api.patch)
user = make_account()
@@ -319,27 +332,23 @@ class TestAccountProfilePatchApi:
account_id=user.id,
active_workspace_id="workspace-1",
)
- profile = MagicMock()
- profile.update.return_value = user
+ update_profile = MagicMock(return_value=user)
payload = {"name": "Jane", "interface_language": "en-US", "timezone": "UTC"}
args = AccountProfilePatchPayload.model_validate(payload)
with (
app.test_request_context("/account/profile", method="PATCH", json=payload),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(profile=profile)),
- ),
+ patch.object(account_services.profile, "update", update_profile),
):
result = method(api, args, request_context)
assert result["id"] == user.id
- profile.update.assert_called_once_with(
+ update_profile.assert_called_once_with(
request_context,
AccountProfileChanges(name="Jane", interface_language="en-US", timezone="UTC"),
)
- def test_empty_patch_is_a_noop(self, app: Flask):
+ def test_empty_patch_is_a_noop(self, app: Flask, account_services: AccountServices):
api = AccountProfileApi()
method = inspect.unwrap(api.patch)
user = make_account()
@@ -349,21 +358,17 @@ class TestAccountProfilePatchApi:
account_id=user.id,
active_workspace_id="workspace-1",
)
- profile = MagicMock()
- profile.update.return_value = user
+ update_profile = MagicMock(return_value=user)
args = AccountProfilePatchPayload.model_validate({})
with (
app.test_request_context("/account/profile", method="PATCH", json={}),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(profile=profile)),
- ),
+ patch.object(account_services.profile, "update", update_profile),
):
result = method(api, args, request_context)
assert result["id"] == user.id
- profile.update.assert_called_once_with(request_context, AccountProfileChanges())
+ update_profile.assert_called_once_with(request_context, AccountProfileChanges())
@pytest.mark.parametrize("payload", [{"name": None}, {"unexpected": "value"}])
def test_rejects_null_or_unknown_changes(self, payload: dict[str, object]):
@@ -372,7 +377,7 @@ class TestAccountProfilePatchApi:
class TestAccountAvatarApiGet:
- def test_get_avatar_delegates_to_service(self, app: Flask):
+ def test_get_avatar_delegates_to_service(self, app: Flask, account_services: AccountServices):
api = AccountAvatarApi()
method = inspect.unwrap(api.get)
file_id = "550e8400-e29b-41d4-a716-446655440000"
@@ -382,22 +387,18 @@ class TestAccountAvatarApiGet:
account_id="account-1",
active_workspace_id="workspace-1",
)
- avatar = MagicMock()
- avatar.resolve.return_value = "https://signed/example"
+ resolve_avatar = MagicMock(return_value="https://signed/example")
with (
app.test_request_context(f"/account/avatar?avatar={file_id}"),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(avatar=avatar)),
- ),
+ patch.object(account_services.avatar, "resolve", resolve_avatar),
):
result = method(api, AccountAvatarQuery(avatar=file_id), request_context)
assert result == {"avatar_url": "https://signed/example"}
- avatar.resolve.assert_called_once_with(request_context, file_id)
+ resolve_avatar.assert_called_once_with(request_context, file_id)
- def test_get_avatar_maps_not_found(self, app: Flask):
+ def test_get_avatar_maps_not_found(self, app: Flask, account_services: AccountServices):
api = AccountAvatarApi()
method = inspect.unwrap(api.get)
file_id = "550e8400-e29b-41d4-a716-446655440001"
@@ -407,15 +408,11 @@ class TestAccountAvatarApiGet:
account_id="account-1",
active_workspace_id="workspace-1",
)
- avatar = MagicMock()
- avatar.resolve.side_effect = AvatarFileNotFoundError
+ resolve_avatar = MagicMock(side_effect=AvatarFileNotFoundError)
with (
app.test_request_context(f"/account/avatar?avatar={file_id}"),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(avatar=avatar)),
- ),
+ patch.object(account_services.avatar, "resolve", resolve_avatar),
):
with pytest.raises(NotFound):
method(api, AccountAvatarQuery(avatar=file_id), request_context)
@@ -433,7 +430,7 @@ class TestAccountAvatarApiGet:
),
patch(
"controllers.console.flask_admission.current_account_with_tenant",
- return_value=SimpleNamespace(account=account, tenant_id="workspace-1"),
+ return_value=AccountWithTenant(account=account, tenant_id="workspace-1"),
),
):
with pytest.raises(UnprocessableEntity) as exc_info:
@@ -457,7 +454,7 @@ class TestConvertedPostDecorator:
),
patch(
"controllers.console.flask_admission.current_account_with_tenant",
- return_value=SimpleNamespace(account=account, tenant_id="workspace-1"),
+ return_value=AccountWithTenant(account=account, tenant_id="workspace-1"),
),
):
with pytest.raises(UnprocessableEntity) as exc_info:
@@ -467,7 +464,7 @@ class TestConvertedPostDecorator:
class TestAccountPasswordApi:
- def test_password_success(self, app: Flask):
+ def test_password_success(self, app: Flask, account_services: AccountServices):
api = AccountPasswordApi()
method = inspect.unwrap(api.post)
@@ -484,26 +481,22 @@ class TestAccountPasswordApi:
account_id=user.id,
active_workspace_id="workspace-1",
)
- password = MagicMock()
- password.change.return_value = user
+ change_password = MagicMock(return_value=user)
with (
app.test_request_context("/", json=payload),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(password=password)),
- ),
+ patch.object(account_services.password, "change", change_password),
):
result = method(api, AccountPasswordPayload.model_validate(payload), request_context)
assert result["id"] == user.id
- password.change.assert_called_once_with(
+ change_password.assert_called_once_with(
request_context,
current_password="old",
new_password="new123",
)
- def test_password_wrong_current(self, app: Flask):
+ def test_password_wrong_current(self, app: Flask, account_services: AccountServices):
api = AccountPasswordApi()
method = inspect.unwrap(api.post)
@@ -519,20 +512,16 @@ class TestAccountPasswordApi:
account_id=user.id,
active_workspace_id="workspace-1",
)
- password = MagicMock()
- password.change.side_effect = CurrentAccountPasswordIncorrectError
+ change_password = MagicMock(side_effect=CurrentAccountPasswordIncorrectError)
with (
app.test_request_context("/", json=payload),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(password=password)),
- ),
+ patch.object(account_services.password, "change", change_password),
):
with pytest.raises(CurrentPasswordIncorrectError):
method(api, AccountPasswordPayload.model_validate(payload), request_context)
- def test_password_policy_error_is_mapped(self, app: Flask):
+ def test_password_policy_error_is_mapped(self, app: Flask, account_services: AccountServices):
api = AccountPasswordApi()
method = inspect.unwrap(api.post)
payload = {
@@ -546,17 +535,15 @@ class TestAccountPasswordApi:
account_id="account-1",
active_workspace_id="workspace-1",
)
- password = MagicMock()
- password.change.side_effect = InvalidAccountPasswordError(
- "Password must contain letters and numbers, and the length must be at least 8 characters."
+ change_password = MagicMock(
+ side_effect=InvalidAccountPasswordError(
+ "Password must contain letters and numbers, and the length must be at least 8 characters."
+ )
)
with (
app.test_request_context("/", json=payload),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(password=password)),
- ),
+ patch.object(account_services.password, "change", change_password),
):
with pytest.raises(InvalidAccountPasswordRequestError) as exc_info:
method(api, AccountPasswordPayload.model_validate(payload), request_context)
@@ -569,7 +556,7 @@ class TestAccountPasswordApi:
class TestAccountIntegrateApi:
- def test_get_integrates(self, app: Flask):
+ def test_get_integrates(self, app: Flask, account_services: AccountServices):
api = AccountIntegrateApi()
method = inspect.unwrap(api.get)
request_context = RequestContext(
@@ -578,22 +565,20 @@ class TestAccountIntegrateApi:
account_id="account-1",
active_workspace_id="workspace-1",
)
- integrations = MagicMock()
- integrations.list.return_value = [
- AccountIntegrationStatus(provider="github", created_at=datetime(2026, 1, 1), is_bound=True),
- AccountIntegrationStatus(provider="google", created_at=None, is_bound=False),
- ]
+ list_integrations = MagicMock(
+ return_value=[
+ AccountIntegrationStatus(provider="github", created_at=datetime(2026, 1, 1), is_bound=True),
+ AccountIntegrationStatus(provider="google", created_at=None, is_bound=False),
+ ]
+ )
with (
app.test_request_context("/"),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(integrations=integrations)),
- ),
+ patch.object(account_services.integrations, "list", list_integrations),
):
result = method(api, request_context)
- integrations.list.assert_called_once_with(request_context)
+ list_integrations.assert_called_once_with(request_context)
assert result["data"][0]["provider"] == "github"
assert result["data"][0]["is_bound"] is True
assert result["data"][0]["link"] is None
@@ -603,7 +588,7 @@ class TestAccountIntegrateApi:
class TestAccountDeleteApi:
- def test_delete_verify_success(self, app: Flask):
+ def test_delete_verify_success(self, app: Flask, account_services: AccountServices):
api = AccountDeleteVerifyApi()
method = inspect.unwrap(api.get)
request_context = RequestContext(
@@ -612,23 +597,19 @@ class TestAccountDeleteApi:
account_id="account-1",
active_workspace_id="workspace-1",
)
- deletion = MagicMock()
- deletion.issue_verification.return_value = "token"
+ issue_verification = MagicMock(return_value="token")
with (
app.test_request_context("/"),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(deletion=deletion)),
- ),
+ patch.object(account_services.deletion, "issue_verification", issue_verification),
):
result = method(api, request_context)
assert result["result"] == "success"
assert result["data"] == "token"
- deletion.issue_verification.assert_called_once_with(request_context)
+ issue_verification.assert_called_once_with(request_context)
- def test_delete_invalid_code(self, app: Flask):
+ def test_delete_invalid_code(self, app: Flask, account_services: AccountServices):
api = AccountDeleteApi()
method = inspect.unwrap(api.post)
@@ -639,20 +620,16 @@ class TestAccountDeleteApi:
account_id="account-1",
active_workspace_id="workspace-1",
)
- deletion = MagicMock()
- deletion.request_deletion.side_effect = InvalidAccountDeletionVerificationError
+ request_deletion = MagicMock(side_effect=InvalidAccountDeletionVerificationError)
with (
app.test_request_context("/", json=payload),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(deletion=deletion)),
- ),
+ patch.object(account_services.deletion, "request_deletion", request_deletion),
):
with pytest.raises(InvalidAccountDeletionCodeError):
method(api, AccountDeletePayload.model_validate(payload), request_context)
- def test_delete_verify_maps_rate_limit(self, app: Flask):
+ def test_delete_verify_maps_rate_limit(self, app: Flask, account_services: AccountServices):
api = AccountDeleteVerifyApi()
method = inspect.unwrap(api.get)
request_context = RequestContext(
@@ -661,20 +638,16 @@ class TestAccountDeleteApi:
account_id="account-1",
active_workspace_id="workspace-1",
)
- deletion = MagicMock()
- deletion.issue_verification.side_effect = AccountDeletionRateLimitError(1)
+ issue_verification = MagicMock(side_effect=AccountDeletionRateLimitError(1))
with (
app.test_request_context("/"),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(deletion=deletion)),
- ),
+ patch.object(account_services.deletion, "issue_verification", issue_verification),
pytest.raises(EmailCodeAccountDeletionRateLimitExceededError),
):
method(api, request_context)
- def test_delete_success(self, app: Flask):
+ def test_delete_success(self, app: Flask, account_services: AccountServices):
api = AccountDeleteApi()
method = inspect.unwrap(api.post)
request_context = RequestContext(
@@ -683,24 +656,21 @@ class TestAccountDeleteApi:
account_id="account-1",
active_workspace_id="workspace-1",
)
- deletion = MagicMock()
+ request_deletion = MagicMock()
payload = {"token": "token", "code": "123456"}
with (
app.test_request_context("/", json=payload),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(deletion=deletion)),
- ),
+ patch.object(account_services.deletion, "request_deletion", request_deletion),
):
result = method(api, AccountDeletePayload.model_validate(payload), request_context)
assert result["result"] == "success"
- deletion.request_deletion.assert_called_once_with(request_context, token="token", code="123456")
+ request_deletion.assert_called_once_with(request_context, token="token", code="123456")
class TestChangeEmailApis:
- def test_check_email_code_invalid(self, app: Flask):
+ def test_check_email_code_invalid(self, app: Flask, account_services: AccountServices):
api = ChangeEmailCheckApi()
method = inspect.unwrap(api.post)
@@ -712,8 +682,7 @@ class TestChangeEmailApis:
account_id=user.id,
active_workspace_id="workspace-1",
)
- change_email = MagicMock()
- change_email.verify_code.side_effect = InvalidChangeEmailCodeError
+ verify_code = MagicMock(side_effect=InvalidChangeEmailCodeError)
with (
app.test_request_context("/", json=payload),
@@ -723,15 +692,12 @@ class TestChangeEmailApis:
new_callable=PropertyMock,
return_value=payload,
),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(change_email=change_email)),
- ),
+ patch.object(account_services.change_email, "verify_code", verify_code),
):
with pytest.raises(EmailCodeError):
method(api, ChangeEmailValidityPayload.model_validate(payload), request_context)
- def test_reset_email_already_used(self, app: Flask):
+ def test_reset_email_already_used(self, app: Flask, account_services: AccountServices):
api = ChangeEmailResetApi()
method = inspect.unwrap(api.post)
@@ -743,8 +709,7 @@ class TestChangeEmailApis:
account_id=user.id,
active_workspace_id="workspace-1",
)
- change_email = MagicMock()
- change_email.reset.side_effect = AccountEmailAlreadyInUseError
+ reset_email = MagicMock(side_effect=AccountEmailAlreadyInUseError)
with (
app.test_request_context("/", json=payload),
@@ -754,22 +719,19 @@ class TestChangeEmailApis:
new_callable=PropertyMock,
return_value=payload,
),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(change_email=change_email)),
- ),
+ patch.object(account_services.change_email, "reset", reset_email),
):
with pytest.raises(EmailAlreadyInUseError):
method(api, ChangeEmailResetPayload.model_validate(payload), request_context)
class TestCheckEmailUniqueApi:
- def test_email_unique_success(self, app: Flask):
+ def test_email_unique_success(self, app: Flask, account_services: AccountServices):
api = CheckEmailUnique()
method = inspect.unwrap(api.post)
payload = {"email": "[email protected]"}
- change_email = MagicMock()
+ ensure_available = MagicMock()
with (
app.test_request_context("/", json=payload),
@@ -779,22 +741,18 @@ class TestCheckEmailUniqueApi:
new_callable=PropertyMock,
return_value=payload,
),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(change_email=change_email)),
- ),
+ patch.object(account_services.change_email, "ensure_available", ensure_available),
):
result = method(api, CheckEmailUniquePayload.model_validate(payload))
assert result["result"] == "success"
- def test_email_in_freeze(self, app: Flask):
+ def test_email_in_freeze(self, app: Flask, account_services: AccountServices):
api = CheckEmailUnique()
method = inspect.unwrap(api.post)
payload = {"email": "[email protected]"}
- change_email = MagicMock()
- change_email.ensure_available.side_effect = AccountEmailFrozenError
+ ensure_available = MagicMock(side_effect=AccountEmailFrozenError)
with (
app.test_request_context("/", json=payload),
@@ -804,21 +762,17 @@ class TestCheckEmailUniqueApi:
new_callable=PropertyMock,
return_value=payload,
),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(change_email=change_email)),
- ),
+ patch.object(account_services.change_email, "ensure_available", ensure_available),
):
with pytest.raises(AccountInFreezeError):
method(api, CheckEmailUniquePayload.model_validate(payload))
- def test_email_domain_is_suspended(self, app: Flask):
+ def test_email_domain_is_suspended(self, app: Flask, account_services: AccountServices):
api = CheckEmailUnique()
method = inspect.unwrap(api.post)
payload = {"email": "[email protected]"}
- change_email = MagicMock()
- change_email.ensure_available.side_effect = AccountEmailDomainSuspendedError
+ ensure_available = MagicMock(side_effect=AccountEmailDomainSuspendedError)
with (
app.test_request_context("/", json=payload),
@@ -828,10 +782,7 @@ class TestCheckEmailUniqueApi:
new_callable=PropertyMock,
return_value=payload,
),
- patch(
- "controllers.console.workspace.account.application_services",
- return_value=SimpleNamespace(accounts=SimpleNamespace(change_email=change_email)),
- ),
+ patch.object(account_services.change_email, "ensure_available", ensure_available),
):
with pytest.raises(EmailDomainSuspendedError):
method(api, CheckEmailUniquePayload.model_validate(payload))