From 89567ccd11d6415ff396a7aedddb451fda3d222a Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 27 May 2019 21:06:29 +0200 Subject: [PATCH] Create a new module for tests and write their setup code --- readthedocs/developing/tests.rst | 85 ++++++++++++++++++++++++++++++++ readthedocs/index.rst | 1 + setup.py | 3 +- tests/client/test_base.py | 10 ++++ tests/conftest.py | 36 ++++++++++++++ 5 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 readthedocs/developing/tests.rst create mode 100644 tests/client/test_base.py create mode 100644 tests/conftest.py diff --git a/readthedocs/developing/tests.rst b/readthedocs/developing/tests.rst new file mode 100644 index 00000000..9e0a0307 --- /dev/null +++ b/readthedocs/developing/tests.rst @@ -0,0 +1,85 @@ +===== +Tests +===== + +In order to test Telegram, the library makes use of the public test servers. + +To run the tests, you need to provide the test runner with two environment +variables, containing the string-sessions for both a user account and a bot +account. + +These accounts will talk to each other in order to run all the tests involving +high level functions. + + +Dependencies +============ + +.. code-block:: python + + pip install pytest pytest-asyncio --user + + +Generating Sessions +=================== + +First you must login with your phone number to the test servers. It is not +possible to use a public phone number because anyone could interrupt the +tests while running, and they are not allowed to create bots. + +With the following code, you will authorize a new user session in the public +test servers, and also create a bot with a randomly-generated username. Once +both are created, it will print a string session of both. + +.. code-block:: python + + import random + import string + + from telethon import TelegramClient + from telethon.sessions import StringSession + + # From https://my.telegram.org + API = 123, 'abc...' + DC = 2, '149.154.167.40', 443 + PHONE = '+34...' + + # Generate a random username for the bot account (24 chars long is good) + bot_username = ''.join(random.choice(string.ascii_lowercase) for _ in range(21)) + 'bot' + + client = TelegramClient(StringSession(), *API) + client.session.set_dc(*DC) + + # You will receive a SMS with the code + with client.start(phone=PHONE): + # Print your client session as a variable + print('CLIENT_SESSION="{}"'.format(client.session.save())) + + # Now, create a new bot to also get a bot session + with client.conversation('BotFather') as conv: + conv.send_message('/newbot') + conv.get_response() + conv.send_message('Telethon Test Bot') + conv.get_response() + conv.send_message(bot_username) + token = conv.get_response().get_entities_text(types.MessageEntityCode)[0][1] + + bot = TelegramClient(StringSession(), *API) + bot.session.set_dc(*DC) + with bot.start(bot_token=token): + print('BOT_SESSION="{}"'.format(bot.session.save())) + + +Running the Tests +================= + +Put the generated environment variables in your environment, and then run: + +.. code-block:: sh + + pytest + +On the root directory of the project. If everything went well, you should be +greeted with all tests passing. If they don't pass, please `open an issue`_. + +.. _open an issue: https://github.com/LonamiWebs/Telethon/issues diff --git a/readthedocs/index.rst b/readthedocs/index.rst index 9f28deda..1b879203 100644 --- a/readthedocs/index.rst +++ b/readthedocs/index.rst @@ -92,6 +92,7 @@ You can also use the menu on the left to quickly skip over sections. developing/philosophy.rst developing/test-servers.rst developing/project-structure.rst + developing/tests.rst developing/coding-style.rst developing/understanding-the-type-language.rst developing/tips-for-porting-the-project.rst diff --git a/setup.py b/setup.py index 8e2c30b3..c4faed23 100755 --- a/setup.py +++ b/setup.py @@ -218,7 +218,8 @@ def main(): install_requires=['pyaes', 'rsa'], extras_require={ 'cryptg': ['cryptg'] - } + }, + tests_require=['pytest', 'pytest-asyncio'] ) diff --git a/tests/client/test_base.py b/tests/client/test_base.py new file mode 100644 index 00000000..fa86ceed --- /dev/null +++ b/tests/client/test_base.py @@ -0,0 +1,10 @@ +import pytest + +from telethon import types + +pytestmark = pytest.mark.asyncio + + +async def test_get_me(client): + me = await client.get_me() + assert isinstance(me, types.User) diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..b2457d09 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,36 @@ +import pytest + + +pytestmark = pytest.mark.asyncio + + +@pytest.fixture +async def bot(request): + import os + from telethon import TelegramClient + from telethon.sessions import StringSession + cl = TelegramClient(StringSession(os.environ['BOT_SESSION']), 1, '-') + + def fin(): + cl.disconnect() # note: this runs the loop + + request.addfinalizer(fin) + + await cl.start() + return cl + + +@pytest.fixture +async def client(request): + import os + from telethon import TelegramClient + from telethon.sessions import StringSession + cl = TelegramClient(StringSession(os.environ['CLIENT_SESSION']), 1, '-') + + def fin(): + cl.disconnect() # note: this runs the loop + + request.addfinalizer(fin) + + await cl.start() + return cl