def _handler(self, handler_function, input_filters: list, update: Update, callback_context: CallbackContext): if not Filter.apply(input_filters, update): return try: with Context(update, callback_context, self.db, self.translations) as context: handler_function(context) except Exception as exc: ReportsSender.report_exception(self.db) raise exc
def test_user_report_sent(self): configuration = Configuration([]) configuration.set('superuser', 'test') bot = MagicMock() updater = MagicMock() type(updater).bot = PropertyMock(return_value=bot) ReportsSender.instance = ReportsSender(bot, configuration) with ScopedSession(self.connection) as session: superuser = User(id=1234, login='******', name='Super User') session.add(superuser) user = User(id=0, login='******', name='Reporting User') session.add(user) context = MagicMock() type(context).sender = PropertyMock(return_value=user) type(context).session = PropertyMock(return_value=session) type(context.update.effective_chat).id = 777 basic.on_user_report_request(context) context.send_response_message.assert_called_once_with( 'waiting_for_Reporting User_report') context.send_response_message.reset_mock() session.flush() type(context.update.message).message_id = PropertyMock( return_value=4321) routing.dispatch_bare_message(context) context.send_response_message.assert_called_once_with( 'Reporting User_report_sent') bot.forward_message.assert_called_once_with(1234, 777, 4321) self.assertEqual(0, session.query(PendingAction).count())
def _set_up(self): global_logging_init() self.configuration = Configuration( [JsonLoader(str(ROOT_DIR / 'configuration.json')), ArgsLoader()]) self.database_connection = DatabaseConnection(self.configuration) self.translations = Translations(APP_DIR.joinpath('i18n'), APP_DIR) self.updater = Updater(token=self.configuration.get_string( 'telegram_bot_token', default=""), use_context=True) self.dispatcher = Dispatcher(self.updater, self.database_connection, self.translations) ReportsSender.instance = ReportsSender(self.updater.bot, self.configuration)
def on_user_report_received(context: Context): ReportsSender.forward_user_message(context) context.send_response_message( _('{user}_report_sent').format(user=context.sender.name))
def test_no_sender_no_crash(self): ReportsSender.instance = None ReportsSender.forward_user_message(MagicMock())