Commit 7db8122d authored by Hermann Krumrey's avatar Hermann Krumrey

Create tests for telegram

parent 4636400e
......@@ -5,3 +5,4 @@ cover/
*.egg-info/
dist/
build/
*.session
\ No newline at end of file
......@@ -21,7 +21,7 @@ import logging
import argparse
from bokkichat.address import Address
from bokkichat.message import TextMessage, MediaMessage, MediaType
from bokkichat.connection import TelegramSettings, TelegramConnection
from bokkichat.connection import TelegramBotSettings, TelegramBotConnection
def parse_args() -> argparse.Namespace:
......@@ -56,11 +56,10 @@ def parse_args() -> argparse.Namespace:
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
args = parse_args()
settings = TelegramSettings(args.api_key)
connection = TelegramConnection(settings)
settings = TelegramBotSettings(args.api_key)
connection = TelegramBotConnection(settings)
if args.mode == "send":
......
......@@ -17,7 +17,6 @@ You should have received a copy of the GNU General Public License
along with bokkichat. If not, see <http://www.gnu.org/licenses/>.
LICENSE"""
import logging
import argparse
from bokkichat.address import Address
from bokkichat.message import TextMessage, MediaMessage, MediaType
......@@ -64,7 +63,6 @@ if __name__ == "__main__":
settings = TelegramSettings(args.api_id, args.api_hash)
connection = TelegramConnection(settings)
connection.close()
if args.mode == "send":
......@@ -98,8 +96,6 @@ if __name__ == "__main__":
elif args.mode == "receive":
messages = connection.receive()
print(messages)
for message in messages:
print(message)
......
......@@ -56,7 +56,7 @@ class TelegramBotConnection(Connection):
A connection must be able to specify its own address
:return: The address of the connection
"""
return Address(str(self.bot.id))
return Address(str(self.bot.name))
def send(self, message: Message):
"""
......@@ -98,7 +98,7 @@ class TelegramBotConnection(Connection):
tempfile.close()
except (telegram.error.Unauthorized, telegram.error.BadRequest):
self.logger.info(
self.logger.warning(
"Failed to send message to {}".format(message.receiver)
)
......@@ -146,7 +146,6 @@ class TelegramBotConnection(Connection):
:return: The generated Message object.
:raises: InvalidMessageData if the parsing failed
"""
address = Address(str(message_data["chat"]["id"]))
if "text" in message_data:
......
......@@ -19,7 +19,8 @@ LICENSE"""
# noinspection PyPackageRequirements
from typing import List
from telethon import TelegramClient
# noinspection PyUnresolvedReferences
from telethon import TelegramClient, sync
from bokkichat.address.Address import Address
from bokkichat.message.Message import Message
from bokkichat.message.TextMessage import TextMessage
......@@ -96,6 +97,7 @@ class TelegramConnection(Connection):
# noinspection PyTypeChecker
for dialog in self.client.get_dialogs():
# noinspection PyTypeChecker
for message in self.client.iter_messages(dialog.entity):
......@@ -105,7 +107,7 @@ class TelegramConnection(Connection):
address = Address(dialog.name)
body = message.message
generated = TextMessage(self.address, address, body)
generated = TextMessage(address, self.address, body)
messages.append(generated)
# TODO ACK
......
"""LICENSE
Copyright 2018 Hermann Krumrey <hermann@krumreyh.com>
This file is part of bokkichat.
bokkichat is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
bokkichat is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with bokkichat. If not, see <http://www.gnu.org/licenses/>.
LICENSE"""
import os
import random
from base64 import b64decode
from unittest import TestCase
from bokkichat.message import TextMessage
from bokkichat.connection import TelegramConnection, TelegramSettings,\
TelegramBotConnection, TelegramBotSettings
class TestTelegram(TestCase):
"""
Class that tests telegram connections, both the telethon and
python-telegram-bot implementations. By using both, they can test each
other.
"""
def setUp(self):
"""
Initializes the connections
:return: None
"""
api_id = os.environ["TELEGRAM_API_ID"]
api_hash = os.environ["TELEGRAM_API_HASH"]
bot_api_key = os.environ["TELEGRAM_BOT_API_KEY"]
if not os.path.isfile("bokkichat.session"):
with open("bokkichat.session", "w") as f:
content = bytes(os.environ["TELEGRAM_SESSION"], "utf-8")
f.write(b64decode(content))
self.telegram = TelegramConnection(TelegramSettings(
api_id, api_hash, "bokkichat"
))
self.telegram_bot = TelegramBotConnection(TelegramBotSettings(
bot_api_key
))
def tearDown(self):
"""
Closes the connections
:return: None
"""
self.telegram.close()
self.telegram_bot.close()
def test_sending_text_messages(self):
"""
Tests sending text messages from one connection to another
:return: None
"""
# First, test telethon -> python-telegram-bot
text_one = "Test1 {}".format(random.randint(0, 10000))
message_one = TextMessage(
self.telegram.address, self.telegram_bot.address, text_one
)
self.telegram.send(message_one)
received_one = self.telegram_bot.receive()[-1] # type: TextMessage
self.assertEqual(text_one, received_one.body)
self.assertEqual(message_one.body, received_one.body)
# Next, test python-telegram-bot -> telethon
text_two = "Test2 {}".format(random.randint(0, 10000))
message_two = TextMessage(
self.telegram_bot.address, received_one.sender, text_two
)
self.telegram_bot.send(message_two)
found = False
for received in self.telegram.receive(): # type: TextMessage
if received.body == text_two:
self.assertEqual(text_two, received.body)
self.assertEqual(message_two.body, received.body)
found = True
self.assertTrue(found)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment