-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
66 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#-e git+https://github.com/fopina/tgbotplug/@v1.2.3#egg=tgbotplug | ||
|
||
tgbotplug==1.2.7 | ||
tgbotplug==1.2.8 | ||
bottle==0.12.8 | ||
psycopg2==2.6.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,41 @@ | ||
# coding=utf-8 | ||
from tgbot import plugintest | ||
from tgbot.botapi import Update | ||
from tgbot import plugintest, pluginbase | ||
from tgbot.botapi import ReplyKeyboardMarkup | ||
from plugins.intro import IntroPlugin | ||
|
||
|
||
class IntroPluginTest(plugintest.PluginTestCase): | ||
class PluginTest(plugintest.PluginTestCase): | ||
def setUp(self): | ||
self.bot = self.fake_bot('', plugins=[IntroPlugin()]) | ||
|
||
def test_start_hello(self): | ||
self.bot = self.fake_bot( | ||
'', | ||
plugins=[IntroPlugin()] | ||
) | ||
self.received_id = 1 | ||
self.receive_message('/start') | ||
self.assertReplied(self.bot, 'Hello!') | ||
self.assertReplied('Hello!') | ||
|
||
def test_start_world_with_menu(self): | ||
def menu(chat): | ||
return ReplyKeyboardMarkup.create(keyboard=[['One']]) | ||
|
||
def test_start_world(self): | ||
self.bot = self.fake_bot( | ||
'', | ||
plugins=[IntroPlugin(intro_text='World!')] | ||
plugins=[IntroPlugin(intro_text='World!', start_menu_builder=menu)] | ||
) | ||
self.received_id = 1 | ||
self.receive_message('/start') | ||
self.assertReplied(self.bot, 'World!') | ||
|
||
def receive_message(self, text, sender=None, chat=None): | ||
if sender is None: | ||
sender = { | ||
'id': 1, | ||
'first_name': 'John', | ||
'last_name': 'Doe', | ||
} | ||
|
||
if chat is None: | ||
chat = sender | ||
|
||
self.bot.process_update( | ||
Update.from_dict({ | ||
'update_id': self.received_id, | ||
'message': { | ||
'message_id': self.received_id, | ||
'text': text, | ||
'chat': chat, | ||
'from': sender, | ||
} | ||
}) | ||
) | ||
reply = self.pop_reply() | ||
self.assertEqual(reply[1]['text'], 'World!') | ||
self.assertEqual(reply[1]['reply_markup'], {'keyboard': [['One']]}) | ||
|
||
def test_help(self): | ||
class TestPlugin(pluginbase.TGPluginBase): | ||
def list_commands(self): | ||
return ( | ||
pluginbase.TGCommandBase('shoot', None, 'method None breaks for sure'), | ||
) | ||
|
||
self.bot = self.fake_bot('', plugins=[IntroPlugin(), TestPlugin()]) | ||
self.receive_message('/help') | ||
self.assertReplied(u'''\ | ||
You can control me by sending these commands: | ||
self.received_id += 1 | ||
/shoot - method None breaks for sure | ||
''') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,16 @@ | ||
# coding=utf-8 | ||
|
||
from tgbot import plugintest, webserver, botapi | ||
from tgbot import plugintest, webserver | ||
import webtest | ||
import priberambot | ||
|
||
|
||
class FakeTelegramBotRPCRequest(botapi.TelegramBotRPCRequest): | ||
# TODO - improve this and add it to tgbot.plugintest | ||
QUEUE = [] | ||
|
||
def _async_call(self): | ||
FakeTelegramBotRPCRequest.QUEUE.append((self.api_method, self.params)) | ||
if self.api_method == 'getMe': | ||
result = { | ||
'id': 9999999, | ||
'first_name': 'Test', | ||
'last_name': 'Bot', | ||
'username': 'test_bot' | ||
} | ||
else: | ||
result = {} | ||
|
||
if self.on_result is None: | ||
self.result = result | ||
else: | ||
self.result = self.on_result(result) | ||
|
||
if self.on_success is not None: | ||
self.on_success(self.result) | ||
|
||
|
||
class WebTest(plugintest.PluginTestCase): | ||
def setUp(self): | ||
botapi.TelegramBotRPCRequest = FakeTelegramBotRPCRequest | ||
FakeTelegramBotRPCRequest.QUEUE = [] | ||
self.bot = priberambot.setup('sqlite:///:memory:', '123') | ||
self.bot = self.prepare_bot(priberambot.setup('sqlite:///:memory:', '123')) | ||
self.bot.setup_db() | ||
self.webapp = webtest.TestApp(webserver.wsgi_app([self.bot])) | ||
self.received_id = 1 | ||
|
||
def test_ping(self): | ||
self.assertEqual(self.webapp.get('/ping/').text, '<b>Pong!</b>') | ||
|
||
def test_update_invalid_token(self): | ||
with self.assertRaisesRegexp(webtest.app.AppError, 'Bad response: 404 Not Found'): | ||
self.webapp.post_json('/update/invalid', params=self.build_update('hello')) | ||
|
||
def test_web(self): | ||
self.assertEqual(len(FakeTelegramBotRPCRequest.QUEUE), 0) | ||
self.webapp.post_json('/update/123', params=self.build_update(u'hello')) | ||
self.assertTrue(len(FakeTelegramBotRPCRequest.QUEUE)) | ||
self.assertEqual(FakeTelegramBotRPCRequest.QUEUE[-1][0], 'sendMessage') | ||
self.assertEqual(FakeTelegramBotRPCRequest.QUEUE[-1][1]['text'], u'Palavra não encontrada') | ||
|
||
def build_update(self, text, sender=None, chat=None, reply_to_message_id=None): | ||
if sender is None: | ||
sender = { | ||
'id': 1, | ||
'first_name': 'John', | ||
'last_name': 'Doe', | ||
} | ||
|
||
if chat is None: | ||
chat = {'type': 'private'} | ||
chat.update(sender) | ||
|
||
reply_to_message = None | ||
|
||
if reply_to_message_id is not None: | ||
reply_to_message = { | ||
'message_id': reply_to_message_id, | ||
'chat': chat, | ||
} | ||
|
||
update = { | ||
'update_id': self.received_id, | ||
'message': { | ||
'message_id': self.received_id, | ||
'text': text, | ||
'chat': chat, | ||
'from': sender, | ||
'reply_to_message': reply_to_message, | ||
} | ||
} | ||
|
||
self.received_id += 1 | ||
|
||
return update | ||
self.webapp.post_json('/update/123', params=self.build_message(u'Supercalifragilisticexpialidocious')) | ||
self.assertReplied(u'Palavra não encontrada') |