예제 #1
0
async def test_process_text(text_update):
    update = json.loads(text_update)
    User.create(id=425606,
                telegram_chat_id=2,
                mode='one_note',
                evernote_access_token='token',
                current_notebook={
                    'guid': '000',
                    'name': 'test_notebook'
                },
                places={'000': 'note_guid'})
    TelegramUpdate.create(user_id=425606,
                          request_type='text',
                          status_message_id=5,
                          message=update['message'])
    dealer = EvernoteDealer()
    mock_note_provider = AsyncMock()
    note = Types.Note()
    mock_note_provider.get_note = AsyncMock(return_value=note)
    mock_telegram_api = AsyncMock()
    dealer._telegram_api = mock_telegram_api
    for k, handlers in dealer._EvernoteDealer__handlers.items():
        for handler in handlers:
            handler._note_provider = mock_note_provider
    updates = dealer.fetch_updates()
    for user_id, update_list in updates.items():
        user = User.get({'id': user_id})
        await dealer.process_user_updates(user, update_list)

    assert mock_note_provider.get_note.call_count == 1
    assert dealer._EvernoteDealer__handlers['text'][
        0]._note_provider.update_note.call_count == 1
예제 #2
0
async def test_process_photo_in_one_note_mode():
    User.create(id=1,
                telegram_chat_id=2,
                mode='one_note',
                evernote_access_token='token',
                current_notebook={
                    'guid': '000',
                    'name': 'test_notebook'
                },
                places={'000': 'note_guid'})
    TelegramUpdate.create(user_id=1,
                          request_type='photo',
                          status_message_id=5,
                          message={
                              'date':
                              123123,
                              'chat': {
                                  'id': 1,
                                  'type': 'private'
                              },
                              'message_id':
                              10,
                              'photo': [
                                  {
                                      'height': 200,
                                      'width': 200,
                                      'file_id': 'xBcZ1dW',
                                      'file_size': 100,
                                  },
                              ],
                              'from': {
                                  'id': 1
                              },
                          })
    dealer = EvernoteDealer()
    mock_note_provider = AsyncMock()
    note = Types.Note()
    mock_note_provider.get_note = AsyncMock(return_value=note)
    mock_note_provider.save_note = AsyncMock(return_value=Types.Note())
    mock_note_provider.get_note_link = AsyncMock(
        return_value='http://evernote.com/some/stuff/here/')
    mock_telegram_api = AsyncMock()
    file_path = "/tmp/xBcZ1dW.txt"
    if exists(file_path):
        os.unlink(file_path)
    with open(file_path, 'w') as f:
        f.write('test data')

    dealer._telegram_api = mock_telegram_api
    for k, handlers in dealer._EvernoteDealer__handlers.items():
        for handler in handlers:
            handler._note_provider = mock_note_provider
            handler.get_downloaded_file = AsyncMock(
                return_value=(file_path, 'text/plain'))

    updates = dealer.fetch_updates()
    for user_id, update_list in updates.items():
        user = User.get({'id': user_id})
        await dealer.process_user_updates(user, update_list)
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
async def test_switch_mode_command(testbot: EvernoteBot, text_update):
    update = 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]
    assert len(args) == 4
    assert args[0] == user.telegram_chat_id
    assert 'Please, select mode' == args[1]
예제 #6
0
def user():
    note_guid = generate_string(32)
    notebook_guid = generate_string(32)
    user = User.create(id=random.randint(1, 100),
                       name=generate_string(5),
                       username=generate_string(5),
                       telegram_chat_id=random.randint(1, 100),
                       mode='one_note',
                       evernote_access_token='token',
                       current_notebook={
                           'guid': notebook_guid,
                           'name': 'test_notebook'
                       },
                       places={notebook_guid: note_guid})
    return user