mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-09 18:27:56 +08:00
update
This commit is contained in:
@@ -12,6 +12,7 @@ async def app(nonebug_init: None, tmp_path: Path, monkeypatch: pytest.MonkeyPatc
|
||||
|
||||
config = nonebot.get_driver().config
|
||||
config.bison_config_path = str(tmp_path)
|
||||
config.command_start = {""}
|
||||
return App(monkeypatch)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{"ok":1,"data":{"isVideoCoverStyle":1,"isStarStyle":0,"userInfo":{"id":6279793937,"screen_name":"\u660e\u65e5\u65b9\u821fArknights","profile_image_url":"https:\/\/tvax4.sinaimg.cn\/crop.0.0.756.756.180\/006QZngZly8gdj05mufr9j30l00l0dq4.jpg?KID=imgbed,tva&Expires=1645010805&ssig=POD2VKV4LB","profile_url":"https:\/\/m.weibo.cn\/u\/6279793937?uid=6279793937&luicode=10000011&lfid=1005056279793937","statuses_count":1553,"verified":true,"verified_type":2,"verified_type_ext":50,"verified_reason":"\u4e0a\u6d77\u9e70\u89d2\u7f51\u7edc\u79d1\u6280\u6709\u9650\u516c\u53f8","close_blue_v":false,"description":"\u300a\u660e\u65e5\u65b9\u821f\u300b\u5b98\u65b9\u5fae\u535a\uff0c\u6b22\u8fce\u54a8\u8be2\u5ba2\u670dQQ\u670d\u52a1\u53f7\uff1a800830064","gender":"f","mbtype":12,"urank":4,"mbrank":7,"follow_me":false,"following":false,"follow_count":34,"followers_count":"145.5\u4e07","followers_count_str":"145.5\u4e07","cover_image_phone":"https:\/\/wx4.sinaimg.cn\/crop.0.0.640.640.640\/006QZngZly1gq8sa16csgj30u00u0akt.jpg","avatar_hd":"https:\/\/wx4.sinaimg.cn\/orj480\/006QZngZly8gdj05mufr9j30l00l0dq4.jpg","like":false,"like_me":false,"toolbar_menus":[{"type":"link","name":"\u804a\u5929","pic":"http:\/\/h5.sinaimg.cn\/upload\/2015\/06\/12\/2\/toolbar_icon_discuss_default.png","params":{"scheme":"sinaweibo:\/\/messagelist?uid=6279793937&nick=\u660e\u65e5\u65b9\u821fArknights"},"scheme":"https:\/\/passport.weibo.cn\/signin\/welcome?entry=mweibo&r=https%3A%2F%2Fm.weibo.cn%2Fapi%2Fcontainer%2FgetIndex%3Fcontainerid%3D1005056279793937"},{"type":"profile_follow","name":"\u5173\u6ce8","pic":"","params":{"uid":6279793937,"extparams":{"followcardid":"0001980001_6279793937_b2f217e4"}},"userInfo":{"id":6279793937,"idstr":"6279793937","screen_name":"\u660e\u65e5\u65b9\u821fArknights","profile_image_url":"https:\/\/tvax4.sinaimg.cn\/crop.0.0.756.756.50\/006QZngZly8gdj05mufr9j30l00l0dq4.jpg?KID=imgbed,tva&Expires=1645010805&ssig=pKJtZwv%2FHm","following":false,"verified":true,"verified_type":2,"remark":"","avatar_large":"https:\/\/tvax4.sinaimg.cn\/crop.0.0.756.756.180\/006QZngZly8gdj05mufr9j30l00l0dq4.jpg?KID=imgbed,tva&Expires=1645010805&ssig=POD2VKV4LB","avatar_hd":"https:\/\/tvax4.sinaimg.cn\/crop.0.0.756.756.1024\/006QZngZly8gdj05mufr9j30l00l0dq4.jpg?KID=imgbed,tva&Expires=1645010805&ssig=RBGOJz6PB6","verified_type_ext":50,"follow_me":false,"mbtype":12,"mbrank":6,"level":2,"type":1,"story_read_state":-1,"allow_msg":1,"friendships_relation":0,"close_friends_type":0,"special_follow":false}}]},"fans_scheme":"https:\/\/m.weibo.cn\/c\/attention\/visit?showmenu=0&sign=6279793937&source=MQ%3D%3D&role=NA%3D%3D&luicode=10000011&lfid=1005056279793937","follow_scheme":"https:\/\/m.weibo.cn\/c\/attention\/visit?showmenu=0&sign=6279793937&source=MA%3D%3D&role=NA%3D%3D&luicode=10000011&lfid=1005056279793937","tabsInfo":{"selectedTab":1,"tabs":[{"id":1,"tabKey":"profile","must_show":1,"hidden":0,"title":"\u7cbe\u9009","tab_type":"profile","containerid":"2302836279793937"},{"id":2,"tabKey":"weibo","must_show":1,"hidden":0,"title":"\u5fae\u535a","tab_type":"weibo","containerid":"1076036279793937","apipath":"\/profile\/statuses","tab_icon":"https:\/\/h5.sinaimg.cn\/upload\/1059\/799\/2021\/04\/01\/weibotab.png","tab_icon_dark":"https:\/\/h5.sinaimg.cn\/upload\/1059\/799\/2021\/04\/07\/weibotab_dark.png","url":"\/index\/my"},{"id":4,"tabKey":"original_video","must_show":0,"hidden":0,"title":"\u89c6\u9891","tab_type":"video","containerid":"2315676279793937"},{"id":10,"tabKey":"album","must_show":0,"hidden":0,"title":"\u76f8\u518c","tab_type":"album","containerid":"1078036279793937"}]},"profile_ext":"touid:6279793937","scheme":"sinaweibo:\/\/userinfo?uid=6279793937&_T_WM=38226655008&v_p=42&uid=6279793937&luicode=10000011&lfid=1005056279793937","showAppTips":0}}
|
||||
@@ -0,0 +1 @@
|
||||
{"ok":0,"msg":"\u8fd9\u91cc\u8fd8\u6ca1\u6709\u5185\u5bb9","data":{"cards":[]}}
|
||||
@@ -0,0 +1,52 @@
|
||||
import typing
|
||||
|
||||
import pytest
|
||||
from nonebug.app import App
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
import sys
|
||||
|
||||
sys.path.append("./src/plugins")
|
||||
import nonebot_bison
|
||||
from nonebot_bison.config import Config
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def config(app: App):
|
||||
from nonebot_bison import config
|
||||
|
||||
config.start_up()
|
||||
return config.Config()
|
||||
|
||||
|
||||
def test_create_and_get(config: "Config", app: App):
|
||||
from nonebot_bison import types
|
||||
from nonebot_bison.types import Target
|
||||
|
||||
config.add_subscribe(
|
||||
user="123",
|
||||
user_type="group",
|
||||
target="weibo_id",
|
||||
target_name="weibo_name",
|
||||
target_type="weibo",
|
||||
cats=[],
|
||||
tags=[],
|
||||
)
|
||||
confs = config.list_subscribe("123", "group")
|
||||
assert len(confs) == 1
|
||||
assert config.target_user_cache["weibo"][Target("weibo_id")] == [
|
||||
types.User("123", "group")
|
||||
]
|
||||
assert confs[0]["cats"] == []
|
||||
config.update_subscribe(
|
||||
user="123",
|
||||
user_type="group",
|
||||
target="weibo_id",
|
||||
target_name="weibo_name",
|
||||
target_type="weibo",
|
||||
cats=["1"],
|
||||
tags=[],
|
||||
)
|
||||
confs = config.list_subscribe("123", "group")
|
||||
assert len(confs) == 1
|
||||
assert confs[0]["cats"] == ["1"]
|
||||
@@ -1,52 +1,103 @@
|
||||
import typing
|
||||
|
||||
import pytest
|
||||
import respx
|
||||
from httpx import Response
|
||||
from nonebot.adapters.onebot.v11.event import Sender
|
||||
from nonebot.adapters.onebot.v11.message import MessageSegment
|
||||
from nonebug.app import App
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
import sys
|
||||
from .platforms.utils import get_json
|
||||
from .utils import fake_admin_user, fake_group_message_event
|
||||
|
||||
sys.path.append("./src/plugins")
|
||||
import nonebot_bison
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_add_with_target(app: App):
|
||||
from nonebot.adapters.onebot.v11.message import Message
|
||||
from nonebot_bison.config import Config
|
||||
from nonebot_bison.config_manager import add_sub_matcher, common_platform
|
||||
from nonebot_bison.platform import platform_manager
|
||||
|
||||
config = Config()
|
||||
config.user_target.truncate()
|
||||
|
||||
@pytest.fixture
|
||||
def config(app: App):
|
||||
from nonebot_bison import config
|
||||
|
||||
config.start_up()
|
||||
return config.Config()
|
||||
|
||||
|
||||
def test_create_and_get(config: "Config", app: App):
|
||||
from nonebot_bison import types
|
||||
from nonebot_bison.types import Target
|
||||
|
||||
config.add_subscribe(
|
||||
user="123",
|
||||
user_type="group",
|
||||
target="weibo_id",
|
||||
target_name="weibo_name",
|
||||
target_type="weibo",
|
||||
cats=[],
|
||||
tags=[],
|
||||
ak_list_router = respx.get(
|
||||
"https://m.weibo.cn/api/container/getIndex?containerid=1005056279793937"
|
||||
)
|
||||
confs = config.list_subscribe("123", "group")
|
||||
assert len(confs) == 1
|
||||
assert config.target_user_cache["weibo"][Target("weibo_id")] == [
|
||||
types.User("123", "group")
|
||||
]
|
||||
assert confs[0]["cats"] == []
|
||||
config.update_subscribe(
|
||||
user="123",
|
||||
user_type="group",
|
||||
target="weibo_id",
|
||||
target_name="weibo_name",
|
||||
target_type="weibo",
|
||||
cats=["1"],
|
||||
tags=[],
|
||||
ak_list_router.mock(
|
||||
return_value=Response(200, json=get_json("weibo_ak_profile.json"))
|
||||
)
|
||||
confs = config.list_subscribe("123", "group")
|
||||
assert len(confs) == 1
|
||||
assert confs[0]["cats"] == ["1"]
|
||||
ak_list_bad_router = respx.get(
|
||||
"https://m.weibo.cn/api/container/getIndex?containerid=100505000"
|
||||
)
|
||||
ak_list_bad_router.mock(
|
||||
return_value=Response(200, json=get_json("weibo_err_profile.json"))
|
||||
)
|
||||
|
||||
async with app.test_matcher(add_sub_matcher) as ctx:
|
||||
bot = ctx.create_bot()
|
||||
event_1 = fake_group_message_event(
|
||||
message=Message("添加订阅"),
|
||||
sender=Sender(card="", nickname="test", role="admin"),
|
||||
to_me=True,
|
||||
)
|
||||
ctx.receive_event(bot, event_1)
|
||||
ctx.should_pass_rule()
|
||||
ctx.should_call_send(
|
||||
event_1,
|
||||
Message(
|
||||
"请输入想要订阅的平台,目前支持,请输入冒号左边的名称:\n"
|
||||
+ "".join(
|
||||
[
|
||||
"{}:{}\n".format(
|
||||
platform_name, platform_manager[platform_name].name
|
||||
)
|
||||
for platform_name in common_platform
|
||||
]
|
||||
)
|
||||
+ "要查看全部平台请输入:“全部”"
|
||||
),
|
||||
True,
|
||||
)
|
||||
event_2 = fake_group_message_event(
|
||||
message=Message("全部"), sender=Sender(card="", nickname="test", role="admin")
|
||||
)
|
||||
ctx.receive_event(bot, event_2)
|
||||
ctx.should_rejected()
|
||||
ctx.should_call_send(
|
||||
event_2,
|
||||
(
|
||||
"全部平台\n"
|
||||
+ "\n".join(
|
||||
[
|
||||
"{}:{}".format(platform_name, platform.name)
|
||||
for platform_name, platform in platform_manager.items()
|
||||
]
|
||||
)
|
||||
),
|
||||
True,
|
||||
)
|
||||
event_3 = fake_group_message_event(
|
||||
message=Message("weibo"), sender=fake_admin_user
|
||||
)
|
||||
ctx.receive_event(bot, event_3)
|
||||
ctx.should_call_send(
|
||||
event_3,
|
||||
Message(
|
||||
"请输入订阅用户的id,详情查阅https://nonebot-bison.vercel.app/usage/#%E6%89%80%E6%94%AF%E6%8C%81%E5%B9%B3%E5%8F%B0%E7%9A%84uid"
|
||||
),
|
||||
True,
|
||||
)
|
||||
event_4_err = fake_group_message_event(
|
||||
message=Message("000"), sender=fake_admin_user
|
||||
)
|
||||
import ipdb
|
||||
|
||||
ipdb.set_trace()
|
||||
ctx.receive_event(bot, event_4_err)
|
||||
ctx.should_call_send(event_4_err, "id输入错误", True)
|
||||
ctx.should_rejected()
|
||||
event_4_ok = fake_group_message_event(
|
||||
message=Message("6279793937"), sender=fake_admin_user
|
||||
)
|
||||
ctx.receive_event(bot, event_4_ok)
|
||||
ctx.should_call_send(event_4_ok, "id输入错误", True)
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from typing_extensions import Literal
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent, PrivateMessageEvent
|
||||
|
||||
|
||||
def fake_group_message_event(**field) -> "GroupMessageEvent":
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent, Message
|
||||
from nonebot.adapters.onebot.v11.event import Sender
|
||||
from pydantic import create_model
|
||||
|
||||
_Fake = create_model("_Fake", __base__=GroupMessageEvent)
|
||||
|
||||
class FakeEvent(_Fake):
|
||||
time: int = 1000000
|
||||
self_id: int = 1
|
||||
post_type: Literal["message"] = "message"
|
||||
sub_type: str = "normal"
|
||||
user_id: int = 10
|
||||
message_type: Literal["group"] = "group"
|
||||
group_id: int = 10000
|
||||
message_id: int = 1
|
||||
message: Message = Message("test")
|
||||
raw_message: str = "test"
|
||||
font: int = 0
|
||||
sender: Sender = Sender(
|
||||
card="",
|
||||
nickname="test",
|
||||
role="member",
|
||||
)
|
||||
to_me: bool = False
|
||||
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
return FakeEvent(**field)
|
||||
|
||||
|
||||
def fake_private_message_event(**field) -> "PrivateMessageEvent":
|
||||
from nonebot.adapters.onebot.v11 import Message, PrivateMessageEvent
|
||||
from nonebot.adapters.onebot.v11.event import Sender
|
||||
from pydantic import create_model
|
||||
|
||||
_Fake = create_model("_Fake", __base__=PrivateMessageEvent)
|
||||
|
||||
class FakeEvent(_Fake):
|
||||
time: int = 1000000
|
||||
self_id: int = 1
|
||||
post_type: Literal["message"] = "message"
|
||||
sub_type: str = "friend"
|
||||
user_id: int = 10
|
||||
message_type: Literal["private"] = "private"
|
||||
message_id: int = 1
|
||||
message: Message = Message("test")
|
||||
raw_message: str = "test"
|
||||
font: int = 0
|
||||
sender: Sender = Sender(nickname="test")
|
||||
to_me: bool = False
|
||||
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
return FakeEvent(**field)
|
||||
|
||||
|
||||
from nonebot.adapters.onebot.v11.event import Sender
|
||||
|
||||
fake_admin_user = Sender(nickname="test", role="admin")
|
||||
Reference in New Issue
Block a user