174 lines
4.9 KiB
Python
174 lines
4.9 KiB
Python
"""账号删除的活动占用保护与关联清理测试。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
|
|
import pytest
|
|
from fastapi import HTTPException
|
|
|
|
from web.backend.models import (
|
|
Account,
|
|
AccountEvent,
|
|
AccountLease,
|
|
AuditLog,
|
|
DouyuTask,
|
|
DouyuWorkbenchAccount,
|
|
LoginTask,
|
|
PlatformFulfillmentTask,
|
|
User,
|
|
)
|
|
from web.backend.routers.accounts import batch_delete_accounts_selection, delete_account
|
|
from web.backend.schemas import AccountBulkSelection
|
|
from web.backend.services.account_deletion import AccountDeleteBlocked, delete_accounts
|
|
|
|
|
|
def _user(db, name: str = "delete-admin") -> User:
|
|
user = User(
|
|
username=name,
|
|
password_hash="h",
|
|
role="super_admin",
|
|
is_active=True,
|
|
)
|
|
db.add(user)
|
|
db.commit()
|
|
return user
|
|
|
|
|
|
def _account(db, name: str) -> Account:
|
|
account = Account(
|
|
username=name,
|
|
password="p",
|
|
email=f"{name}@example.com",
|
|
email_password="ep",
|
|
)
|
|
db.add(account)
|
|
db.commit()
|
|
return account
|
|
|
|
|
|
def _lease(db, account: Account, user: User, status: str = "active") -> AccountLease:
|
|
lease = AccountLease(
|
|
account_id=account.id,
|
|
holder_user_id=user.id,
|
|
scope="elite",
|
|
purpose="platform_fulfillment",
|
|
status=status,
|
|
released_at=datetime.now(UTC) if status != "active" else None,
|
|
)
|
|
db.add(lease)
|
|
db.commit()
|
|
return lease
|
|
|
|
|
|
def test_active_lease_blocks_single_delete_without_partial_cleanup(db):
|
|
user = _user(db)
|
|
account = _account(db, "delete-active")
|
|
_lease(db, account, user)
|
|
event = AccountEvent(
|
|
account_id=account.id,
|
|
event_type="test",
|
|
category="ops",
|
|
occurred_at=datetime.now(UTC),
|
|
)
|
|
db.add(event)
|
|
db.commit()
|
|
|
|
with pytest.raises(HTTPException) as exc_info:
|
|
delete_account(account.id, db=db, current=user)
|
|
|
|
assert exc_info.value.status_code == 409
|
|
assert "活动履约租约" in str(exc_info.value.detail)
|
|
assert db.get(Account, account.id) is not None
|
|
assert db.get(AccountEvent, event.id) is not None
|
|
|
|
|
|
def test_delete_cleans_non_cascade_children_and_keeps_audit(db):
|
|
user = _user(db, "delete-history-admin")
|
|
account = _account(db, "delete-history")
|
|
lease = _lease(db, account, user, status="expired")
|
|
task = PlatformFulfillmentTask(
|
|
source_order_id="delete-order",
|
|
source_order_item_id="delete-item",
|
|
platform_order_id="delete-platform-order",
|
|
account_id=account.id,
|
|
lease_id=lease.id,
|
|
execution_status="completed",
|
|
binding_status="bound",
|
|
exchange_status="not_started",
|
|
)
|
|
event = AccountEvent(
|
|
account_id=account.id,
|
|
event_type="test",
|
|
category="business",
|
|
occurred_at=datetime.now(UTC),
|
|
)
|
|
login = LoginTask(
|
|
batch_id="delete-login",
|
|
account_id=account.id,
|
|
status="success",
|
|
created_by=user.id,
|
|
)
|
|
douyu = DouyuTask(
|
|
batch_id="delete-douyu",
|
|
account_id=account.id,
|
|
task_type="query_points",
|
|
status="success",
|
|
created_by=user.id,
|
|
)
|
|
workbench = DouyuWorkbenchAccount(
|
|
user_id=user.id,
|
|
handbook_scope="elite",
|
|
account_id=account.id,
|
|
)
|
|
db.add_all([task, event, login, douyu, workbench])
|
|
db.commit()
|
|
event_id = event.id
|
|
lease_id = lease.id
|
|
login_id = login.id
|
|
douyu_id = douyu.id
|
|
workbench_id = workbench.id
|
|
task_id = task.id
|
|
account_id = account.id
|
|
|
|
delete_account(account_id, db=db, current=user)
|
|
|
|
assert db.get(Account, account_id) is None
|
|
assert db.get(AccountEvent, event_id) is None
|
|
assert db.get(AccountLease, lease_id) is None
|
|
assert db.get(LoginTask, login_id) is None
|
|
assert db.get(DouyuTask, douyu_id) is None
|
|
assert db.get(DouyuWorkbenchAccount, workbench_id) is None
|
|
refreshed_platform_task = db.get(PlatformFulfillmentTask, task_id)
|
|
assert refreshed_platform_task is not None
|
|
assert refreshed_platform_task.account_id is None
|
|
assert refreshed_platform_task.lease_id is None
|
|
audit = (
|
|
db.query(AuditLog)
|
|
.filter(AuditLog.action == "account:delete", AuditLog.account_id == account_id)
|
|
.one()
|
|
)
|
|
assert audit.target == "delete-history"
|
|
|
|
|
|
def test_batch_delete_is_atomic_when_one_account_is_active(db):
|
|
user = _user(db, "delete-batch-admin")
|
|
free = _account(db, "delete-batch-free")
|
|
busy = _account(db, "delete-batch-busy")
|
|
_lease(db, busy, user)
|
|
|
|
with pytest.raises(AccountDeleteBlocked):
|
|
delete_accounts(db, account_ids=[free.id, busy.id])
|
|
db.rollback()
|
|
|
|
assert db.get(Account, free.id) is not None
|
|
assert db.get(Account, busy.id) is not None
|
|
|
|
with pytest.raises(HTTPException) as exc_info:
|
|
batch_delete_accounts_selection(
|
|
AccountBulkSelection(account_ids=[free.id, busy.id]),
|
|
db=db,
|
|
current=user,
|
|
)
|
|
assert exc_info.value.status_code == 409
|