Beispiel #1
0
    def test_plugin_attachments(self):
        plugin = Plugin()

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

        plugin.on_attachment()(on_attachment)

        wrapper = plugin._callbacks[0][1]

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

        self.assertEqual(res, "DONE")
Beispiel #2
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)
Beispiel #3
0
    def test_vk_full(self):
        plugin = Plugin()

        self.called = False
        self.called_on_raw = False
        self.called_on_attachment = False

        async def on_attachment(message, attachments, env):
            self.called_on_attachment = True
            return "GOON"

        plugin.on_attachment("photo")(on_attachment)

        async def on_regexp(message, attachments, env):
            # Test receiving
            self.assertEqual(env.match.group(1), "message")
            self.assertEqual(env.match.group(0), "echo message")

            self.assertEqual(message.attachments, attachments)
            self.assertEqual(len(attachments), 2)

            self.assertTrue(attachments[0].link)
            self.assertTrue(attachments[1].link)

            # Test sending
            a_image = await env.upload_photo("test/test_assets/author.png")

            a_image = await env.upload_photo("test/test_assets/author.png",
                                             peer_id=False)

            a_audio = await env.upload_doc("test/test_assets/girl.ogg",
                                           doctype="audio_message",
                                           filename="file.ogg")

            self.assertTrue(a_image.id)
            self.assertTrue(a_audio.id)

            resps = await env.reply("Спасибо.", attachment=a_image)

            self.assertTrue(resps[0].response)

            for resp in resps:
                resp = await env.request("messages.delete",
                                         message_ids=str(resp.response),
                                         delete_for_all=1)

                self.assertTrue(resp.response)

            # Test failed request
            resp = await env.request("messages.send")

            self.assertTrue(resp.error)
            self.assertTrue(resp.errors[0][1])
            self.assertEqual(resp.errors[0][0], "VK_req")
            self.assertFalse(resp.response)

            self.called = True

        plugin.on_regexp_text(r"echo (.+)")(on_regexp)

        async def on_raw(update, env):
            self.called_on_raw = True

            return "GOON"

        plugin.on_raw()(on_raw)

        self.kutana.executor.register_plugins(plugin)

        self.kutana.run()

        self.assertTrue(self.called)
        self.assertTrue(self.called_on_raw)
        self.assertTrue(self.called_on_attachment)