async def test_help_command(testbot: EvernoteBot, text_update: str):
    update = TelegramUpdate(json.loads(text_update))
    help_cmd = HelpCommand(testbot)
    user = User.create(id=1, telegram_chat_id=update.message.chat.id)
    await help_cmd.execute(update.message)
    await asyncio.sleep(0.0001)

    assert testbot.api.sendMessage.call_count == 1
    args = testbot.api.sendMessage.call_args[0]
    assert len(args) == 2
    assert args[0] == user.telegram_chat_id
    assert 'This is bot for Evernote' in args[1]
async def test_notebook_command(testbot: EvernoteBot, text_update: str):
    update = TelegramUpdate(json.loads(text_update))
    notebook_cmd = NotebookCommand(testbot)
    user = User.create(id=update.message.user.id,
                       telegram_chat_id=update.message.chat.id,
                       evernote_access_token='',
                       current_notebook={ 'guid': 1 })
    await notebook_cmd.execute(update.message)
    await asyncio.sleep(0.0001)

    user = User.get({'id': user.id})
    assert user.state == 'select_notebook'
    assert testbot.api.sendMessage.call_count == 1
    assert testbot.update_notebooks_cache.call_count == 1
Esempio n. 3
0
    async def handle_update(self, data: dict):
        try:
            update = TelegramUpdate(data)
            await self.on_before_handle_update(update)

            if update.callback_query:
                await self.handle_callback_query(update.callback_query)
            elif update.message:
                await self.handle_message(update.message)
            # TODO: process inline query
            # TODO: process inline result
            # TODO: process callback query
        except Exception as e:
            self.logger.error('Error: {0}\nData: {1}\n\n'.format(e, data),
                              exc_info=1)
async def test_switch_mode_command(testbot: EvernoteBot, text_update: str):
    update = TelegramUpdate(json.loads(text_update))
    switch_mode_cmd = SwitchModeCommand(testbot)
    user = User.create(id=update.message.user.id,
                       telegram_chat_id=update.message.chat.id,
                       mode='one_note')
    await switch_mode_cmd.execute(update.message)
    await asyncio.sleep(0.0001)
    user = User.get({'id': user.id})
    assert user.state == 'switch_mode'
    assert testbot.api.sendMessage.call_count == 1
    args = testbot.api.sendMessage.call_args[0]
    kwargs = testbot.api.sendMessage.call_args[1]
    assert len(args) == 2
    assert args[0] == user.telegram_chat_id
    assert 'Please, select mode' == args[1]
    assert 'reply_markup' in kwargs
Esempio n. 5
0
def text_update(user):
    return TelegramUpdate({
        'update_id': 213,
        'message': {
            'message_id': 1,
            'date': datetime.datetime.now(),
            'from': {
                'id': user.id,
                'username': '******'
            },
            'chat': {
                'id': user.telegram_chat_id,
                'type': 'private'
            },
            'text': 'test text'
        },
    })
async def test_start_command(testbot: EvernoteBot, text_update: str):
    update = TelegramUpdate(json.loads(text_update))
    start_cmd = StartCommand(testbot)
    await start_cmd.execute(update.message)
    await asyncio.sleep(0.0001)
    sessions = StartSession.find()
    assert len(sessions) == 1
    assert sessions[0].id == update.message.user.id
    assert sessions[0].oauth_data['oauth_url'] == 'test_oauth_url'
    # TODO:
    # assert new_user.username == 'testuser'
    # assert new_user.first_name == 'test_first'
    # assert new_user.last_name == 'test_last'
    assert testbot.api.sendMessage.call_count == 1
    args = testbot.api.sendMessage.call_args[0]
    assert len(args) == 3
    assert 'Welcome' in args[1]
    assert testbot.api.editMessageReplyMarkup.call_count == 1