示例#1
0
def test_router_priority():
    app, debug, hu = make_kutana_no_run()

    pl = Plugin("")

    received = []

    @pl.on_updates(router_priority=10)
    async def __(upd, ctx):
        received.append(upd.raw * 2)

    @pl.on_unprocessed_updates(priority=1, router_priority=10)
    async def __(upd, ctx):
        received.append(upd.raw * 3)

    @pl.on_updates(router_priority=20)
    async def __(upd, ctx):
        if upd.raw == 'a':
            return hr.SKIPPED
        received.append(upd.raw)

    app.add_plugin(pl)

    hu(Update('a', UpdateType.UPD, {}))
    hu(Update('b', UpdateType.UPD, {}))
    assert hu(Message(None, UpdateType.MSG, ".123", (), 1, 0, 0, 0,
                      {})) == hr.SKIPPED

    assert received[0] == 'aaa'
    assert received[1] == 'b'
示例#2
0
def test_commands():
    app, debug, hu = make_kutana_no_run()

    pl = Plugin("")

    @pl.on_commands(["echo", "ec"])
    async def _(msg, ctx):
        await ctx.reply(msg.text)

    app.add_plugin(pl)

    hu(Message(None, UpdateType.MSG, ".echo 123", (), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, ".ec abc", (), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, ".echo", (), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, ".ec\n123", (), 1, 0, 0, 0))

    hu(Message(None, UpdateType.MSG, ".ecabc", (), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, ".e cabc", (), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, "abc", (), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, "echo abc", (), 1, 0, 0, 0))

    assert hu(Update(None, UpdateType.UPD)) == hr.SKIPPED

    assert len(debug.answers[1]) == 4
    assert debug.answers[1][0] == (".echo 123", (), {})
    assert debug.answers[1][1] == (".ec abc", (), {})
    assert debug.answers[1][2] == (".echo", (), {})
    assert debug.answers[1][3] == (".ec\n123", (), {})
示例#3
0
def test_attachments():
    app, debug, hu = make_kutana_no_run()

    pl = Plugin("")

    @pl.on_attachments(["image", "voice"])
    async def _(msg, ctx):
        await ctx.reply(msg.text)

    app.add_plugin(pl)

    image = Attachment("", "image", "", None, "", None, "present")
    voice = Attachment("", "voice", "", None, "", None, "present")
    sticker = Attachment("", "sticker", "", None, "", None, "present")

    hu(Message(None, UpdateType.MSG, "i", (image, ), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, "v", (voice, ), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, "vi", (voice, image), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, "si", (
        sticker,
        image,
    ), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, "s", (sticker, ), 1, 0, 0, 0))

    assert hu(Update(None, UpdateType.UPD)) == hr.SKIPPED

    assert len(debug.answers[1]) == 4
    assert debug.answers[1][0] == ("i", (), {})
    assert debug.answers[1][1] == ("v", (), {})
    assert debug.answers[1][2] == ("vi", (), {})
    assert debug.answers[1][3] == ("si", (), {})
示例#4
0
def test_on_payloads_types():
    app, debug, hu = make_kutana_no_run(backend_source="vkontakte")

    pl = Plugin("")

    @pl.vk.on_payloads([{"command": {"why": "test"}}, "txt"])
    async def __(msg, ctx):
        await ctx.reply(msg.text)

    app.add_plugin(pl)

    raw1 = make_message_update('{"command": {"why": "test"}}')
    raw2 = make_message_update('{"command": []}')
    raw3 = make_message_update('{"command": [{"a": 1}]}')
    raw4 = make_message_update('"txt"')
    raw5 = make_message_update("error")

    hu(Message(raw1, UpdateType.MSG, "hey1", (), 1, 0, 0, 0, {}))
    hu(Message(raw2, UpdateType.MSG, "hey2", (), 1, 0, 0, 0, {}))
    hu(Message(raw3, UpdateType.MSG, "hey3", (), 1, 0, 0, 0, {}))
    hu(Message(raw4, UpdateType.MSG, "hey4", (), 1, 0, 0, 0, {}))
    hu(Message(raw5, UpdateType.MSG, "hey5", (), 1, 0, 0, 0, {}))

    assert hu(Update(None, UpdateType.UPD, {})) == hr.SKIPPED

    assert len(debug.answers[1]) == 2
    assert debug.answers[1][0] == ("hey1", (), {})
    assert debug.answers[1][1] == ("hey4", (), {})
示例#5
0
def test_ignore_non_vkontakte():
    app, debug, hu = make_kutana_no_run()

    pl = Plugin("")

    @pl.vk.on_payloads([{"command": "echo"}])
    async def __(msg, ctx):
        await ctx.reply(ctx.payload["text"])

    @pl.vk.on_callbacks([{"val": 1}])
    async def __(upd, ctx):
        await ctx.send_message_event_answer({
            "type": "show_snackbar",
            "text": "hey hey hey",
        })

    app.add_plugin(pl)

    raw1 = make_message_update('{"command": "echo", "text": "hello"}')
    raw2 = MESSAGES["inline_callback_1"]

    hu(Message(raw1, UpdateType.MSG, "hey1", (), 1, 0, 0, 0, {}))
    hu(Update(raw2, UpdateType.UPD, {}))

    assert not debug.answers
    assert not debug.requests
示例#6
0
def test_any_update():
    app, debug, hu = make_kutana_no_run()

    pl = Plugin("")

    received = []

    @pl.on_updates()
    async def __(upd, ctx):
        received.append(upd.raw)

    app.add_plugin(pl)

    hu(Update('a', UpdateType.UPD, {}))
    hu(Update('b', UpdateType.UPD, {}))
    assert hu(Message(None, UpdateType.MSG, ".123", (), 1, 0, 0, 0,
                      {})) == hr.SKIPPED

    assert received[0] == 'a'
    assert received[1] == 'b'
示例#7
0
def test_on_callback():
    app, debug, hu = make_kutana_no_run(backend_source="vkontakte")

    pl = Plugin("")

    @pl.vk.on_callbacks([{"val": 1}])
    async def __(upd, ctx):
        await ctx.send_message_event_answer({
            "type": "show_snackbar",
            "text": "hey hey hey",
        })

    @pl.vk.on_callbacks(['val'])
    async def __(upd, ctx):
        await ctx.send_message_event_answer({
            "type": "show_snackbar",
            "text": "val val val",
        })

    app.add_plugin(pl)

    hu(Update(MESSAGES["inline_callback_1"], UpdateType.UPD, {}))
    hu(Update(MESSAGES["inline_callback_2"], UpdateType.UPD, {}))
    hu(Update(MESSAGES["inline_callback_val"], UpdateType.UPD, {}))
    hu(Message(make_message_update({"val": 1}), UpdateType.MSG, "hey3", (), 1, 0, 0, 0, {}))

    assert debug.requests == [
        ("messages.sendMessageEventAnswer", {
            'event_data': '{"type": "show_snackbar", "text": "hey hey hey"}',
            'event_id': '3159dc190b1g',
            'user_id': 87641997,
            'peer_id': 87641997,
        }),
        ("messages.sendMessageEventAnswer", {
            'event_data': '{"type": "show_snackbar", "text": "val val val"}',
            'event_id': '3159dc190b1g',
            'user_id': 87641997,
            'peer_id': 87641997,
        }),
    ]
示例#8
0
def test_any_message():
    app, debug, hu = make_kutana_no_run()

    pl = Plugin("")

    @pl.on_any_message()
    async def _(msg, ctx):
        await ctx.reply(msg.text)

    app.add_plugin(pl)

    hu(Message(None, UpdateType.MSG, "123", (), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, "abc", (), 1, 0, 0, 0))
    assert hu(Update(None, UpdateType.UPD)) == hr.SKIPPED

    assert debug.answers[1][0] == ("123", (), {})
    assert debug.answers[1][1] == ("abc", (), {})
示例#9
0
def test_on_message_actions_types():
    app, debug, hu = make_kutana_no_run(backend_source="vkontakte")

    pl = Plugin("")

    @pl.vk.on_message_actions(["chat_photo_remove"])
    async def __(msg, ctx):
        await ctx.reply(msg.raw['object']['message']["action"]["photo"]["photo_50"])

    @pl.vk.on_message_actions(['chat_invite_user'])
    async def __(msg, ctx):
        if msg.raw['object']['message']['action']['member_id'] < 0:
            await ctx.reply(msg.raw['object']['message']['action']['email'])

    app.add_plugin(pl)

    raw1 = make_message_action('chat_photo_update', 1, None, None, {
        "photo_50": "http://example.com/50",
        "photo_100": "http://example.com/100",
        "photo_200": "http://example.com/200",
    })
    raw2 = make_message_action('chat_photo_remove', 1, None, None, {
        "photo_50": "http://example.com/50",
        "photo_100": "http://example.com/100",
        "photo_200": "http://example.com/200",
    })
    raw3 = make_message_action('chat_title_update', 1, 'title1', None, None)
    raw4 = make_message_action('chat_invite_user', -1, None, "*****@*****.**", None)
    raw5 = make_message_action('chat_kick_user', -1, None, "*****@*****.**", None)
    raw6 = make_message_update("error")

    hu(Message(raw1, UpdateType.MSG, "hey1", (), 1, 0, 0, 0, {}))
    hu(Message(raw2, UpdateType.MSG, "hey2", (), 1, 0, 0, 0, {}))
    hu(Message(raw3, UpdateType.MSG, "hey3", (), 1, 0, 0, 0, {}))
    hu(Message(raw4, UpdateType.MSG, "hey4", (), 1, 0, 0, 0, {}))
    hu(Message(raw5, UpdateType.MSG, "hey5", (), 1, 0, 0, 0, {}))
    hu(Message(raw6, UpdateType.MSG, "hey6", (), 1, 0, 0, 0, {}))

    assert hu(Update(None, UpdateType.UPD, {})) == hr.SKIPPED

    assert len(debug.answers[1]) == 2
    assert debug.answers[1][0] == ("http://example.com/50", (), {})
    assert debug.answers[1][1] == ("*****@*****.**", (), {})
示例#10
0
def test_any_unprocessed_message():
    app, debug, hu = make_kutana_no_run()

    pl = Plugin("")

    @pl.on_commands(["abc"])
    async def __(msg, ctx):
        await ctx.reply(msg.text)

    @pl.on_unprocessed_messages()
    async def __(msg, ctx):
        await ctx.reply(msg.text * 2)

    app.add_plugin(pl)

    hu(Message(None, UpdateType.MSG, ".123", (), 1, 0, 0, 0, {}))
    hu(Message(None, UpdateType.MSG, ".abc", (), 1, 0, 0, 0, {}))
    assert hu(Update(None, UpdateType.UPD, {})) == hr.SKIPPED

    assert debug.answers[1][0] == (".123" * 2, (), {})
    assert debug.answers[1][1] == (".abc", (), {})
示例#11
0
def test_matches():
    app, debug, hu = make_kutana_no_run()

    pl = Plugin("")

    @pl.on_match(r"\d.\d")
    async def _(msg, ctx):
        await ctx.reply(ctx.match.group(0))

    @pl.on_match(r"(a|b|c).[a-z]")
    async def _(msg, ctx):
        await ctx.reply(ctx.match.group(0))
        await ctx.reply(ctx.match.group(1))

    app.add_plugin(pl)

    hu(Message(None, UpdateType.MSG, "123", (), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, "abc", (), 1, 0, 0, 0))
    assert hu(Update(None, UpdateType.UPD)) == hr.SKIPPED

    assert debug.answers[1][0] == ("123", (), {})
    assert debug.answers[1][1] == ("abc", (), {})
    assert debug.answers[1][2] == ("a", (), {})