def setUp(self): self._db_connection = DbConnection(in_memory=True) self._user = User(self._db_connection) self.rut1 = Rut.build_rut('2.343.234-k') self.rut2 = Rut.build_rut('12.444.333-4') self.rut3 = Rut.build_rut('18.123.021-5') self.rut4 = Rut.build_rut('12.456.371-2') self.assertNotEqual(None, self.rut1) self.assertNotEqual(None, self.rut2) self.assertNotEqual(None, self.rut3) self.assertNotEqual(None, self.rut4)
def _retrieve(self, telegram_user_id: int, web_retriever: WebRetriever, cache: Cache): user_id = User(self._db_connection).get_id(telegram_user_id) cached_results = cache.get(user_id, self.rut) self._retrieved_from_cache = cached_results is not None self._cache_changed = False if cached_results: self._old_cache_and_user_str = cached_results self.web_result = Parser.cache_string_to_web_result(cached_results) return raw_page = web_retriever.retrieve(self.rut) web_result = Parser.parse(raw_page) # Cache even error results to prevent users to trigger # too many requests to the bank. if web_result.get_type() != TypeOfWebResult.NO_ERROR: self._old_cache_and_user_str = web_result.get_error() else: self._old_cache_and_user_str = Parser.events_to_cache_string( web_result.get_events()) try: self._cache_changed = cache.update(user_id, self.rut, self._old_cache_and_user_str) # Non fatal error. except Exception: # pylint: disable=broad-except logger.exception("Unable to update the cache") self.web_result = web_result
def get_rut(self, unused_bot, update: telegram.Update): """Query info for a previously set rut.""" telegram_id = update.message.from_user.id rut = User(self._db_connection).get_rut(telegram_id) if rut: logger.debug('USR[%s]; GET_RUT[%s]', telegram_id, rut) self.query_the_bank_and_reply(telegram_id, rut, update.message.reply_text, self.ReplyWhen.ALWAYS) return logger.debug('USR[%s]; GET_NO_RUT', telegram_id) update.message.reply_text(Messages.NO_RUT_MSG)
def step(self, updater, hours=HOURS_TO_UPDATE): """Checks the bank for subscribed users. If useful new data is available, send a message to the user. """ user_conn = User(self._db_connection) users_to_update = user_conn.get_subscribers_to_update(hours) if not users_to_update: return user_to_update = users_to_update[random.randint( 0, len(users_to_update) - 1)] logger.debug("To update queue length: %s. Updating: user_id=%s", len(users_to_update), user_to_update.id) rut = Rut.build_rut_sin_digito(user_to_update.rut) user_chat_id = user_conn.get_chat_id(user_to_update.id) try: self.query_the_bank_and_reply( user_to_update.telegram_id, rut, partial(updater.bot.sendMessage, user_chat_id), ValeVistaBot.ReplyWhen.IS_USEFUL_FOR_USER) except telegram.error.Unauthorized: logger.debug( 'USR[%s]; CHAT_ID[%s] Unauthorized us, unsubscribing...', user_to_update.telegram_id, user_chat_id) user_conn.unsubscribe(user_to_update.telegram_id, user_chat_id)
def unsubscribe(self, unused_bot, update: telegram.Update): """Stop getting updates.""" logger.debug("USR:[%s]; UNSUBSC", update.message.from_user.id) try: User(self._db_connection).unsubscribe(update.message.from_user.id, update.message.chat.id) except UserBadUseError as bad_user_exep: logger.warning(bad_user_exep.public_message) update.message.reply_text(bad_user_exep.public_message) except UserDoesNotExistError as user_exep: logger.warning(user_exep.public_message) update.message.reply_text(Messages.UNSUBSCRIBE_NON_SUBSCRIBED) else: logger.info("User %s unsubscribed", update.message.from_user.id) update.message.reply_text(Messages.UNSUBSCRIBED)
def subscribe(self, unused_bot, update: telegram.Update): """Subscribe and get updates on valevista changes for your rut.""" logger.debug("USR:[%s]; SUBSC", update.message.from_user.id) chat_type = update.message.chat.type if chat_type != 'private': logger.debug('USR[%s]; FROM NON PRIVATE CHAT[%s]', update.message.from_user.id, chat_type) update.message.reply_text(Messages.FROM_NON_PRIVATE_CHAT) return try: User(self._db_connection).subscribe(update.message.from_user.id, update.message.chat.id) except UserBadUseError as bad_user_exep: logger.warning(bad_user_exep.public_message) update.message.reply_text(bad_user_exep.public_message) else: update.message.reply_text(Messages.SUBSCRIBED)
def set_rut(self, unused_bot, update: telegram.Update): """Set a rut to easily query it in the future.""" spl = update.message.text.split(' ') if len(spl) < 2: logger.debug('USR[%s]; EMPTY_RUT', update.message.from_user.id) update.message.reply_text(Messages.SET_EMPTY_RUT) return rut = Rut.build_rut(spl[1]) if rut is None: logger.debug('USR[%s]; INVALID_RUT', update.message.from_user.id) update.message.reply_text(Messages.SET_INVALID_RUT) return User(self._db_connection).set_rut(update.message.from_user.id, rut) logger.debug("USR[%s]; SET_RUT[%s]", update.message.from_user.id, rut) update.message.reply_text(Messages.SET_RUT % rut)
class TestSubscription(TestCase): def setUp(self): self._db_connection = DbConnection(in_memory=True) self._user = User(self._db_connection) self.rut1 = Rut.build_rut('2.343.234-k') self.rut2 = Rut.build_rut('12.444.333-4') self.rut3 = Rut.build_rut('18.123.021-5') self.rut4 = Rut.build_rut('12.456.371-2') self.assertNotEqual(None, self.rut1) self.assertNotEqual(None, self.rut2) self.assertNotEqual(None, self.rut3) self.assertNotEqual(None, self.rut4) def testSimpleSubscribeUnsubscribe(self): telegram_id = 34 chat_id = 5657 telegram_id2 = 35 chat_id2 = 5658 # self._user isn't subscribed self.assertFalse(self._user.is_subscribed(telegram_id, chat_id)) # self._user does not have a registered rut self.assertRaises(UserBadUseError, self._user.subscribe, telegram_id, chat_id) self.assertIsNone(self._user.set_rut(telegram_id, self.rut1)) self.assertFalse(self._user.is_subscribed(telegram_id, chat_id)) self.assertIsNone(self._user.subscribe(telegram_id, chat_id)) self.assertTrue(self._user.is_subscribed(telegram_id, chat_id)) self.assertRaises(UserBadUseError, self._user.subscribe, telegram_id, chat_id) self.assertIsNone(self._user.unsubscribe(telegram_id, chat_id)) self.assertFalse(self._user.is_subscribed(telegram_id, chat_id)) self.assertIsNone(self._user.subscribe(telegram_id, chat_id)) self.assertIsNone(self._user.set_rut(telegram_id2, self.rut2)) self.assertFalse(self._user.is_subscribed(telegram_id2, chat_id2)) self.assertRaises(sqlalchemy.exc.IntegrityError, self._user.subscribe, telegram_id, chat_id2) self.assertRaises(sqlalchemy.exc.IntegrityError, self._user.subscribe, telegram_id2, chat_id) self.assertIsNone(self._user.subscribe(telegram_id2, chat_id2)) def testGetSubscribersToUpdate(self): telegram_id = 23 telegram_id2 = 24 telegram_id3 = 25 telegram_id4 = 26 chat_id = 33 chat_id2 = 34 chat_id3 = 35 chat_id4 = 36 self._user.set_rut(telegram_id, self.rut1) self._user.set_rut(telegram_id2, self.rut2) self._user.set_rut(telegram_id3, self.rut3) self._user.set_rut(telegram_id4, self.rut4) self._user.subscribe(telegram_id, chat_id) self._user.subscribe(telegram_id2, chat_id2) self._user.subscribe(telegram_id3, chat_id3) cache = Cache(self._db_connection) cache.update(self._user.get_id(telegram_id), self.rut1, "result") cache.update(self._user.get_id(telegram_id2), self.rut2, "result2") self.assertTrue(self._user.is_subscribed(telegram_id, chat_id)) self.assertTrue(self._user.is_subscribed(telegram_id2, chat_id2)) self.assertEqual(1, len(self._user.get_subscribers_to_update(2))) self.assertEqual(3, len(self._user.get_subscribers_to_update(0))) self.assertEqual( "%s" % chat_id, self._user.get_chat_id(self._user.get_id(telegram_id))) def testGetChatId(self): telegram_id = 23 telegram_id2 = 24 telegram_id3 = 25 chat_id = 33 chat_id2 = 34 chat_id3 = 35 self._user.set_rut(telegram_id, self.rut1) self._user.set_rut(telegram_id2, self.rut2) self._user.set_rut(telegram_id3, self.rut3) self._user.subscribe(telegram_id, chat_id) self._user.subscribe(telegram_id2, chat_id2) self._user.subscribe(telegram_id3, chat_id3) self.assertEqual( "%s" % chat_id, self._user.get_chat_id(self._user.get_id(telegram_id))) self.assertEqual( "%s" % chat_id2, self._user.get_chat_id(self._user.get_id(telegram_id2))) self.assertEqual( "%s" % chat_id3, self._user.get_chat_id(self._user.get_id(telegram_id3)))
class TestModelInterface(TestCase): def setUp(self): self._db_connection = DbConnection(in_memory=True) self._user = User(self._db_connection) self.rut1 = Rut.build_rut('2.343.234-k') self.rut2 = Rut.build_rut('12.444.333-4') self.rut3 = Rut.build_rut('18.123.021-5') self.rut4 = Rut.build_rut('12.456.371-2') self.assertNotEqual(None, self.rut1) self.assertNotEqual(None, self.rut2) self.assertNotEqual(None, self.rut3) self.assertNotEqual(None, self.rut4) def testGetUserId(self): self.assertRaises(model_interface.UserDoesNotExistError, self._user.get_id, 2342, False) new_id = self._user.get_id(2342, True) self.assertEqual(new_id, self._user.get_id(2342, False)) self.assertNotEqual(new_id, self._user.get_id(2351, True)) def testCacheResult(self): result = "stored_result" result2 = "stored_result2" user_id = self._user.get_id(9, True) cache = Cache(self._db_connection) self.assertIsNone(cache.get(user_id, self.rut1)) cache.update(user_id, self.rut1, result) self.assertEqual(result, cache.get(user_id, self.rut1)) cache.update(user_id, self.rut1, result2) self.assertEqual(result2, cache.get(user_id, self.rut1)) def testRutSetAndGet(self): self.assertIsNone(self._user.get_rut(32)) self._user.set_rut(32, self.rut1) self.assertEqual(self.rut1, self._user.get_rut(32)) self._user.set_rut(32, self.rut2) self.assertEqual(self.rut2, self._user.get_rut(32)) def testGetTelegramId(self): self.assertRaises(model_interface.UserDoesNotExistError, self._user.get_telegram_id, 3) self._user.set_rut(23, self.rut1) user_id = self._user.get_id(23, False) self.assertEqual(23, self._user.get_telegram_id(user_id))