Files
live-hub-py/tests/test_account_role_change_lock.py
T
yml 2e6303361c feat(accounts): 统一可换绑时间并加本地 7 天锁
- 新增官方时间、本地 bind_ok、7 天锁到期、检查时间和来源字段
- role_change_available_at 改为官方时间与本地锁取较晚者
- 绑定成功和官方探测统一写有效时间
- 分配过滤增加最近 elite bind_ok 的数据库兜底
- 迁移 0129 从 account_events 回填历史账号
- 补充字段/回填过滤测试和生命周期文档
2026-11-24 17:37:15 +08:00

136 lines
4.0 KiB
Python

"""账号可换绑时间:本地 7 天锁、有效时间与分配过滤。"""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
from web.backend.models import Account, AccountEvent
from web.backend.services.account_binding import (
effective_role_change_available_at,
role_bind_lock_until,
)
from web.backend.services.account_query_filters import role_change_ready_filter
from web.backend.services.business_events import record_bind
NOW = datetime.now(UTC).replace(microsecond=0)
def _account(name: str, **overrides) -> Account:
values = {
"username": name,
"password": "p",
"email": f"{name}@example.com",
"email_password": "ep",
"status": "active",
"auto_managed": True,
"cookie": "acf_uid=1",
"ck_check_status": "valid",
"ck_checked_at": NOW,
"cookie_updated_at": NOW,
}
values.update(overrides)
return Account(**values)
def _bind_event(account: Account, occurred_at: datetime, ref: str) -> AccountEvent:
return AccountEvent(
account_id=account.id,
event_type="bind_ok",
category="business",
occurred_at=occurred_at,
title="精英宝典绑定:旧角色",
detail={"scope": "elite", "role_name": "旧角色"},
source="douyu_task",
subject_type="elite_role",
subject_key="旧角色",
source_ref=ref,
)
def test_effective_time_uses_later_of_official_and_local_lock():
official = datetime(2026, 11, 24, 0, 0, tzinfo=UTC)
bound = datetime(2026, 11, 17, 14, 49, 25, tzinfo=UTC)
assert role_bind_lock_until(bound) == datetime(2026, 11, 24, 14, 49, 25, tzinfo=UTC)
assert effective_role_change_available_at(
official_at=official, last_bind_ok_at=bound
) == datetime(2026, 11, 24, 14, 49, 25, tzinfo=UTC)
assert effective_role_change_available_at(
official_at=official, last_bind_ok_at=None
) == official
assert effective_role_change_available_at(
official_at=None, last_bind_ok_at=None
) is None
def test_record_bind_sets_local_lock_and_effective_time(db):
account = _account(
"role-lock-bind",
role_change_official_at=datetime(2026, 11, 24, 0, 0, tzinfo=UTC),
role_change_available_at=datetime(2026, 11, 24, 0, 0, tzinfo=UTC),
)
db.add(account)
db.commit()
occurred = datetime(2026, 11, 17, 14, 49, 25, tzinfo=UTC)
assert record_bind(
db,
account,
task_id=990001,
scope="elite",
role_name="旧角色",
occurred_at=occurred,
actor_id=1,
)
db.commit()
db.refresh(account)
assert account.last_bind_ok_at == occurred
assert account.role_bind_lock_until == datetime(2026, 11, 24, 14, 49, 25, tzinfo=UTC)
assert account.role_change_available_at == account.role_bind_lock_until
assert account.role_change_source == "local_bind"
def test_role_change_filter_rejects_recent_bind_even_if_field_says_ready(db):
ready = _account(
"role-lock-ready",
role_change_available_at=NOW - timedelta(days=1),
)
locked = _account(
"role-lock-recent",
role_change_available_at=NOW - timedelta(days=1),
)
db.add_all([ready, locked])
db.flush()
db.add(_bind_event(locked, NOW - timedelta(days=1), "role-filter:recent"))
db.commit()
rows = (
db.query(Account.id)
.filter(role_change_ready_filter(NOW))
.filter(Account.id.in_([ready.id, locked.id]))
.all()
)
assert [row[0] for row in rows] == [ready.id]
def test_role_change_filter_allows_old_bind(db):
old = _account(
"role-lock-old",
role_change_available_at=NOW - timedelta(days=8),
)
db.add(old)
db.flush()
db.add(_bind_event(old, NOW - timedelta(days=8), "role-filter:old"))
db.commit()
rows = (
db.query(Account.id)
.filter(role_change_ready_filter(NOW))
.filter(Account.id == old.id)
.all()
)
assert [row[0] for row in rows] == [old.id]