抓包确认 /aj/game/exchangegoods 必须 POST + form body,并带 x-xsrf-token/Origin/_failtoast;原实现用 GET,服务端返回 400404 迷路页,接口稳定 502。 - 兑换改为 POST + form,query 与 body 同带 F/goods_id/_t - 请求失败分类:连接超时=未提交、其他网络错误/5xx/非 JSON=结果未知、 4xx=未提交、401 类错误码=CK 失效 - 100002 是参数校验错误(实测 GoodsID required),移出鉴权失效集合, 真实未登录是 400002 - 新增 /aj/mall/detail 预检(价格/余额/库存/兑换按钮),fail-close - 复核只认余额变化,取消“库存变化即成功”的假阳性;缺字段不再写 0 - 复核请求用 8s 短超时,避免打穿前端 120s - 同一账号兑换加进程内非阻塞锁,并发请求返回 409;余额回写失败降级为 结果未知 502 - 兑换记录状态列映射 2=已发放,其余展示原始状态码 - 版本 0.4.29 → 0.4.31
1155 lines
36 KiB
Python
1155 lines
36 KiB
Python
"""微博微豆基础客户端协议测试。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import pytest
|
|
import requests
|
|
|
|
from core.weibo import (
|
|
WeiboApiError,
|
|
WeiboAuthError,
|
|
WeiboBindError,
|
|
WeiboBindQrCode,
|
|
WeiboClient,
|
|
WeiboExchangeError,
|
|
WeiboExchangeUncertainError,
|
|
WeiboHomeSnapshot,
|
|
WeiboRechargeError,
|
|
WeiboTransportError,
|
|
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,
|
|
url: str = "",
|
|
status_code: int = 200,
|
|
headers: dict[str, str] | None = None,
|
|
):
|
|
self.payload = payload
|
|
self.text = payload if isinstance(payload, str) else ""
|
|
self.url = url
|
|
self.status_code = status_code
|
|
self.headers = {key.lower(): value for key, value in (headers or {}).items()}
|
|
|
|
def raise_for_status(self):
|
|
if self.status_code >= 400:
|
|
raise requests.HTTPError(f"HTTP {self.status_code}")
|
|
|
|
def json(self):
|
|
if not isinstance(self.payload, dict):
|
|
raise requests.exceptions.JSONDecodeError("not json", self.text, 0)
|
|
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
|
|
response = result if isinstance(result, FakeResponse) else FakeResponse(result)
|
|
if not response.url:
|
|
response.url = url
|
|
return response
|
|
|
|
|
|
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_fetch_home_treats_sina_login_redirect_as_auth_error():
|
|
session = FakeSession(
|
|
[
|
|
(
|
|
"<html><head><meta http-equiv='refresh' "
|
|
"content=\"0; url='https://vipclub.weibo.com/aj/vmember/"
|
|
"gfsupport?retcode=6102'\"></head></html>"
|
|
)
|
|
]
|
|
)
|
|
client = WeiboClient("SUB=expired", session=session)
|
|
|
|
with pytest.raises(WeiboAuthError, match="重新登录微博"):
|
|
client.fetch_home()
|
|
|
|
|
|
def test_fetch_home_reports_non_json_endpoint_path():
|
|
session = FakeSession(["<html><body>gateway error</body></html>"])
|
|
client = WeiboClient("SUB=test", session=session)
|
|
|
|
with pytest.raises(WeiboTransportError, match="/aj/mall/home"):
|
|
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_generate_bind_qrcode_posts_channel_and_preserves_raw_query():
|
|
session = FakeSession(
|
|
[
|
|
{
|
|
"iRet": 0,
|
|
"sMsg": "ok",
|
|
"jData": {
|
|
"qrcode": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB",
|
|
"qrcodeToken": "token-1",
|
|
},
|
|
}
|
|
]
|
|
)
|
|
client = WeiboClient(session=session)
|
|
|
|
qrcode = client.generate_bind_qrcode(BIND_URL, "qq")
|
|
|
|
assert isinstance(qrcode, WeiboBindQrCode)
|
|
assert qrcode.image_src == (
|
|
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB"
|
|
)
|
|
assert qrcode.token == "token-1"
|
|
method, url, kwargs = session.calls[0]
|
|
assert method == "POST"
|
|
assert url == "https://livelinkbind.game.qq.com/api/qrcode/getMiniAppQrcode"
|
|
body = kwargs["json"]
|
|
raw_url = extract_bind_raw_url(BIND_URL)
|
|
assert body["queryString"].startswith(f"{raw_url}&sessionId=")
|
|
assert body["accountType"] == "qq"
|
|
assert kwargs["headers"]["Gap-Act-Id"] == "16722"
|
|
assert kwargs["headers"]["Gap-Plat-Id"] == "weiyouxi"
|
|
assert kwargs["headers"]["Gap-User-From"] == "pc"
|
|
assert "Cookie" not in kwargs["headers"]
|
|
|
|
|
|
def test_generate_bind_qrcode_supports_jpeg_and_server_rate_limit():
|
|
jpeg_session = FakeSession(
|
|
[
|
|
{
|
|
"iRet": 0,
|
|
"sMsg": "ok",
|
|
"jData": {"qrcode": "/9j/4AAQSkZJRgABAQ", "qrcodeToken": "token-2"},
|
|
}
|
|
]
|
|
)
|
|
qrcode = WeiboClient(session=jpeg_session).generate_bind_qrcode(BIND_URL, "wx")
|
|
assert qrcode.image_src == "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ"
|
|
|
|
rate_limited = FakeSession(
|
|
[{"iRet": -1140603, "sMsg": "刷新二维码频率过快,请间隔5秒后重试"}]
|
|
)
|
|
with pytest.raises(WeiboBindError, match="间隔5秒"):
|
|
WeiboClient(session=rate_limited).generate_bind_qrcode(BIND_URL, "qq")
|
|
|
|
|
|
def test_mall_exchange_and_point_queries():
|
|
session = FakeSession(
|
|
[
|
|
{
|
|
"code": 100000,
|
|
"data": {
|
|
"list": [
|
|
{
|
|
"id": 37,
|
|
"goods_name": "会员1天卡",
|
|
"goods_image": "http://wx3.sinaimg.cn/large/demo.jpg",
|
|
"price": 100,
|
|
},
|
|
{
|
|
"id": 38,
|
|
"goods_name": "会员3天卡",
|
|
"goods_image": "//wx4.sinaimg.cn/large/demo-2.jpg",
|
|
"price": 200,
|
|
},
|
|
],
|
|
"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 mall["list"][0]["goods_image"] == "https://wx3.sinaimg.cn/large/demo.jpg"
|
|
assert mall["list"][1]["goods_image"] == "https://wx4.sinaimg.cn/large/demo-2.jpg"
|
|
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"]
|
|
assert not any("/aj/pay/modular/prepare" in call[1] for call in session.calls)
|
|
|
|
|
|
def test_create_recharge_order_reports_payment_rejection():
|
|
client = WeiboClient("SCF=a; SUB=b", session=FakeSession([]))
|
|
|
|
def fake_context(
|
|
activity: str,
|
|
*,
|
|
home: WeiboHomeSnapshot | None = None,
|
|
) -> tuple[str, dict[str, str], dict[str, Any], dict[str, Any]]:
|
|
_ = (activity, home)
|
|
return (
|
|
"4014000001",
|
|
{"vuid": "vuid", "source_itemid": "20601", "F": "bowen_tjg"},
|
|
{"tab_type": "8", "cashier_id": "205"},
|
|
{
|
|
"tab_info": [
|
|
{
|
|
"is_current": 1,
|
|
"tab_type": "8",
|
|
"cashier_id": "205",
|
|
"menu": [
|
|
{
|
|
"subitemid": "2060101",
|
|
"itemid": 20601,
|
|
"appkey": 2000,
|
|
"commodity_id": "2060101",
|
|
"max_money": 1000,
|
|
}
|
|
],
|
|
}
|
|
]
|
|
},
|
|
)
|
|
|
|
def fake_bid(
|
|
vuid: str,
|
|
source_itemid: str,
|
|
tab_type: str,
|
|
activity: str,
|
|
) -> str:
|
|
_ = (vuid, source_itemid, tab_type, activity)
|
|
return "1000294191"
|
|
|
|
client._load_recharge_context = fake_context
|
|
client._recharge_bid = fake_bid
|
|
client._request_json = lambda *_args, **_kwargs: {
|
|
"code": 100000,
|
|
"data": {
|
|
"url": "https://pay.sc.weibo.com/api/merchant/pay/cashier?sign=test",
|
|
"orderid": "order-1",
|
|
},
|
|
}
|
|
|
|
def rejected_page(*_args, **_kwargs):
|
|
client._last_text_url = "https://pay.sc.weibo.com/sorry"
|
|
return "<title>404错误</title>"
|
|
|
|
client._request_text = rejected_page
|
|
|
|
with pytest.raises(WeiboRechargeError, match=r"支付页拒绝.*/sorry"):
|
|
client.create_recharge_order(amount_yuan=1, activity="bowen_tjg")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("location", "message"),
|
|
[
|
|
(
|
|
"https://pay.sc.weibo.com/sso/login.php?url=test",
|
|
"微博支付未登录",
|
|
),
|
|
(
|
|
"https://weibo.com/sorry?usernotexists",
|
|
"支付账号不存在",
|
|
),
|
|
],
|
|
)
|
|
def test_recharge_preflight_detects_payment_auth_redirect(location, message):
|
|
home = WeiboHomeSnapshot(
|
|
uid="4014000001",
|
|
nickname="测试微博",
|
|
is_bound=False,
|
|
role_name="",
|
|
can_rebind=True,
|
|
rebind_remain_days=0,
|
|
weidou_balance=100,
|
|
bind_url=BIND_URL,
|
|
recharge_url=(
|
|
"https://vipclub.weibo.com/vmember/subscribe"
|
|
"?vuid=8455793145&source_itemid=20601&F=bowen_tjg"
|
|
),
|
|
)
|
|
session = FakeSession(
|
|
[
|
|
{"code": 100000, "data": {}},
|
|
{
|
|
"code": 100000,
|
|
"data": {
|
|
"cashier_id": 205,
|
|
"tab_info": [
|
|
{
|
|
"cashier_id": "205",
|
|
"tab_id": "536",
|
|
"tab_type": "8",
|
|
"is_current": 1,
|
|
}
|
|
],
|
|
},
|
|
},
|
|
{
|
|
"code": 100000,
|
|
"data": {
|
|
"tab_info": [
|
|
{
|
|
"cashier_id": "205",
|
|
"tab_id": "536",
|
|
"tab_type": "8",
|
|
"is_current": 1,
|
|
"menu": [
|
|
{
|
|
"price": 6000,
|
|
"weibo_unified_params": {
|
|
"seller_id": 6329048632,
|
|
"modular_id": "7d5d81dfb1",
|
|
"default_cashier": "ali_cashier",
|
|
},
|
|
}
|
|
],
|
|
}
|
|
]
|
|
},
|
|
},
|
|
FakeResponse(
|
|
"",
|
|
status_code=302,
|
|
headers={"Location": location},
|
|
),
|
|
]
|
|
)
|
|
client = WeiboClient("SCF=a; SUB=b", session=session)
|
|
|
|
with pytest.raises(WeiboRechargeError, match=message):
|
|
client.check_payment_access("bowen_tjg", home=home)
|
|
|
|
|
|
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"
|
|
|
|
|
|
GOODS_ID = 37
|
|
|
|
|
|
def _detail_payload(
|
|
*,
|
|
price: Any = 100,
|
|
balance: Any = 200,
|
|
remain: Any = 10,
|
|
clickable: Any = True,
|
|
button_text: str = "立即兑换",
|
|
button_type: Any = "exchange",
|
|
) -> dict:
|
|
return {
|
|
"code": 100000,
|
|
"msg": "success",
|
|
"data": {
|
|
"goods_info": {
|
|
"id": GOODS_ID,
|
|
"goods_name": "微博会员1天卡",
|
|
"price": price,
|
|
"remain_count": remain,
|
|
"goods_desc": "兑换后立即生效",
|
|
},
|
|
"payment_button": {
|
|
"text": button_text,
|
|
"clickable": clickable,
|
|
"type": button_type,
|
|
},
|
|
"weidou_balance": balance,
|
|
"nickname": "测试微博",
|
|
"game_nickname": "测试角色",
|
|
},
|
|
}
|
|
|
|
|
|
def _mall_list_payload(
|
|
*,
|
|
balance: Any = 200,
|
|
remain: Any = 10,
|
|
drop_balance: bool = False,
|
|
) -> dict:
|
|
data: dict[str, Any] = {
|
|
"list": [
|
|
{
|
|
"id": GOODS_ID,
|
|
"goods_name": "微博会员1天卡",
|
|
"price": 100,
|
|
"remain_count": remain,
|
|
"is_sold_out": False,
|
|
}
|
|
],
|
|
}
|
|
if not drop_balance:
|
|
data["weidou_balance"] = balance
|
|
return {"code": 100000, "msg": "success", "data": data}
|
|
|
|
|
|
def _exchange_calls(session: FakeSession) -> list:
|
|
return [call for call in session.calls if call[1].endswith("/game/exchangegoods")]
|
|
|
|
|
|
def _detail_calls(session: FakeSession) -> list:
|
|
return [call for call in session.calls if call[1].endswith("/mall/detail")]
|
|
|
|
|
|
def test_exchange_goods_posts_form_with_xsrf_and_verifies_balance():
|
|
session = FakeSession(
|
|
[
|
|
_detail_payload(),
|
|
{"code": 100000, "msg": "success", "data": None},
|
|
_mall_list_payload(balance=100, remain=9),
|
|
]
|
|
)
|
|
client = WeiboClient("SUB=valid; XSRF-TOKEN=xsrf-token-1", session=session)
|
|
|
|
result = client.exchange_goods(
|
|
GOODS_ID,
|
|
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
|
|
assert result.verify_error == ""
|
|
|
|
exchange_calls = _exchange_calls(session)
|
|
assert len(exchange_calls) == 1
|
|
method, url, kwargs = exchange_calls[0]
|
|
assert method == "POST"
|
|
assert url.endswith("/game/exchangegoods")
|
|
assert kwargs["params"]["goods_id"] == GOODS_ID
|
|
assert kwargs["params"]["F"] == "bowen_tjg"
|
|
assert "_t" in kwargs["params"]
|
|
assert kwargs["data"]["goods_id"] == GOODS_ID
|
|
assert kwargs["data"]["F"] == "bowen_tjg"
|
|
headers = {key.lower(): value for key, value in kwargs["headers"].items()}
|
|
assert headers["x-xsrf-token"] == "xsrf-token-1"
|
|
assert headers["content-type"].startswith("application/x-www-form-urlencoded")
|
|
assert headers["origin"] == "https://vipclub.e.weibo.com"
|
|
assert headers["_failtoast"] == "true"
|
|
|
|
detail_calls = _detail_calls(session)
|
|
assert len(detail_calls) == 1
|
|
assert session.calls.index(detail_calls[0]) < session.calls.index(exchange_calls[0])
|
|
|
|
|
|
def test_exchange_goods_cancels_when_payment_button_not_clickable():
|
|
session = FakeSession(
|
|
[
|
|
_detail_payload(clickable=False, button_text="已达兑换上限"),
|
|
_mall_list_payload(),
|
|
]
|
|
)
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
with pytest.raises(WeiboExchangeError, match="已达兑换上限"):
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
assert _exchange_calls(session) == []
|
|
|
|
|
|
def test_exchange_goods_cancels_when_detail_preflight_fails():
|
|
session = FakeSession([{"code": 100500, "msg": "商品不存在"}])
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
with pytest.raises(WeiboExchangeError, match="已取消"):
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
assert _exchange_calls(session) == []
|
|
|
|
|
|
def test_exchange_goods_rejects_insufficient_balance_before_write():
|
|
session = FakeSession([_detail_payload(balance=99)])
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
with pytest.raises(WeiboExchangeError, match="余额不足"):
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
assert _exchange_calls(session) == []
|
|
|
|
|
|
def test_exchange_goods_marks_connection_error_as_uncertain():
|
|
session = FakeSession(
|
|
[
|
|
_detail_payload(),
|
|
requests.ConnectionError("connection reset"),
|
|
]
|
|
)
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
with pytest.raises(WeiboExchangeUncertainError, match="结果未知"):
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
|
|
def test_exchange_goods_treats_100002_as_explicit_rejection():
|
|
session = FakeSession(
|
|
[
|
|
_detail_payload(),
|
|
{"code": 100002, "msg": "参数错误"},
|
|
]
|
|
)
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
with pytest.raises(WeiboExchangeError, match="参数错误") as exc_info:
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
assert not isinstance(exc_info.value, WeiboAuthError)
|
|
|
|
|
|
def test_exchange_goods_treats_400002_as_auth_failure():
|
|
session = FakeSession(
|
|
[
|
|
_detail_payload(),
|
|
{"code": 400002, "msg": "未登陆"},
|
|
]
|
|
)
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
with pytest.raises(WeiboAuthError, match="未登陆"):
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
|
|
def test_exchange_goods_does_not_verify_on_stock_change_only():
|
|
session = FakeSession(
|
|
[
|
|
_detail_payload(),
|
|
{"code": 100000, "msg": "success", "data": None},
|
|
_mall_list_payload(balance=200, remain=9),
|
|
_mall_list_payload(balance=200, remain=8),
|
|
_mall_list_payload(balance=200, remain=7),
|
|
]
|
|
)
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
result = client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
assert result.verified is False
|
|
assert result.verify_error
|
|
assert result.balance_after is None
|
|
assert result.remain_after == 7
|
|
|
|
|
|
def test_exchange_goods_keeps_balance_after_none_when_balance_missing():
|
|
session = FakeSession(
|
|
[
|
|
_detail_payload(),
|
|
{"code": 100000, "msg": "success", "data": None},
|
|
_mall_list_payload(drop_balance=True),
|
|
_mall_list_payload(drop_balance=True),
|
|
_mall_list_payload(drop_balance=True),
|
|
]
|
|
)
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
result = client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
assert result.balance_after is None
|
|
assert result.verified is False
|
|
assert result.verify_error
|
|
|
|
|
|
def test_exchange_goods_marks_connect_timeout_as_not_submitted():
|
|
session = FakeSession(
|
|
[
|
|
_detail_payload(),
|
|
requests.exceptions.ConnectTimeout("connect timeout"),
|
|
]
|
|
)
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
with pytest.raises(WeiboExchangeError, match="未提交") as exc_info:
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
assert not isinstance(exc_info.value, WeiboExchangeUncertainError)
|
|
|
|
|
|
def test_exchange_goods_passes_auth_error_through_on_detail_preflight():
|
|
session = FakeSession([{"code": 400002, "msg": "未登陆"}])
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
with pytest.raises(WeiboAuthError, match="未登陆") as exc_info:
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
assert not isinstance(exc_info.value, WeiboExchangeError)
|
|
|
|
|
|
def test_exchange_goods_treats_403_as_rejected_without_verify():
|
|
session = FakeSession(
|
|
[
|
|
_detail_payload(),
|
|
FakeResponse({"code": 1}, status_code=403),
|
|
]
|
|
)
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
with pytest.raises(WeiboExchangeError, match="403"):
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
# 只应发生 detail + 提交两次请求,403 之后不再复核。
|
|
assert len(session.calls) == 2
|
|
assert len(_detail_calls(session)) == 1
|
|
assert len(_exchange_calls(session)) == 1
|
|
|
|
|
|
def test_exchange_goods_treats_500_as_uncertain():
|
|
session = FakeSession(
|
|
[
|
|
_detail_payload(),
|
|
FakeResponse({"code": 1}, status_code=500),
|
|
]
|
|
)
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
with pytest.raises(WeiboExchangeUncertainError, match="结果未知"):
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
|
|
@pytest.mark.parametrize("clickable", [1, "1", "true", "True", True])
|
|
def test_exchange_goods_accepts_truthy_clickable(clickable):
|
|
session = FakeSession(
|
|
[
|
|
_detail_payload(clickable=clickable),
|
|
{"code": 100000, "msg": "success", "data": None},
|
|
_mall_list_payload(balance=100, remain=9),
|
|
]
|
|
)
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
result = client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
assert result.verified is True
|
|
assert len(_exchange_calls(session)) == 1
|
|
|
|
|
|
@pytest.mark.parametrize("clickable", ["0", 0, False, None])
|
|
def test_exchange_goods_rejects_falsy_clickable(clickable):
|
|
session = FakeSession([_detail_payload(clickable=clickable)])
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
with pytest.raises(WeiboExchangeError, match="不允许兑换"):
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
assert _exchange_calls(session) == []
|
|
|
|
|
|
def test_exchange_goods_cancels_when_payment_button_missing():
|
|
payload = _detail_payload()
|
|
del payload["data"]["payment_button"]
|
|
session = FakeSession([payload])
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
with pytest.raises(WeiboExchangeError, match="未返回兑换按钮状态"):
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
assert _exchange_calls(session) == []
|
|
|
|
|
|
def test_exchange_goods_verify_uses_shorter_timeout():
|
|
session = FakeSession(
|
|
[
|
|
_detail_payload(),
|
|
{"code": 100000, "msg": "success", "data": None},
|
|
_mall_list_payload(balance=100, remain=9),
|
|
]
|
|
)
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
mall_calls = [call for call in session.calls if call[1].endswith("/mall/list")]
|
|
assert mall_calls
|
|
for _method, _url, kwargs in mall_calls:
|
|
assert kwargs["timeout"] == 8.0
|
|
for _method, _url, kwargs in _detail_calls(session) + _exchange_calls(session):
|
|
assert kwargs["timeout"] == client.timeout
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"payload",
|
|
[
|
|
{},
|
|
{"msg": "success"},
|
|
{"code": None},
|
|
{"code": ["100000"]},
|
|
{"code": {"value": 100000}},
|
|
{"code": "100000"},
|
|
{"code": True},
|
|
{"code": False},
|
|
],
|
|
ids=[
|
|
"empty",
|
|
"missing-code",
|
|
"none-code",
|
|
"list-code",
|
|
"dict-code",
|
|
"str-code",
|
|
"true-code",
|
|
"false-code",
|
|
],
|
|
)
|
|
def test_submit_exchange_treats_missing_or_bad_code_as_uncertain(payload):
|
|
session = FakeSession([_detail_payload(), payload])
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
with pytest.raises(WeiboExchangeUncertainError, match="结果未知"):
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
assert len(_exchange_calls(session)) == 1
|
|
|
|
|
|
def test_exchange_goods_cancels_when_remain_count_missing():
|
|
payload = _detail_payload()
|
|
del payload["data"]["goods_info"]["remain_count"]
|
|
session = FakeSession([payload])
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
with pytest.raises(WeiboExchangeError, match="已取消兑换") as exc_info:
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
assert not isinstance(exc_info.value, WeiboExchangeUncertainError)
|
|
assert _exchange_calls(session) == []
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"button_type",
|
|
["recharge", None, "", "exchangegoods"],
|
|
ids=["recharge", "missing", "empty", "other"],
|
|
)
|
|
def test_exchange_goods_cancels_when_payment_button_type_unusable(button_type):
|
|
payload = _detail_payload(button_type=button_type)
|
|
if button_type is None:
|
|
del payload["data"]["payment_button"]["type"]
|
|
session = FakeSession([payload])
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
with pytest.raises(WeiboExchangeError, match="兑换按钮类型") as exc_info:
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
assert not isinstance(exc_info.value, WeiboExchangeUncertainError)
|
|
assert _exchange_calls(session) == []
|
|
|
|
|
|
def test_exchange_goods_accepts_payment_button_type_with_padding():
|
|
session = FakeSession(
|
|
[
|
|
_detail_payload(button_type=" exchange "),
|
|
{"code": 100000, "msg": "success", "data": None},
|
|
_mall_list_payload(balance=100, remain=9),
|
|
]
|
|
)
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
result = client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
assert result.verified is True
|
|
assert len(_exchange_calls(session)) == 1
|
|
|
|
|
|
def test_exchange_goods_cancels_when_price_is_fractional():
|
|
session = FakeSession([_detail_payload(price=100.9)])
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
with pytest.raises(WeiboExchangeError, match="已取消兑换"):
|
|
client.exchange_goods(GOODS_ID, expected_price=100)
|
|
|
|
assert _exchange_calls(session) == []
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("value", "expected"),
|
|
[
|
|
(100.9, None),
|
|
(float("inf"), None),
|
|
(float("nan"), None),
|
|
(100.0, 100),
|
|
(100, 100),
|
|
(True, None),
|
|
("100", 100),
|
|
(" 100 ", 100),
|
|
("100.5", None),
|
|
("100.0", None),
|
|
("", None),
|
|
(None, None),
|
|
([100], None),
|
|
],
|
|
)
|
|
def test_as_int_rejects_fractional_and_exotic_values(value, expected):
|
|
from core.weibo.client import _as_int
|
|
|
|
assert _as_int(value) == expected
|
|
|
|
|
|
def test_xsrf_headers_prefers_most_specific_domain_cookie():
|
|
session = FakeSession([])
|
|
session.cookies.set("XSRF-TOKEN", "wide-domain", domain=".weibo.com")
|
|
session.cookies.set("XSRF-TOKEN", "narrow-domain", domain="vipclub.e.weibo.com")
|
|
session.cookies.set("XSRF-TOKEN", "other-domain", domain=".example.com")
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
assert client._xsrf_headers() == {"x-xsrf-token": "narrow-domain"}
|
|
|
|
|
|
def test_xsrf_headers_accepts_parent_domain_cookie_only():
|
|
session = FakeSession([])
|
|
session.cookies.set("XSRF-TOKEN", "wide-domain", domain=".weibo.com")
|
|
session.cookies.set("XSRF-TOKEN", "other-domain", domain=".example.com")
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
assert client._xsrf_headers() == {"x-xsrf-token": "wide-domain"}
|
|
|
|
|
|
def test_xsrf_headers_returns_empty_when_cookie_missing():
|
|
session = FakeSession([])
|
|
client = WeiboClient("SUB=valid", session=session)
|
|
|
|
assert client._xsrf_headers() == {}
|