def test_not_signed_in_evernote(self): user_id = 42 message = Message( message_id=1, date=time(), from_user={'id': user_id, 'is_bot': False, 'first_name': 'test'}, chat={'id': 1, 'type': 'private'} ) start_command(self.bot, message) oauth_data = self.bot.evernote._oauth_data evernote_oauth_callback(self.bot, OauthParams(oauth_data['callback_key'], '', 'basic')) update_data = { 'update_id': 1, 'message': { 'message_id': 2, 'date': time(), 'text': 'Hello, World!', 'from_user': { 'id': user_id, 'is_bot': False, 'first_name': 'test', }, 'chat': {'id': 1, 'type': 'private'}, }, } with self.assertRaises(EvernoteBotException) as ctx: self.bot.process_update(update_data) self.assertEqual(str(ctx.exception), 'You have to sign in to Evernote first. Send /start and press the button') self.assertEqual(self.bot.api.sendMessage.call_count, 3) self.assertEqual(self.bot.api.sendMessage.calls[1]['args'][1], 'We are sorry, but you have declined authorization.') self.assertEqual(self.bot.api.sendMessage.calls[2]['args'][1], '❌ Error. You have to sign in to Evernote first. Send /start and press the button')
def test_switch_to_one_note_mode(self): user_id = 8 update_data = { "update_id": 1, "message": { "message_id": 1, "date": 123, "text": '/switch_mode', "entities": [{ "type": "bot_command", "offset": 0, "length": len("/switch_mode"), }], "from_user": { "id": user_id, "is_bot": False, "first_name": "test", }, "chat": { "id": 9, "type": "" }, }, } self.bot.process_update(update_data) user_data = self.bot.users.get(user_id) self.assertEqual(user_data["state"], "switch_mode") self.assertEqual(self.bot.api.sendMessage.call_count, 1) call = self.bot.api.sendMessage.calls[0] self.assertEqual(call["args"][1], "Please, select mode") update_data = { "update_id": 1, "message": { "message_id": 2, "date": time(), "text": 'One note', "from_user": { "id": user_id, "is_bot": False, "first_name": "test", }, "chat": { "id": 2, "type": "private" }, }, } self.assertEqual(self.bot.evernote.create_note.call_count, 0) self.bot.process_update(update_data) call = self.bot.api.sendMessage.calls[1] self.assertTrue(call["args"][1].startswith( 'To enable "One note" mode you have to allow')) oauth_data = self.bot.evernote._oauth_data evernote_oauth_callback(self.bot, oauth_data["callback_key"], "oauth_verifier", "full") self.assertEqual(self.bot.evernote.create_note.call_count, 1) self.assertEqual(self.bot.evernote.get_note_link.call_count, 1)
def evernote_oauth(request: Request): bot = request.app.bot params = request.GET callback_key = params['key'] access_type = params['access'] verifier = params.get('oauth_verifier') oauth_params = OauthParams(callback_key, verifier, access_type) evernote_oauth_callback(bot, oauth_params) return HTTPFound(bot.url)
def evernote_oauth(request: Request): params = request.GET bot = request.app.bot callback_key = params["key"] if not re.match(r"^[a-z0-9]{40}$", callback_key): raise EvernoteBotException("Invalid callback key") access_type = params.get("access") if access_type not in ("basic", "full"): raise EvernoteBotException("Invalid access") evernote_oauth_callback( bot, callback_key=callback_key, oauth_verifier=params.get("oauth_verifier"), access_type=access_type, ) return HTTPFound(bot.url)
def setUp(self): bot = EvernoteBot(self.config) bot.api = TelegramApiMock() bot.evernote = EvernoteApiMock() message = Message( message_id=1, date=time(), from_user={"id": 6, "is_bot": False, "first_name": "test"}, chat={"id": 1, "type": "private"} ) start_command(bot, message) oauth_data = bot.evernote._oauth_data evernote_oauth_callback(bot, OauthParams(oauth_data["callback_key"], "oauth_verifier", "basic")) # creating new mocks because we want get a clean picture bot.api = TelegramApiMock() bot.evernote = EvernoteApiMock() self.bot = bot