mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Merge pull request #240 from aiogram/#239-check_token-bug-report
#239 check_token fix
This commit is contained in:
commit
e4bf120ec8
2 changed files with 28 additions and 8 deletions
|
|
@ -24,11 +24,17 @@ 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 (len(left) < 3):
|
||||
if (not sep) or (not left.isdigit()) or (not right):
|
||||
raise exceptions.ValidationError('Token is invalid!')
|
||||
|
||||
return True
|
||||
|
|
|
|||
|
|
@ -1,18 +1,32 @@
|
|||
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
|
||||
None, # is None
|
||||
12345678, # is digit
|
||||
{}, # is dict
|
||||
[], # is dict
|
||||
]
|
||||
|
||||
|
||||
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue