diff --git a/nonebot_bison/sub_manager/add_sub.py b/nonebot_bison/sub_manager/add_sub.py index 38dddf088..eb90b4bbd 100644 --- a/nonebot_bison/sub_manager/add_sub.py +++ b/nonebot_bison/sub_manager/add_sub.py @@ -9,6 +9,7 @@ from ..types import Target from ..config import config from ..apis import check_sub_target +from ..plugin_config import plugin_config from ..platform import Platform, platform_manager from ..config.db_config import SubscribeDupException from .utils import common_platform, ensure_user_info, gen_handle_cancel @@ -39,6 +40,8 @@ async def parse_platform(state: T_State, platform: str = ArgPlainText()) -> None elif platform == "取消": await add_sub.finish("已中止订阅") elif platform in platform_manager: + if platform_manager[platform].site.require_browser and not plugin_config.bison_use_browser: + await add_sub.finish(f"{platform} 需要启用 bison_use_browser 选项,否则将无法正确订阅") state["platform"] = platform else: await add_sub.reject("平台输入错误") diff --git a/tests/sub_manager/test_add.py b/tests/sub_manager/test_add.py index 68bc41553..fa3a255e7 100644 --- a/tests/sub_manager/test_add.py +++ b/tests/sub_manager/test_add.py @@ -615,3 +615,47 @@ async def test_add_with_bilibili_bangumi_target_parser(app: App, init_scheduler) assert sub.tags == [] assert sub.target.platform_name == "bilibili-bangumi" assert sub.target.target_name == "汉化日记 第三季" + + +@pytest.mark.asyncio +async def test_subscribe_platform_requires_browser(app: App, mocker: MockerFixture): + from nonebot.adapters.onebot.v11.event import Sender + from nonebot.adapters.onebot.v11.message import Message + + from nonebot_bison.platform import platform_manager + from nonebot_bison.plugin_config import plugin_config + from nonebot_bison.sub_manager import add_sub_matcher, common_platform + + mocker.patch.object(plugin_config, "bison_use_browser", False) + + async with app.test_matcher(add_sub_matcher) as ctx: + bot = ctx.create_bot() + event_1 = fake_group_message_event( + message=Message("添加订阅"), + sender=Sender(card="", nickname="test", role="admin"), + to_me=True, + ) + ctx.receive_event(bot, event_1) + ctx.should_pass_rule() + ctx.should_call_send( + event_1, + BotReply.add_reply_on_platform(platform_manager=platform_manager, common_platform=common_platform), + True, + ) + event_2 = fake_group_message_event( + message=Message("全部"), sender=Sender(card="", nickname="test", role="admin") + ) + ctx.receive_event(bot, event_2) + ctx.should_rejected() + ctx.should_call_send( + event_2, + BotReply.add_reply_on_platform_input_allplatform(platform_manager), + True, + ) + event_3 = fake_group_message_event(message=Message("bilibili"), sender=fake_admin_user) + ctx.receive_event(bot, event_3) + ctx.should_call_send( + event_3, + BotReply.add_reply_need_use_browser("bilibili"), + True, + ) diff --git a/tests/utils.py b/tests/utils.py index 5ea3440cf..0726d8362 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -146,6 +146,10 @@ def add_reply_on_id(platform: object) -> str: extra_text = ("1." + target_promot + "\n2.") if target_promot else "" return extra_text + base_text + @staticmethod + def add_reply_need_use_browser(platform: str) -> str: + return f"{platform} 需要启用 bison_use_browser 选项,否则将无法正确订阅" + add_reply_on_id_input_error = "id输入错误" add_reply_on_target_parse_input_error = "不能从你的输入中提取出id,请检查你输入的内容是否符合预期" add_reply_on_platform_input_error = "平台输入错误"