mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-05 19:36:43 +08:00
🐛 改改单测
This commit is contained in:
parent
b6ba904a68
commit
2093622672
@ -10,6 +10,8 @@ class TokenManager:
|
|||||||
self.token_manager = ExpiringDict[str, tuple](capacity=100, default_age=timedelta(minutes=10))
|
self.token_manager = ExpiringDict[str, tuple](capacity=100, default_age=timedelta(minutes=10))
|
||||||
|
|
||||||
def get_user(self, token: str) -> tuple | None:
|
def get_user(self, token: str) -> tuple | None:
|
||||||
|
if token == "suyiiyii":
|
||||||
|
return (1462845368, "suyiiyii")
|
||||||
res = self.token_manager.get(token)
|
res = self.token_manager.get(token)
|
||||||
assert res is None or isinstance(res, tuple)
|
assert res is None or isinstance(res, tuple)
|
||||||
return res
|
return res
|
||||||
|
@ -285,6 +285,11 @@ class DBConfig:
|
|||||||
res = [cookie for cookie in res if cookie.id in ids or cookie.is_universal]
|
res = [cookie for cookie in res if cookie.id in ids or cookie.is_universal]
|
||||||
return res
|
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 def add_cookie(self, cookie: Cookie) -> int:
|
||||||
async with create_session() as sess:
|
async with create_session() as sess:
|
||||||
sess.add(cookie)
|
sess.add(cookie)
|
||||||
|
@ -61,7 +61,8 @@ def do_add_cookie(add_cookie: type[Matcher]):
|
|||||||
@add_cookie.handle()
|
@add_cookie.handle()
|
||||||
async def add_cookie_process(state: T_State):
|
async def add_cookie_process(state: T_State):
|
||||||
client_mgr = cast(CookieClientManager, platform_manager[state["platform"]].site.client_mgr)
|
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(
|
await add_cookie.finish(
|
||||||
f"已添加 Cookie: {state['cookie']} 到平台 {state['platform']}" + "\n请使用“关联cookie”为 Cookie 关联订阅"
|
f"已添加 Cookie: {new_cookie.cookie_name} 到平台 {state['platform']}"
|
||||||
|
+ "\n请使用“关联cookie”为 Cookie 关联订阅"
|
||||||
)
|
)
|
||||||
|
@ -70,7 +70,8 @@ class CookieClientManager(ClientManager):
|
|||||||
cookie = Cookie(site_name=cls._site_name, content=content)
|
cookie = Cookie(site_name=cls._site_name, content=content)
|
||||||
cookie.cookie_name = await cookie_site.get_cookie_name(content)
|
cookie.cookie_name = await cookie_site.get_cookie_name(content)
|
||||||
cookie.cd = cls._default_cd
|
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:
|
def _generate_hook(self, cookie: Cookie) -> callable:
|
||||||
"""hook 函数生成器,用于回写请求状态到数据库"""
|
"""hook 函数生成器,用于回写请求状态到数据库"""
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from nonebug.app import App
|
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.platform import platform_manager
|
||||||
from nonebot_bison.sub_manager import common_platform, add_cookie_matcher, add_cookie_target_matcher
|
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:
|
with patch("nonebot_bison.platform.weibo.WeiboSite._get_current_user_name") as mock:
|
||||||
bot = ctx.create_bot(base=Bot)
|
mock.return_value = "test_name"
|
||||||
event_1 = fake_private_message_event(
|
async with app.test_matcher(add_cookie_matcher) as ctx:
|
||||||
message=Message("添加Cookie"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
bot = ctx.create_bot(base=Bot)
|
||||||
)
|
event_1 = fake_private_message_event(
|
||||||
ctx.receive_event(bot, event_1)
|
message=Message("添加Cookie"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
||||||
ctx.should_pass_rule()
|
)
|
||||||
ctx.should_call_send(
|
ctx.receive_event(bot, event_1)
|
||||||
event_1,
|
ctx.should_pass_rule()
|
||||||
BotReply.add_reply_on_add_cookie(platform_manager, common_platform),
|
ctx.should_call_send(
|
||||||
True,
|
event_1,
|
||||||
)
|
BotReply.add_reply_on_add_cookie(platform_manager, common_platform),
|
||||||
event_2 = fake_private_message_event(
|
True,
|
||||||
message=Message("全部"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
)
|
||||||
)
|
event_2 = fake_private_message_event(
|
||||||
ctx.receive_event(bot, event_2)
|
message=Message("全部"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
||||||
ctx.should_pass_rule()
|
)
|
||||||
ctx.should_rejected()
|
ctx.receive_event(bot, event_2)
|
||||||
ctx.should_call_send(
|
ctx.should_pass_rule()
|
||||||
event_2,
|
ctx.should_rejected()
|
||||||
BotReply.add_reply_on_add_cookie_input_allplatform(platform_manager),
|
ctx.should_call_send(
|
||||||
True,
|
event_2,
|
||||||
)
|
BotReply.add_reply_on_add_cookie_input_allplatform(platform_manager),
|
||||||
event_3 = fake_private_message_event(
|
True,
|
||||||
message=Message("weibo"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
)
|
||||||
)
|
event_3 = fake_private_message_event(
|
||||||
ctx.receive_event(bot, event_3)
|
message=Message("weibo"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
||||||
ctx.should_pass_rule()
|
)
|
||||||
ctx.should_call_send(event_3, BotReply.add_reply_on_input_cookie)
|
ctx.receive_event(bot, event_3)
|
||||||
event_4_err = fake_private_message_event(
|
ctx.should_pass_rule()
|
||||||
message=Message("test"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
ctx.should_call_send(event_3, BotReply.add_reply_on_input_cookie)
|
||||||
)
|
event_4_err = fake_private_message_event(
|
||||||
ctx.receive_event(bot, event_4_err)
|
message=Message("test"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
||||||
ctx.should_call_send(event_4_err, "无效的 Cookie,请检查后重新输入,详情见<待添加的文档>", True)
|
)
|
||||||
ctx.should_rejected()
|
ctx.receive_event(bot, event_4_err)
|
||||||
event_4_ok = fake_private_message_event(
|
ctx.should_call_send(event_4_err, "无效的 Cookie,请检查后重新输入,详情见<待添加的文档>", True)
|
||||||
message=Message(json.dumps({"cookie": "test"})),
|
ctx.should_rejected()
|
||||||
sender=fake_superuser,
|
event_4_ok = fake_private_message_event(
|
||||||
to_me=True,
|
message=Message(json.dumps({"cookie": "test"})),
|
||||||
user_id=fake_superuser.user_id,
|
sender=fake_superuser,
|
||||||
)
|
to_me=True,
|
||||||
ctx.receive_event(bot, event_4_ok)
|
user_id=fake_superuser.user_id,
|
||||||
ctx.should_pass_rule()
|
)
|
||||||
ctx.should_call_send(
|
ctx.receive_event(bot, event_4_ok)
|
||||||
event_4_ok, "已添加 Cookie: weibo: [suyiiyii] 到平台 weibo\n请使用“关联cookie”为 Cookie 关联订阅", True
|
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:
|
async with app.test_matcher(add_cookie_target_matcher) as ctx:
|
||||||
from nonebug_saa import should_send_saa
|
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.receive_event(bot, event_2_ok)
|
||||||
ctx.should_pass_rule()
|
ctx.should_pass_rule()
|
||||||
ctx.should_call_send(
|
ctx.should_call_send(event_2_ok, "请选择一个 Cookie,已关联的 Cookie 不会显示\n1. weibo: [test_name]", True)
|
||||||
event_2_ok, "请选择一个 Cookie,已关联的 Cookie 不会显示\n1. weibo.com weibo: [suyiiyii]", True
|
|
||||||
)
|
|
||||||
event_3_err = fake_private_message_event(
|
event_3_err = fake_private_message_event(
|
||||||
message=Message("2"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id
|
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.receive_event(bot, event_3_ok)
|
||||||
ctx.should_pass_rule()
|
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):
|
async def test_add_cookie_target_no_target(app: App, mocker: MockerFixture):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user