def test_vk_request_error(self): mngr = TGManager("token") exception = None 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): if exception: raise exception return json.dumps({"ok": False}) 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("method1", a1="v1", a2="v2")) self.assertEqual(response.error, True) self.assertEqual(response.response, "") self.assertTrue(response.errors) exception = aiohttp.ClientError response = self.loop.run_until_complete( mngr.request("method2", a1="v1", a2="v2")) self.assertEqual(response.error, True) self.assertEqual(response.response, "") self.assertTrue(response.errors) self.loop.run_until_complete(mngr.dispose())
def test_tg_get_file_from_attachment(self): mngr = TGManager("token") env = self.loop.run_until_complete( mngr.get_environment({"message": { "chat": { "id": 1 } }})) async def request(_, method, **kwargs): self.assertEqual(kwargs["file_id"], 13) return TGResponse(False, (), {"file_path": "path"}) mngr.request = types.MethodType(request, mngr) async def request_file(_, path): self.assertEqual(path, "path") return "file" mngr.request_file = types.MethodType(request_file, mngr) attachment = Attachment("photo", 13, None, None, None, None) file = self.loop.run_until_complete( env.get_file_from_attachment(attachment)) self.assertEqual(file, "file")
def test_tg_send_message(self): mngr = TGManager("token") attachment1 = Attachment("photo", 13, None, None, None, None) attachment2 = Attachment("doc", 14, None, None, None, None) attachment3 = Attachment("document", 15, None, None, None, None) attachment4 = 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, attachment4])) 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, attachment2)) self.assertEqual(len(res2), 1) self.assertEqual(res2[0].response, ["sendDocument", { "chat_id": '1', "document": "14" }]) res3 = self.loop.run_until_complete( mngr.send_message("", 1, attachment3)) self.assertEqual(len(res3), 1) self.assertEqual(res3[0].response, ["sendDocument", { "chat_id": '1', "document": "15" }]) self.loop.run_until_complete(mngr.dispose())
def test_tg_get_receiver_coroutine_function_error(self): mngr = TGManager("token") async def request(self, method, **kwargs): if method == "getMe": return TGResponse(True, (), {}) mngr.request = types.MethodType(request, mngr) with self.assertRaises(ValueError): self.loop.run_until_complete( mngr.get_receiver_coroutine_function()) self.loop.run_until_complete(mngr.dispose())
def test_tg_receiver_error(self): mngr = TGManager("token") async def request(self, method, **kwargs): if method == "getUpdates": return TGResponse(True, (), "") mngr.request = types.MethodType(request, mngr) updates = self.loop.run_until_complete(mngr.receiver()) self.assertEqual(updates, ()) self.loop.run_until_complete(mngr.dispose())
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())
def test_tg_replay(self): mngr = TGManager("token") env = self.loop.run_until_complete( mngr.get_environment({"message": { "chat": { "id": 1 } }})) async def request(_, method, **kwargs): self.assertEqual(kwargs["chat_id"], "1") return TGResponse(True, (), "") mngr.request = types.MethodType(request, mngr) res = self.loop.run_until_complete(env.reply("hi")) self.assertTrue(res[0].error) self.assertFalse(res[0].response)
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())
def test_tg_upload_doc(self): mngr = TGManager("token") async def request(self, method, **kwargs): return TGResponse(False, (), [method, kwargs]) mngr.request = types.MethodType(request, mngr) env = self.loop.run_until_complete( mngr.get_environment({"message": { "chat": { "id": 1 } }})) document_a = self.loop.run_until_complete( env.upload_doc("document_content", "filename")) self.assertEqual(document_a.type, "doc") self.assertEqual(document_a.content, "document_content") self.assertEqual(document_a.kwargs, {}) res = self.loop.run_until_complete( mngr.send_message("hi", 1, document_a)) self.assertEqual(len(res), 2) self.assertEqual(res[0].response, ["sendMessage", { "chat_id": '1', "text": "hi" }]) self.assertEqual( res[1].response, ["sendDocument", { "chat_id": '1', "document": "document_content" }])