From d6205867b8750f6859586129808dca0c14638f23 Mon Sep 17 00:00:00 2001 From: suyiiyii Date: Sun, 1 Sep 2024 22:32:47 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=97=A0=E6=9D=83=E9=99=90?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=B0=9D=E8=AF=95=E6=B7=BB=E5=8A=A0=E8=AE=A2?= =?UTF-8?q?=E9=98=85=E6=97=B6=E8=BF=94=E5=9B=9E=E6=8F=90=E7=A4=BA=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=20(#617)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :sparkles: 无权限用户尝试添加订阅时返回提示信息 * :sparkles: 添加no_permission_matcher相关的单元测试 * :bug: 优化无权限提示的排版 * :bug: 移除没有必要的命令 --- nonebot_bison/sub_manager/__init__.py | 11 ++++++ tests/sub_manager/test_no_permission.py | 45 +++++++++++++++++++++++++ tests/utils.py | 1 + 3 files changed, 57 insertions(+) create mode 100644 tests/sub_manager/test_no_permission.py diff --git a/nonebot_bison/sub_manager/__init__.py b/nonebot_bison/sub_manager/__init__.py index 119a0e9..1c984dc 100644 --- a/nonebot_bison/sub_manager/__init__.py +++ b/nonebot_bison/sub_manager/__init__.py @@ -108,10 +108,21 @@ async def do_dispatch_command( asyncio.create_task(new_matcher_ins.run(bot, event, state)) +no_permission_matcher = on_command( + "添加订阅", rule=configurable_to_me, aliases={"删除订阅", "群管理"}, priority=8, block=True +) + + +@no_permission_matcher.handle() +async def send_no_permission(): + await no_permission_matcher.finish("您没有权限进行此操作,请联系 Bot 管理员") + + __all__ = [ "common_platform", "add_sub_matcher", "query_sub_matcher", "del_sub_matcher", "group_manage_matcher", + "no_permission_matcher", ] diff --git a/tests/sub_manager/test_no_permission.py b/tests/sub_manager/test_no_permission.py new file mode 100644 index 0000000..25a6849 --- /dev/null +++ b/tests/sub_manager/test_no_permission.py @@ -0,0 +1,45 @@ +import pytest +from nonebug import App + +from ..utils import BotReply, fake_admin_user, fake_group_message_event + + +@pytest.mark.asyncio +async def test_with_permission(app: App): + from nonebot.adapters.onebot.v11.bot import Bot + from nonebot.adapters.onebot.v11.message import Message + + from nonebot_bison.platform import platform_manager + from nonebot_bison.sub_manager import add_sub_matcher, common_platform, no_permission_matcher + + async with app.test_matcher([add_sub_matcher, no_permission_matcher]) as ctx: + bot = ctx.create_bot(base=Bot) + event = fake_group_message_event(message=Message("添加订阅"), sender=fake_admin_user, to_me=True) + ctx.receive_event(bot, event) + ctx.should_call_send( + event, + BotReply.add_reply_on_platform(platform_manager, common_platform), + True, + ) + ctx.should_pass_rule() + ctx.should_pass_permission() + + +@pytest.mark.asyncio +async def test_without_permission(app: App): + from nonebot.adapters.onebot.v11.bot import Bot + from nonebot.adapters.onebot.v11.message import Message + + from nonebot_bison.sub_manager import add_sub_matcher, no_permission_matcher + + async with app.test_matcher([add_sub_matcher, no_permission_matcher]) as ctx: + bot = ctx.create_bot(base=Bot) + event = fake_group_message_event(message=Message("添加订阅"), to_me=True) + ctx.receive_event(bot, event) + ctx.should_call_send( + event, + BotReply.no_permission, + True, + ) + ctx.should_pass_rule() + ctx.should_pass_permission() diff --git a/tests/utils.py b/tests/utils.py index c8dd343..04efad5 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -158,3 +158,4 @@ class BotReply: ) add_reply_on_tags_need_more_info = "订阅标签直接输入标签内容\n屏蔽标签请在标签名称前添加~号\n详见https://nonebot-bison.netlify.app/usage/#%E5%B9%B3%E5%8F%B0%E8%AE%A2%E9%98%85%E6%A0%87%E7%AD%BE-tag" add_reply_abort = "已中止订阅" + no_permission = "您没有权限进行此操作,请联系 Bot 管理员"