X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

from telegram import (User, Message, Update, UserProfilePhotos, File, ReplyMarkup, TelegramObject,
NullHandler)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flake8 failed because NullHandler is unused.

from telegram.error import InvalidToken
from telegram.utils import request
from telegram.utils.validate import validate_token

logging.getLogger(__name__).addHandler(NullHandler())
Copy link
Copy Markdown
Member

@leandrotoledo leandrotoledo May 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIR not having a NullHandler defined prints "No handler found" #21
https://docs.python.org/2/howto/logging.html#library-config


Expand All @@ -48,7 +48,7 @@ class Bot(TelegramObject):
"""

def __init__(self, token, base_url=None, base_file_url=None):
self.token = validate_token(token)
self.token = self._validate_token(token)

if not base_url:
self.base_url = 'https://api.telegram.org/bot{0}'.format(self.token)
Expand All @@ -64,6 +64,18 @@ def __init__(self, token, base_url=None, base_file_url=None):

self.logger = logging.getLogger(__name__)

@staticmethod
def _validate_token(token):
"""a very basic validation on token"""
if any(x.isspace() for x in token):
raise InvalidToken()

left, sep, _right = token.partition(':')
if (not sep) or (not left.isdigit()) or (len(left) < 3):
raise InvalidToken()

return token

def info(func):

@functools.wraps(func)
Expand Down
29 changes: 0 additions & 29 deletions telegram/utils/validate.py

This file was deleted.

8 changes: 6 additions & 2 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,12 @@ def testInvalidToken2(self):
def testInvalidToken3(self):
self._test_invalid_token('12:')

# def testInvalidToken4(self):
# self._test_invalid_token('1234:abcd1234\n')
def testInvalidToken4(self):
# white spaces are invalid
self._test_invalid_token('1234:abcd1234\n')
self._test_invalid_token(' 1234:abcd1234')
self._test_invalid_token(' 1234:abcd1234\r')
self._test_invalid_token('1234:abcd 1234')

def testUnauthToken(self):
with self.assertRaisesRegexp(telegram.error.Unauthorized, 'Unauthorized'):
Expand Down
X Tutup