Files
live-hub-py/tests/test_platform_fulfillment_integration.py
T
yml bba3d9dac9 feat(fulfillment): 增加签名 Webhook 事务 Outbox
- 新增直播履约状态事务 Outbox 与签名投递 worker
- 增加 order_site 状态回传、重试和低频对账兜底
- 补充 Outbox、签名校验与集成接口测试
2026-11-18 10:59:20 +08:00

143 lines
4.3 KiB
Python

"""直播账号履约服务端集成接口测试。"""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
import pytest
from fastapi import HTTPException
from web.backend.models import (
Account,
AccountLease,
DouyuTask,
PlatformFulfillmentTask,
User,
)
from web.backend.routers.platform_fulfillment_integration import require_integration_key
from web.backend.services.platform_fulfillment import service
def test_integration_key_is_required(monkeypatch):
monkeypatch.setenv("PLATFORM_FULFILLMENT_INTEGRATION_KEY", "secret")
require_integration_key("secret")
with pytest.raises(HTTPException) as missing:
require_integration_key("")
with pytest.raises(HTTPException) as invalid:
require_integration_key("wrong")
assert missing.value.status_code == 401
assert invalid.value.status_code == 401
def test_customer_binding_out_hides_internal_account_data(db):
user = User(
username="integration-actor",
password_hash="h",
role="super_admin",
is_active=True,
)
account = Account(
username="internal-douyu-account",
password="p",
email="internal@example.com",
email_password="ep",
cookie="secret-cookie",
)
db.add_all([user, account])
db.flush()
lease = AccountLease(
account_id=account.id,
holder_user_id=user.id,
purpose="platform_fulfillment",
status="active",
)
bind = DouyuTask(
batch_id="bind",
account_id=account.id,
account_assigned_to=user.id,
task_type="get_bind_qr",
handbook_scope="elite",
status="running",
message="等待扫码",
result={
"url": "https://livenlink.example/private-qr-token",
"bind_phase": "waiting_scan",
"qr_generated_at": datetime.now(UTC).isoformat(),
"qr_expires_at": (datetime.now(UTC) + timedelta(minutes=10)).isoformat(),
},
created_by=user.id,
)
db.add_all([lease, bind])
db.flush()
task = PlatformFulfillmentTask(
source_system="order_site",
source_order_id="order-1",
source_order_item_id="item-1",
platform_order_id="DY-001",
sku_name="斗鱼精英宝典",
quantity=1,
account_id=account.id,
lease_id=lease.id,
bind_task_id=bind.id,
binding_status="qr_pending",
execution_status="waiting_bind",
)
db.add(task)
db.commit()
payload = service.customer_binding_out(db, task.id)
assert payload["sessionId"] == task.id
assert payload["qrUrl"] == "https://livenlink.example/private-qr-token"
assert payload["accountReady"] is True
assert "account" not in payload
assert "cookie" not in payload
assert "points" not in payload
assert "lease" not in payload
batch = service.customer_refresh_bindings(db, task_ids=[task.id, 999999])
assert [item["sessionId"] for item in batch["items"]] == [task.id]
assert batch["items"][0]["qrUrl"] == "https://livenlink.example/private-qr-token"
def test_binding_qr_expiry_uses_customer_ttl():
now = datetime.now(UTC)
task = DouyuTask(
batch_id="expired",
account_id=1,
account_assigned_to=1,
task_type="get_bind_qr",
handbook_scope="elite",
status="success",
message="",
result={
"qr_expires_at": (now - timedelta(seconds=1)).isoformat(),
},
created_by=1,
)
task.created_at = now - timedelta(minutes=5)
assert service._binding_qr_expired(task, now=now) is True
def test_auto_order_sync_is_throttled(monkeypatch):
calls: list[int] = []
monkeypatch.setattr(service, "_AUTO_SYNC_LAST_ATTEMPT", float("-inf"))
monkeypatch.setattr(
service,
"sync_pending_tasks",
lambda _db, *, limit: calls.append(limit) or {"fetched": 1},
)
assert service.sync_pending_tasks_if_due(object(), limit=100, now=100.0) == {
"fetched": 1
}
assert service.sync_pending_tasks_if_due(object(), limit=100, now=110.0) is None
assert service.sync_pending_tasks_if_due(object(), limit=100, now=999.0) is None
assert service.sync_pending_tasks_if_due(object(), limit=100, now=1000.0) == {
"fetched": 1
}
assert calls == [100, 100]