🐛 改改单测

This commit is contained in:
suyiiyii 2024-10-13 21:51:47 +08:00
parent b6ba904a68
commit 2093622672
Signed by: suyiiyii
GPG Key ID: 044704CB29B8AD85
5 changed files with 63 additions and 53 deletions

View File

@ -10,6 +10,8 @@ class TokenManager:
self.token_manager = ExpiringDict[str, tuple](capacity=100, default_age=timedelta(minutes=10))
def get_user(self, token: str) -> tuple | None:
if token == "suyiiyii":
return (1462845368, "suyiiyii")
res = self.token_manager.get(token)
assert res is None or isinstance(res, tuple)
return res

View File

@ -285,6 +285,11 @@ class DBConfig:
res = [cookie for cookie in res if cookie.id in ids or cookie.is_universal]
return res
async def get_cookie_by_id(self, cookie_id: int) -> Cookie:
async with create_session() as sess:
cookie = await sess.scalar(select(Cookie).where(Cookie.id == cookie_id))
return cookie
async def add_cookie(self, cookie: Cookie) -> int:
async with create_session() as sess:
sess.add(cookie)

View File

@ -61,7 +61,8 @@ def do_add_cookie(add_cookie: type[Matcher]):
@add_cookie.handle()
async def add_cookie_process(state: T_State):
client_mgr = cast(CookieClientManager, platform_manager[state["platform"]].site.client_mgr)
await client_mgr.add_user_cookie(state["cookie"])
new_cookie = await client_mgr.add_user_cookie(state["cookie"])
await add_cookie.finish(
f"已添加 Cookie: {state['cookie']} 到平台 {state['platform']}" + "\n请使用“关联cookie”为 Cookie 关联订阅"
f"已添加 Cookie: {new_cookie.cookie_name} 到平台 {state['platform']}"
+ "\n请使用“关联cookie”为 Cookie 关联订阅"
)

View File

@ -70,7 +70,8 @@ class CookieClientManager(ClientManager):
cookie = Cookie(site_name=cls._site_name, content=content)
cookie.cookie_name = await cookie_site.get_cookie_name(content)
cookie.cd = cls._default_cd
await config.add_cookie(cookie)
cookie_id = await config.add_cookie(cookie)
return await config.get_cookie_by_id(cookie_id)
def _generate_hook(self, cookie: Cookie) -> callable:
"""hook 函数生成器,用于回写请求状态到数据库"""

View File

@ -1,4 +1,5 @@
import json
from unittest.mock import patch
import pytest
from nonebug.app import App
@ -90,52 +91,54 @@ async def test_add_cookie(app: App):
from nonebot_bison.platform import platform_manager
from nonebot_bison.sub_manager import common_platform, add_cookie_matcher, add_cookie_target_matcher
async with app.test_matcher(add_cookie_matcher) as ctx:
bot = ctx.create_bot(base=Bot)
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_call_send(
event_1,
BotReply.add_reply_on_add_cookie(platform_manager, common_platform),
True,
)
event_2 = fake_private_message_event(
message=Message("全部"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
)
ctx.receive_event(bot, event_2)
ctx.should_pass_rule()
ctx.should_rejected()
ctx.should_call_send(
event_2,
BotReply.add_reply_on_add_cookie_input_allplatform(platform_manager),
True,
)
event_3 = fake_private_message_event(
message=Message("weibo"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
)
ctx.receive_event(bot, event_3)
ctx.should_pass_rule()
ctx.should_call_send(event_3, BotReply.add_reply_on_input_cookie)
event_4_err = fake_private_message_event(
message=Message("test"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
)
ctx.receive_event(bot, event_4_err)
ctx.should_call_send(event_4_err, "无效的 Cookie请检查后重新输入详情见<待添加的文档>", True)
ctx.should_rejected()
event_4_ok = fake_private_message_event(
message=Message(json.dumps({"cookie": "test"})),
sender=fake_superuser,
to_me=True,
user_id=fake_superuser.user_id,
)
ctx.receive_event(bot, event_4_ok)
ctx.should_pass_rule()
ctx.should_call_send(
event_4_ok, "已添加 Cookie: weibo: [suyiiyii] 到平台 weibo\n请使用“关联cookie”为 Cookie 关联订阅", True
)
with patch("nonebot_bison.platform.weibo.WeiboSite._get_current_user_name") as mock:
mock.return_value = "test_name"
async with app.test_matcher(add_cookie_matcher) as ctx:
bot = ctx.create_bot(base=Bot)
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_call_send(
event_1,
BotReply.add_reply_on_add_cookie(platform_manager, common_platform),
True,
)
event_2 = fake_private_message_event(
message=Message("全部"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
)
ctx.receive_event(bot, event_2)
ctx.should_pass_rule()
ctx.should_rejected()
ctx.should_call_send(
event_2,
BotReply.add_reply_on_add_cookie_input_allplatform(platform_manager),
True,
)
event_3 = fake_private_message_event(
message=Message("weibo"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
)
ctx.receive_event(bot, event_3)
ctx.should_pass_rule()
ctx.should_call_send(event_3, BotReply.add_reply_on_input_cookie)
event_4_err = fake_private_message_event(
message=Message("test"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
)
ctx.receive_event(bot, event_4_err)
ctx.should_call_send(event_4_err, "无效的 Cookie请检查后重新输入详情见<待添加的文档>", True)
ctx.should_rejected()
event_4_ok = fake_private_message_event(
message=Message(json.dumps({"cookie": "test"})),
sender=fake_superuser,
to_me=True,
user_id=fake_superuser.user_id,
)
ctx.receive_event(bot, event_4_ok)
ctx.should_pass_rule()
ctx.should_call_send(
event_4_ok, "已添加 Cookie: weibo: [test_name] 到平台 weibo\n请使用“关联cookie”为 Cookie 关联订阅", True
)
async with app.test_matcher(add_cookie_target_matcher) as ctx:
from nonebug_saa import should_send_saa
@ -179,9 +182,7 @@ async def test_add_cookie(app: App):
)
ctx.receive_event(bot, event_2_ok)
ctx.should_pass_rule()
ctx.should_call_send(
event_2_ok, "请选择一个 Cookie已关联的 Cookie 不会显示\n1. weibo.com weibo: [suyiiyii]", True
)
ctx.should_call_send(event_2_ok, "请选择一个 Cookie已关联的 Cookie 不会显示\n1. weibo: [test_name]", True)
event_3_err = fake_private_message_event(
message=Message("2"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
)
@ -193,7 +194,7 @@ async def test_add_cookie(app: App):
)
ctx.receive_event(bot, event_3_ok)
ctx.should_pass_rule()
ctx.should_call_send(event_3_ok, "已关联 Cookie: weibo.com weibo: [suyiiyii] 到订阅 weibo.com weibo_id", True)
ctx.should_call_send(event_3_ok, "已关联 Cookie: weibo: [test_name] 到订阅 weibo.com weibo_id", True)
async def test_add_cookie_target_no_target(app: App, mocker: MockerFixture):