Merge remote-tracking branch 'origin/master' into bot2.1

Conflicts:
	telegram/bot.py
	tests/test_bot.py
This commit is contained in:
Leandro Toledo 2016-05-26 16:15:50 -03:00
commit 9a13de4a96
4 changed files with 22 additions and 32 deletions

View file

@ -97,7 +97,7 @@ def error(bot, update, error):
updater = Updater("TOKEN")
# The command
updater.dispatcher.addHandler(CommandHandler('set', set_value))
updater.dispatcher.add_handler(CommandHandler('set', set_value))
# The answer
updater.dispatcher.add_handler(MessageHandler([Filters.text], entered_value))
# The confirmation

View file

@ -24,8 +24,8 @@ import logging
from telegram import (User, Message, Update, Chat, ChatMember, UserProfilePhotos, File,
ReplyMarkup, TelegramObject, NullHandler)
from telegram.error import InvalidToken
from telegram.utils import request
from telegram.utils.validate import validate_token
logging.getLogger(__name__).addHandler(NullHandler())
@ -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)
@ -64,6 +64,18 @@ class Bot(TelegramObject):
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)

View file

@ -1,29 +0,0 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2016
# 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 module contains functions to validate function arguments"""
from telegram.error import InvalidToken
def validate_token(token):
"""a very basic validation on token"""
left, sep, _right = token.partition(':')
if (not sep) or (not left.isdigit()) or (len(left) < 3):
raise InvalidToken()
return token

View file

@ -188,6 +188,13 @@ class BotTest(BaseTest, unittest.TestCase):
def testInvalidToken3(self):
self._test_invalid_token('12:')
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'):
bot = telegram.Bot('1234:abcd1234')