From 4cfd884916f3872e7a63c4480af5ad527863ce99 Mon Sep 17 00:00:00 2001 From: AkashiCoin Date: Mon, 2 Sep 2024 11:52:44 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E6=81=B6=E6=84=8F=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E6=A3=80=E6=B5=8B=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin_store/test_plugin_store.py | 85 +++++++++++++++++-- tests/config.py | 2 + zhenxun/builtin_plugins/hooks/chkdsk_hook.py | 19 +++-- 3 files changed, 92 insertions(+), 14 deletions(-) diff --git a/tests/builtin_plugins/plugin_store/test_plugin_store.py b/tests/builtin_plugins/plugin_store/test_plugin_store.py index 3b884007..aaf548fd 100644 --- a/tests/builtin_plugins/plugin_store/test_plugin_store.py +++ b/tests/builtin_plugins/plugin_store/test_plugin_store.py @@ -371,7 +371,7 @@ async def test_remove_plugin( assert not (mock_base_path / "plugins" / "search_image" / "__init__.py").is_file() -async def test_plugin_not_exist( +async def test_plugin_not_exist_add( app: App, mocker: MockerFixture, mocked_api: MockRouter, @@ -379,12 +379,12 @@ async def test_plugin_not_exist( tmp_path: Path, ) -> None: """ - 测试插件不存在 + 测试插件不存在,添加插件 """ from zhenxun.builtin_plugins.plugin_store import _matcher init_mocked_api(mocked_api=mocked_api) - plugin_id = 10 + plugin_id = -1 async with app.test_matcher(_matcher) as ctx: bot = create_bot(ctx) @@ -412,13 +412,32 @@ async def test_plugin_not_exist( bot=bot, ) + +async def test_plugin_not_exist_update( + app: App, + mocker: MockerFixture, + mocked_api: MockRouter, + create_bot: Callable, + tmp_path: Path, +) -> None: + """ + 测试插件不存在,更新插件 + """ + from zhenxun.builtin_plugins.plugin_store import _matcher + + init_mocked_api(mocked_api=mocked_api) + plugin_id = -1 + + async with app.test_matcher(_matcher) as ctx: + bot = create_bot(ctx) + bot: Bot = cast(Bot, bot) raw_message = f"更新插件 {plugin_id}" event: GroupMessageEvent = _v11_group_message_event( message=raw_message, self_id=BotId.QQ_BOT, user_id=UserId.SUPERUSER, group_id=GroupId.GROUP_ID_LEVEL_5, - message_id=MessageId.MESSAGE_ID, + message_id=MessageId.MESSAGE_ID_2, to_me=True, ) ctx.receive_event(bot=bot, event=event) @@ -434,13 +453,69 @@ async def test_plugin_not_exist( result=None, bot=bot, ) + + +async def test_plugin_not_exist_remove( + app: App, + mocker: MockerFixture, + mocked_api: MockRouter, + create_bot: Callable, + tmp_path: Path, +) -> None: + """ + 测试插件不存在,移除插件 + """ + from zhenxun.builtin_plugins.plugin_store import _matcher + + init_mocked_api(mocked_api=mocked_api) + plugin_id = -1 + + async with app.test_matcher(_matcher) as ctx: + bot = create_bot(ctx) + bot: Bot = cast(Bot, bot) + raw_message = f"移除插件 {plugin_id}" + event: GroupMessageEvent = _v11_group_message_event( + message=raw_message, + self_id=BotId.QQ_BOT, + user_id=UserId.SUPERUSER, + group_id=GroupId.GROUP_ID_LEVEL_5, + message_id=MessageId.MESSAGE_ID_2, + to_me=True, + ) + ctx.receive_event(bot=bot, event=event) + ctx.should_call_send( + event=event, + message=Message(message="插件ID不存在..."), + result=None, + bot=bot, + ) + + +async def test_plugin_not_exist_search( + app: App, + mocker: MockerFixture, + mocked_api: MockRouter, + create_bot: Callable, + tmp_path: Path, +) -> None: + """ + 测试插件不存在,搜索插件 + """ + from zhenxun.builtin_plugins.plugin_store import _matcher + + init_mocked_api(mocked_api=mocked_api) + plugin_id = -1 + + async with app.test_matcher(_matcher) as ctx: + bot = create_bot(ctx) + bot: Bot = cast(Bot, bot) raw_message = f"搜索插件 {plugin_id}" event: GroupMessageEvent = _v11_group_message_event( message=raw_message, self_id=BotId.QQ_BOT, user_id=UserId.SUPERUSER, group_id=GroupId.GROUP_ID_LEVEL_5, - message_id=MessageId.MESSAGE_ID, + message_id=MessageId.MESSAGE_ID_3, to_me=True, ) ctx.receive_event(bot=bot, event=event) diff --git a/tests/config.py b/tests/config.py index 7a54cd45..d472c0a8 100644 --- a/tests/config.py +++ b/tests/config.py @@ -18,3 +18,5 @@ class GroupId: class MessageId: MESSAGE_ID = 30001 MESSAGE_ID_2 = 30002 + MESSAGE_ID_3 = 30003 + MESSAGE_ID_4 = 30004 diff --git a/zhenxun/builtin_plugins/hooks/chkdsk_hook.py b/zhenxun/builtin_plugins/hooks/chkdsk_hook.py index 5fb66a09..915a1969 100644 --- a/zhenxun/builtin_plugins/hooks/chkdsk_hook.py +++ b/zhenxun/builtin_plugins/hooks/chkdsk_hook.py @@ -2,19 +2,19 @@ import time from collections import defaultdict from nonebot.adapters import Event -from nonebot.adapters.onebot.v11 import Bot -from nonebot.exception import IgnoredException -from nonebot.matcher import Matcher -from nonebot.message import run_preprocessor from nonebot.typing import T_State +from nonebot.matcher import Matcher from nonebot_plugin_alconna import At +from nonebot.adapters.onebot.v11 import Bot +from nonebot.message import run_preprocessor +from nonebot.exception import IgnoredException from nonebot_plugin_session import EventSession -from zhenxun.configs.config import Config -from zhenxun.models.ban_console import BanConsole from zhenxun.services.log import logger +from zhenxun.configs.config import Config from zhenxun.utils.enum import PluginType from zhenxun.utils.message import MessageUtils +from zhenxun.models.ban_console import BanConsole malicious_check_time = Config.get_config("hook", "MALICIOUS_CHECK_TIME") malicious_ban_count = Config.get_config("hook", "MALICIOUS_BAN_COUNT") @@ -36,12 +36,12 @@ class BanCheckLimiter: self.default_check_time = default_check_time self.default_count = default_count - def add(self, key: str | int | float): + def add(self, key: str | float): if self.mint[key] == 1: self.mtime[key] = time.time() self.mint[key] += 1 - def check(self, key: str | int | float) -> bool: + def check(self, key: str | float) -> bool: if time.time() - self.mtime[key] > self.default_check_time: self.mtime[key] = time.time() self.mint[key] = 0 @@ -76,6 +76,7 @@ async def _( PluginType.HIDDEN, PluginType.DEPENDANT, PluginType.ADMIN, + PluginType.SUPERUSER, ]: return else: @@ -101,7 +102,7 @@ async def _( await MessageUtils.build_message( [ At(flag="user", target=user_id), - f"检测到恶意触发命令,您将被封禁 30 分钟", + "检测到恶意触发命令,您将被封禁 30 分钟", ] ).send() logger.debug(