Make cryptography Dependency Optional & Refactor Some Tests (#2386)

* Make cryptography optional

* Try fixing CI

* Try some more

* Update pytest, mypy & pyupgrade, refactor test_meta, hope that things start to work

* Fix filterwarnings

* Mama mia! Here we go again!

* Add stupid debug prints

* A new hope
This commit is contained in:
Bibo-Joshi 2021-02-13 22:07:37 +01:00 committed by GitHub
parent eee8921598
commit a34f0b9bee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 196 additions and 51 deletions

View file

@ -15,13 +15,6 @@ jobs:
matrix: matrix:
python-version: [3.6, 3.7, 3.8, 3.9] python-version: [3.6, 3.7, 3.8, 3.9]
os: [ubuntu-latest, windows-latest, macos-latest] os: [ubuntu-latest, windows-latest, macos-latest]
include:
- os: ubuntu-latest
python-version: 3.7
test-build: True
- os: windows-latest
python-version: 3.7
test-build: True
fail-fast: False fail-fast: False
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
@ -40,18 +33,24 @@ jobs:
python -W ignore -m pip install -r requirements-dev.txt python -W ignore -m pip install -r requirements-dev.txt
- name: Test with pytest - name: Test with pytest
# We run three different suites here
# 1. Test just the build process
# 2. Test just test_no_passport.py without passport dependencies being installed
# 3. Test everything else
# The second one is achieved by mocking the imports, see test_no_passport.py for details
run: | run: |
pytest -v -m nocoverage pytest -v -s --cov -k test_no_passport.py
nocov_exit=$? no_passport_exit=$?
pytest -v -m "not nocoverage" --cov export TEST_NO_PASSPORT='false'
cov_exit=$? pytest -v -s --cov --cov-append
global_exit=$(( nocov_exit > cov_exit ? nocov_exit : cov_exit )) passport_exit=$?
global_exit=$(( passport_exit > no_passport_exit ? passport_exit : no_passport_exit ))
exit ${global_exit} exit ${global_exit}
env: env:
JOB_INDEX: ${{ strategy.job-index }} JOB_INDEX: ${{ strategy.job-index }}
BOTS: W3sidG9rZW4iOiAiNjk2MTg4NzMyOkFBR1Z3RUtmSEhsTmpzY3hFRE5LQXdraEdzdFpfa28xbUMwIiwgInBheW1lbnRfcHJvdmlkZX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ZpZGVyX3Rva2VuIjogIjI4NDY4NTA2MzpURVNUOk5XWmtNV1ZoWWpsallqVTUiLCAibmFtZSI6ICJQVEIgdGVzdHMgWzJdIiwgInN1cGVyX2dyb3VwX2lkIjogIi0xMDAxNDAyMjU1MDcwIiwgInVzZXJuYW1lIjogInB0Yl8yX2JvdCJ9XQ==
TEST_BUILD: ${{ matrix.test-build }} TEST_BUILD: "true"
TEST_PRE_COMMIT: ${{ matrix.test-pre-commit }} TEST_NO_PASSPORT: "true"
shell: bash --noprofile --norc {0} shell: bash --noprofile --norc {0}
- name: Submit coverage - name: Submit coverage

View file

@ -21,12 +21,12 @@ repos:
args: args:
- --rcfile=setup.cfg - --rcfile=setup.cfg
- repo: https://github.com/pre-commit/mirrors-mypy - repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.790 rev: v0.800
hooks: hooks:
- id: mypy - id: mypy
files: ^(telegram|examples)/.*\.py$ files: ^(telegram|examples)/.*\.py$
- repo: https://github.com/asottile/pyupgrade - repo: https://github.com/asottile/pyupgrade
rev: v2.7.4 rev: v2.10.0
hooks: hooks:
- id: pyupgrade - id: pyupgrade
files: ^(telegram|examples|tests)/.*\.py$ files: ^(telegram|examples|tests)/.*\.py$

View file

@ -137,6 +137,16 @@ In case you have a previously cloned local repository already, you should initia
$ git submodule update --init --recursive $ git submodule update --init --recursive
---------------------
Optional Dependencies
---------------------
PTB can be installed with optional dependencies:
* ``pip install python-telegram-bot[passport]`` installs the `cryptography <https://cryptography.io>`_ library. Use this, if you want to use Telegram Passport related functionality.
* ``pip install python-telegram-bot[ujson]`` installs the `ujson <https://pypi.org/project/ujson/>`_ library. It will then be used for JSON de- & encoding, which can bring speed up compared to the standard `json <https://docs.python.org/3/library/json.html>`_ library.
* ``pip install python-telegram-bot[socks]`` installs the `PySocks <https://pypi.org/project/PySocks/>`_ library. Use this, if you want to work behind a Socks5 server.
=============== ===============
Getting started Getting started
=============== ===============

View file

@ -137,6 +137,15 @@ Note
Installing the `.tar.gz` archive available on PyPi directly via `pip` will *not* work as expected, as `pip` does not recognize that it should use `setup-raw.py` instead of `setup.py`. Installing the `.tar.gz` archive available on PyPi directly via `pip` will *not* work as expected, as `pip` does not recognize that it should use `setup-raw.py` instead of `setup.py`.
---------------------
Optional Dependencies
---------------------
PTB can be installed with optional dependencies:
* ``pip install python-telegram-bot-raw[passport]`` installs the `cryptography <https://cryptography.io>`_ library. Use this, if you want to use Telegram Passport related functionality.
* ``pip install python-telegram-bot-raw[ujson]`` installs the `ujson <https://pypi.org/project/ujson/>`_ library. It will then be used for JSON de- & encoding, which can bring speed up compared to the standard `json <https://docs.python.org/3/library/json.html>`_ library.
=============== ===============
Getting started Getting started
=============== ===============

View file

@ -1,14 +1,15 @@
# cryptography is an optional dependency, but running the tests properly requires it
cryptography!=3.4,!=3.4.1,!=3.4.2,!=3.4.3
pre-commit pre-commit
# Make sure that the versions specified here match the pre-commit settings # Make sure that the versions specified here match the pre-commit settings
black==20.8b1 black==20.8b1
flake8==3.8.4 flake8==3.8.4
pylint==2.6.0 pylint==2.6.0
mypy==0.790 mypy==0.800
pyupgrade==2.7.4 pyupgrade==2.10.0
pytest==4.2.0 pytest==6.2.2
# Need older attrs version for pytest 4.2.0
attrs==19.1.0
flaky flaky
beautifulsoup4 beautifulsoup4

View file

@ -1,5 +1,4 @@
certifi certifi
cryptography!=3.4
# only telegram.ext: # Keep this line here; used in setup(-raw).py # only telegram.ext: # Keep this line here; used in setup(-raw).py
tornado>=5.1 tornado>=5.1
APScheduler==3.6.3 APScheduler==3.6.3

View file

@ -27,7 +27,9 @@ addopts = --no-success-flaky-report -rsxX
filterwarnings = filterwarnings =
error error
ignore::DeprecationWarning ignore::DeprecationWarning
ignore::telegram.utils.deprecate.TelegramDeprecationWarning ; Unfortunately due to https://github.com/pytest-dev/pytest/issues/8343 we can't have this here
; and instead do a trick directly in tests/conftest.py
; ignore::telegram.utils.deprecate.TelegramDeprecationWarning
[coverage:run] [coverage:run]
branch = True branch = True

View file

@ -84,7 +84,9 @@ def get_setup_kwargs(raw=False):
install_requires=requirements, install_requires=requirements,
extras_require={ extras_require={
'json': 'ujson', 'json': 'ujson',
'socks': 'PySocks' 'socks': 'PySocks',
# 3.4-3.4.3 contained some cyclical import bugs
'passport': 'cryptography!=3.4,!=3.4.1,!=3.4.2,!=3.4.3',
}, },
include_package_data=True, include_package_data=True,
classifiers=[ classifiers=[

View file

@ -41,8 +41,15 @@ try:
except ImportError: except ImportError:
import json # type: ignore[no-redef] # noqa: F723 import json # type: ignore[no-redef] # noqa: F723
from cryptography.hazmat.backends import default_backend try:
from cryptography.hazmat.primitives import serialization from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
CRYPTO_INSTALLED = True
except ImportError:
default_backend = None # type: ignore[assignment]
serialization = None # type: ignore[assignment]
CRYPTO_INSTALLED = False
from telegram import ( from telegram import (
Animation, Animation,
@ -212,6 +219,11 @@ class Bot(TelegramObject):
self.logger = logging.getLogger(__name__) self.logger = logging.getLogger(__name__)
if private_key: if private_key:
if not CRYPTO_INSTALLED:
raise RuntimeError(
'To use Telegram Passports, PTB must be installed via `pip install '
'python-telegram-bot[passport]`.'
)
self.private_key = serialization.load_pem_private_key( self.private_key = serialization.load_pem_private_key(
private_key, password=private_key_password, backend=default_backend() private_key, password=private_key_password, backend=default_backend()
) )

View file

@ -25,12 +25,21 @@ except ImportError:
from base64 import b64decode from base64 import b64decode
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union, no_type_check from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union, no_type_check
from cryptography.hazmat.backends import default_backend try:
from cryptography.hazmat.primitives.asymmetric.padding import MGF1, OAEP from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import Cipher from cryptography.hazmat.primitives.asymmetric.padding import MGF1, OAEP
from cryptography.hazmat.primitives.ciphers.algorithms import AES from cryptography.hazmat.primitives.ciphers import Cipher
from cryptography.hazmat.primitives.ciphers.modes import CBC from cryptography.hazmat.primitives.ciphers.algorithms import AES
from cryptography.hazmat.primitives.hashes import SHA1, SHA256, SHA512, Hash from cryptography.hazmat.primitives.ciphers.modes import CBC
from cryptography.hazmat.primitives.hashes import SHA1, SHA256, SHA512, Hash
CRYPTO_INSTALLED = True
except ImportError:
default_backend = None
MGF1, OAEP, Cipher, AES, CBC = (None, None, None, None, None) # type: ignore[misc]
SHA1, SHA256, SHA512, Hash = (None, None, None, None) # type: ignore[misc]
CRYPTO_INSTALLED = False
from telegram import TelegramError, TelegramObject from telegram import TelegramError, TelegramObject
from telegram.utils.types import JSONDict from telegram.utils.types import JSONDict
@ -74,6 +83,11 @@ def decrypt(secret, hash, data):
:obj:`bytes`: The decrypted data as bytes. :obj:`bytes`: The decrypted data as bytes.
""" """
if not CRYPTO_INSTALLED:
raise RuntimeError(
'To use Telegram Passports, PTB must be installed via `pip install '
'python-telegram-bot[passport]`.'
)
# Make a SHA512 hash of secret + update # Make a SHA512 hash of secret + update
digest = Hash(SHA512(), backend=default_backend()) digest = Hash(SHA512(), backend=default_backend())
digest.update(secret + hash) digest.update(secret + hash)
@ -153,6 +167,11 @@ class EncryptedCredentials(TelegramObject):
private/public key but can also suggest malformed/tampered data. private/public key but can also suggest malformed/tampered data.
""" """
if self._decrypted_secret is None: if self._decrypted_secret is None:
if not CRYPTO_INSTALLED:
raise RuntimeError(
'To use Telegram Passports, PTB must be installed via `pip install '
'python-telegram-bot[passport]`.'
)
# Try decrypting according to step 1 at # Try decrypting according to step 1 at
# https://core.telegram.org/passport#decrypting-data # https://core.telegram.org/passport#decrypting-data
# We make sure to base64 decode the secret first. # We make sure to base64 decode the secret first.

View file

@ -46,6 +46,14 @@ from telegram.ext import Dispatcher, JobQueue, Updater, MessageFilter, Defaults,
from telegram.error import BadRequest from telegram.error import BadRequest
from tests.bots import get_bot from tests.bots import get_bot
# This is here instead of in setup.cfg due to https://github.com/pytest-dev/pytest/issues/8343
def pytest_runtestloop(session):
session.add_marker(
pytest.mark.filterwarnings('ignore::telegram.utils.deprecate.TelegramDeprecationWarning')
)
GITHUB_ACTION = os.getenv('GITHUB_ACTION', False) GITHUB_ACTION = os.getenv('GITHUB_ACTION', False)
if GITHUB_ACTION: if GITHUB_ACTION:
@ -56,6 +64,14 @@ if GITHUB_ACTION:
PRIVATE_KEY = b"-----BEGIN RSA PRIVATE KEY-----\r\nMIIEowIBAAKCAQEA0AvEbNaOnfIL3GjB8VI4M5IaWe+GcK8eSPHkLkXREIsaddum\r\nwPBm/+w8lFYdnY+O06OEJrsaDtwGdU//8cbGJ/H/9cJH3dh0tNbfszP7nTrQD+88\r\nydlcYHzClaG8G+oTe9uEZSVdDXj5IUqR0y6rDXXb9tC9l+oSz+ShYg6+C4grAb3E\r\nSTv5khZ9Zsi/JEPWStqNdpoNuRh7qEYc3t4B/a5BH7bsQENyJSc8AWrfv+drPAEe\r\njQ8xm1ygzWvJp8yZPwOIYuL+obtANcoVT2G2150Wy6qLC0bD88Bm40GqLbSazueC\r\nRHZRug0B9rMUKvKc4FhG4AlNzBCaKgIcCWEqKwIDAQABAoIBACcIjin9d3Sa3S7V\r\nWM32JyVF3DvTfN3XfU8iUzV7U+ZOswA53eeFM04A/Ly4C4ZsUNfUbg72O8Vd8rg/\r\n8j1ilfsYpHVvphwxaHQlfIMa1bKCPlc/A6C7b2GLBtccKTbzjARJA2YWxIaqk9Nz\r\nMjj1IJK98i80qt29xRnMQ5sqOO3gn2SxTErvNchtBiwOH8NirqERXig8VCY6fr3n\r\nz7ZImPU3G/4qpD0+9ULrt9x/VkjqVvNdK1l7CyAuve3D7ha3jPMfVHFtVH5gqbyp\r\nKotyIHAyD+Ex3FQ1JV+H7DkP0cPctQiss7OiO9Zd9C1G2OrfQz9el7ewAPqOmZtC\r\nKjB3hUECgYEA/4MfKa1cvaCqzd3yUprp1JhvssVkhM1HyucIxB5xmBcVLX2/Kdhn\r\nhiDApZXARK0O9IRpFF6QVeMEX7TzFwB6dfkyIePsGxputA5SPbtBlHOvjZa8omMl\r\nEYfNa8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UUyNrg4DBAwn\r\nqE6E3ebHXt/2WmqIbUD653ECgYBQCC8EAQNX3AFegPd1GGxU33Lz4tchJ4kMCNU0\r\nN2NZh9VCr3nTYjdTbxsXU8YP44CCKFG2/zAO4kymyiaFAWEOn5P7irGF/JExrjt4\r\nibGy5lFLEq/HiPtBjhgsl1O0nXlwUFzd7OLghXc+8CPUJaz5w42unqT3PBJa40c3\r\nQcIPdQKBgBnSb7BcDAAQ/Qx9juo/RKpvhyeqlnp0GzPSQjvtWi9dQRIu9Pe7luHc\r\nm1Img1EO1OyE3dis/rLaDsAa2AKu1Yx6h85EmNjavBqP9wqmFa0NIQQH8fvzKY3/\r\nP8IHY6009aoamLqYaexvrkHVq7fFKiI6k8myMJ6qblVNFv14+KXU\r\n-----END RSA PRIVATE KEY-----" # noqa: E501
def env_var_2_bool(env_var: object) -> bool:
if isinstance(env_var, bool):
return env_var
if not isinstance(env_var, str):
return False
return env_var.lower().strip() == 'true'
@pytest.fixture(scope='session') @pytest.fixture(scope='session')
def bot_info(): def bot_info():
return get_bot() return get_bot()

View file

@ -354,7 +354,7 @@ class TestBot:
assert message_quiz.poll.explanation_entities == explanation_entities assert message_quiz.poll.explanation_entities == explanation_entities
@flaky(3, 1) @flaky(3, 1)
@pytest.mark.timeout(10) @pytest.mark.timeout(15)
@pytest.mark.parametrize(['open_period', 'close_date'], [(5, None), (None, True)]) @pytest.mark.parametrize(['open_period', 'close_date'], [(5, None), (None, True)])
def test_send_open_period(self, bot, super_group_id, open_period, close_date): def test_send_open_period(self, bot, super_group_id, open_period, close_date):
question = 'Is this a test?' question = 'Is this a test?'

View file

@ -32,7 +32,6 @@ class TestConstants:
with pytest.raises( with pytest.raises(
BadRequest, BadRequest,
match='Message is too long', match='Message is too long',
message='MAX_MESSAGE_LENGTH is no longer valid',
): ):
bot.send_message(chat_id=chat_id, text='a' * (constants.MAX_MESSAGE_LENGTH + 1)) bot.send_message(chat_id=chat_id, text='a' * (constants.MAX_MESSAGE_LENGTH + 1))
@ -48,7 +47,6 @@ class TestConstants:
with pytest.raises( with pytest.raises(
BadRequest, BadRequest,
match="Media_caption_too_long", match="Media_caption_too_long",
message='MAX_CAPTION_LENGTH is no longer valid',
): ):
with open('tests/data/telegram.png', 'rb') as f: with open('tests/data/telegram.png', 'rb') as f:
bot.send_photo(photo=f, caption=bad_caption, chat_id=chat_id) bot.send_photo(photo=f, caption=bad_caption, chat_id=chat_id)

View file

@ -20,26 +20,18 @@ import os
import pytest import pytest
from tests.conftest import env_var_2_bool
def call_pre_commit_hook(hook_id):
__tracebackhide__ = True
return os.system(' '.join(['pre-commit', 'run', hook_id, '--all-files'])) # pragma: no cover
@pytest.mark.nocoverage @pytest.mark.skipif(
@pytest.mark.parametrize('hook_id', ('black', 'flake8', 'pylint', 'mypy')) not env_var_2_bool(os.getenv('TEST_BUILD', False)), reason='TEST_BUILD not enabled'
@pytest.mark.skipif(not os.getenv('TEST_PRE_COMMIT', False), reason='TEST_PRE_COMMIT not enabled') )
def test_pre_commit_hook(hook_id):
assert call_pre_commit_hook(hook_id) == 0 # pragma: no cover
@pytest.mark.nocoverage
@pytest.mark.skipif(not os.getenv('TEST_BUILD', False), reason='TEST_BUILD not enabled')
def test_build(): def test_build():
assert os.system('python setup.py bdist_dumb') == 0 # pragma: no cover assert os.system('python setup.py bdist_dumb') == 0 # pragma: no cover
@pytest.mark.nocoverage @pytest.mark.skipif(
@pytest.mark.skipif(not os.getenv('TEST_BUILD', False), reason='TEST_BUILD not enabled') not env_var_2_bool(os.getenv('TEST_BUILD', False)), reason='TEST_BUILD not enabled'
)
def test_build_raw(): def test_build_raw():
assert os.system('python setup-raw.py bdist_dumb') == 0 # pragma: no cover assert os.system('python setup-raw.py bdist_dumb') == 0 # pragma: no cover

82
tests/test_no_passport.py Normal file
View file

@ -0,0 +1,82 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2021
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""
This file tests the case that PTB was installed *without* the optional dependency `passport`.
Currently this only means that cryptography is not installed.
Because imports in pytest are intricate, we just run
pytest -k test_no_passport.py
with the TEST_NO_PASSPORT environment variable set in addition to the regular test suite.
Because actually uninstalling the optional dependencies would lead to errors in the test suite we
just mock the import to raise the expected exception.
Note that a fixture that just does this for every test that needs it is a nice idea, but for some
reason makes test_updater.py hang indefinitely on GitHub Actions (at least when Hinrich tried that)
"""
import os
from importlib import reload
from unittest import mock
import pytest
from telegram import bot
from telegram.passport import credentials
from tests.conftest import env_var_2_bool
TEST_NO_PASSPORT = env_var_2_bool(os.getenv('TEST_NO_PASSPORT', False))
if TEST_NO_PASSPORT:
orig_import = __import__
def import_mock(module_name, *args, **kwargs):
if module_name.startswith('cryptography'):
raise ModuleNotFoundError('We are testing without cryptography here')
return orig_import(module_name, *args, **kwargs)
with mock.patch('builtins.__import__', side_effect=import_mock):
reload(bot)
reload(credentials)
class TestNoPassport:
"""
The monkeypatches simulate cryptography not being installed even when TEST_NO_PASSPORT is
False, though that doesn't test the actual imports
"""
def test_bot_init(self, bot_info, monkeypatch):
if not TEST_NO_PASSPORT:
monkeypatch.setattr(bot, 'CRYPTO_INSTALLED', False)
with pytest.raises(RuntimeError, match='passport'):
bot.Bot(bot_info['token'], private_key=1, private_key_password=2)
def test_credentials_decrypt(self, monkeypatch):
if not TEST_NO_PASSPORT:
monkeypatch.setattr(credentials, 'CRYPTO_INSTALLED', False)
with pytest.raises(RuntimeError, match='passport'):
credentials.decrypt(1, 1, 1)
def test_encrypted_credentials_decrypted_secret(self, monkeypatch):
if not TEST_NO_PASSPORT:
monkeypatch.setattr(credentials, 'CRYPTO_INSTALLED', False)
ec = credentials.EncryptedCredentials('data', 'hash', 'secret')
with pytest.raises(RuntimeError, match='passport'):
ec.decrypted_secret

View file

@ -25,6 +25,7 @@ from bs4 import BeautifulSoup
from telegram.vendor.ptb_urllib3 import urllib3 from telegram.vendor.ptb_urllib3 import urllib3
import telegram import telegram
from tests.conftest import env_var_2_bool
IGNORED_OBJECTS = ('ResponseParameters', 'CallbackGame') IGNORED_OBJECTS = ('ResponseParameters', 'CallbackGame')
IGNORED_PARAMETERS = { IGNORED_PARAMETERS = {
@ -170,6 +171,8 @@ for thing in soup.select('h4 > a.anchor'):
@pytest.mark.parametrize(('method', 'data'), argvalues=argvalues, ids=names) @pytest.mark.parametrize(('method', 'data'), argvalues=argvalues, ids=names)
@pytest.mark.skipif(os.getenv('TEST_OFFICIAL') != 'true', reason='test_official is not enabled') @pytest.mark.skipif(
not env_var_2_bool(os.getenv('TEST_OFFICIAL')), reason='test_official is not enabled'
)
def test_official(method, data): def test_official(method, data):
method(data) method(data)

View file

@ -111,6 +111,7 @@ class TestUpdater:
self.err_handler_called.clear() self.err_handler_called.clear()
self.err_handler_called.wait() self.err_handler_called.wait()
@pytest.mark.filterwarnings('ignore:.*:pytest.PytestUnhandledThreadExceptionWarning')
def test_get_updates_bailout_err(self, monkeypatch, updater, caplog): def test_get_updates_bailout_err(self, monkeypatch, updater, caplog):
error = InvalidToken() error = InvalidToken()