Exemple #1
0
async def test_price_default(app: App, mocker: MockerFixture):
    """测试查价,默认值"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.ff14 import price_cmd

    get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get)

    async with app.test_matcher(price_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/查价 默认值"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "当前设置的默认值为:猫小胖", True)
        ctx.should_finished()

    async with app.test_matcher(price_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/查价 萨维奈舞裙"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(
            event,
            "萨维奈舞裙 在市场的价格是:\n233100*1  服务器: 静语庄园\n233193*1 HQ 服务器: 静语庄园\n233330*1  服务器: 静语庄园\n233334*1  服务器: 静语庄园\n239166*1 HQ 服务器: 紫水栈桥\n239166*1 HQ 服务器: 紫水栈桥\n数据更新时间: 2021年12月29日 05时00分",
            True,
        )
        ctx.should_finished()

    async with app.test_matcher(price_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/查价 默认值 静语庄园"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "查询区域默认值设置成功!", True)
        ctx.should_finished()

    async with app.test_matcher(price_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/查价 默认值"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "当前设置的默认值为:静语庄园", True)
        ctx.should_finished()

    get.assert_has_calls([
        mocker.call("https://cafemaker.wakingsands.com/search?string=萨维奈舞裙"),
        mocker.call("https://universalis.app/api/v2/猫小胖/10393?listings=6"),
    ])
Exemple #2
0
async def test_price_world_not_found(app: App, mocker: MockerFixture):
    """测试查价,服务器/大区不存在"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.ff14 import price_cmd

    get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get)

    async with app.test_matcher(price_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/查价 萨维奈舞裙 静语"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(
            event,
            "抱歉,没有找到 静语 的数据,请检查大区或服务器名称是否正确。",
            True,
        )
        ctx.should_finished()

    get.assert_has_calls([
        mocker.call("https://cafemaker.wakingsands.com/search?string=萨维奈舞裙"),
        mocker.call("https://universalis.app/api/v2/静语/10393?listings=6"),
    ])
Exemple #3
0
async def test_gete(
    app: App,
    mocker: MockerFixture,
):
    """测试藏宝选门,两个门的情况"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.ff14 import gate_cmd
    from src.plugins.ff14.gate import EXPR_GATE

    render_expression = mocker.patch("src.plugins.ff14.gate.render_expression")
    render_expression.return_value = Message("test")
    randint = mocker.patch("src.plugins.ff14.gate.randint")
    randint.return_value = 1

    async with app.test_matcher(gate_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/gate 2"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(event, Message("test"), "result", at_sender=True)
        ctx.should_finished()

    randint.assert_called_once_with(1, 2)
    render_expression.assert_called_once_with(EXPR_GATE, direction="左边")
Exemple #4
0
async def test_music_get_arg(
    app: App,
    mocker: MockerFixture,
):
    """测试启动问候已开启的情况"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message, MessageSegment
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.music import music_cmd

    get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get)

    async with app.test_matcher(music_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/music"))
        next_event = fake_group_message_event(
            message=Message(MessageSegment.image("12")))
        final_event = fake_group_message_event(message=Message("test"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "你想听哪首歌呢?", "result")
        ctx.should_rejected()
        ctx.receive_event(bot, next_event)
        ctx.should_call_send(next_event, "歌曲名不能为空呢,请重新输入!", "result")
        ctx.should_rejected()
        ctx.receive_event(bot, final_event)
        ctx.should_call_send(final_event,
                             MessageSegment.music("163", 1825190456), "result")
        ctx.should_finished()

    get.assert_called_once_with("http://netease:3000/search?keywords=test")
Exemple #5
0
async def test_price_item_not_found(app: App, mocker: MockerFixture):
    """测试查价,物品不存在"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.ff14 import price_cmd

    get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get)

    async with app.test_matcher(price_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/查价 未命名 猫小胖"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(
            event,
            "抱歉,没有找到 未命名,请检查物品名称是否正确。",
            True,
        )
        ctx.should_finished()

    get.assert_called_once_with(
        "https://cafemaker.wakingsands.com/search?string=未命名")
Exemple #6
0
async def test_nuannuan(
    app: App,
    mocker: MockerFixture,
):
    """测试时尚品鉴"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.ff14 import nuannuan_cmd

    with open(Path(__file__).parent / "nuannuan.json", "r", encoding="utf-8") as f:
        data = json.load(f)
    async_client = mocker.patch("httpx.AsyncClient.get")
    r = mocker.MagicMock()
    r.json = mocker.MagicMock(return_value=data)
    async_client.return_value = r

    async with app.test_matcher(nuannuan_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/时尚品鉴"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(
            event,
            "【FF14/时尚品鉴】第204期 满分攻略 12月24日 最终幻想14\n游玩C哩酱攻略站:www.youwanc.com\n-\n【12月24日 第204周时尚品鉴预告预测】\n主题:高贵的魔法师\n\n【身体防具】*提示内容:决斗者\n——*往期:116 92 57\n狂妄长衣 莽撞长衣 匿盗外套 鬼盗外套 红盗外套 瘟疫使者长衣 瘟疫医生长衣 \n60级副本地脉灵灯天狼星灯塔/草木庭园圣茉夏娜植物园\n\n【腿部防具】*提示内容:暗魔\n——*往期:169 139\n暗魔xx打底裤\n24人副本影之国获得\n\n【脚部防具】*提示内容:祭祀仪式\n——*往期:132 115\n真狮鹫革XX凉鞋/XX战靴\n70\nhttps://www.bilibili.com/video/BV1Pq4y1m7wK",
            "result",
        )
        ctx.should_finished()
Exemple #7
0
async def test_roll_get_arg(
    app: App,
    mocker: MockerFixture,
):
    """测试点数,获取参数"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.roll import roll_cmd

    randint = mocker.patch("src.plugins.roll.roll.randint")
    randint.return_value = 1

    async with app.test_matcher(roll_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/roll"))
        next_event = fake_group_message_event(message=Message("d100"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(
            event,
            "欢迎使用 NGA 风格 ROLL 点插件\n请问你想怎么 ROLL 点\n你可以输入 d100\n也可以输入 2d100+2d50",
            "result",
        )
        ctx.should_rejected()
        ctx.receive_event(bot, next_event)
        ctx.should_call_send(next_event, "d100=d100(1)=1", "result", at_sender=True)
        ctx.should_finished()

    randint.assert_called_once_with(1, 100)
Exemple #8
0
async def test_eorzean(
    app: App,
    mocker: MockerFixture,
):
    """测试艾欧泽亚天气"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.weather import weather_cmd

    mocked_time = mocker.patch("src.plugins.weather.eorzean_api.time")
    mocked_time.time.return_value = 1641619586

    async with app.test_matcher(weather_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/天气 利姆萨·罗敏萨"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(
            event,
            "利姆萨·罗敏萨\n当前天气:碧空\n还剩13分34秒切换到天气:阴云\n还剩36分54秒切换到天气:碧空\n还剩1时0分14秒切换到天气:碧空\n还剩1时23分34秒切换到天气:晴朗\n还剩1时46分54秒切换到天气:碧空\n还剩2时10分14秒切换到天气:碧空\n还剩2时33分34秒切换到天气:碧空\n还剩2时56分54秒切换到天气:晴朗\n还剩3时20分14秒切换到天气:晴朗",
            "result",
        )
        ctx.should_finished()

    mocked_time.time.assert_called_once()
Exemple #9
0
async def test_roll_complex(
    app: App,
    mocker: MockerFixture,
):
    """测试点数,复杂点的输入"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.roll import roll_cmd

    randint = mocker.patch("src.plugins.roll.roll.randint")
    randint.return_value = 1

    async with app.test_matcher(roll_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/roll d100+2d50"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(
            event,
            "d100+2d50=d100(1)+d50(1)+d50(1)=3",
            "result",
            at_sender=True,
        )
        ctx.should_finished()

    randint.assert_has_calls(
        [
            mocker.call(1, 100),
            mocker.call(1, 50),
            mocker.call(1, 50),
        ]
    )
Exemple #10
0
async def test_status(
    app: App,
    mocker: MockerFixture,
):
    """测试状态"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.repeat import recorder_obj, status_cmd

    recorder_obj.start_time = datetime(2020, 1, 1, 0, 0, 0)
    mocked_datetime = mocker.patch("src.plugins.repeat.status.datetime")
    mocked_datetime.now.return_value = datetime(2021, 2, 2, 1, 1, 1)
    mocked_server_status = mocker.patch("src.plugins.repeat.status.server_status")
    mocked_server_status.return_value = "test"

    async with app.test_matcher(status_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/status"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "已在线 1 年 1 月 1 天 1 小时 1 分钟 1 秒\ntest", "result")
        ctx.should_finished()

    mocked_datetime.now.assert_called_once()
    mocked_server_status.assert_called_once()
Exemple #11
0
async def test_calendar(
    app: App,
    mocker: MockerFixture,
):
    """测试日程"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.pcr import calendar_cmd

    get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get)
    mocked_datetime = mocker.patch("src.plugins.pcr.data.datetime")
    mocked_datetime.now.return_value = datetime(2022, 1, 8)
    mocked_datetime.strptime = datetime.strptime

    async with app.test_matcher(calendar_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/pcr.calendar"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(
            event,
            "一周日程:\n======20220108======\n⨠圣迹2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n⨠神殿2.0倍\n======20220109======\n⨠圣迹2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n⨠神殿2.0倍\n======20220110======\n⨠圣迹2.0倍\n⨠地下城2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n⨠神殿2.0倍\n======20220111======\n⨠圣迹2.0倍\n⨠地下城2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n⨠神殿2.0倍\n======20220112======\n⨠N图2.0倍\n⨠地下城2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n======20220113======\n⨠N图2.0倍\n⨠地下城2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n======20220114======\n⨠N图2.0倍\n⨠地下城2.0倍\n⨠探索2.0倍\n⨠活动:新春破晓之星大危机\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n\n更多日程:https://pcrbot.github.io/pcr-calendar/#cn",
            "result",
        )
        ctx.should_finished()

    get.assert_called_once_with(
        "https://pcrbot.github.io/calendar-updater-action/cn.json")
    assert mocked_datetime.now.call_count == 2
Exemple #12
0
async def test_rank(app: App):
    """测试历史"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.repeat import rank_cmd, recorder_obj

    recorder_obj._msg_number_list = {10000: {1: {10: 100}}}
    recorder_obj._repeat_list = {10000: {1: {10: 10}}}

    async with app.test_matcher(rank_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/rank"))

        ctx.receive_event(bot, event)
        ctx.should_call_api(
            "get_group_member_info",
            data={
                "group_id": 10000,
                "user_id": 10,
                "no_cache": True
            },
            result={"card": "test"},
        )
        ctx.should_call_send(
            event, "Love Love Ranking\ntest:10.00%\n\n复读次数排行榜\ntest:10次",
            "result")
        ctx.should_finished()
Exemple #13
0
async def test_ban_group_get_arg(
    app: App,
    mocker: MockerFixture,
    sender: dict,
):
    """测试群聊天,获取参数禁言 1 分钟

    机器人为普通群员,禁言对象为管理员或普通群员
    """
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.ban import EXPR_OK, ban_cmd

    render_expression = mocker.patch("src.plugins.ban.render_expression")
    render_expression.return_value = Message("test")

    async with app.test_matcher(ban_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/ban"),
                                         sender=sender)
        next_event = fake_group_message_event(message=Message("1"),
                                              sender=sender)

        ctx.receive_event(bot, event)
        ctx.should_call_api("get_group_list",
                            data={},
                            result=[{
                                "group_id": 10000
                            }])
        ctx.should_call_api(
            "get_group_member_info",
            data={
                "group_id": 10000,
                "user_id": 1
            },
            result={"role": "owner"},
        )
        ctx.should_call_send(event, "你想被禁言多少分钟呢?", True)
        ctx.should_rejected()
        ctx.receive_event(bot, next_event)
        ctx.should_call_api(
            "set_group_ban",
            data={
                "group_id": 10000,
                "user_id": 10,
                "duration": 60
            },
            result=[],
        )
        ctx.should_call_send(next_event, Message("test"), True, at_sender=True)
        ctx.should_finished()

    render_expression.assert_called_once_with(EXPR_OK, duration=1)
Exemple #14
0
async def test_query_sub_empty(app: App):
    """测试查询订阅,订阅为空的情况"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.web.nonebot_bison import query_sub_cmd

    async with app.test_matcher(query_sub_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/sub.query"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(event, Message("当前无订阅"), "result")
        ctx.should_finished()
Exemple #15
0
async def test_help_not_found(app: App):
    """测试帮助,插件不存在"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.help import help_cmd

    async with app.test_matcher(help_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/help test"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "请输入支持的命令", "result")
        ctx.should_finished()
Exemple #16
0
async def test_hello_not_enabled(app: App):
    """测试启动问候关闭的情况"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.morning import hello_cmd

    async with app.test_matcher(hello_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/hello"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "启动问候功能关闭中", "result")
        ctx.should_finished()
Exemple #17
0
async def test_query_sub(app: App):
    """测试查询订阅"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.web.nonebot_bison import query_sub_cmd
    from src.web.nonebot_bison.config import DATA

    with DATA.open("bison.json", "w", encoding="utf8") as f:
        json.dump(
            {
                "user_target": {
                    "1": {
                        "user":
                        10000,
                        "user_type":
                        "group",
                        "subs": [{
                            "target":
                            "https://devblogs.microsoft.com/python/feed/",
                            "target_type": "rss",
                            "target_name": "Python",
                            "cats": [],
                            "tags": [],
                        }],
                    }
                }
            },
            f,
        )

    async with app.test_matcher(query_sub_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/sub.query"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(
            event,
            Message(
                "订阅的帐号为:\nrss Python https://devblogs.microsoft.com/python/feed/"
            ),
            "result",
        )
        ctx.should_finished()
Exemple #18
0
async def test_add_sub_bilibili(
    app: App,
    mocker: MockerFixture,
):
    """测试添加订阅,添加 Bilibili"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.web.nonebot_bison import add_sub_cmd

    config = get_driver().config
    config.superusers = {"10"}

    get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get)

    async with app.test_matcher(add_sub_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/sub.add"))
        platform_event = fake_group_message_event(message=Message("bilibili"))
        id_event = fake_group_message_event(message=Message("4549624"))
        cats_event = fake_group_message_event(message=Message("一般动态,视频,纯文字,转发"))
        tags_event = fake_group_message_event(message=Message("全部标签"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(
            event,
            Message(
                '请输入想要订阅的平台,目前支持:\nbilibili:B站\nrss:RSS\nweibo:新浪微博\n要查看全部平台请输入:"全部"'
            ),
            "",
        )
        ctx.receive_event(bot, platform_event)
        ctx.should_call_send(platform_event, Message("请输入订阅用户的 ID"), "")
        ctx.receive_event(bot, id_event)
        ctx.should_call_send(
            id_event, Message("请输入要订阅的类别,以逗号分隔,支持的类别有:一般动态,专栏文章,视频,纯文字,转发"), ""
        )
        ctx.receive_event(bot, cats_event)
        ctx.should_call_send(cats_event, Message('请输入要订阅的标签,以逗号分隔,订阅所有标签输入"全部标签"'), "")
        ctx.receive_event(bot, tags_event)
        ctx.should_call_send(tags_event, "添加 Liyuu_ 成功", "")
        ctx.should_finished()

    get.assert_called_once()
Exemple #19
0
async def test_morning_enabled(app: App):
    """测试每日早安已开启的情况"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.morning import morning_cmd, plugin_config

    plugin_config.morning_group_id = [10000]

    async with app.test_matcher(morning_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/morning"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "每日早安功能开启中", "result")
        ctx.should_finished()
Exemple #20
0
async def test_delete_sub_empty(app: App):
    """测试删除订阅,订阅为空的情况"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.web.nonebot_bison import del_sub_cmd

    config = get_driver().config
    config.superusers = {"10"}

    async with app.test_matcher(del_sub_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/sub.delete"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "当前无订阅", "result")
        ctx.should_finished()
Exemple #21
0
async def test_price_help(app: App):
    """测试查价,参数不足两个的情况"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.ff14 import price_cmd

    async with app.test_matcher(price_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/查价"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(
            event,
            "最终幻想XIV 价格查询\n\n查询大区中的最低价格\n/查价 萨维奈舞裙 猫小胖\n查询服务器中的最低价格\n/查价 萨维奈舞裙 静语庄园\n设置默认查询的区域\n/查价 默认值 静语庄园\n查询当前设置的默认值\n/查价 默认值",
            True,
        )
        ctx.should_finished()
Exemple #22
0
async def test_help_help(app: App):
    """测试帮助,获取帮助插件帮助"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.help import help_cmd

    async with app.test_matcher(help_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/help help"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(
            event,
            "获取帮助\n\n获取所有支持的命令\n/help all\n获取某个命令的帮助\n/help 命令名",
            "result",
        )
        ctx.should_finished()
Exemple #23
0
async def test_hello_enable(app: App):
    """测试启动问候,在群里启用的情况"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.morning import hello_cmd, plugin_config

    assert plugin_config.hello_group_id == []

    async with app.test_matcher(hello_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/hello 1"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "已在本群开启启动问候功能", "result")
        ctx.should_finished()

    assert plugin_config.hello_group_id == [10000]
Exemple #24
0
async def test_morning_today(
    app: App,
    mocker: MockerFixture,
):
    """测试每日早安,查询今日早安的情况"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.morning import morning_cmd
    from src.plugins.morning.data import EXPR_MORNING

    mocked_date = mocker.patch("src.plugins.morning.data.date")
    mocked_date.today.return_value = date(2022, 1, 1)
    get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get)
    render_expression = mocker.patch(
        "src.plugins.morning.data.render_expression")
    render_expression.return_value = Message("test")

    async with app.test_matcher(morning_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/morning today"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(
            event,
            Message("test"),
            "result",
        )
        ctx.should_finished()

    mocked_date.today.assert_called()
    get.assert_called_once_with(
        "https://cdn.jsdelivr.net/gh/he0119/coolqbot@master/src/plugins/morning/holidays.json",
        timeout=30,
    )
    render_expression.assert_called_once_with(
        EXPR_MORNING,
        message="今天就是元旦,好好玩吧!",
    )
Exemple #25
0
async def test_repeat(
    app: App,
    mocker: MockerFixture,
):
    """测试复读"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.repeat import plugin_config, recorder_obj, repeat_message

    mocked_rule_datetime = mocker.patch("src.plugins.repeat.repeat_rule.datetime")
    mocked_rule_datetime.now.return_value = datetime(2021, 1, 1, 0, 0, 0)
    mocked_recorder_datetime = mocker.patch("src.plugins.repeat.recorder.datetime")
    mocked_recorder_datetime.now.side_effect = [
        datetime(2020, 1, 1, 0, 0, 0),  # add_new_group
        datetime(2021, 1, 1, 1, 0, 0),  # add_msg_number_list
        datetime(2021, 1, 1, 2, 0, 0),  # reset_last_message_on
        datetime(2021, 1, 1, 3, 0, 0),  # add_repeat_list
    ]
    mocked_random = mocker.patch("src.plugins.repeat.repeat_rule.secrets.SystemRandom")
    mocked_random().randint.return_value = 1

    plugin_config.group_id = [10000]
    recorder_obj.add_new_group()

    mocked_recorder_datetime.now.assert_called_once()

    async with app.test_matcher(repeat_message) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("123"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(event, event.message, "result")
        ctx.should_finished()

    assert mocked_recorder_datetime.now.call_count == 4
    assert recorder_obj.last_message_on(10000) == datetime(2021, 1, 1, 2, 0, 0)
    assert recorder_obj.repeat_list(10000) == {10: 1}
Exemple #26
0
async def test_heweather_with_adm(
    app: App,
    mocker: MockerFixture,
):
    """测试和风天气,带行政区划"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.weather import weather_cmd
    from src.plugins.weather.heweather_api import plugin_config

    plugin_config.heweather_key = "1234567890"

    get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get)

    async with app.test_matcher(weather_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/天气 成都 四川"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(
            event,
            "中国 四川省 成都\n当前温度:9℃ 湿度:86%(体感温度:9℃)\n2022-01-08 阴转多云 温度:9~6℃ 峨眉月\n2022-01-09 多云转小雨 温度:10~7℃ 峨眉月\n2022-01-10 多云 温度:13~3℃ 上弦月",
            "result",
        )
        ctx.should_finished()

    get.assert_has_calls([
        mocker.call(
            "https://geoapi.qweather.com/v2/city/lookup?location=%E6%88%90%E9%83%BD&adm=%E5%9B%9B%E5%B7%9D&key=1234567890"
        ),
        mocker.call(
            "https://devapi.qweather.com/v7/weather/now?location=101270101&key=1234567890"
        ),
        mocker.call(
            "https://devapi.qweather.com/v7/weather/3d?location=101270101&key=1234567890"
        ),
    ])
Exemple #27
0
async def test_repeat_disable(app: App):
    """测试复读,在群里关闭的情况"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.repeat import plugin_config, repeat_cmd

    plugin_config.group_id = [10000]

    assert plugin_config.group_id == [10000]

    async with app.test_matcher(repeat_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/repeat 0"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "已在本群关闭复读功能", "result")
        ctx.should_finished()

    assert plugin_config.group_id == []
Exemple #28
0
async def test_add_sub_rss(
    app: App,
    mocker: MockerFixture,
):
    """测试添加订阅,添加 RSS"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.web.nonebot_bison import add_sub_cmd

    config = get_driver().config
    config.superusers = {"10"}

    get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get)

    async with app.test_matcher(add_sub_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/sub.add"))
        next_event = fake_group_message_event(message=Message("rss"))
        final_event = fake_group_message_event(
            message=Message("https://devblogs.microsoft.com/python/feed/")
        )

        ctx.receive_event(bot, event)
        ctx.should_call_send(
            event,
            Message(
                '请输入想要订阅的平台,目前支持:\nbilibili:B站\nrss:RSS\nweibo:新浪微博\n要查看全部平台请输入:"全部"'
            ),
            "",
        )
        ctx.receive_event(bot, next_event)
        ctx.should_call_send(next_event, Message("请输入订阅用户的 ID"), "")
        ctx.receive_event(bot, final_event)
        ctx.should_call_send(final_event, "添加 Python 成功", "")

    get.assert_called_once()
Exemple #29
0
async def test_rand(
    app: App,
    mocker: MockerFixture,
):
    """测试点数"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.roll import rand_cmd

    randint = mocker.patch("src.plugins.roll.rand.randint")
    randint.return_value = 1

    async with app.test_matcher(rand_cmd) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("/rand"))

        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "你的点数是 1", "result", at_sender=True)
        ctx.should_finished()

    randint.assert_called_once_with(0, 100)
Exemple #30
0
async def test_tencent(
    app: App,
    mocker: MockerFixture,
):
    """测试腾讯闲聊机器人"""
    from nonebot import get_driver
    from nonebot.adapters.onebot.v11 import Adapter, Bot, Message
    from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot

    from src.plugins.robot import robot_message

    call_tencent_api = mocker.patch("src.plugins.robot.call_tencent_api")
    call_tencent_api.return_value = "test"

    async with app.test_matcher(robot_message) as ctx:
        adapter = make_fake_adapter(Adapter)(get_driver(), ctx)
        bot = make_fake_bot(Bot)(adapter, "1")
        event = fake_group_message_event(message=Message("你好"), to_me=True)

        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "test", "result")
        ctx.should_finished()

    call_tencent_api.assert_called_once_with("你好")