From f5d008938f9ecfd55f694293f565cc9962a15ba4 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sat, 23 Nov 2019 11:46:50 +0300 Subject: [PATCH 1/3] #239 added check for right part of token exists; removed check for left part length --- aiogram/bot/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiogram/bot/api.py b/aiogram/bot/api.py index 675626ac..9dea86ea 100644 --- a/aiogram/bot/api.py +++ b/aiogram/bot/api.py @@ -28,7 +28,7 @@ def check_token(token: str) -> bool: raise exceptions.ValidationError('Token is invalid!') left, sep, right = token.partition(':') - if (not sep) or (not left.isdigit()) or (len(left) < 3): + if (not sep) or (not left.isdigit()) or (not right): raise exceptions.ValidationError('Token is invalid!') return True From 89b0754b33e5d211bc11973ed56a1e9719e01ecd Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sat, 23 Nov 2019 12:02:30 +0300 Subject: [PATCH 2/3] #239 added test cases for check_token --- tests/test_bot/test_api.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tests/test_bot/test_api.py b/tests/test_bot/test_api.py index c5193bcc..0543a11f 100644 --- a/tests/test_bot/test_api.py +++ b/tests/test_bot/test_api.py @@ -1,18 +1,28 @@ import pytest -from aiogram.bot.api import check_token +from aiogram.bot.api import check_token from aiogram.utils.exceptions import ValidationError - VALID_TOKEN = '123456789:AABBCCDDEEFFaabbccddeeff-1234567890' -INVALID_TOKEN = '123456789:AABBCCDDEEFFaabbccddeeff 123456789' # Space in token and wrong length +INVALID_TOKENS = [ + '123456789:AABBCCDDEEFFaabbccddeeff 123456789', # space is exists + 'ABC:AABBCCDDEEFFaabbccddeeff123456789', # left part is not digit + ':AABBCCDDEEFFaabbccddeeff123456789', # there is no left part + '123456789:', # there is no right part + 'ABC AABBCCDDEEFFaabbccddeeff123456789', # there is no ':' separator +] -class Test_check_token: +@pytest.fixture(params=INVALID_TOKENS, name='invalid_token') +def invalid_token_fixture(request): + return request.param + + +class TestCheckToken: def test_valid(self): assert check_token(VALID_TOKEN) is True - def test_invalid_token(self): + def test_invalid_token(self, invalid_token): with pytest.raises(ValidationError): - check_token(INVALID_TOKEN) + check_token(invalid_token) From 4523a1cab397b3cdf36c853c43f9c70119e952a0 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sat, 23 Nov 2019 12:45:47 +0300 Subject: [PATCH 3/3] #239 added token type validation --- aiogram/bot/api.py | 8 +++++++- tests/test_bot/test_api.py | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/aiogram/bot/api.py b/aiogram/bot/api.py index 9dea86ea..9589c3e5 100644 --- a/aiogram/bot/api.py +++ b/aiogram/bot/api.py @@ -24,8 +24,14 @@ def check_token(token: str) -> bool: :param token: :return: """ + if not isinstance(token, str): + message = (f"Token is invalid! " + f"It must be 'str' type instead of {type(token)} type.") + raise exceptions.ValidationError(message) + if any(x.isspace() for x in token): - raise exceptions.ValidationError('Token is invalid!') + message = "Token is invalid! It can't contains spaces." + raise exceptions.ValidationError(message) left, sep, right = token.partition(':') if (not sep) or (not left.isdigit()) or (not right): diff --git a/tests/test_bot/test_api.py b/tests/test_bot/test_api.py index 0543a11f..29418169 100644 --- a/tests/test_bot/test_api.py +++ b/tests/test_bot/test_api.py @@ -10,6 +10,10 @@ INVALID_TOKENS = [ ':AABBCCDDEEFFaabbccddeeff123456789', # there is no left part '123456789:', # there is no right part 'ABC AABBCCDDEEFFaabbccddeeff123456789', # there is no ':' separator + None, # is None + 12345678, # is digit + {}, # is dict + [], # is dict ]