mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-02 09:26:12 +08:00
🐛 优化对 bilibili cookie 的 mock
This commit is contained in:
parent
0deb406692
commit
42cc56ac24
@ -74,7 +74,7 @@ class CookieClientManager(ClientManager):
|
||||
await config.add_cookie(new_anonymous_cookie)
|
||||
|
||||
@classmethod
|
||||
async def add_user_cookie(cls, content: str, cookie_name=str | None) -> Cookie:
|
||||
async def add_user_cookie(cls, content: str, cookie_name: str | None = None) -> Cookie:
|
||||
"""添加用户 cookie"""
|
||||
from ..platform import site_manager
|
||||
|
||||
|
@ -29,6 +29,16 @@ def load_adapters(nonebug_init: None):
|
||||
return driver
|
||||
|
||||
|
||||
def _patch_refresh_bilibili_anonymous_cookie(mocker: MockerFixture):
|
||||
# patch 掉bilibili的匿名cookie生成函数,避免真实请求
|
||||
|
||||
from nonebot_bison.platform.bilibili.scheduler import BilibiliClientManager
|
||||
|
||||
mocker.patch.object(
|
||||
BilibiliClientManager, "_get_cookies", return_value=[{"name": "test anonymous", "content": "test"}]
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def app(tmp_path: Path, request: pytest.FixtureRequest, mocker: MockerFixture):
|
||||
sys.path.append(str(Path(__file__).parent.parent / "src" / "plugins"))
|
||||
@ -51,6 +61,10 @@ async def app(tmp_path: Path, request: pytest.FixtureRequest, mocker: MockerFixt
|
||||
|
||||
param: AppReq = getattr(request, "param", AppReq())
|
||||
|
||||
# 如果在 app 前调用会报错“无法找到调用者”
|
||||
# 而在后面调用又来不及mock,所以只能在中间mock
|
||||
_patch_refresh_bilibili_anonymous_cookie(mocker)
|
||||
|
||||
if not param.get("no_init_db"):
|
||||
await init_db()
|
||||
# if not param.get("refresh_bot"):
|
||||
@ -126,7 +140,7 @@ async def _no_browser(app: App, mocker: MockerFixture):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def _clear_db():
|
||||
async def _clear_db(app: App):
|
||||
from nonebot_bison.config import config
|
||||
|
||||
await config.clear_db()
|
||||
@ -135,25 +149,10 @@ async def _clear_db():
|
||||
return
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _patch_refresh_anonymous_cookie(app: App, mocker: MockerFixture):
|
||||
from datetime import datetime
|
||||
|
||||
from nonebot_bison.config.db_config import Cookie
|
||||
from nonebot_bison.utils.site import CookieClientManager
|
||||
|
||||
mock_anonymous_cookie = Cookie(
|
||||
cookie_name="test anonymous",
|
||||
site_name="test",
|
||||
content="{}",
|
||||
is_universal=True,
|
||||
is_anonymous=True,
|
||||
last_usage=datetime.now(),
|
||||
cd_milliseconds=0,
|
||||
tags="{}",
|
||||
status="",
|
||||
)
|
||||
mocker.patch.object(CookieClientManager, "_generate_anonymous_cookie", return_value=mock_anonymous_cookie)
|
||||
@pytest.fixture
|
||||
def _patch_weibo_get_cookie_name(app: App, mocker: MockerFixture):
|
||||
from nonebot_bison.platform import weibo
|
||||
|
||||
mocker.patch.object(weibo.WeiboSite, "get_cookie_name", return_value="weibo_cookie_name")
|
||||
yield
|
||||
mocker.stopall()
|
||||
|
@ -96,7 +96,7 @@ async def test_add_cookie(app: App):
|
||||
async with app.test_matcher(add_cookie_matcher) as ctx:
|
||||
bot = ctx.create_bot(base=Bot)
|
||||
event_1 = fake_private_message_event(
|
||||
message=Message("添加Cookie"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
||||
message=Message("添加cookie"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
||||
)
|
||||
ctx.receive_event(bot, event_1)
|
||||
ctx.should_pass_rule()
|
||||
|
@ -67,6 +67,7 @@ async def test_del_cookie(app: App):
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("_clear_db")
|
||||
@pytest.mark.usefixtures("_patch_weibo_get_cookie_name")
|
||||
async def test_del_cookie_err(app: App):
|
||||
from nonebug_saa import should_send_saa
|
||||
from nonebot.adapters.onebot.v11.bot import Bot
|
||||
|
Loading…
x
Reference in New Issue
Block a user