feat: 添加账号删除功能
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
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:
|
||||
pass
|
||||
|
||||
|
||||
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="test@example.com")
|
||||
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="test@example.com")
|
||||
|
||||
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="test@example.com")
|
||||
|
||||
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}/")]
|
||||
Reference in New Issue
Block a user