SiteInteractor 的 Chromium 是进程级单例,此前启动后即假定永远活着:全仓唯一的 is_connected() 探活在 scraping 侧,交易侧既不探活也不重启。容器里 Chromium 崩溃 是有真实前提的(/dev/shm 不足、OOM kill、seccomp 挡 sandbox,docker-compose.yml 里已有相关注释),一旦发生,进程还活着但之后每一单都会失败,且 /health 恒返回 ok,restart: unless-stopped 永远不会被触发。 更隐蔽的一条:clear_cart / enter_checkout / pay 等处的 new_page()、context.request 写在 try 之外,掉线抛的 TargetClosedError 不是 AppError,会穿过 runner 的 except AppError 落到主循环那个只记日志的兜底里——任务一次都不上报,网关侧要干等 整个 lease_ttl(默认 300s)才被 sweep 置 stale。clear_cart 是 execute() 的 step 0, 浏览器死时最先撞上的正是它。 分三层处理: - 任务边界自愈。_launch() 从 start() 抽出,_context_options() 统一 context 参数 (重建必须与启动完全一致,指纹漂移就是一次风控事件);_refresh_context_if_stale 改名 _ensure_context_ready(),先探活重建再做原有的 storage_state mtime 检查 (顺序不可换,mtime 重建要用 self._browser)。重建前丢弃 _checkout_pages 里的 残留确认页并记 warning——那些 Page 已随浏览器一起没了。 - 中途掉线不重建,抛 BrowserDeadError(新增,5006)。新增 _new_page() 与 _request() 两个壳收口裸异常;_request() 只在确认浏览器真死了时才改写异常, 站点 5xx 这类正常业务失败原样抛出。submit_order / pay 入口用 _require_live_browser() 直接拒绝:这两步复用 enter_checkout 留存的 Page, 重建救不回服务端订单草稿,而 pay 跑的时候订单已经真的提交了。顺带修掉一个 误诊——浏览器死时 submit_order 原先报「未找到确认按钮」,把「浏览器崩了」 说成「站点改版了」,两者的处置方式完全不同。 - runner 把 BrowserDeadError 转 needs_human 而非 failed,except 分支排在 except AppError 之前(子类,顺序反了就报 failed)。掉线发生在动作中途, 站点侧生效与否无从判断,不能给上游「明确失败」的结论。 /health 暴露 browser 状态,掉线时 degraded + HTTP 503 + code 5006,让 Dockerfile.trading 的 HEALTHCHECK 探到并重启容器。重启不会导致重复下单:网关侧 任务绝不自动重投,租约过期只置 stale 等人工 reclaim(docs/order-gateway.md §5), 重启只是恢复领新任务的能力。启动窗口期 started=False 不算掉线。 README 错误码表补 5005(此前遗漏)与 5006。 新增 15 个用例覆盖探活三态、is_connected() 自身抛错、边界重建/不重建/丢弃残留页/ 重建失败、两个包装壳的分支、submit/pay 拒绝、runner 转 needs_human、/health 503。 真实 Chromium 崩溃无法在离线测试里制造,用例模拟的是 is_connected() 返回 False 这个唯一可观测信号,覆盖的是代码对该信号的反应而非崩溃本身;容器 HEALTHCHECK 真的触发重启这条链路尚未实跑验证。 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
482 lines
16 KiB
Python
482 lines
16 KiB
Python
"""交易服务 API 测试:健康检查、登录态查询与重载、购物车接口、鉴权
|
|
|
|
登录态会话与站点交互器都被替换为桩,不触达真实站点、不需要真实账号、不起 Playwright。
|
|
AuthSession 自身的行为(cookie 加载、探测判据)在 tests/test_auth_session.py。
|
|
SiteInteractor 的 DOM 解析纯函数在 tests/test_site_interact.py。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.shared.config import get_settings
|
|
from app.shared.errors import CartOperationError, NotLoggedInError
|
|
from app.trading.main import create_app
|
|
from app.trading.services.auth_session import AuthStatus
|
|
|
|
TOKEN = get_settings().bearer_token
|
|
AUTH = {"Authorization": f"Bearer {TOKEN}"}
|
|
|
|
|
|
class StubAuthSession:
|
|
"""记录调用并返回固定登录态的桩"""
|
|
|
|
def __init__(self) -> None:
|
|
self.checked: list[str] = []
|
|
self.reloaded: list[str] = []
|
|
self.relogin_calls: list[str] = []
|
|
self.logged_in = True
|
|
# try_relogin 的结果;None 表示「登录成功并转为登录态」
|
|
self.relogin_result: bool | None = None
|
|
|
|
@property
|
|
def sites(self) -> tuple[str, ...]:
|
|
return ("rakuten",)
|
|
|
|
def _status(self, site: str) -> AuthStatus:
|
|
return AuthStatus(
|
|
site=site,
|
|
state_file_exists=True,
|
|
logged_in=self.logged_in,
|
|
checked_at=None,
|
|
detail="stub",
|
|
)
|
|
|
|
async def check(self, site: str) -> AuthStatus:
|
|
self.checked.append(site)
|
|
return self._status(site)
|
|
|
|
def status(self, site: str) -> AuthStatus:
|
|
return self._status(site)
|
|
|
|
def status_all(self) -> dict[str, dict]:
|
|
return {site: self._status(site).to_dict() for site in self.sites}
|
|
|
|
def reload(self, site: str) -> int:
|
|
self.reloaded.append(site)
|
|
return 3
|
|
|
|
async def try_relogin(self, site: str) -> bool:
|
|
"""默认「登录成功」:翻成登录态并返回 True;relogin_result 可注入失败"""
|
|
self.relogin_calls.append(site)
|
|
if self.relogin_result is None:
|
|
self.logged_in = True
|
|
return True
|
|
return self.relogin_result
|
|
|
|
async def require_logged_in(self, site: str) -> None:
|
|
if not self.logged_in:
|
|
raise NotLoggedInError(site=site, detail="stub")
|
|
|
|
async def close(self) -> None:
|
|
"""lifespan 收尾会调用;桩没有真实客户端要关"""
|
|
|
|
|
|
class StubSiteInteractor:
|
|
"""记录调用并返回固定结果的 SiteInteractor 桩
|
|
|
|
真实 SiteInteractor 起 Playwright,本桩不打外网、不开浏览器。
|
|
通过 fail_with 注入异常可测错误路径(5001/5002)。
|
|
"""
|
|
|
|
def __init__(self) -> None:
|
|
self.add_calls: list[dict] = []
|
|
self.status_calls = 0
|
|
self.clear_calls = 0
|
|
self.remove_calls: list[str] = []
|
|
self.fail_with: Exception | None = None
|
|
self.browser_alive = True
|
|
|
|
async def add_to_cart_payload(
|
|
self,
|
|
*,
|
|
item_url: str,
|
|
quantity: int = 1,
|
|
variant_id: str | None = None,
|
|
choice: str | list[str] | None = None,
|
|
) -> dict:
|
|
self.add_calls.append(
|
|
{
|
|
"item_url": item_url,
|
|
"quantity": quantity,
|
|
"variant_id": variant_id,
|
|
"choice": choice,
|
|
}
|
|
)
|
|
if self.fail_with:
|
|
raise self.fail_with
|
|
return {
|
|
"item_id": "10000382",
|
|
"shop_bid": "284609",
|
|
"basket_domain": "https://sp.basket.step.rakuten.co.jp/rms/mall/bss/cartadd/set",
|
|
"cart_count": 1,
|
|
}
|
|
|
|
async def cart_status(self) -> dict:
|
|
self.status_calls += 1
|
|
if self.fail_with:
|
|
raise self.fail_with
|
|
return {"logged_in": True, "count": 1, "raw_status": "100"}
|
|
|
|
async def clear_cart(self) -> dict:
|
|
self.clear_calls += 1
|
|
if self.fail_with:
|
|
raise self.fail_with
|
|
return {"removed_count": 1, "cart_count": 0}
|
|
|
|
async def remove_item(self, item_id: str) -> dict:
|
|
self.remove_calls.append(item_id)
|
|
if self.fail_with:
|
|
raise self.fail_with
|
|
return {"removed": True, "item_id": item_id}
|
|
|
|
def browser_status(self) -> dict:
|
|
"""/health 读的浏览器状态;browser_alive 可翻成 False 模拟掉线"""
|
|
return {
|
|
"started": True,
|
|
"alive": self.browser_alive,
|
|
"pending_checkout_tasks": [],
|
|
"detail": "连接正常" if self.browser_alive else "浏览器已掉线",
|
|
}
|
|
|
|
async def close(self) -> None:
|
|
"""lifespan 收尾会调用;桩没有真实浏览器要关"""
|
|
|
|
|
|
@pytest.fixture
|
|
def client_and_stubs():
|
|
app = create_app()
|
|
with TestClient(app) as client:
|
|
stub = StubAuthSession()
|
|
stub_site = StubSiteInteractor()
|
|
app.state.container.auth_session = stub
|
|
app.state.container.site = stub_site
|
|
yield client, stub, stub_site
|
|
|
|
|
|
@pytest.fixture
|
|
def client(client_and_stubs):
|
|
return client_and_stubs[0]
|
|
|
|
|
|
@pytest.fixture
|
|
def stub(client_and_stubs):
|
|
return client_and_stubs[1]
|
|
|
|
|
|
@pytest.fixture
|
|
def stub_site(client_and_stubs):
|
|
return client_and_stubs[2]
|
|
|
|
|
|
# ---- 健康检查 ----
|
|
|
|
|
|
def test_health_needs_no_token(client):
|
|
response = client.get("/health")
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["data"]["status"] == "ok"
|
|
assert set(body["data"]["auth"]) == {"rakuten"}
|
|
assert body["data"]["browser"]["alive"] is True
|
|
|
|
|
|
def test_health_returns_503_when_browser_is_dead(client, stub_site):
|
|
"""浏览器掉线 → 503,让容器 HEALTHCHECK 探到并触发重启
|
|
|
|
进程还活着、端口还通,但这个服务的所有站点操作都要靠那一个浏览器,它没了以后
|
|
每一单都会失败。返回 200 的话 HEALTHCHECK 永远绿灯,缺口就一直挂在那儿。
|
|
重启是安全的:网关侧任务绝不自动重投(docs/order-gateway.md §5)。
|
|
"""
|
|
stub_site.browser_alive = False
|
|
|
|
response = client.get("/health")
|
|
|
|
assert response.status_code == 503
|
|
body = response.json()
|
|
assert body["success"] is False
|
|
assert body["code"] == 5006 # 与 BrowserDeadError 同码
|
|
assert body["data"]["status"] == "degraded"
|
|
assert body["data"]["browser"]["alive"] is False
|
|
|
|
|
|
def test_health_does_not_probe_the_site(client, stub):
|
|
"""健康检查只读缓存:它会被高频轮询,不能每次都去打站点"""
|
|
client.get("/health")
|
|
assert stub.checked == []
|
|
|
|
|
|
# ---- 鉴权 ----
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"path", ["/api/auth/status", "/api/auth/login", "/api/auth/reload"]
|
|
)
|
|
def test_auth_endpoints_reject_missing_token(client, path):
|
|
response = client.post(path, json={})
|
|
assert response.status_code == 401
|
|
assert response.json()["code"] == 1001
|
|
|
|
|
|
def test_auth_endpoints_reject_wrong_token(client):
|
|
response = client.post(
|
|
"/api/auth/status", json={}, headers={"Authorization": "Bearer wrong-token"}
|
|
)
|
|
assert response.status_code == 401
|
|
|
|
|
|
# ---- 登录态查询 ----
|
|
|
|
|
|
def test_status_probes_site_by_default(client, stub):
|
|
response = client.post("/api/auth/status", json={}, headers=AUTH)
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert [item["site"] for item in body["data"]["sites"]] == ["rakuten"]
|
|
assert stub.checked == ["rakuten"]
|
|
|
|
|
|
def test_status_can_skip_the_probe(client, stub):
|
|
"""refresh=false 时读缓存,不打站点"""
|
|
response = client.post("/api/auth/status", json={"refresh": False}, headers=AUTH)
|
|
assert response.status_code == 200
|
|
assert stub.checked == []
|
|
|
|
|
|
def test_status_accepts_a_single_site(client, stub):
|
|
response = client.post("/api/auth/status", json={"site": "rakuten"}, headers=AUTH)
|
|
assert response.status_code == 200
|
|
assert stub.checked == ["rakuten"]
|
|
|
|
|
|
def test_status_rejects_unknown_site(client):
|
|
"""站点名是枚举,未知值应在校验层就被挡下"""
|
|
response = client.post("/api/auth/status", json={"site": "mercari"}, headers=AUTH)
|
|
assert response.status_code == 422
|
|
assert response.json()["code"] == 1002
|
|
|
|
|
|
# ---- 自动登录 ----
|
|
|
|
|
|
def test_login_skips_when_already_logged_in(client, stub):
|
|
"""已登录时不该白起一次登录流程(起浏览器 + 打站点,代价不小)"""
|
|
response = client.post("/api/auth/login", json={}, headers=AUTH)
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["data"]["logged_in"] is True
|
|
assert body["data"]["relogin_attempted"] == {"rakuten": False}
|
|
assert stub.relogin_calls == []
|
|
# 结论必须来自真实探测,不能只看缓存
|
|
assert stub.checked == ["rakuten"]
|
|
|
|
|
|
def test_login_triggers_relogin_when_logged_out(client, stub):
|
|
stub.logged_in = False
|
|
response = client.post("/api/auth/login", json={"site": "rakuten"}, headers=AUTH)
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["data"]["logged_in"] is True
|
|
assert body["data"]["relogin_attempted"] == {"rakuten": True}
|
|
assert stub.relogin_calls == ["rakuten"]
|
|
# 登录后必须再探测一次确认,不能拿登录流程的自述当结论
|
|
assert stub.checked == ["rakuten", "rakuten"]
|
|
|
|
|
|
def test_login_reports_failure_without_raising(client, stub):
|
|
"""登录失败按 logged_in=false 正常返回,不是 5001
|
|
|
|
调用方要据此决定人工接管还是换账号;抛错会把「为什么失败」压成一个错误码。
|
|
"""
|
|
stub.logged_in = False
|
|
stub.relogin_result = False
|
|
response = client.post("/api/auth/login", json={}, headers=AUTH)
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["success"] is True
|
|
assert body["data"]["logged_in"] is False
|
|
assert body["data"]["relogin_attempted"] == {"rakuten": True}
|
|
|
|
|
|
def test_login_rejects_unknown_site(client):
|
|
response = client.post("/api/auth/login", json={"site": "mercari"}, headers=AUTH)
|
|
assert response.status_code == 422
|
|
assert response.json()["code"] == 1002
|
|
|
|
|
|
# ---- 启动时自动登录(RAKUTEN_AUTO_LOGIN_ON_START)----
|
|
|
|
|
|
class _FakeContainer:
|
|
def __init__(self, auth_session) -> None:
|
|
self.auth_session = auth_session
|
|
|
|
|
|
async def test_auto_login_on_start_skips_when_logged_in():
|
|
from app.trading.main import auto_login_on_start
|
|
|
|
stub = StubAuthSession()
|
|
await auto_login_on_start(_FakeContainer(stub))
|
|
assert stub.relogin_calls == []
|
|
|
|
|
|
async def test_auto_login_on_start_logs_in_when_logged_out():
|
|
from app.trading.main import auto_login_on_start
|
|
|
|
stub = StubAuthSession()
|
|
stub.logged_in = False
|
|
await auto_login_on_start(_FakeContainer(stub))
|
|
assert stub.relogin_calls == ["rakuten"]
|
|
|
|
|
|
async def test_auto_login_on_start_swallows_errors():
|
|
"""探测抛错也不能把启动流程带崩——服务要能起来报「未登录」"""
|
|
from app.trading.main import auto_login_on_start
|
|
|
|
class Boom(StubAuthSession):
|
|
async def check(self, site: str):
|
|
raise RuntimeError("站点不可达")
|
|
|
|
stub = Boom()
|
|
await auto_login_on_start(_FakeContainer(stub))
|
|
assert stub.relogin_calls == []
|
|
|
|
|
|
# ---- 登录态重载 ----
|
|
|
|
|
|
def test_reload_reloads_then_probes(client, stub):
|
|
response = client.post("/api/auth/reload", json={"site": "rakuten"}, headers=AUTH)
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["data"]["reloaded"] == {"rakuten": 3}
|
|
# 重载后必须立刻探测一次,否则调用方拿不到「这次登录到底成没成」
|
|
assert stub.reloaded == ["rakuten"]
|
|
assert stub.checked == ["rakuten"]
|
|
|
|
|
|
# ---- 购物车接口 ----
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"path",
|
|
["/api/cart/add", "/api/cart/status", "/api/cart/clear", "/api/cart/remove"],
|
|
)
|
|
def test_cart_endpoints_reject_missing_token(client, path):
|
|
"""所有 cart 接口都要 Bearer token"""
|
|
response = client.post(path, json={})
|
|
assert response.status_code == 401
|
|
assert response.json()["code"] == 1001
|
|
|
|
|
|
def test_cart_add_happy_path(client, stub_site):
|
|
"""加购成功:返回 added=true 与 cart_count"""
|
|
response = client.post(
|
|
"/api/cart/add",
|
|
json={"item_url": "https://item.rakuten.co.jp/shop/x/", "quantity": 2},
|
|
headers=AUTH,
|
|
)
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["data"]["added"] is True
|
|
assert body["data"]["item_id"] == "10000382"
|
|
assert body["data"]["shop_bid"] == "284609"
|
|
assert body["data"]["cart_count"] == 1
|
|
# 桩记下了入参
|
|
assert stub_site.add_calls == [
|
|
{
|
|
"item_url": "https://item.rakuten.co.jp/shop/x/",
|
|
"quantity": 2,
|
|
"variant_id": None,
|
|
"choice": None,
|
|
}
|
|
]
|
|
|
|
|
|
def test_cart_add_missing_item_url(client):
|
|
"""item_url 必填,缺失时 Pydantic 在校验层挡下(422)"""
|
|
response = client.post("/api/cart/add", json={"quantity": 1}, headers=AUTH)
|
|
assert response.status_code == 422
|
|
assert response.json()["code"] == 1002
|
|
|
|
|
|
def test_cart_add_propagates_not_logged_in(client, stub_site):
|
|
"""SiteInteractor 抛 NotLoggedInError(5001)→ HTTP 401"""
|
|
stub_site.fail_with = NotLoggedInError(site="rakuten", detail="stub")
|
|
response = client.post(
|
|
"/api/cart/add",
|
|
json={"item_url": "https://item.rakuten.co.jp/shop/x/"},
|
|
headers=AUTH,
|
|
)
|
|
assert response.status_code == 401
|
|
assert response.json()["code"] == 5001
|
|
|
|
|
|
def test_cart_add_propagates_cart_error(client, stub_site):
|
|
"""SiteInteractor 抛 CartOperationError(5002)→ HTTP 400"""
|
|
stub_site.fail_with = CartOperationError("商品不可购买:purchaseCondition=disabled")
|
|
response = client.post(
|
|
"/api/cart/add",
|
|
json={"item_url": "https://item.rakuten.co.jp/shop/x/"},
|
|
headers=AUTH,
|
|
)
|
|
assert response.status_code == 400
|
|
assert response.json()["code"] == 5002
|
|
|
|
|
|
def test_cart_status_happy_path(client, stub_site):
|
|
response = client.post("/api/cart/status", json={}, headers=AUTH)
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["data"]["logged_in"] is True
|
|
assert body["data"]["count"] == 1
|
|
assert body["data"]["raw_status"] == "100"
|
|
assert stub_site.status_calls == 1
|
|
|
|
|
|
def test_cart_clear_happy_path(client, stub_site):
|
|
response = client.post("/api/cart/clear", json={}, headers=AUTH)
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["data"]["removed_count"] == 1
|
|
assert body["data"]["cart_count"] == 0
|
|
assert stub_site.clear_calls == 1
|
|
|
|
|
|
def test_cart_remove_happy_path(client, stub_site):
|
|
response = client.post(
|
|
"/api/cart/remove", json={"item_id": "10000382"}, headers=AUTH
|
|
)
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["data"]["removed"] is True
|
|
assert body["data"]["item_id"] == "10000382"
|
|
assert stub_site.remove_calls == ["10000382"]
|
|
|
|
|
|
def test_cart_remove_missing_item_id(client):
|
|
response = client.post("/api/cart/remove", json={}, headers=AUTH)
|
|
assert response.status_code == 422
|
|
assert response.json()["code"] == 1002
|
|
|
|
|
|
def test_cart_remove_propagates_cart_error(client, stub_site):
|
|
"""指定 item_id 不在购物车里 → 5002"""
|
|
stub_site.fail_with = CartOperationError("购物车里没有 item_id=99999")
|
|
response = client.post(
|
|
"/api/cart/remove", json={"item_id": "99999"}, headers=AUTH
|
|
)
|
|
assert response.status_code == 400
|
|
assert response.json()["code"] == 5002
|
|
|
|
|
|
def test_cart_endpoints_unavailable_when_site_is_none(client):
|
|
"""SiteInteractor 未就绪(container.site 为 None)→ UpstreamRequestError 3001
|
|
|
|
生产环境 lifespan 一定构造了 site;本测试模拟异常启动路径。
|
|
"""
|
|
client.app.state.container.site = None
|
|
response = client.post("/api/cart/status", json={}, headers=AUTH)
|
|
assert response.status_code == 400
|
|
assert response.json()["code"] == 3001
|