Skip to content

Commit

Permalink
🐛 修复插件商店检查插件更新问题 (#1597)
Browse files Browse the repository at this point in the history
* 🐛 修复插件商店检查插件更新问题

* 🐛 恶意命令检测问题
  • Loading branch information
AkashiCoin authored Sep 2, 2024
1 parent a56d1f2 commit 9eca6a9
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 27 deletions.
232 changes: 225 additions & 7 deletions tests/builtin_plugins/plugin_store/test_plugin_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@

from tests.utils import _v11_group_message_event
from tests.config import BotId, UserId, GroupId, MessageId
from tests.utils import get_content_bytes as _get_content_bytes
from tests.utils import get_response_json as _get_response_json


def get_response_json(file: str) -> dict:
return _get_response_json(Path() / "plugin_store", file=file)


def get_content_bytes(file: str) -> bytes:
return _get_content_bytes(Path() / "plugin_store", file)


def init_mocked_api(mocked_api: MockRouter) -> None:
mocked_api.get(
"https://data.jsdelivr.com/v1/packages/gh/xuanerwa/zhenxun_github_sub@main",
Expand All @@ -40,15 +45,15 @@ def init_mocked_api(mocked_api: MockRouter) -> None:
mocked_api.get(
"https://raw.githubusercontent.com/zhenxun-org/zhenxun_bot_plugins/main/plugins/search_image/__init__.py",
name="search_image_plugin_file_init",
).respond(content=b"")
).respond(content=get_content_bytes("search_image.py"))
mocked_api.get(
"https://raw.githubusercontent.com/zhenxun-org/zhenxun_bot_plugins/main/plugins/alapi/jitang.py",
name="jitang_plugin_file",
).respond(content=b"")
).respond(content=get_content_bytes("jitang.py"))
mocked_api.get(
"https://raw.githubusercontent.com/xuanerwa/zhenxun_github_sub/main/github_sub/__init__.py",
name="github_sub_plugin_file_init",
).respond(content=b"")
).respond(content=get_content_bytes("github_sub.py"))


async def test_add_plugin_basic(
Expand Down Expand Up @@ -207,15 +212,15 @@ async def test_add_plugin_extra(
assert (mock_base_path / "plugins" / "github_sub" / "__init__.py").is_file()


async def test_update_plugin_basic(
async def test_update_plugin_basic_need_update(
app: App,
mocker: MockerFixture,
mocked_api: MockRouter,
create_bot: Callable,
tmp_path: Path,
) -> None:
"""
测试更新基础插件
测试更新基础插件,插件需要更新
"""
from zhenxun.builtin_plugins.plugin_store import _matcher

Expand All @@ -224,6 +229,10 @@ async def test_update_plugin_basic(
"zhenxun.builtin_plugins.plugin_store.data_source.BASE_PATH",
new=tmp_path / "zhenxun",
)
mock_base_path = mocker.patch(
"zhenxun.builtin_plugins.plugin_store.data_source.ShopManage.get_loaded_plugins",
return_value=[("search_image", "0.0")],
)

plugin_id = 1

Expand Down Expand Up @@ -259,6 +268,59 @@ async def test_update_plugin_basic(
assert (mock_base_path / "plugins" / "search_image" / "__init__.py").is_file()


async def test_update_plugin_basic_is_new(
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)
mocker.patch(
"zhenxun.builtin_plugins.plugin_store.data_source.BASE_PATH",
new=tmp_path / "zhenxun",
)
mocker.patch(
"zhenxun.builtin_plugins.plugin_store.data_source.ShopManage.get_loaded_plugins",
return_value=[("search_image", "0.1")],
)

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,
to_me=True,
)
ctx.receive_event(bot=bot, event=event)
ctx.should_call_send(
event=event,
message=Message(message=f"正在更新插件 Id: {plugin_id}"),
result=None,
bot=bot,
)
ctx.should_call_send(
event=event,
message=Message(message="插件 识图 已是最新版本"),
result=None,
bot=bot,
)
assert mocked_api["basic_plugins"].called
assert mocked_api["extra_plugins"].called


async def test_remove_plugin(
app: App,
mocker: MockerFixture,
Expand All @@ -280,8 +342,8 @@ async def test_remove_plugin(
plugin_path = mock_base_path / "plugins" / "search_image"
plugin_path.mkdir(parents=True, exist_ok=True)

with open(plugin_path / "__init__.py", "w") as f:
f.write("")
with open(plugin_path / "__init__.py", "wb") as f:
f.write(get_content_bytes("search_image.py"))

plugin_id = 1

Expand All @@ -307,3 +369,159 @@ async def test_remove_plugin(
assert mocked_api["basic_plugins"].called
assert mocked_api["extra_plugins"].called
assert not (mock_base_path / "plugins" / "search_image" / "__init__.py").is_file()


async def test_plugin_not_exist_add(
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,
to_me=True,
)
ctx.receive_event(bot=bot, event=event)
ctx.should_call_send(
event=event,
message=Message(message=f"正在添加插件 Id: {plugin_id}"),
result=None,
bot=bot,
)
ctx.should_call_send(
event=event,
message=Message(message="插件ID不存在..."),
result=None,
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_2,
to_me=True,
)
ctx.receive_event(bot=bot, event=event)
ctx.should_call_send(
event=event,
message=Message(message=f"正在更新插件 Id: {plugin_id}"),
result=None,
bot=bot,
)
ctx.should_call_send(
event=event,
message=Message(message="插件ID不存在..."),
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_3,
to_me=True,
)
ctx.receive_event(bot=bot, event=event)
ctx.should_call_send(
event=event,
message=Message(message="未找到相关插件..."),
result=None,
bot=bot,
)
2 changes: 2 additions & 0 deletions tests/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ class GroupId:
class MessageId:
MESSAGE_ID = 30001
MESSAGE_ID_2 = 30002
MESSAGE_ID_3 = 30003
MESSAGE_ID_4 = 30004
24 changes: 24 additions & 0 deletions tests/content/plugin_store/github_sub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from nonebot.plugin import PluginMetadata

from zhenxun.configs.utils import PluginExtraData

__plugin_meta__ = PluginMetadata(
name="github订阅",
description="订阅github用户或仓库",
usage="""
usage:
github新Comment,PR,Issue等提醒
指令:
添加github ['用户'/'仓库'] [用户名/{owner/repo}]
删除github [用户名/{owner/repo}]
查看github
示例:添加github订阅 用户 HibiKier
示例:添加gb订阅 仓库 HibiKier/zhenxun_bot
示例:添加github 用户 HibiKier
示例:删除gb订阅 HibiKier
""".strip(),
extra=PluginExtraData(
author="xuanerwa",
version="0.7",
).dict(),
)
17 changes: 17 additions & 0 deletions tests/content/plugin_store/jitang.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from nonebot.plugin import PluginMetadata

from zhenxun.configs.utils import PluginExtraData

__plugin_meta__ = PluginMetadata(
name="鸡汤",
description="喏,亲手为你煮的鸡汤",
usage="""
不喝点什么感觉有点不舒服
指令:
鸡汤
""".strip(),
extra=PluginExtraData(
author="HibiKier",
version="0.1",
).dict(),
)
18 changes: 18 additions & 0 deletions tests/content/plugin_store/search_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from nonebot.plugin import PluginMetadata

from zhenxun.configs.utils import PluginExtraData

__plugin_meta__ = PluginMetadata(
name="识图",
description="以图搜图,看破本源",
usage="""
识别图片 [二次元图片]
指令:
识图 [图片]
""".strip(),
extra=PluginExtraData(
author="HibiKier",
version="0.1",
menu_type="一些工具",
).dict(),
)
Loading

0 comments on commit 9eca6a9

Please sign in to comment.