def test_change_srv_picture_interval() -> None: """Tests surveillance picture interval changing action.""" update = get_mocked_update_object() context = get_mocked_context_object() parameters, update.callback_query.edit_message_text = get_kwargs_grabber() BotConfig.ensure_defaults(context) getattr(BotConfig, '_change_srv_picture_interval')(update, context) assert '*Surveillance picture interval*' in parameters[0]['text']
def test_change_motion_contours() -> None: """Tests motion contours changing action.""" update = get_mocked_update_object() context = get_mocked_context_object() parameters, update.callback_query.edit_message_text = get_kwargs_grabber() BotConfig.ensure_defaults(context) getattr(BotConfig, '_change_motion_contours')(update, context) assert '*Motion contours*' in parameters[0]['text']
def test_change_od_video_duration() -> None: """Tests on demand video duration changing action.""" update = get_mocked_update_object() context = get_mocked_context_object() parameters, update.callback_query.edit_message_text = get_kwargs_grabber() BotConfig.ensure_defaults(context) getattr(BotConfig, '_change_od_video_duration')(update, context) assert '*On Demand video duration*' in parameters[0]['text']
def test_ensure_defaults() -> None: """Tests default configuration generation.""" context = get_mocked_context_object() BotConfig.ensure_defaults(context) assert context.bot_data == { 'timestamp': True, 'od_video_duration': 5, 'srv_video_duration': 30, 'srv_picture_interval': 5, 'srv_motion_contours': True }
def wrapped(update: Update, context: CallbackContext) -> Any: # Checks if user is authorized if update.effective_chat.username != self.authorized_user: logger.warning('Unauthorized call to "%s" command by @%s', command, update.effective_chat.username) update.message.reply_text(text="Unauthorized") return None BotConfig.ensure_defaults(context) logger.debug('Received "%s" command', command) return callback(update, context)
def test_get_config_handler() -> None: """Tests bot config states definition.""" handler = BotConfig.get_config_handler(TelegramBotMock()) assert len(handler.states) == 5 assert BotConfig.MAIN_MENU in handler.states assert BotConfig.GENERAL_CONFIG in handler.states assert BotConfig.SURVEILLANCE_CONFIG in handler.states assert BotConfig.BOOLEAN_INPUT in handler.states assert BotConfig.INTEGER_INPUT in handler.states
def __init__(self, token: str, username: str, persistence_dir: Optional[str] = None, log_level: Union[int, str, None] = None) -> None: self.logger = logging.getLogger(__name__) if log_level: self.logger.setLevel(log_level) if not token: self.logger.critical("Error! Missing BOT_API_KEY configuration") sys.exit(1) if not username: self.logger.critical( "Error! Missing AUTHORIZED_USER configuration") sys.exit(1) try: self.camera = Camera() except CameraConnectionError: self.logger.critical("Error! Can not connect to the camera.") sys.exit(2) except CodecNotAvailable: self.logger.critical( "Error! There are no suitable video codec available.") sys.exit(2) self.authorized_user = username persistence: Optional[PicklePersistence] if persistence_dir: os.makedirs(persistence_dir) path = os.path.join(persistence_dir, 'surveillance-bot.pickle') persistence = PicklePersistence(filename=path) else: persistence = None self.updater = Updater(token=token, persistence=persistence, use_context=True) dispatcher: Dispatcher = self.updater.dispatcher # Registers commands in the dispatcher for name, method in inspect.getmembers(self, inspect.ismethod): if name.startswith('_command_'): command = name.replace('_command_', '') dispatcher.add_handler(self.command_handler(command, method)) # Registers configuration menu dispatcher.add_handler(BotConfig.get_config_handler(self)) # Register error handler dispatcher.add_error_handler(self._error)
def test_surveillance_config() -> None: """Tests surveillance config menu generation.""" update = get_mocked_update_object() context = get_mocked_context_object() parameters, update.message.reply_text = get_kwargs_grabber() BotConfig.ensure_defaults(context) assert getattr(BotConfig, '_surveillance_config')( update, context ) == BotConfig.SURVEILLANCE_CONFIG assert '*Surveillance Mode configuration*' in parameters[0]['text'] assert parameters[0]['reply_markup'].to_dict() == { 'inline_keyboard': [ [ { 'text': 'Video duration', 'callback_data': str(BotConfig.CHANGE_SRV_VIDEO_DURATION) } ], [ { 'text': 'Picture Interval', 'callback_data': str(BotConfig.CHANGE_SRV_PICTURE_INTERVAL) } ], [ { 'text': 'Draw motion contours', 'callback_data': str(BotConfig.CHANGE_SRV_MOTION_CONTOURS) } ], [ { 'text': 'Back', 'callback_data': str(BotConfig.END) } ] ] }
def test_general_config() -> None: """Tests general config menu generation.""" update = get_mocked_update_object() context = get_mocked_context_object() parameters, update.message.reply_text = get_kwargs_grabber() BotConfig.ensure_defaults(context) assert getattr(BotConfig, '_general_config')( update, context ) == BotConfig.GENERAL_CONFIG assert '*General configuration*' in parameters[0]['text'] assert parameters[0]['reply_markup'].to_dict() == { 'inline_keyboard': [ [ { 'text': 'Timestamp', 'callback_data': str(BotConfig.CHANGE_TIMESTAMP) } ], [ { 'text': 'On Demand video duration', 'callback_data': str( BotConfig.CHANGE_OD_VIDEO_DURATION ) } ], [ { 'text': 'Back', 'callback_data': str(BotConfig.END) } ] ] }