async def execute(self, message: Message): self.bot.track(message) user = User.get({'id': message.user.id}) notebooks = await self.bot.list_notebooks(user) buttons = [] for notebook in notebooks: if notebook['guid'] == user.current_notebook['guid']: name = "> %s <" % notebook['name'] else: name = notebook['name'] buttons.append({'text': name}) markup = json.dumps({ 'keyboard': [[b] for b in buttons], 'resize_keyboard': True, 'one_time_keyboard': True, }) asyncio.ensure_future( self.bot.api.sendMessage(user.telegram_chat_id, 'Please, select notebook', reply_markup=markup)) user.state = 'select_notebook' user.save() asyncio.ensure_future(self.bot.update_notebooks_cache(user))
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_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_notebook_command(testbot: EvernoteBot, user, text_update): update = text_update notebook_cmd = SwitchNotebookCommand(testbot) 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 args = testbot.api.sendMessage.call_args[0] assert args[0] == user.telegram_chat_id assert args[1] == 'Please, select notebook'
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]
async def execute(self, message: Message): self.bot.track(message) user = User.get({'id': message.user.id}) buttons = [] for mode in ['one_note', 'multiple_notes']: if user.mode == mode: name = "> %s <" % mode.capitalize().replace('_', ' ') else: name = mode.capitalize().replace('_', ' ') buttons.append({'text': name}) markup = json.dumps({ 'keyboard': [[b] for b in buttons], 'resize_keyboard': True, 'one_time_keyboard': True, }) asyncio.ensure_future( self.bot.api.sendMessage(user.telegram_chat_id, 'Please, select mode', reply_markup=markup)) user.state = 'switch_mode' user.save()