119 lines
3.4 KiB
Python
119 lines
3.4 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
|
|
|
|
|
|
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
|