Files
eryao/backend/tests/unit/test_user_service_delete_account.py
T
qzl c55be6d3fd fix: preserve points balance across account re-registration
Persist a per-email balance snapshot before account deletion and restore it on same-email re-registration, preventing both unintended balance reset and repeated signup bonus grants.
2026-04-13 11:28:58 +08:00

95 lines
2.8 KiB
Python

from __future__ import annotations
from uuid import uuid4
import pytest
from core.auth.models import CurrentUser
from core.http.errors import ApiProblemError
from v1.users.service import UserService
class _NoopRepository:
session = None
class _FakeStorage:
def __init__(self) -> None:
self.deleted_prefix_calls: list[tuple[str, str]] = []
self.deleted_auth_user_calls: list[str] = []
async def delete_prefix(self, *, bucket: str, prefix: str) -> int:
self.deleted_prefix_calls.append((bucket, prefix))
return 0
async def delete_auth_user(self, *, user_id: str) -> None:
self.deleted_auth_user_calls.append(user_id)
@pytest.mark.asyncio
async def test_delete_account_success_calls_storage_cleanup_and_auth_delete() -> None:
user = CurrentUser(id=uuid4(), email=None)
storage = _FakeStorage()
service = UserService(
current_user=user,
repository=_NoopRepository(), # type: ignore[arg-type]
attachment_storage=storage, # type: ignore[arg-type]
)
await service.delete_account()
assert storage.deleted_prefix_calls == [("avatars", f"{user.id}/")]
assert storage.deleted_auth_user_calls == [str(user.id)]
@pytest.mark.asyncio
async def test_delete_account_raises_profile_delete_failed_on_storage_cleanup_error() -> (
None
):
user = CurrentUser(id=uuid4(), email=None)
class _FailingStorage(_FakeStorage):
async def delete_prefix(self, *, bucket: str, prefix: str) -> int:
raise RuntimeError("storage unavailable")
storage = _FailingStorage()
service = UserService(
current_user=user,
repository=_NoopRepository(), # type: ignore[arg-type]
attachment_storage=storage, # type: ignore[arg-type]
)
with pytest.raises(ApiProblemError) as exc_info:
await service.delete_account()
err = exc_info.value
assert err.status_code == 502
assert err.code == "PROFILE_DELETE_FAILED"
assert storage.deleted_auth_user_calls == []
@pytest.mark.asyncio
async def test_delete_account_raises_profile_delete_failed_on_auth_delete_error() -> (
None
):
user = CurrentUser(id=uuid4(), email=None)
class _FailingStorage(_FakeStorage):
async def delete_auth_user(self, *, user_id: str) -> None:
raise RuntimeError("delete user failed")
storage = _FailingStorage()
service = UserService(
current_user=user,
repository=_NoopRepository(), # type: ignore[arg-type]
attachment_storage=storage, # type: ignore[arg-type]
)
with pytest.raises(ApiProblemError) as exc_info:
await service.delete_account()
err = exc_info.value
assert err.status_code == 502
assert err.code == "PROFILE_DELETE_FAILED"
assert storage.deleted_prefix_calls == [("avatars", f"{user.id}/")]