Ejemplo n.º 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
async def test_process_user_updates(user):
    update = TelegramUpdate.create(user_id=user.id,
                                   request_type='text',
                                   status_message_id=2,
                                   message={
                                       'message_id': 1,
                                       'date': datetime.datetime.now(),
                                       'from': {
                                           'id': user.id,
                                           'username': '******'
                                       },
                                       'chat': {
                                           'id': 123,
                                           'type': 'private'
                                       },
                                       'text': 'test text'
                                   },
                                   created=datetime.datetime(
                                       2016, 9, 1, 12, 30, 4))
    dealer = EvernoteDealer()
    handler = TextHandler()
    handler.evernote.create_note = AsyncMock()
    handler.evernote.update_note = AsyncMock()
    handler.telegram.editMessageText = AsyncMock()
    dealer.handlers['text'] = [handler]
    await dealer.process_user_updates(user, [update])
    await asyncio.sleep(0.1)
    assert handler.evernote.update_note.call_count == 1
    assert handler.telegram.editMessageText.call_count == 1
def test_fetch_updates():
    TelegramUpdate.create(user_id=1,
                          request_type='text',
                          status_message_id=2,
                          message={'data': 'ok'},
                          created=datetime.datetime(2016, 9, 1, 12, 30, 4))
    TelegramUpdate.create(user_id=1,
                          request_type='text',
                          status_message_id=3,
                          message={'data': 'woohoo'},
                          created=datetime.datetime(2016, 9, 1, 12, 30, 1))
    TelegramUpdate.create(user_id=2,
                          request_type='text',
                          status_message_id=4,
                          message={'data': 'yeah!'},
                          created=datetime.datetime(2016, 9, 1, 12, 30, 2))
    dealer = EvernoteDealer()
    user_updates = dealer.fetch_updates()
    updates = user_updates[1]
    updates2 = user_updates[2]
    assert len(updates) == 2
    assert len(updates2) == 1
    assert updates[0].created < updates2[0].created < updates[1].created
    assert updates[0].status_message_id == 3
    assert updates[1].status_message_id == 2
    assert updates2[0].status_message_id == 4
Ejemplo n.º 4
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_save_text_multiple_notes_mode(user):
    user.mode = 'multiple_notes'
    user.save()

    update_data = {
        'update_id': 93710840,
        'message': {
            'date': datetime.datetime.now(),
            'from': {
                'username': user.username,
                'id': user.id,
            },
            'chat': {
                'id': user.telegram_chat_id,
                'type': 'private',
                'username': user.username,
            },
            'message_id': 164,
            'text': 'test text',
        },
    }

    bot = EvernoteBot('token', 'test_bot')
    message_id = random.randint(1, 100)
    bot.api.sendMessage = AsyncMock(return_value={'message_id': message_id})

    await bot.handle_update(update_data)
    await asyncio.sleep(0.1)

    dealer = EvernoteDealer()
    handler = TextHandler()
    handler.evernote.create_note = AsyncMock()
    handler.evernote.update_note = AsyncMock()
    handler.telegram.editMessageText = AsyncMock()
    dealer.handlers['text'] = [handler]

    user_updates = dealer.fetch_updates()
    await dealer.process_user_updates(user, user_updates[user.id])
    await asyncio.sleep(0.1)
    assert handler.evernote.create_note.call_count == 1
    args = handler.evernote.create_note.call_args[0]
    assert args[0] == 'token'
    assert args[1] == 'Text'
    assert args[2] == 'test text'
    assert args[3] == user.current_notebook['guid']
    assert handler.evernote.update_note.call_count == 0
    assert handler.telegram.editMessageText.call_count == 1
    args = handler.telegram.editMessageText.call_args[0]
    assert args[0] == user.telegram_chat_id
    assert args[1] == message_id
    assert 'Text saved' in args[2]
Ejemplo n.º 6
0
 def run(self):
     dealer = EvernoteDealer()
     dealer.run()
async def test_save_photo_one_note_mode(user):
    user.mode = 'one_note'
    user.save()

    file_id = str(random.randint(1, 10000))
    update_data = {
        'update_id': 93710840,
        'message': {
            'date':
            datetime.datetime.now(),
            'from': {
                'username': user.username,
                'id': user.id,
            },
            'chat': {
                'id': user.telegram_chat_id,
                'type': 'private',
                'username': user.username,
            },
            'message_id':
            164,
            'text':
            '',
            'photo': [
                {
                    'file_size': 12345,
                    'file_id': file_id,
                    'mime_type': 'text/html',
                    'width': 800,
                    'height': 600,
                },
            ],
        },
    }

    bot = EvernoteBot('token', 'test_bot')
    message_id = random.randint(1, 100)
    bot.api.sendMessage = AsyncMock(return_value={'message_id': message_id})

    await bot.handle_update(update_data)
    await asyncio.sleep(0.1)
    dealer = EvernoteDealer()
    handler = PhotoHandler()
    handler.downloader.telegram_api.getFile = AsyncMock(
        return_value='http://yandex.ru/robots.txt')
    handler.evernote.create_note = AsyncMock()
    handler.evernote.update_note = AsyncMock()
    handler.telegram.editMessageText = AsyncMock()
    dealer.handlers['photo'] = [handler]
    user_updates = dealer.fetch_updates()
    await dealer.process_user_updates(user, user_updates[user.id])
    await asyncio.sleep(0.1)

    assert handler.evernote.create_note.call_count == 0
    assert handler.evernote.update_note.call_count == 1
    args = handler.evernote.update_note.call_args[0]
    notebook_guid = user.current_notebook['guid']
    note_guid = user.places[notebook_guid]
    assert args[0] == 'token'
    assert args[1] == note_guid
    assert args[2] == notebook_guid
    assert args[3] == ''
    files = args[4]
    assert len(files) == 1
    assert files[0][0] == os.path.join(handler.downloader.download_dir,
                                       file_id)
    assert not os.path.exists(files[0][0])
async def test_save_voice_multiple_notes_mode(user):
    user.mode = 'multiple_notes'
    user.save()

    file_id = str(random.randint(1, 10000))
    update_data = {
        'update_id': 93710840,
        'message': {
            'date': datetime.datetime.now(),
            'from': {
                'username': user.username,
                'id': user.id,
            },
            'chat': {
                'id': user.telegram_chat_id,
                'type': 'private',
                'username': user.username,
            },
            'message_id': 164,
            'text': '',
            'voice': {
                'file_size': 12345,
                'file_id': file_id,
                'duration': 10,
            },
        },
    }

    bot = EvernoteBot('token', 'test_bot')
    message_id = random.randint(1, 100)
    bot.api.sendMessage = AsyncMock(return_value={'message_id': message_id})

    await bot.handle_update(update_data)
    await asyncio.sleep(0.1)

    dealer = EvernoteDealer()
    handler = VoiceHandler()
    handler.downloader.telegram_api.getFile = AsyncMock(
        return_value='http://yandex.ru/robots.txt')
    handler.evernote.create_note = AsyncMock()
    handler.evernote.update_note = AsyncMock()
    handler.telegram.editMessageText = AsyncMock()
    downloaded_filename = os.path.join(handler.downloader.download_dir,
                                       file_id)
    handler.get_files = AsyncMock(return_value=[(downloaded_filename,
                                                 'audio/wav')])
    dealer.handlers['voice'] = [handler]
    user_updates = dealer.fetch_updates()
    await dealer.process_user_updates(user, user_updates[user.id])
    await asyncio.sleep(0.1)

    assert handler.evernote.create_note.call_count == 1
    args = handler.evernote.create_note.call_args[0]
    assert args[0] == 'token'
    assert args[1] == 'Voice'
    assert args[2] == ''
    assert args[3] == user.current_notebook['guid']
    assert len(args[4]) == 1
    assert args[4][0][0] == downloaded_filename
    assert handler.get_files.call_count == 1
    assert handler.evernote.update_note.call_count == 0
    assert handler.telegram.editMessageText.call_count == 1
    args = handler.telegram.editMessageText.call_args[0]
    assert args[0] == user.telegram_chat_id
    assert args[1] == message_id
    assert 'Voice saved' in args[2]