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
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]
async def test_choice_month_no_remained_choices(api_mock, plugin, cco, choice_data): """Show only chosen games if no more choices left.""" cco.remained_choices = 0 cco.content_choices_made = ['c'] cco.content_choices = [ Mock(**{ 'id': 'a', 'title': 'A' }), Mock(**{ 'id': 'b', 'title': 'B' }), Mock(**{ 'id': 'c', 'title': 'C' }), ] cco.extrases = [Mock(**{'machine_name': 'e', 'human_name': 'E'})] api_mock.get_choice_content_data = AsyncMock(return_value=choice_data) ctx = None async for one_month_games in plugin.get_subscription_games( 'Humble Choice 2020-05', ctx): assert one_month_games == [ SubscriptionGame(game_title='C', game_id='c'), SubscriptionGame(game_title='E', game_id='e'), ] assert plugin._choice_games == { # cache 'c': ChoiceGame(id='c', title='C', slug='may-2020', is_extras=False), 'e': ChoiceGame(id='e', title='E', slug='may-2020', is_extras=True), }
async def test_TeslaApiClient_get(client, mocker, vehicle): ctx = MagicMock() resp = AsyncMock() ctx.__aenter__.return_value = resp ctx.__aexit__.return_value = resp err = AsyncMock(return_value={"error": "ICE > EV"}) ok = AsyncMock(return_value={"response": FULL_DATA}) resp.json = err mocker.patch.object(client, "_get_headers", return_value={"hello": "you"}) mocker.patch.object(client, "authenticate", new=AsyncMock(return_value=None)) mocker.patch.object(client._session, "get", return_value=ctx) with pytest.raises(ApiError): await client.get("vehicles") resp.json = ok raw = await client.get("vehicles") assert raw == FULL_DATA == vehicle._data
def mock_async_fn(): return AsyncMock()
async def test_requre_full_permissions(user): StartSession.create(id=user.id, key='', data={}, oauth_data={}) config['evernote']['full_access'] = {} update_data = { 'update_id': 93710840, 'message': { 'date': datetime.datetime.now(), 'from': { 'username': user.username, 'id': user.id, }, 'chat': { 'id': user.id, 'type': 'private', 'username': user.username, }, 'message_id': 164, 'text': '/switch_mode', 'entities': [ { 'type': 'bot_command', 'offset': 0, 'length': 12 }, ], }, } bot = EvernoteBot('token', 'test_bot') bot.api.sendMessage = AsyncMock() await bot.handle_update(update_data) await asyncio.sleep(0.1) user = User.get({'id': user.id}) user.mode = 'multiple_notes' user.save() assert user.state == 'switch_mode' assert bot.api.sendMessage.call_count == 1 args = bot.api.sendMessage.call_args[0] assert args[1] == 'Please, select mode' bot.api.sendMessage.reset_mock() update_data = { 'update_id': 93710840, 'message': { 'date': datetime.datetime.now(), 'from': { 'username': user.username, 'id': user.id, }, 'chat': { 'id': user.id, 'type': 'private', 'username': user.username, }, 'message_id': 164, 'text': 'One note', }, } bot.api.sendMessage = AsyncMock(return_value={'message_id': 123}) bot.api.editMessageReplyMarkup = AsyncMock() bot.evernote.get_oauth_data = AsyncMock(return_value={'oauth_url': 'url'}) await bot.handle_update(update_data) await asyncio.sleep(0.1) session = StartSession.get({'id': user.id}) assert session.oauth_data['oauth_url'] == 'url' assert bot.evernote.get_oauth_data.call_count == 1 assert bot.api.editMessageReplyMarkup.call_count == 1 assert bot.api.sendMessage.call_count == 2
async def test_evernote_provider(): provider = NoteProvider() provider.cache = AsyncMock() provider._api = AsyncMock() # TODO:
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_switch_notebook(user): update_data = { 'update_id': 93710840, 'message': { 'date': datetime.datetime.now(), 'from': { 'username': user.username, 'id': user.id, }, 'chat': { 'id': user.id, 'type': 'private', 'username': user.username, }, 'message_id': 164, 'text': '/notebook', 'entities': [ { 'type': 'bot_command', 'offset': 0, 'length': 9 }, ], }, } bot = EvernoteBot('token', 'test_bot') Notebook = namedtuple('Notebook', ['guid', 'name']) notebooks = [ Notebook(user.current_notebook['guid'], user.current_notebook['name']), Notebook(guid='123', name='test_nb') ] bot.evernote.api.list_notebooks = AsyncMock(return_value=notebooks) bot.evernote.create_note = AsyncMock() bot.evernote.get_note_link = AsyncMock() bot.api.editMessageText = AsyncMock() bot.api.sendMessage = AsyncMock() await bot.handle_update(update_data) user = User.get({'id': user.id}) user.mode = 'multiple_notes' user.save() assert user.state == 'select_notebook' await asyncio.sleep(0.1) assert bot.api.sendMessage.call_args[0][1] == 'Please, select notebook' markup = json.loads(bot.api.sendMessage.call_args[0][2]) assert markup['one_time_keyboard'] assert markup['keyboard'] nb = markup['keyboard'][0][0] assert nb['text'] == '> {0} <'.format(user.current_notebook['name']) bot.evernote.api.list_notebooks = AsyncMock(return_value=notebooks) bot.evernote.create_note.reset_mock() bot.api.sendMessage.reset_mock() update_data = { 'update_id': 93710840, 'message': { 'date': datetime.datetime.now(), 'from': { 'username': user.username, 'id': user.id, }, 'chat': { 'id': user.id, 'type': 'private', 'username': user.username, }, 'message_id': 164, 'text': 'test_nb', }, } await bot.handle_update(update_data) await asyncio.sleep(0.1) user = User.get({'id': user.id}) assert user.current_notebook['guid'] == '123' assert bot.evernote.api.list_notebooks.call_count == 1 assert bot.api.sendMessage.call_count == 1 assert bot.api.sendMessage.call_args[0][1].startswith( 'From now your current notebook is: test_nb' )
async def test_TeslaApiClient_get_vehicle(client, vehicle, mocker): mocker.patch.object(client, "get_vehicle", new=AsyncMock(return_value=vehicle)) assert await client.get_vehicle("Lightning McQueen") is vehicle
async def test_TeslaApiClient_list_vehicles(client, vehicle, mocker): mocker.patch.object(client, "list_vehicles", new=AsyncMock(return_value=[vehicle])) assert len(await client.list_vehicles())
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]
async def test_local_size(plugin): local_game_mock = Mock(spec=LocalHumbleGame) local_game_mock.get_size = AsyncMock(return_value=100200300) plugin._local_games = {'loc': local_game_mock} ctx = await plugin.prepare_local_size_context(['loc']) assert await plugin.get_local_size('loc', ctx) == 100200300