feat(weibo): 增加微博微豆基础自动化
This commit is contained in:
@@ -26,7 +26,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261107_0067"
|
||||
HEAD_REVISION = "20261107_0069"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
|
||||
@@ -0,0 +1,404 @@
|
||||
"""微博 CK 导入与绑定二维码接口契约。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import ClassVar
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy import text
|
||||
|
||||
from core.weibo import (
|
||||
WeiboAuthError,
|
||||
WeiboBindError,
|
||||
WeiboExchangeResult,
|
||||
WeiboHomeSnapshot,
|
||||
WeiboPaymentStatus,
|
||||
WeiboRechargeOrder,
|
||||
)
|
||||
from web.backend.models import User, WeiboAccount
|
||||
from web.backend.routers import weibo as weibo_router
|
||||
from web.backend.routers.weibo import (
|
||||
create_bind_qrcodes,
|
||||
import_cookies,
|
||||
list_accounts,
|
||||
)
|
||||
from web.backend.schemas import (
|
||||
WeiboCookieImport,
|
||||
WeiboExchangeRequest,
|
||||
WeiboPaymentStatusRequest,
|
||||
WeiboRechargeRequest,
|
||||
)
|
||||
from web.backend.services import weibo_service
|
||||
|
||||
BIND_URL = (
|
||||
"sinaweibo://wbdiversion?username=gh_test"
|
||||
"&path=%2Fpages%2FgameAccountBind%2Findex%3FactId%3D16722"
|
||||
"%26code%3Dabc%252Bdef%253D%26gameId%3Dcjm"
|
||||
"%26livePlatId%3Dweiyouxi%26nonce%3Dabc123"
|
||||
"%26sig%3D0123456789abcdef%26t%3D1794046086%26v%3D2.0"
|
||||
)
|
||||
|
||||
|
||||
class StubWeiboClient:
|
||||
snapshot = WeiboHomeSnapshot(
|
||||
uid="4014000001",
|
||||
nickname="测试微博",
|
||||
is_bound=False,
|
||||
role_name="",
|
||||
can_rebind=True,
|
||||
rebind_remain_days=0,
|
||||
weidou_balance=100,
|
||||
bind_url=BIND_URL,
|
||||
)
|
||||
scheme_errors: ClassVar[dict[str, str]] = {}
|
||||
calls: ClassVar[list[tuple[str, str]]] = []
|
||||
|
||||
def __init__(self, cookie: str = "", **_kwargs):
|
||||
self.cookie = cookie
|
||||
|
||||
def fetch_home(self, activity: str = ""):
|
||||
self.calls.append(("home", activity))
|
||||
if "expired" in self.cookie:
|
||||
raise WeiboAuthError("未登陆")
|
||||
return self.snapshot
|
||||
|
||||
def generate_bind_scheme(self, bind_url: str, account_type: str):
|
||||
self.calls.append(("scheme", account_type))
|
||||
error = self.scheme_errors.get(account_type)
|
||||
if error:
|
||||
raise WeiboBindError(error)
|
||||
prefix = (
|
||||
"weixin://dl/business/?appid=wx-test"
|
||||
if account_type == "wx"
|
||||
else "mqqapi://miniapp/open?_mappid=qq-test"
|
||||
)
|
||||
return prefix
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def current_user(db):
|
||||
user = User(username="weibo-operator", password_hash="hash", role="super_admin")
|
||||
db.add(user)
|
||||
db.commit()
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def stub_client(monkeypatch):
|
||||
StubWeiboClient.calls = []
|
||||
StubWeiboClient.scheme_errors = {}
|
||||
StubWeiboClient.snapshot = WeiboHomeSnapshot(
|
||||
uid="4014000001",
|
||||
nickname="测试微博",
|
||||
is_bound=False,
|
||||
role_name="",
|
||||
can_rebind=True,
|
||||
rebind_remain_days=0,
|
||||
weidou_balance=100,
|
||||
bind_url=BIND_URL,
|
||||
)
|
||||
monkeypatch.setattr(weibo_service, "WeiboClient", StubWeiboClient)
|
||||
|
||||
|
||||
def test_import_creates_encrypted_account_and_is_idempotent(db, current_user):
|
||||
first = import_cookies(
|
||||
WeiboCookieImport(text="SCF=a; SUB=b", tag="运营"),
|
||||
db=db,
|
||||
current=current_user,
|
||||
)
|
||||
second = import_cookies(
|
||||
WeiboCookieImport(text="SCF=c; SUB=d", tag="新标签"),
|
||||
db=db,
|
||||
current=current_user,
|
||||
)
|
||||
|
||||
assert first.count == 1
|
||||
assert second.count == 1
|
||||
assert db.query(WeiboAccount).count() == 1
|
||||
account = db.query(WeiboAccount).one()
|
||||
assert account.uid == "4014000001"
|
||||
assert account.cookie == "SCF=c; SUB=d"
|
||||
assert account.tag == "新标签"
|
||||
assert account.ck_check_status == "valid"
|
||||
raw_cookie = db.execute(text("SELECT cookie FROM weibo_accounts")).scalar_one()
|
||||
assert raw_cookie.startswith("enc:v1:")
|
||||
assert "SUB=d" not in raw_cookie
|
||||
|
||||
dumped = second.accounts[0].model_dump()
|
||||
assert dumped["uid"] == "4014000001"
|
||||
assert dumped["is_bound"] is False
|
||||
assert dumped["weidou_balance"] == 100
|
||||
assert "cookie" not in dumped
|
||||
|
||||
|
||||
def test_import_reports_invalid_ck_per_line_and_keeps_valid_account(db, current_user):
|
||||
result = import_cookies(
|
||||
WeiboCookieImport(text="SCF=a; SUB=expired\nSCF=b; SUB=valid", tag=""),
|
||||
db=db,
|
||||
current=current_user,
|
||||
)
|
||||
|
||||
assert result.count == 1
|
||||
assert len(result.errors) == 1
|
||||
assert result.errors[0].line == 1
|
||||
assert "未登陆" in result.errors[0].message
|
||||
assert db.query(WeiboAccount).count() == 1
|
||||
|
||||
|
||||
def test_list_accounts_returns_parallel_page_shape(db, current_user):
|
||||
assert (
|
||||
import_cookies(
|
||||
WeiboCookieImport(text="SCF=a; SUB=b", tag=""),
|
||||
db=db,
|
||||
current=current_user,
|
||||
).count
|
||||
== 1
|
||||
)
|
||||
|
||||
page = list_accounts(page=1, page_size=20, db=db, current=current_user)
|
||||
|
||||
assert page["total"] == 1
|
||||
dumped = page["items"][0].model_dump()
|
||||
assert set(dumped) >= {
|
||||
"id",
|
||||
"uid",
|
||||
"nickname",
|
||||
"is_bound",
|
||||
"can_rebind",
|
||||
"rebind_remain_days",
|
||||
"weidou_balance",
|
||||
"ck_check_status",
|
||||
}
|
||||
|
||||
|
||||
def test_bind_qrcodes_returns_both_channels_and_partial_error(db, current_user):
|
||||
account = import_cookies(
|
||||
WeiboCookieImport(text="SCF=a; SUB=b", tag=""),
|
||||
db=db,
|
||||
current=current_user,
|
||||
).accounts[0]
|
||||
StubWeiboClient.scheme_errors = {"qq": "QQ Scheme 暂时不可用"}
|
||||
|
||||
result = create_bind_qrcodes(account.id, db=db, current=current_user)
|
||||
|
||||
dumped = result.model_dump()
|
||||
assert dumped["account_id"] == account.id
|
||||
assert dumped["is_bound"] is False
|
||||
assert dumped["can_rebind"] is True
|
||||
by_channel = {item["channel"]: item for item in dumped["schemes"]}
|
||||
assert by_channel["wx"]["scheme"].startswith("weixin://")
|
||||
assert by_channel["wx"]["error"] == ""
|
||||
assert by_channel["qq"]["scheme"] == ""
|
||||
assert by_channel["qq"]["error"] == "QQ Scheme 暂时不可用"
|
||||
|
||||
|
||||
def test_bind_qrcodes_rejects_bound_account_without_rebind(db, current_user):
|
||||
account = import_cookies(
|
||||
WeiboCookieImport(text="SCF=a; SUB=b", tag=""),
|
||||
db=db,
|
||||
current=current_user,
|
||||
).accounts[0]
|
||||
StubWeiboClient.snapshot = WeiboHomeSnapshot(
|
||||
uid=account.uid,
|
||||
nickname=account.nickname,
|
||||
is_bound=True,
|
||||
role_name="测试角色",
|
||||
can_rebind=False,
|
||||
rebind_remain_days=7,
|
||||
weidou_balance=100,
|
||||
bind_url=BIND_URL,
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
create_bind_qrcodes(account.id, db=db, current=current_user)
|
||||
|
||||
assert exc_info.value.status_code == 409
|
||||
assert "7 天" in str(exc_info.value.detail)
|
||||
|
||||
|
||||
def test_manual_mall_exchange_and_point_routes_serialize(monkeypatch, db, current_user):
|
||||
account = import_cookies(
|
||||
WeiboCookieImport(text="SCF=a; SUB=b", tag=""),
|
||||
db=db,
|
||||
current=current_user,
|
||||
).accounts[0]
|
||||
monkeypatch.setattr(
|
||||
weibo_router,
|
||||
"query_weibo_mall",
|
||||
lambda *_args, **_kwargs: {
|
||||
"weidou_balance": 120,
|
||||
"list": [
|
||||
{
|
||||
"id": 37,
|
||||
"goods_name": "会员1天卡",
|
||||
"price": 100,
|
||||
"remain_count": 99,
|
||||
"is_sold_out": False,
|
||||
}
|
||||
],
|
||||
},
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
weibo_router,
|
||||
"query_weibo_exchange_records",
|
||||
lambda *_args, **_kwargs: {
|
||||
"page": 1,
|
||||
"pageSize": 20,
|
||||
"totalCount": 1,
|
||||
"list": [
|
||||
{
|
||||
"goods_name": "会员1天卡",
|
||||
"game_account": "测试角色",
|
||||
"cost_weidou": 100,
|
||||
"exchange_time": "2026-11-07 18:00:00",
|
||||
"reward_status": 2,
|
||||
}
|
||||
],
|
||||
},
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
weibo_router,
|
||||
"query_weibo_point_history",
|
||||
lambda *_args, **_kwargs: {
|
||||
"page_num": 1,
|
||||
"page_size": 20,
|
||||
"total": 1,
|
||||
"list": [
|
||||
{
|
||||
"id": 1,
|
||||
"source_name": "V+助威",
|
||||
"point_num": 100,
|
||||
"source_id": "order-1",
|
||||
}
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
mall = weibo_router.list_mall_goods(
|
||||
account.id,
|
||||
db=db,
|
||||
current=current_user,
|
||||
).model_dump()
|
||||
exchange = weibo_router.list_exchange_records(
|
||||
account.id,
|
||||
db=db,
|
||||
current=current_user,
|
||||
).model_dump()
|
||||
points = weibo_router.list_point_history(
|
||||
account.id,
|
||||
db=db,
|
||||
current=current_user,
|
||||
).model_dump()
|
||||
|
||||
assert mall["goods"][0]["goods_name"] == "会员1天卡"
|
||||
assert mall["weidou_balance"] == 120
|
||||
exchange_item = exchange["items"][0]
|
||||
assert set(exchange_item) == {
|
||||
"goods_name",
|
||||
"game_account",
|
||||
"cost_weidou",
|
||||
"exchange_time",
|
||||
"reward_status",
|
||||
"raw",
|
||||
}
|
||||
assert exchange_item["game_account"] == "测试角色"
|
||||
assert exchange_item["cost_weidou"] == 100
|
||||
assert exchange_item["exchange_time"] == "2026-11-07 18:00:00"
|
||||
assert exchange_item["reward_status"] == 2
|
||||
assert "id" not in exchange_item
|
||||
assert exchange["total"] == 1
|
||||
assert points["items"][0]["source_id"] == "order-1"
|
||||
|
||||
|
||||
def test_manual_recharge_order_and_status_routes_serialize(
|
||||
monkeypatch, db, current_user
|
||||
):
|
||||
account = import_cookies(
|
||||
WeiboCookieImport(text="SCF=a; SUB=b", tag=""),
|
||||
db=db,
|
||||
current=current_user,
|
||||
).accounts[0]
|
||||
monkeypatch.setattr(
|
||||
weibo_router,
|
||||
"create_weibo_recharge_order",
|
||||
lambda *_args, **_kwargs: WeiboRechargeOrder(
|
||||
order_id="order-1",
|
||||
pay_id="pay-1",
|
||||
amount_yuan=1,
|
||||
single_reward=100,
|
||||
alipay_qr_code="https://qr.alipay.com/upx-test",
|
||||
desktop_pay_url="https://pay.sc.weibo.com/pay/pc/comb?pay_id=pay-1",
|
||||
),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
weibo_router,
|
||||
"query_weibo_payment_status",
|
||||
lambda *_args, **_kwargs: WeiboPaymentStatus(
|
||||
pay_id="pay-1",
|
||||
out_pay_id="order-1",
|
||||
status="PAY_STATUS_PENDING",
|
||||
sub_status="PAY_SUB_STATUS_INITIAL",
|
||||
),
|
||||
)
|
||||
|
||||
order = weibo_router.create_recharge_order(
|
||||
account.id,
|
||||
WeiboRechargeRequest(amount_yuan=1),
|
||||
db=db,
|
||||
current=current_user,
|
||||
).model_dump()
|
||||
status = weibo_router.get_recharge_status(
|
||||
account.id,
|
||||
WeiboPaymentStatusRequest(pay_id="pay-1"),
|
||||
db=db,
|
||||
current=current_user,
|
||||
).model_dump()
|
||||
|
||||
assert order["single_reward"] == 100
|
||||
assert order["alipay_qr_code"] == "https://qr.alipay.com/upx-test"
|
||||
assert status["status"] == "PAY_STATUS_PENDING"
|
||||
|
||||
|
||||
def test_manual_exchange_requires_confirm_and_serializes_result(
|
||||
monkeypatch, db, current_user
|
||||
):
|
||||
account = import_cookies(
|
||||
WeiboCookieImport(text="SCF=a; SUB=b", tag=""),
|
||||
db=db,
|
||||
current=current_user,
|
||||
).accounts[0]
|
||||
monkeypatch.setattr(
|
||||
weibo_router,
|
||||
"exchange_weibo_goods",
|
||||
lambda *_args, **_kwargs: WeiboExchangeResult(
|
||||
goods_id=37,
|
||||
goods_name="微博会员1天卡",
|
||||
price=100,
|
||||
balance_before=200,
|
||||
balance_after=100,
|
||||
remain_before=10,
|
||||
remain_after=9,
|
||||
verified=True,
|
||||
),
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
weibo_router.exchange_goods(
|
||||
account.id,
|
||||
WeiboExchangeRequest(goods_id=37, expected_price=100, confirm=False),
|
||||
db=db,
|
||||
current=current_user,
|
||||
)
|
||||
assert exc_info.value.status_code == 400
|
||||
|
||||
result = weibo_router.exchange_goods(
|
||||
account.id,
|
||||
WeiboExchangeRequest(goods_id=37, expected_price=100, confirm=True),
|
||||
db=db,
|
||||
current=current_user,
|
||||
).model_dump()
|
||||
|
||||
assert result["goods_id"] == 37
|
||||
assert result["balance_after"] == 100
|
||||
assert result["verified"] is True
|
||||
@@ -0,0 +1,530 @@
|
||||
"""微博微豆基础客户端协议测试。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
from core.weibo import (
|
||||
WeiboApiError,
|
||||
WeiboAuthError,
|
||||
WeiboBindError,
|
||||
WeiboClient,
|
||||
WeiboExchangeError,
|
||||
WeiboExchangeUncertainError,
|
||||
WeiboRechargeError,
|
||||
extract_bind_raw_url,
|
||||
normalize_weibo_cookie,
|
||||
)
|
||||
|
||||
BIND_URL = (
|
||||
"sinaweibo://wbdiversion?username=gh_test"
|
||||
"&path=%2Fpages%2FgameAccountBind%2Findex%3FactId%3D16722"
|
||||
"%26code%3Dabc%252Bdef%253D%26gameId%3Dcjm"
|
||||
"%26livePlatId%3Dweiyouxi%26nonce%3Dabc123"
|
||||
"%26sig%3D0123456789abcdef%26t%3D1794046086%26v%3D2.0"
|
||||
)
|
||||
|
||||
|
||||
class FakeResponse:
|
||||
def __init__(self, payload):
|
||||
self.payload = payload
|
||||
self.text = payload if isinstance(payload, str) else ""
|
||||
|
||||
def raise_for_status(self):
|
||||
return None
|
||||
|
||||
def json(self):
|
||||
if not isinstance(self.payload, dict):
|
||||
raise TypeError("not json")
|
||||
return self.payload
|
||||
|
||||
|
||||
class FakeSession(requests.Session):
|
||||
def __init__(self, responses):
|
||||
super().__init__()
|
||||
self.responses = list(responses)
|
||||
self.calls = []
|
||||
|
||||
def request(self, method, url, **_kwargs):
|
||||
kwargs = _kwargs
|
||||
self.calls.append((method, url, kwargs))
|
||||
result = self.responses.pop(0)
|
||||
if isinstance(result, Exception):
|
||||
raise result
|
||||
return FakeResponse(result)
|
||||
|
||||
|
||||
def test_normalize_cookie_accepts_prefix_and_multiline():
|
||||
raw = "Cookie: SCF=a;\n SUB=b;SUBP=c\n"
|
||||
assert normalize_weibo_cookie(raw) == "SCF=a; SUB=b; SUBP=c"
|
||||
|
||||
|
||||
def test_extract_bind_raw_url_preserves_inner_encoding():
|
||||
raw = extract_bind_raw_url(BIND_URL)
|
||||
assert raw.startswith("actId=16722&code=abc%2Bdef%3D")
|
||||
assert "faceUrl" not in raw
|
||||
# 内层 code 的多重编码不能被再次解开
|
||||
assert "abc+def=" not in raw
|
||||
|
||||
|
||||
def test_extract_bind_raw_url_rejects_wrong_path():
|
||||
with pytest.raises(WeiboBindError, match="绑定小程序路径"):
|
||||
extract_bind_raw_url("sinaweibo://wbdiversion?path=%2Fwrong")
|
||||
|
||||
|
||||
def test_fetch_home_returns_basic_snapshot():
|
||||
session = FakeSession(
|
||||
[
|
||||
{
|
||||
"code": 100000,
|
||||
"msg": "success",
|
||||
"data": {
|
||||
"user_info": {"uid": "4014000001", "nickname": "测试微博"},
|
||||
"game_role": {
|
||||
"is_bound": False,
|
||||
"role_name": "",
|
||||
"can_rebind": True,
|
||||
"rebind_remain_days": 0,
|
||||
"bind_url": BIND_URL,
|
||||
},
|
||||
"weidou_balance": {"balance": 100},
|
||||
"task_list": [
|
||||
{
|
||||
"button_type": "cashier",
|
||||
"action_url": (
|
||||
"https://vipclub.weibo.com/vmember/subscribe"
|
||||
"?vuid=8455793145&source_itemid=20601&F=bowen_tjg"
|
||||
),
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
]
|
||||
)
|
||||
client = WeiboClient("SCF=a; SUB=b", session=session)
|
||||
|
||||
snapshot = client.fetch_home("bowen_tjg")
|
||||
|
||||
assert snapshot.uid == "4014000001"
|
||||
assert snapshot.nickname == "测试微博"
|
||||
assert snapshot.is_bound is False
|
||||
assert snapshot.can_rebind is True
|
||||
assert snapshot.weidou_balance == 100
|
||||
assert snapshot.activity == "bowen_tjg"
|
||||
assert "source_itemid=20601" in snapshot.recharge_url
|
||||
method, url, kwargs = session.calls[0]
|
||||
assert method == "GET"
|
||||
assert "F=bowen_tjg" in url
|
||||
assert "Cookie" not in kwargs["headers"]
|
||||
assert session.cookies.get("SUB", domain=".weibo.com") == "b"
|
||||
|
||||
|
||||
def test_fetch_home_marks_auth_error():
|
||||
session = FakeSession([{"code": 400002, "msg": "未登陆"}])
|
||||
client = WeiboClient("SUB=expired", session=session)
|
||||
|
||||
with pytest.raises(WeiboAuthError, match="未登陆"):
|
||||
client.fetch_home()
|
||||
|
||||
|
||||
def test_fetch_home_keeps_trace_id_for_other_api_error():
|
||||
session = FakeSession(
|
||||
[{"code": 100003, "msg": "活动异常", "trace_id": "trace-001"}]
|
||||
)
|
||||
client = WeiboClient("SUB=test", session=session)
|
||||
|
||||
with pytest.raises(WeiboApiError, match="trace-001"):
|
||||
client.fetch_home()
|
||||
|
||||
|
||||
def test_generate_bind_scheme_posts_account_type_without_weibo_cookie():
|
||||
session = FakeSession(
|
||||
[
|
||||
{
|
||||
"iRet": 0,
|
||||
"sMsg": "ok",
|
||||
"jData": {
|
||||
"urlScheme": "weixin://dl/business/?appid=wx-test&query=scheme_id%3D1"
|
||||
},
|
||||
"apiName": "GenMiniappScheme",
|
||||
}
|
||||
]
|
||||
)
|
||||
client = WeiboClient(session=session)
|
||||
|
||||
scheme = client.generate_bind_scheme(BIND_URL, "wx")
|
||||
|
||||
assert scheme.startswith("weixin://dl/business/")
|
||||
method, url, kwargs = session.calls[0]
|
||||
assert method == "POST"
|
||||
assert "c=Sdk&a=GenMiniappScheme&actId=16722" in url
|
||||
assert kwargs["json"] == {"accountType": "wx"}
|
||||
assert "Cookie" not in kwargs["headers"]
|
||||
|
||||
|
||||
def test_generate_bind_scheme_rejects_missing_url_scheme():
|
||||
session = FakeSession([{"iRet": 0, "sMsg": "ok", "jData": {}}])
|
||||
client = WeiboClient(session=session)
|
||||
|
||||
with pytest.raises(WeiboBindError, match="urlScheme"):
|
||||
client.generate_bind_scheme(BIND_URL, "qq")
|
||||
|
||||
|
||||
def test_mall_exchange_and_point_queries():
|
||||
session = FakeSession(
|
||||
[
|
||||
{
|
||||
"code": 100000,
|
||||
"data": {
|
||||
"list": [{"id": 37, "goods_name": "会员1天卡", "price": 100}],
|
||||
"weidou_balance": 100,
|
||||
},
|
||||
},
|
||||
{
|
||||
"code": 100000,
|
||||
"data": {
|
||||
"page": 1,
|
||||
"pageSize": 20,
|
||||
"totalCount": 0,
|
||||
"list": [],
|
||||
},
|
||||
},
|
||||
{
|
||||
"code": 100000,
|
||||
"data": {
|
||||
"page_num": 1,
|
||||
"page_size": 20,
|
||||
"total": 1,
|
||||
"list": [{"id": 1, "source_name": "V+助威", "point_num": 100}],
|
||||
},
|
||||
},
|
||||
]
|
||||
)
|
||||
client = WeiboClient("SUB=valid", session=session)
|
||||
|
||||
mall = client.list_mall("bowen_tjg")
|
||||
records = client.list_exchange_records("bowen_tjg")
|
||||
points = client.list_point_history("bowen_tjg")
|
||||
|
||||
assert mall["weidou_balance"] == 100
|
||||
assert records["totalCount"] == 0
|
||||
assert points["total"] == 1
|
||||
assert session.calls[0][2]["params"]["F"] == "bowen_tjg"
|
||||
|
||||
|
||||
def test_create_recharge_order_full_manual_chain():
|
||||
home = {
|
||||
"code": 100000,
|
||||
"data": {
|
||||
"user_info": {"uid": "4014000001", "nickname": "测试微博"},
|
||||
"game_role": {
|
||||
"is_bound": True,
|
||||
"role_name": "测试角色",
|
||||
"can_rebind": False,
|
||||
"rebind_remain_days": 7,
|
||||
"bind_url": BIND_URL,
|
||||
},
|
||||
"weidou_balance": {"balance": 0},
|
||||
"task_list": [
|
||||
{
|
||||
"button_type": "cashier",
|
||||
"action_url": (
|
||||
"https://vipclub.weibo.com/vmember/subscribe"
|
||||
"?vuid=8455793145&source_itemid=20601&F=bowen_tjg"
|
||||
),
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
userinfo = {
|
||||
"code": 100000,
|
||||
"data": {
|
||||
"cashier_id": 205,
|
||||
"tab_info": [
|
||||
{
|
||||
"cashier_id": "205",
|
||||
"tab_id": "536",
|
||||
"tab_type": "8",
|
||||
"is_current": 1,
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
cashierinfo = {
|
||||
"code": 100000,
|
||||
"data": {
|
||||
"tab_info": [
|
||||
{
|
||||
"cashier_id": "205",
|
||||
"tab_id": "536",
|
||||
"tab_type": "8",
|
||||
"is_current": 1,
|
||||
"menu": [
|
||||
{
|
||||
"commodity_id": "2060101",
|
||||
"subitemid": "2060101",
|
||||
"itemid": 20601,
|
||||
"appkey": 2000,
|
||||
"max_money": 1000,
|
||||
"discount_info": {"desc": "1元=100微豆"},
|
||||
}
|
||||
],
|
||||
}
|
||||
]
|
||||
},
|
||||
}
|
||||
privilege = {
|
||||
"code": 100000,
|
||||
"data": {
|
||||
"privilege": {
|
||||
"url": "https://vipclub.weibo.com/appreciate/description?bid=1000294191"
|
||||
}
|
||||
},
|
||||
}
|
||||
createorder = {
|
||||
"code": 100000,
|
||||
"msg": "下单成功",
|
||||
"data": {
|
||||
"url": "https://pay.sc.weibo.com/api/merchant/pay/cashier?sign=test",
|
||||
"orderid": "26111000000000000001",
|
||||
},
|
||||
}
|
||||
cashier_html = """
|
||||
<input type="hidden" value="1220351218153064346" node-type="pay_id" name="pay_id">
|
||||
<img node-type="QRCode" src="https://qr.weibo.cn/inf/gen?sign=test">
|
||||
<a href="https://pay.sc.weibo.com/pay/pc/comb?pay_id=1220351218153064346">立即支付</a>
|
||||
"""
|
||||
alipay_form_html = """
|
||||
<form action="https://mapi.alipay.com/gateway.do" method="GET">
|
||||
<input name="out_trade_no" value="1020351218229058766">
|
||||
<input name="total_fee" value="1.00">
|
||||
</form>
|
||||
"""
|
||||
alipay_html = """
|
||||
<input name="qrCode" type="hidden" value="https://qr.alipay.com/upx-test" id="J_qrCode" />
|
||||
"""
|
||||
session = FakeSession(
|
||||
[
|
||||
home,
|
||||
{"code": 100000, "data": {}},
|
||||
userinfo,
|
||||
cashierinfo,
|
||||
privilege,
|
||||
createorder,
|
||||
cashier_html,
|
||||
alipay_form_html,
|
||||
alipay_html,
|
||||
]
|
||||
)
|
||||
client = WeiboClient("SCF=a; SUB=b", session=session)
|
||||
|
||||
order = client.create_recharge_order(amount_yuan=1, activity="bowen_tjg")
|
||||
|
||||
assert order.order_id == "26111000000000000001"
|
||||
assert order.pay_id == "1220351218153064346"
|
||||
assert order.single_reward == 100
|
||||
assert order.alipay_qr_code == "https://qr.alipay.com/upx-test"
|
||||
assert order.desktop_pay_url.startswith("https://pay.sc.weibo.com/")
|
||||
create_call = next(
|
||||
call for call in session.calls if call[1].endswith("/paycomponent/createorder")
|
||||
)
|
||||
assert create_call[2]["data"]["single_reward"] == "100"
|
||||
assert '"bid":"1000294191"' in create_call[2]["data"]["ext"]
|
||||
assert create_call[2]["params"] == create_call[2]["data"]
|
||||
|
||||
|
||||
def test_create_recharge_order_rejects_missing_dynamic_menu():
|
||||
home = {
|
||||
"code": 100000,
|
||||
"data": {
|
||||
"user_info": {"uid": "1", "nickname": "测试"},
|
||||
"game_role": {
|
||||
"is_bound": False,
|
||||
"role_name": "",
|
||||
"can_rebind": True,
|
||||
"rebind_remain_days": 0,
|
||||
"bind_url": BIND_URL,
|
||||
},
|
||||
"weidou_balance": {"balance": 0},
|
||||
"task_list": [
|
||||
{
|
||||
"button_type": "cashier",
|
||||
"action_url": (
|
||||
"https://vipclub.weibo.com/vmember/subscribe"
|
||||
"?vuid=8455793145&source_itemid=20601&F=bowen_tjg"
|
||||
),
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
userinfo = {
|
||||
"code": 100000,
|
||||
"data": {
|
||||
"cashier_id": 205,
|
||||
"tab_info": [
|
||||
{
|
||||
"cashier_id": "205",
|
||||
"tab_id": "536",
|
||||
"tab_type": "8",
|
||||
"is_current": 1,
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
cashierinfo = {
|
||||
"code": 100000,
|
||||
"data": {
|
||||
"tab_info": [
|
||||
{
|
||||
"cashier_id": "205",
|
||||
"tab_id": "536",
|
||||
"tab_type": "8",
|
||||
"is_current": 1,
|
||||
"menu": [],
|
||||
}
|
||||
]
|
||||
},
|
||||
}
|
||||
session = FakeSession([home, {"code": 100000, "data": {}}, userinfo, cashierinfo])
|
||||
client = WeiboClient("SUB=valid", session=session)
|
||||
|
||||
with pytest.raises(WeiboRechargeError, match="自定义微豆充值档位"):
|
||||
client.create_recharge_order(amount_yuan=1)
|
||||
|
||||
|
||||
def test_get_payment_status():
|
||||
session = FakeSession(
|
||||
[
|
||||
{
|
||||
"code": "100000",
|
||||
"msg": "操作成功",
|
||||
"data": {
|
||||
"pay_id": "pay-1",
|
||||
"out_pay_id": "out-1",
|
||||
"status": "PAY_STATUS_PENDING",
|
||||
"sub_status": "PAY_SUB_STATUS_INITIAL",
|
||||
},
|
||||
}
|
||||
]
|
||||
)
|
||||
client = WeiboClient("SUB=valid", session=session)
|
||||
|
||||
status = client.get_payment_status("pay-1")
|
||||
|
||||
assert status.status == "PAY_STATUS_PENDING"
|
||||
assert session.calls[0][2]["data"]["pay_id"] == "pay-1"
|
||||
|
||||
|
||||
def test_exchange_goods_executes_once_and_verifies_balance_and_stock():
|
||||
before = {
|
||||
"code": 100000,
|
||||
"data": {
|
||||
"weidou_balance": 200,
|
||||
"list": [
|
||||
{
|
||||
"id": 37,
|
||||
"goods_name": "微博会员1天卡",
|
||||
"price": 100,
|
||||
"remain_count": 10,
|
||||
"is_sold_out": False,
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
after = {
|
||||
"code": 100000,
|
||||
"data": {
|
||||
"weidou_balance": 100,
|
||||
"list": [
|
||||
{
|
||||
"id": 37,
|
||||
"goods_name": "微博会员1天卡",
|
||||
"price": 100,
|
||||
"remain_count": 9,
|
||||
"is_sold_out": False,
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
session = FakeSession(
|
||||
[
|
||||
before,
|
||||
{"code": 100000, "msg": "success", "data": None},
|
||||
after,
|
||||
]
|
||||
)
|
||||
client = WeiboClient("SUB=valid", session=session)
|
||||
|
||||
result = client.exchange_goods(
|
||||
37,
|
||||
expected_price=100,
|
||||
activity="bowen_tjg",
|
||||
)
|
||||
|
||||
assert result.goods_name == "微博会员1天卡"
|
||||
assert result.balance_before == 200
|
||||
assert result.balance_after == 100
|
||||
assert result.remain_before == 10
|
||||
assert result.remain_after == 9
|
||||
assert result.verified is True
|
||||
exchange_calls = [
|
||||
call for call in session.calls if call[1].endswith("/game/exchangegoods")
|
||||
]
|
||||
assert len(exchange_calls) == 1
|
||||
assert exchange_calls[0][2]["params"]["goods_id"] == 37
|
||||
|
||||
|
||||
def test_exchange_goods_rejects_insufficient_balance_before_write():
|
||||
session = FakeSession(
|
||||
[
|
||||
{
|
||||
"code": 100000,
|
||||
"data": {
|
||||
"weidou_balance": 99,
|
||||
"list": [
|
||||
{
|
||||
"id": 37,
|
||||
"goods_name": "微博会员1天卡",
|
||||
"price": 100,
|
||||
"remain_count": 10,
|
||||
"is_sold_out": False,
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
]
|
||||
)
|
||||
client = WeiboClient("SUB=valid", session=session)
|
||||
|
||||
with pytest.raises(WeiboExchangeError, match="余额不足"):
|
||||
client.exchange_goods(37, expected_price=100)
|
||||
|
||||
assert not any(call[1].endswith("/game/exchangegoods") for call in session.calls)
|
||||
|
||||
|
||||
def test_exchange_goods_marks_transport_failure_as_uncertain():
|
||||
session = FakeSession(
|
||||
[
|
||||
{
|
||||
"code": 100000,
|
||||
"data": {
|
||||
"weidou_balance": 200,
|
||||
"list": [
|
||||
{
|
||||
"id": 37,
|
||||
"goods_name": "微博会员1天卡",
|
||||
"price": 100,
|
||||
"remain_count": 10,
|
||||
"is_sold_out": False,
|
||||
}
|
||||
],
|
||||
},
|
||||
},
|
||||
requests.ConnectionError("connection reset"),
|
||||
]
|
||||
)
|
||||
client = WeiboClient("SUB=valid", session=session)
|
||||
|
||||
with pytest.raises(WeiboExchangeUncertainError, match="结果未知"):
|
||||
client.exchange_goods(37, expected_price=100)
|
||||
Reference in New Issue
Block a user