Example #1
0
def test_receiver_states():
    app, debug, hu = make_kutana_no_run()

    pl = Plugin("")
    pl.app = app

    @pl.on_commands(["a"])
    @pl.expect_receiver(state="")
    async def __(msg, ctx):
        await ctx.receiver.update({"state": "state"})
        await ctx.reply("ok:a")

    @pl.on_commands(["b"])
    @pl.expect_receiver(state="state")
    async def __(msg, ctx):
        await ctx.receiver.update({"state": ""})
        await ctx.reply("ok:b")

    @pl.on_commands(["a"])
    @pl.expect_receiver(state="other_state")
    async def __(msg, ctx):
        await ctx.receiver.update({"state": ""})
        await ctx.reply("ok:b")

    app.add_plugin(pl)

    hu(Message(None, UpdateType.MSG, ".a", (), 1, 0, 0, 0, {}))
    hu(Message(None, UpdateType.MSG, ".b", (), 1, 0, 0, 0, {}))
    hu(Message(None, UpdateType.MSG, ".b", (), 1, 0, 0, 0, {}))
    hu(Message(None, UpdateType.MSG, ".a", (), 1, 0, 0, 0, {}))

    assert len(debug.answers[1]) == 3
    assert debug.answers[1].count(("ok:a", (), {})) == 2
    assert debug.answers[1].count(("ok:b", (), {})) == 1
    assert debug.answers[1][-1] == ("ok:a", (), {})
Example #2
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", (), {})
Example #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", (), {})
Example #4
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", (), {})
Example #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
Example #6
0
async def on_has_text(message, env):
    match = plugin.pattern.match(message.text)

    if message.from_id != message.peer_id:
        mention_match = re.search(
            r"\[[a-zA-Z0-9]+\|.+?\]",
            message.raw_update["object"]["text"]
        )

    else:
        mention_match = None

    if not (mention_match or match and match.group("text")):
        return "DONE"

    env.parent.set_message(
        Message(
            match and match.group("text") or message.text,
            message.attachments,
            message.from_id,
            message.peer_id,
            message.date,
            message.raw_update
        )
    )

    return "GOON"
Example #7
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'
Example #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", (), {})
Example #9
0
async def on_has_text(message, attachments, env):
    if not message.text.startswith(PREFIX):
        return "DONE"

    env.eenv._cached_message = Message(message.text[len(PREFIX):],
                                       message.attachments, message.from_id,
                                       message.peer_id, message.raw_update)

    return "GOON"
Example #10
0
def test_commands_with_spaces():
    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, ". echo 123", (), 1, 0, 0, 0, {}))
    hu(Message(None, UpdateType.MSG, ".\nec abc", (), 1, 0, 0, 0, {}))

    assert len(debug.answers[1]) == 3
    assert debug.answers[1][0] == (" . echo 123", (), {})
    assert debug.answers[1][1] == (". echo 123", (), {})
    assert debug.answers[1][2] == (".\nec abc", (), {})
Example #11
0
def test_on_message_actions():
    app, debug, hu = make_kutana_no_run(backend_source="vkontakte")

    pl = Plugin("")

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

    app.add_plugin(pl)

    raw1 = make_message_action('chat_invite_user_by_link', 1, None, None, None)
    raw2 = make_message_action('chat_title_update', 1, 'they1', None, None)

    hu(Message(raw1, UpdateType.MSG, 'hey1', (), 1, 0, 0, 0, {}))
    hu(Message(raw2, UpdateType.MSG, 'hey2', (), 1, 0, 0, 0, {}))

    assert len(debug.answers[1]) == 1
    assert debug.answers[1][0] == ("they1", (), {})
Example #12
0
def test_on_payloads():
    app, debug, hu = make_kutana_no_run(backend_source="vkontakte")

    pl = Plugin("")

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

    app.add_plugin(pl)

    raw1 = make_message_update('{"command": "test"}')
    raw2 = make_message_update('{"command": "error"}')

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

    assert len(debug.answers[1]) == 1
    assert debug.answers[1][0] == ("hey1", (), {})
Example #13
0
async def on_has_text(message, attachments, env):
    if not message.text.startswith(env.eenv.prefix):
        return "DONE"  # "GOON" if you want to just keep message

    env.eenv['prefixes'] = env.eenv.prefix

    env.eenv._cached_message = Message(message.text[len(env.eenv.prefix):],
                                       message.attachments, message.from_id,
                                       message.peer_id, message.raw_update)

    return "GOON"
Example #14
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", (), {})
Example #15
0
async def _(message, env):
    for prefix in PREFIXES:
        if message.text[:len(prefix)] == prefix:
            break

    else:
        return "DONE"

    env.parent.set_message(
        Message(message.text[len(prefix):], message.attachments,
                message.from_id, message.peer_id, message.date,
                message.raw_update))

    return "GOON"
Example #16
0
    def test_on_message(self):
        plugin = Plugin()

        @plugin.on_message()
        async def _(message, env):
            return "DONE"

        wrapper = plugin._callbacks[0][1]

        res = asyncio.get_event_loop().run_until_complete(
            wrapper(Message("", ("attachment"), 0, 0, 0, {}),
                    DebugEnvironment(None, 0)))

        self.assertEqual(res, "DONE")
Example #17
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", (), {})
Example #18
0
def test_command_full_body():
    app, debug, hu = make_kutana_no_run()

    pl = Plugin("")

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

    app.add_plugin(pl)

    hu(Message(None, UpdateType.MSG, ".echo abc\nabc\nabc", (), 1, 0, 0, 0))

    assert len(debug.answers[1]) == 1
    assert debug.answers[1][0] == ("abc\nabc\nabc", (), {})
Example #19
0
    def test_plugin_attachments_type(self):
        plugin = Plugin()

        @plugin.on_attachment("photo")
        async def _(message, env):
            return "DONE"

        wrapper = plugin._callbacks[0][1]

        res = asyncio.get_event_loop().run_until_complete(
            wrapper(
                Message("text", [Attachment("photo", 0, 0, 0, 0, {})], 0, 0, 0,
                        {}), DebugEnvironment(None, 0)))

        self.assertEqual(res, "DONE")
Example #20
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'
Example #21
0
    def test_plugin_no_attachments_type(self):
        plugin = Plugin()

        async def on_attachment(message, env):
            return "DONE"

        plugin.on_attachment("photo")(on_attachment)

        wrapper = plugin._callbacks[0][1]

        attachments = [
            Attachment("audio", 0, 0, 0, 0, {}),
            Attachment("video", 0, 0, 0, 0, {})
        ]

        res = asyncio.get_event_loop().run_until_complete(
            wrapper(Message("", attachments, 0, 0, 0, {}),
                    DebugEnvironment(None, 0)))

        self.assertEqual(res, None)
Example #22
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,
        }),
    ]
Example #23
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] == ("*****@*****.**", (), {})
Example #24
0
def test_quest():
    app, debug, hu = make_kutana_no_run()

    app.add_plugin(pl)

    hu(Message(None, UpdateType.MSG, ".start", (), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, ".left", (), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, ".ok", (), 1, 0, 0, 0))
    assert debug.answers[1][-1] == ("Bye", (), {})

    hu(Message(None, UpdateType.MSG, ".start", (), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, ".start", (), 2, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, ".left", (), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, ".right", (), 2, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, ".ok", (), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, ".ok", (), 2, 0, 0, 0))
    assert debug.answers[1][-1] == ("Bye", (), {})
    assert debug.answers[2][-1] == ("Bye", (), {})

    hu(Message(None, UpdateType.MSG, ".left", (), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, ".right", (), 1, 0, 0, 0))
    assert debug.answers[1][-1] == ("Bye", (), {})

    hu(Message(None, UpdateType.MSG, ".start", (), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, ".left", (), 1, 0, 0, 0))
    hu(Message(None, UpdateType.MSG, ".bruh", (), 1, 0, 0, 0))
    assert debug.answers[1][-1] == ("Write '.ok'", (), {})
    hu(Message(None, UpdateType.MSG, ".ok", (), 1, 0, 0, 0))
    assert debug.answers[1][-1] == ("Bye", (), {})
    hu(Message(None, UpdateType.MSG, ".bruh", (), 1, 0, 0, 0))
    assert debug.answers[1][-1] == ("Bye", (), {})