Skip to content

Commit

Permalink
🐛 forbid adding platform that needs browser in no-browser env
Browse files Browse the repository at this point in the history
  • Loading branch information
felinae98 committed Aug 12, 2024
1 parent 29f23eb commit 83aa7c4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nonebot_bison/sub_manager/add_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("平台输入错误")
Expand Down
44 changes: 44 additions & 0 deletions tests/sub_manager/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
4 changes: 4 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "平台输入错误"
Expand Down

0 comments on commit 83aa7c4

Please sign in to comment.