mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-04 02:26:11 +08:00
✅ 初步实现删除cookie的单元测试
This commit is contained in:
parent
dd802a9c17
commit
c784417ecc
@ -16,7 +16,7 @@ def do_del_cookie(del_cookie: type[Matcher]):
|
|||||||
async def send_list(state: T_State):
|
async def send_list(state: T_State):
|
||||||
cookies = await config.get_cookie(is_anonymous=False)
|
cookies = await config.get_cookie(is_anonymous=False)
|
||||||
if not cookies:
|
if not cookies:
|
||||||
await del_cookie.finish("暂无已添加 Cookie\n请使用“添加cookie”命令添加")
|
await del_cookie.finish("暂无已添加的 Cookie\n请使用“添加cookie”命令添加")
|
||||||
res = "已添加的 Cookie 为:\n"
|
res = "已添加的 Cookie 为:\n"
|
||||||
state["cookie_table"] = {}
|
state["cookie_table"] = {}
|
||||||
for index, cookie in enumerate(cookies, 1):
|
for index, cookie in enumerate(cookies, 1):
|
||||||
@ -40,6 +40,8 @@ def do_del_cookie(del_cookie: type[Matcher]):
|
|||||||
if cookie.targets:
|
if cookie.targets:
|
||||||
await del_cookie.reject("只能删除未关联的 Cookie,请使用“取消关联cookie”命令取消关联")
|
await del_cookie.reject("只能删除未关联的 Cookie,请使用“取消关联cookie”命令取消关联")
|
||||||
await config.delete_cookie_by_id(cookie.id)
|
await config.delete_cookie_by_id(cookie.id)
|
||||||
|
except KeyError:
|
||||||
|
await del_cookie.reject("序号错误")
|
||||||
except Exception:
|
except Exception:
|
||||||
await del_cookie.reject("删除错误")
|
await del_cookie.reject("删除错误")
|
||||||
else:
|
else:
|
||||||
|
133
tests/sub_manager/test_delete_cookie.py
Normal file
133
tests/sub_manager/test_delete_cookie.py
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
from nonebug.app import App
|
||||||
|
|
||||||
|
from ..utils import fake_superuser, fake_private_message_event
|
||||||
|
|
||||||
|
|
||||||
|
async def test_del_cookie_err(app: App):
|
||||||
|
from nonebug_saa import should_send_saa
|
||||||
|
from nonebot.adapters.onebot.v11.bot import Bot
|
||||||
|
from nonebot.adapters.onebot.v11.message import Message
|
||||||
|
from nonebot_plugin_saa import TargetQQGroup, MessageFactory
|
||||||
|
|
||||||
|
from nonebot_bison.config import config
|
||||||
|
from nonebot_bison.config.db_model import Cookie
|
||||||
|
from nonebot_bison.types import Target as T_Target
|
||||||
|
from nonebot_bison.sub_manager import del_cookie_matcher
|
||||||
|
|
||||||
|
async with app.test_matcher(del_cookie_matcher) as ctx:
|
||||||
|
bot = ctx.create_bot(base=Bot)
|
||||||
|
event = fake_private_message_event(
|
||||||
|
message=Message("删除cookie"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
||||||
|
)
|
||||||
|
ctx.receive_event(bot, event)
|
||||||
|
ctx.should_pass_rule()
|
||||||
|
ctx.should_pass_permission()
|
||||||
|
ctx.should_call_send(event, "暂无已添加的 Cookie\n请使用“添加cookie”命令添加", True)
|
||||||
|
|
||||||
|
async with app.test_matcher(del_cookie_matcher) as ctx:
|
||||||
|
bot = ctx.create_bot(base=Bot)
|
||||||
|
target = T_Target("weibo_id")
|
||||||
|
platform_name = "weibo"
|
||||||
|
await config.add_subscribe(
|
||||||
|
TargetQQGroup(group_id=123),
|
||||||
|
target=target,
|
||||||
|
target_name="weibo_name",
|
||||||
|
platform_name=platform_name,
|
||||||
|
cats=[],
|
||||||
|
tags=[],
|
||||||
|
)
|
||||||
|
await config.add_cookie(Cookie(content=json.dumps({"cookie": "test"}), site_name="weibo.com"))
|
||||||
|
cookies = await config.get_cookie(is_anonymous=False)
|
||||||
|
await config.add_cookie_target(target, platform_name, cookies[0].id)
|
||||||
|
|
||||||
|
event_1 = fake_private_message_event(
|
||||||
|
message=Message("删除cookie"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
||||||
|
)
|
||||||
|
ctx.receive_event(bot, event_1)
|
||||||
|
ctx.should_pass_rule()
|
||||||
|
ctx.should_pass_permission()
|
||||||
|
should_send_saa(
|
||||||
|
ctx,
|
||||||
|
MessageFactory(
|
||||||
|
'已添加的 Cookie 为:\n1 weibo.com weibo.com [{"cookie":] '
|
||||||
|
"1个关联\n请输入要删除的 Cookie 的序号\n输入'取消'中止"
|
||||||
|
),
|
||||||
|
bot,
|
||||||
|
event=event_1,
|
||||||
|
)
|
||||||
|
event_2_err = fake_private_message_event(
|
||||||
|
message=Message("2"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
||||||
|
)
|
||||||
|
ctx.receive_event(bot, event_2_err)
|
||||||
|
ctx.should_call_send(event_2_err, "序号错误", True)
|
||||||
|
ctx.should_rejected()
|
||||||
|
|
||||||
|
event_2 = fake_private_message_event(
|
||||||
|
message=Message("1"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
||||||
|
)
|
||||||
|
ctx.receive_event(bot, event_2)
|
||||||
|
ctx.should_pass_rule()
|
||||||
|
ctx.should_call_send(event_2, "只能删除未关联的 Cookie,请使用“取消关联cookie”命令取消关联", True)
|
||||||
|
ctx.should_call_send(event_2, "删除错误", True)
|
||||||
|
ctx.should_rejected()
|
||||||
|
|
||||||
|
|
||||||
|
async def test_del_cookie(app: App):
|
||||||
|
from nonebug_saa import should_send_saa
|
||||||
|
from nonebot.adapters.onebot.v11.bot import Bot
|
||||||
|
from nonebot.adapters.onebot.v11.message import Message
|
||||||
|
from nonebot_plugin_saa import TargetQQGroup, MessageFactory
|
||||||
|
|
||||||
|
from nonebot_bison.config import config
|
||||||
|
from nonebot_bison.config.db_model import Cookie
|
||||||
|
from nonebot_bison.types import Target as T_Target
|
||||||
|
from nonebot_bison.sub_manager import del_cookie_matcher
|
||||||
|
|
||||||
|
async with app.test_matcher(del_cookie_matcher) as ctx:
|
||||||
|
bot = ctx.create_bot(base=Bot)
|
||||||
|
event = fake_private_message_event(
|
||||||
|
message=Message("删除cookie"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
||||||
|
)
|
||||||
|
ctx.receive_event(bot, event)
|
||||||
|
ctx.should_pass_rule()
|
||||||
|
ctx.should_pass_permission()
|
||||||
|
ctx.should_call_send(event, "暂无已添加的 Cookie\n请使用“添加cookie”命令添加", True)
|
||||||
|
|
||||||
|
async with app.test_matcher(del_cookie_matcher) as ctx:
|
||||||
|
bot = ctx.create_bot(base=Bot)
|
||||||
|
target = T_Target("weibo_id")
|
||||||
|
platform_name = "weibo"
|
||||||
|
await config.add_subscribe(
|
||||||
|
TargetQQGroup(group_id=123),
|
||||||
|
target=target,
|
||||||
|
target_name="weibo_name",
|
||||||
|
platform_name=platform_name,
|
||||||
|
cats=[],
|
||||||
|
tags=[],
|
||||||
|
)
|
||||||
|
await config.add_cookie(Cookie(content=json.dumps({"cookie": "test"}), site_name="weibo.com"))
|
||||||
|
|
||||||
|
event_1 = fake_private_message_event(
|
||||||
|
message=Message("删除cookie"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
||||||
|
)
|
||||||
|
ctx.receive_event(bot, event_1)
|
||||||
|
ctx.should_pass_rule()
|
||||||
|
ctx.should_pass_permission()
|
||||||
|
should_send_saa(
|
||||||
|
ctx,
|
||||||
|
MessageFactory(
|
||||||
|
'已添加的 Cookie 为:\n1 weibo.com weibo.com [{"cookie":]'
|
||||||
|
" 0个关联\n请输入要删除的 Cookie 的序号\n输入'取消'中止"
|
||||||
|
),
|
||||||
|
bot,
|
||||||
|
event=event_1,
|
||||||
|
)
|
||||||
|
event_2 = fake_private_message_event(
|
||||||
|
message=Message("1"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
||||||
|
)
|
||||||
|
ctx.receive_event(bot, event_2)
|
||||||
|
ctx.should_pass_rule()
|
||||||
|
ctx.should_pass_permission()
|
||||||
|
ctx.should_call_send(event_2, "删除成功", True)
|
Loading…
x
Reference in New Issue
Block a user