Files
live-hub-py/tests/test_huya_cookie_rebuild.py
T
yml2213andCursor e81119117f feat(huya): 多选登录改单线程队列并拆分路由与工作台
默认同时只登一个号,失败两次跳下一个,号间留间隔,结束后可一键重试失败账号。
顺带拆分虎牙路由与斗鱼工作台页面,并补齐 raw_cookie 重建入口。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-10 11:31:50 +08:00

85 lines
3.0 KiB
Python

"""虎牙网页 Cookie 回写与 raw_cookie 重建(不发起登录或支付)。"""
import pytest
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker
from core.huya.web_cookie_fields import cookie_fields, missing_web_cookie_fields
from web.backend.database import Base
from web.backend.models import HuyaAccount, User
from web.backend.routers.huya.cookies import rebuild_web_cookie
from web.backend.services.huya_service import (
persist_huya_session_cookie,
rebuild_web_cookie_from_raw,
)
RAW_COOKIE = (
"udb_cred=cred; udb_uid=100; yyuid=100; udb_biztoken=biz; udb_appid=5008; sdid=sdid"
)
class TestHuyaCookieRebuild:
def setup_method(self):
self.engine = create_engine("sqlite://")
@event.listens_for(self.engine, "connect")
def enable_foreign_keys(connection, _):
connection.execute("PRAGMA foreign_keys=ON")
Base.metadata.create_all(self.engine)
self.session = sessionmaker(bind=self.engine)()
self.user = User(username="operator", password_hash="hash", role="super_admin")
self.account = HuyaAccount(
uid="100",
yyuid="100",
username="",
cookie=RAW_COOKIE,
raw_cookie=RAW_COOKIE,
)
self.session.add_all([self.user, self.account])
self.session.commit()
def teardown_method(self):
self.session.close()
Base.metadata.drop_all(self.engine)
self.engine.dispose()
def test_persist_writes_filled_session_cookie(self):
filled = RAW_COOKIE + "; guid=0123456789abcdef0123456789abcdef"
assert persist_huya_session_cookie(
self.session, self.account.id, RAW_COOKIE, filled
)
self.session.refresh(self.account)
assert "guid=" in self.account.cookie
assert (
persist_huya_session_cookie(self.session, self.account.id, filled, filled)
is False
)
def test_rebuild_from_raw_keeps_app_creds_and_fills_web_fields(self):
rebuilt = rebuild_web_cookie_from_raw(self.session, self.account)
fields = cookie_fields(rebuilt)
assert fields["udb_cred"] == "cred"
assert fields["udb_appid"] == "5002"
assert not missing_web_cookie_fields(rebuilt)
self.session.refresh(self.account)
assert self.account.cookie == rebuilt
assert self.account.raw_cookie == RAW_COOKIE
def test_rebuild_without_raw_cookie_raises(self):
self.account.raw_cookie = ""
self.session.commit()
with pytest.raises(ValueError, match="原始凭据"):
rebuild_web_cookie_from_raw(self.session, self.account)
def test_rebuild_route_returns_cookie_without_login(self):
payload = rebuild_web_cookie(
account_id=self.account.id,
db=self.session,
current=self.user,
)
assert payload["id"] == self.account.id
assert payload["has_raw_cookie"] is True
assert payload["cookie"]
assert "udb_cred=cred" in payload["cookie"]