feat(weibo): 支持超级管理员强制恢复作废订单
This commit is contained in:
+174
-3
@@ -2,8 +2,10 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import UTC, datetime
|
||||
|
||||
import pytest
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from core.weibo import WeiboAuthError
|
||||
@@ -15,7 +17,11 @@ from web.backend.models import (
|
||||
WeiboRechargeRecord,
|
||||
)
|
||||
from web.backend.routers import weibo_collab as weibo_collab_router
|
||||
from web.backend.schemas import WeiboCollabCkExportOut
|
||||
from web.backend.schemas import (
|
||||
WeiboCollabCkExportOut,
|
||||
WeiboCollabStatusBatchRequest,
|
||||
WeiboCollabStatusRequest,
|
||||
)
|
||||
from web.backend.services import weibo_collab_service as collab
|
||||
from web.backend.services import weibo_service
|
||||
|
||||
@@ -42,14 +48,179 @@ def make_account(db: Session, uid: str = "4014000001") -> WeiboAccount:
|
||||
return account
|
||||
|
||||
|
||||
def make_user(db: Session) -> User:
|
||||
user = User(username="weibo-collab", password_hash="hash", role="super_admin")
|
||||
def make_user(
|
||||
db: Session,
|
||||
username: str = "weibo-collab",
|
||||
role: str = "super_admin",
|
||||
) -> User:
|
||||
user = User(username=username, password_hash="hash", role=role)
|
||||
db.add(user)
|
||||
db.commit()
|
||||
db.refresh(user)
|
||||
return user
|
||||
|
||||
|
||||
def test_super_admin_can_force_restore_void_order(db):
|
||||
user = make_user(db)
|
||||
account = make_account(db)
|
||||
order = collab.create_order(
|
||||
db,
|
||||
user,
|
||||
order_no="WB-FORCE-001",
|
||||
account_id=account.id,
|
||||
goods_name="商品A",
|
||||
required_weidou=100,
|
||||
require_verified=False,
|
||||
)
|
||||
order.scripted_at = datetime.now(UTC)
|
||||
order.done_at = datetime.now(UTC)
|
||||
db.commit()
|
||||
collab.set_status(db, user, order, collab.STATUS_VOID, reason="误作废")
|
||||
|
||||
result = collab.set_status(
|
||||
db,
|
||||
user,
|
||||
order,
|
||||
collab.STATUS_INCOMPLETE,
|
||||
reason="复核后恢复流程",
|
||||
force=True,
|
||||
)
|
||||
|
||||
db.refresh(order)
|
||||
assert result == {"from": collab.STATUS_VOID, "to": collab.STATUS_INCOMPLETE}
|
||||
assert order.status == collab.STATUS_INCOMPLETE
|
||||
assert order.void_reason == ""
|
||||
assert order.scripted_at is None
|
||||
assert order.done_at is None
|
||||
|
||||
audit = (
|
||||
db.query(AuditLog)
|
||||
.filter(AuditLog.action == "weibo:collab:status")
|
||||
.order_by(AuditLog.id.desc())
|
||||
.first()
|
||||
)
|
||||
assert audit is not None
|
||||
detail = json.loads(audit.detail)
|
||||
assert detail["force"] is True
|
||||
assert detail["reason"] == "复核后恢复流程"
|
||||
assert detail["previous_void_reason"] == "误作废"
|
||||
|
||||
|
||||
def test_force_restore_void_requires_super_admin_and_reason(db):
|
||||
admin = make_user(db, username="wb-force-admin", role="super_admin")
|
||||
operator = make_user(db, username="wb-force-operator", role="operation")
|
||||
account = make_account(db)
|
||||
order = collab.create_order(
|
||||
db,
|
||||
admin,
|
||||
order_no="WB-FORCE-002",
|
||||
account_id=account.id,
|
||||
goods_name="商品A",
|
||||
required_weidou=100,
|
||||
require_verified=False,
|
||||
)
|
||||
collab.set_status(db, admin, order, collab.STATUS_VOID, reason="客户退款")
|
||||
|
||||
with pytest.raises(collab.CollabError) as exc_info:
|
||||
collab.set_status(
|
||||
db,
|
||||
operator,
|
||||
order,
|
||||
collab.STATUS_TO_RECHARGE,
|
||||
reason="尝试恢复",
|
||||
force=True,
|
||||
)
|
||||
assert exc_info.value.status_code == 403
|
||||
assert exc_info.value.code == "force_admin_required"
|
||||
|
||||
with pytest.raises(collab.CollabError) as exc_info:
|
||||
collab.set_status(
|
||||
db,
|
||||
admin,
|
||||
order,
|
||||
collab.STATUS_TO_RECHARGE,
|
||||
force=True,
|
||||
)
|
||||
assert exc_info.value.code == "force_reason_required"
|
||||
|
||||
with pytest.raises(collab.CollabError) as exc_info:
|
||||
collab.set_status(
|
||||
db,
|
||||
admin,
|
||||
order,
|
||||
collab.STATUS_FAILED,
|
||||
reason="不允许恢复到旁路状态",
|
||||
force=True,
|
||||
)
|
||||
assert exc_info.value.code == "force_target_invalid"
|
||||
db.refresh(order)
|
||||
assert order.status == collab.STATUS_VOID
|
||||
|
||||
|
||||
def test_batch_status_router_passes_force_restore(db):
|
||||
user = make_user(db, username="wb-batch-force-admin")
|
||||
account = make_account(db)
|
||||
order = collab.create_order(
|
||||
db,
|
||||
user,
|
||||
order_no="WB-FORCE-BATCH-001",
|
||||
shop="店铺A",
|
||||
account_id=account.id,
|
||||
goods_name="商品A",
|
||||
required_weidou=100,
|
||||
require_verified=False,
|
||||
)
|
||||
collab.set_status(db, user, order, collab.STATUS_VOID, reason="误操作")
|
||||
|
||||
result = weibo_collab_router.set_external_orders_status_batch(
|
||||
WeiboCollabStatusBatchRequest(
|
||||
ids=[order.id],
|
||||
status=collab.STATUS_TO_RECHARGE,
|
||||
reason="批量复核恢复",
|
||||
force=True,
|
||||
),
|
||||
db=db,
|
||||
current=user,
|
||||
)
|
||||
|
||||
db.refresh(order)
|
||||
assert result["updated"] == 1
|
||||
assert order.status == collab.STATUS_TO_RECHARGE
|
||||
assert order.void_reason == ""
|
||||
|
||||
|
||||
def test_single_status_router_passes_force_restore(db):
|
||||
user = make_user(db, username="wb-single-force-admin")
|
||||
account = make_account(db)
|
||||
order = collab.create_order(
|
||||
db,
|
||||
user,
|
||||
order_no="WB-FORCE-SINGLE-001",
|
||||
account_id=account.id,
|
||||
goods_name="商品A",
|
||||
required_weidou=100,
|
||||
require_verified=False,
|
||||
)
|
||||
collab.set_status(db, user, order, collab.STATUS_VOID, reason="误操作")
|
||||
|
||||
result = weibo_collab_router.set_external_order_status(
|
||||
order.id,
|
||||
WeiboCollabStatusRequest(
|
||||
status=collab.STATUS_INCOMPLETE,
|
||||
reason="复核后恢复",
|
||||
force=True,
|
||||
expected_version=order.version,
|
||||
),
|
||||
db=db,
|
||||
current=user,
|
||||
)
|
||||
|
||||
db.refresh(order)
|
||||
assert result["status"] == collab.STATUS_INCOMPLETE
|
||||
assert order.status == collab.STATUS_INCOMPLETE
|
||||
assert order.void_reason == ""
|
||||
|
||||
|
||||
def test_create_orders_groups_and_balance_check(monkeypatch, db):
|
||||
user = make_user(db)
|
||||
account = make_account(db)
|
||||
|
||||
Reference in New Issue
Block a user