Files
live-hub-py/tests/test_weibo_collab.py
T

169 lines
4.8 KiB
Python

"""微博外部单协作核心流程测试。"""
from __future__ import annotations
from sqlalchemy.orm import Session
from web.backend.models import User, WeiboAccount, WeiboExternalOrderMetric
from web.backend.services import weibo_collab_service as collab
def make_account(db: Session, uid: str = "4014000001") -> WeiboAccount:
account = WeiboAccount(
uid=uid,
nickname="测试微博",
cookie="SCF=a; SUB=b",
tag="",
remark="",
activity="bowen_tjg",
is_enabled=True,
ck_check_status="valid",
is_bound=True,
game_role_name="角色A",
can_rebind=False,
rebind_remain_days=7,
weidou_balance=0,
)
db.add(account)
db.commit()
db.refresh(account)
return account
def make_user(db: Session) -> User:
user = User(username="weibo-collab", password_hash="hash", role="super_admin")
db.add(user)
db.commit()
db.refresh(user)
return user
def test_create_orders_groups_and_balance_check(monkeypatch, db):
user = make_user(db)
account = make_account(db)
class FakeClient:
def __init__(self, *_args, **_kwargs):
pass
def list_mall(self, *_args, **_kwargs):
return {"weidou_balance": 400, "list": []}
monkeypatch.setattr(collab, "WeiboClient", FakeClient)
first = collab.create_order(
db,
user,
order_no="WB-001",
shop="店铺A",
account_id=account.id,
goods_name="商品A",
required_weidou=100,
home_images=["proof/a.png"],
require_verified=False,
)
second = collab.create_order(
db,
user,
order_no="WB-002",
shop="店铺A",
account_id=account.id,
goods_name="商品B",
required_weidou=200,
require_verified=False,
)
assert first.status == collab.STATUS_TO_RECHARGE
assert second.status == collab.STATUS_TO_RECHARGE
assert first.game_role_name == "角色A"
result = collab.check_balance_groups(
db,
user,
[first],
threshold=100,
activity="bowen_tjg",
)
assert result["updated"] == 2
assert result["groups"][0]["balance"] == 400
assert result["groups"][0]["required_total"] == 300
assert result["groups"][0]["surplus"] == 100
db.refresh(first)
db.refresh(second)
assert first.status == collab.STATUS_RECHARGED
assert second.status == collab.STATUS_RECHARGED
assert first.recharged_at is not None
assert db.query(WeiboExternalOrderMetric).count() == 2
def test_exchange_record_unique_match_and_archive(monkeypatch, db):
user = make_user(db)
account = make_account(db)
first = collab.create_order(
db,
user,
order_no="WB-101",
shop="店铺A",
account_id=account.id,
goods_name="商品A",
required_weidou=100,
require_verified=False,
)
second = collab.create_order(
db,
user,
order_no="WB-102",
shop="店铺A",
account_id=account.id,
goods_name="商品B",
required_weidou=200,
require_verified=False,
)
class FakeClient:
def __init__(self, *_args, **_kwargs):
pass
def list_mall(self, *_args, **_kwargs):
return {"weidou_balance": 500, "list": []}
def list_exchange_records(self, *_args, **_kwargs):
return {
"list": [
{
"goods_name": "商品A",
"game_account": "角色A",
"cost_weidou": 100,
"exchange_time": "2099-01-01 00:00:00",
"reward_status": 2,
}
]
}
monkeypatch.setattr(collab, "WeiboClient", FakeClient)
collab.check_balance_groups(db, user, [first], threshold=100)
collab.set_status(db, user, first, collab.STATUS_EXCHANGE_PENDING)
collab.set_status(db, user, second, collab.STATUS_EXCHANGE_PENDING)
result = collab.check_exchange_records(db, user, [first, second])
assert result["success"] == 1
assert result["unknown"] == 1
db.refresh(first)
db.refresh(second)
assert first.status == collab.STATUS_DONE
assert first.exchange_verdict == collab.VERDICT_SUCCESS
assert second.status == collab.STATUS_EXCHANGE_PENDING
assert second.exchange_verdict == collab.VERDICT_UNKNOWN
archived = collab.archive_orders(db, user, [first.id], reason="客户验收")
assert archived["archived"] == 1
db.refresh(first)
assert first.archived_at is not None
assert first.archive_reason == "客户验收"
timeline = collab.order_timeline(db, first.id)
assert timeline
assert any(item["action"] == "weibo:collab:archive" for item in timeline)