def test_build_message_with_non_message_data(): engine = TelegramEngine(token='') data = {'update_id': 123456, 'message_updated': {}} message = engine.build_message(data) assert message is None
def test_build_message(): engine = TelegramEngine(token='') data = { 'update_id': 123456, 'message': { 'message_id': 1, 'from': { 'id': 321, 'is_bot': False, 'first_name': 'Andrew', 'last_name': 'Martin', 'username': '******', 'language_code': 'en-US' }, 'chat': { 'id': 42, 'first_name': 'Andrew', 'last_name': 'Martin', 'username': '******', 'type': 'private' }, 'date': 1506805222, 'text': 'ping' } } message = engine.build_message(data) assert type(message) == Message assert message.text == 'ping'
def test_telegram_engine_configure_not_ok(mocked_debug): '''Make sure logger.debug is not called if the response from webhook is negative.''' engine = TelegramEngine(token='') engine.session = '' engine.configure() assert not mocked_debug.called
def test_telegram_engine_configure(mocked_debug): '''Make sure logger.debug is called if the response from webhook is positive.''' def mocked_json(): return {'ok': True} def mocked_delete_webhook(): return type('response', (), {'json': mocked_json}) engine = TelegramEngine(token='') engine.api.delete_webhook = mocked_delete_webhook engine.session = '' engine.configure() assert mocked_debug.called
async def test_engine_missing_configuration_method(token, session): engine = TelegramEngine(token=token, session=session, mode='not-defined-mode') with pytest.raises(ImproperlyConfigured): await engine.configure()
async def test_engine_configure_polling(token, mock_session): session = mock_session({'ok': True}) engine = TelegramEngine(token=token, session=session) await engine.configure() url = 'https://api.telegram.org/bot{}/deleteWebhook'.format(token) engine.session.post.assert_called_once_with(url, json={}) assert engine.tasks == [engine.polling]
async def test_engine_configure_webhook(server, token, mock_session): with mock.patch('bottery.platform.telegram.settings') as mocked_settings: mocked_settings.HOSTNAME = 'localhost' session = mock_session({'ok': True}) engine = TelegramEngine(token=token, session=session, server=server, mode='webhook') await engine.configure() url = 'https://api.telegram.org/bot{}/setWebhook'.format(engine.token) payload = {'url': 'localhost'} assert engine.tasks == [] assert engine.server.router.add_post.called engine.session.post.assert_called_once_with(url, json=payload)
def test_engine_set_mode(token, session): engine = TelegramEngine(token=token, session=session, mode='webhook') assert engine.mode == 'webhook'
def test_engine_default_mode(token, session): engine = TelegramEngine(token=token, session=session) assert engine.mode == 'polling'
def test_telegram_engine_define_new_mode(): '''Make sure TelegramEngine supports new modes.''' engine = TelegramEngine(token='', mode='else') assert engine.mode == 'else'
def test_telegram_engine_tasks(): engine = TelegramEngine(token='') engine.polling = 'something' assert engine.tasks() == ['something']
def test_telegram_engine_tasks(): engine = TelegramEngine(token='') assert engine.tasks == [engine.polling]