Beispiel #1
0
    def test_tg_send_message(self):
        mngr = TGManager("token")

        attachment1 = Attachment("photo", 13, None, None, None, None)
        attachment2 = TGAttachmentTemp("bad_type", "strange_content", {})

        async def request(self, method, **kwargs):
            return TGResponse(False, (), [method, kwargs])

        mngr.request = types.MethodType(request, mngr)

        res0 = self.loop.run_until_complete(mngr.send_message("hi", None))

        self.assertEqual(len(res0), 0)

        res1 = self.loop.run_until_complete(
            mngr.send_message("hi", 1, [attachment1, attachment2]))

        self.assertEqual(len(res1), 2)

        self.assertEqual(res1[0].response,
                         ["sendMessage", {
                             "chat_id": '1',
                             "text": "hi"
                         }])

        self.assertEqual(res1[1].response,
                         ["sendPhoto", {
                             "chat_id": '1',
                             "photo": "13"
                         }])

        res2 = self.loop.run_until_complete(
            mngr.send_message("", 1, attachment1))

        self.assertEqual(len(res2), 1)

        self.assertEqual(res2[0].response,
                         ["sendPhoto", {
                             "chat_id": '1',
                             "photo": "13"
                         }])

        self.loop.run_until_complete(mngr.dispose())
Beispiel #2
0
    def test_tg_get_receiver_coroutine_function(self):
        mngr = TGManager("token")

        async def request(self, method, **kwargs):
            if method == "getMe":
                return TGResponse(False, (), {
                    "first_name": "A",
                    "last_name": "B",
                    "username": "******"
                })

        mngr.request = types.MethodType(request, mngr)

        receiver = self.loop.run_until_complete(
            mngr.get_receiver_coroutine_function())

        self.assertEqual(receiver, mngr.receiver)

        self.loop.run_until_complete(mngr.dispose())
Beispiel #3
0
    def test_vk_request(self):
        mngr = TGManager("token")

        async def prepare():
            class FakeSession:
                def post(self, url, proxy, data):
                    class FakePost:
                        def __init__(self, url, proxy, data):
                            pass

                        async def __aenter__(self):
                            class FakeResponse:
                                async def text(self):
                                    return json.dumps({
                                        "ok": True,
                                        "result": "result"
                                    })

                            return FakeResponse()

                        async def __aexit__(self, exc_type, exc, tb):
                            pass

                    return FakePost(url, proxy, data)

                async def close(self):
                    pass

            mngr.session = FakeSession()

        self.loop.run_until_complete(prepare())

        response = self.loop.run_until_complete(
            mngr.request("method", a1="v1", a2="v2"))

        self.assertEqual(response.error, False)
        self.assertEqual(response.response, "result")
        self.assertEqual(response.errors, ())

        self.loop.run_until_complete(mngr.dispose())