Example #1
0
    def test_usecase2_open_door_after_received_correct_totp_code(self):
        """UseCase 2 tests includes:

        receive telegram message,
        check if telegrom text message matches totp code,
        response telegram message that door will be opened
        call RPI GPIO to open door.
        """

        # prepare telegram config class
        with self.assertLogs('fdia_telegram',
                             level='DEBUG') as self.instance_log:
            self.instance_TelegramMessages = TelegramMessages(
                self.bot, self.blink, self.auth)
        self.mock_os_isfile.assert_called()
        self.assertEqual(self.instance_TelegramMessages.bot, self.bot)
        self.assertEqual(self.instance_TelegramMessages.blink, self.blink)
        self.assertEqual(self.instance_TelegramMessages.auth, self.auth)
        self.assertEqual(self.instance_TelegramMessages.config,
                         self.CONFIG_DICT)
        expected_instance_log = ['DEBUG:fdia_telegram:reading config']
        self.assertEqual(self.instance_log.output, expected_instance_log)
        # generate new totp code
        new_totp = generate_totp_code(
            self.instance_TelegramMessages.otp_password,
            self.instance_TelegramMessages.otp_length,
            self.instance_TelegramMessages.otp_interval,
            self.instance_TelegramMessages.hash_type)
        # set code not to run on RPi
        self.instance_TelegramMessages.run_on_raspberry = False
        # set new totp code to be received
        self.mocked_receive_msg_totp_code['text'] = new_totp
        # define expected log output
        expected_method_log = [
            'INFO:fdia_telegram:received a telegram message',
            f"DEBUG:fdia_telegram:receiving a message text in chat id {self.CONFIG_DICT['telegram']['chat_number']}",
            f"INFO:fdia_telegram:received message = {new_totp}",
            f"INFO:fdia_telegram:chat msg allowed: chat_group_id {self.CONFIG_DICT['telegram']['chat_number']} is in config",
            f"INFO:fdia_telegram:chat msg allowed: user FirstName with from_id {self.CONFIG_DICT['telegram']['allowed_user_ids'][0]} is in config",
            'DEBUG:fdia_telegram:text not matched checking for totp code',
            'DEBUG:fdia_telegram:regex search string',
            'DEBUG:TOTP:verify totp with library passlib',
            f"INFO:fdia_telegram:{new_totp} TOTP code correct",
            'INFO:send_telegram_msg:send message : Code accepted.',
            'INFO:door-opener:not running on raspberry pi - will not open the door',
            'INFO:fdia_telegram:Door opened for 5 Sec.'
        ]
        # run code test
        with self.assertLogs(level='DEBUG') as self.method_log:
            self.instance_TelegramMessages.handle_received_message(
                self.mocked_receive_msg_totp_code)
        # check expected results
        self.assertEqual(self.method_log.output, expected_method_log)
        self.assertEqual(self.instance_TelegramMessages.content_type, "text")
        self.assertEqual(str(self.instance_TelegramMessages.chat_id),
                         self.instance_TelegramMessages.telegram_chat_nr)
        self.assertIn(str(self.instance_TelegramMessages.from_id),
                      self.instance_TelegramMessages.allowed_user_ids)
Example #2
0
    def test_usecase3_gather_foto_from_front_door(self):
        """UseCase 4 tests includes:

        receive telegram message,
        check if telegram text message has blink token,
        add / update blink token to blink_config file,
        send telegram message about blink config update.
        """

        # prepare telegram config class
        with self.assertLogs('fdia_telegram',
                             level='DEBUG') as self.instance_log:
            self.instance_TelegramMessages = TelegramMessages(
                self.bot, self.blink, self.auth)
        self.mock_os_isfile.assert_called()
        self.assertEqual(self.instance_TelegramMessages.bot, self.bot)
        self.assertEqual(self.instance_TelegramMessages.blink, self.blink)
        self.assertEqual(self.instance_TelegramMessages.auth, self.auth)
        self.assertEqual(self.instance_TelegramMessages.config,
                         self.CONFIG_DICT)
        expected_instance_log = ['DEBUG:fdia_telegram:reading config']
        self.assertEqual(self.instance_log.output, expected_instance_log)

        self.mocked_receive_msg['text'] = "blink 123456"
        expected_method_log = [
            'INFO:fdia_telegram:received a telegram message',
            f"DEBUG:fdia_telegram:receiving a message text in chat id {self.CONFIG_DICT['telegram']['chat_number']}",
            'INFO:fdia_telegram:received message = blink 123456',
            f"INFO:fdia_telegram:chat msg allowed: chat_group_id {self.CONFIG_DICT['telegram']['chat_number']} is in config",
            f"INFO:fdia_telegram:chat msg allowed: user FirstName with from_id {self.CONFIG_DICT['telegram']['allowed_user_ids'][0]} is in config",
            'DEBUG:fdia_telegram:text match blink found',
            'INFO:fdia_telegram:blink token received - will save config',
            'INFO:send_telegram_msg:send message : Blink token received 123456',
            'DEBUG:blink_cam:add a 2FA token for authentication',
            'DEBUG:blink_cam:verify 2FA token',
            'INFO:blink_cam:added 2FA token 123456',
            'DEBUG:blink_cam:load blink config file',
            'DEBUG:blink_cam:saved blink config file differs from running config',
            'DEBUG:blink_cam:blink config object = False',
            'DEBUG:blink_cam:blink config file   = True',
            'INFO:blink_cam:will update blink config file',
            'INFO:blink_cam:saving blink authenticated session infos into config file'
        ]

        with self.assertLogs(level='DEBUG') as self.method_log:
            self.instance_TelegramMessages.handle_received_message(
                self.mocked_receive_msg)
        self.assertEqual(self.method_log.output, expected_method_log)
        self.assertEqual(self.instance_TelegramMessages.content_type, "text")
        self.assertEqual(str(self.instance_TelegramMessages.chat_id),
                         self.instance_TelegramMessages.telegram_chat_nr)
        self.assertIn(str(self.instance_TelegramMessages.from_id),
                      self.instance_TelegramMessages.allowed_user_ids)
        self.blink.setup_post_verify.assert_called()
        self.blink.save.assert_called()
        self.auth.send_auth_key.assert_called()
Example #3
0
    def test_usecase3_gather_foto_from_front_door(self):    
        """UseCase 3 tests includes:

        receive telegram message,
        check if telegrom text message has foto request,
        send telegram message foto snapshot will be took,
        take a blink cam foto snapshot,
        send foto via telegram back.
        """

        # prepare telegram config class
        with self.assertLogs('fdia_telegram', level='DEBUG') as self.instance_log:
            self.instance_TelegramMessages = TelegramMessages(self.bot, self.blink, self.auth)    
        self.mock_os_isfile.assert_called()
        self.assertEqual (self.instance_TelegramMessages.bot,self.bot)
        self.assertEqual (self.instance_TelegramMessages.blink, self.blink)
        self.assertEqual (self.instance_TelegramMessages.auth, self.auth)
        self.assertEqual (self.instance_TelegramMessages.config, self.CONFIG_DICT)
        expected_instance_log = ['DEBUG:fdia_telegram:reading config']
        self.assertEqual(self.instance_log.output, expected_instance_log)

        self.mocked_receive_msg['text'] = "foto"
        expected_method_log = [
            'INFO:fdia_telegram:received a telegram message', 
            f"DEBUG:fdia_telegram:receiving a message text in chat id {self.CONFIG_DICT['telegram']['chat_number']}", 
            'INFO:fdia_telegram:received message = foto', 
            f"INFO:fdia_telegram:chat msg allowed: chat_group_id {self.CONFIG_DICT['telegram']['chat_number']} is in config", 
            f"INFO:fdia_telegram:chat msg allowed: user FirstName with from_id {self.CONFIG_DICT['telegram']['allowed_user_ids'][0]} is in config", 
            'DEBUG:fdia_telegram:text match foto found', 
            'DEBUG:telegram thread:Foto request received', 
            'INFO:send_telegram_msg:send message : I will send a foto!', 
            'DEBUG:cam_common:choose camera', 
            'DEBUG:cam_common:blink cam choosen', 
            'INFO:cam_common:take a Blink Cam snapshot', 
            "INFO:blink_cam:i'll take a snapshot from cam Blink_Camera_name_here and store it here /tmp/foto.jpg", 
            'DEBUG:blink_cam:create a camera instance', 
            'DEBUG:blink_cam:take a snpshot', 
            'DEBUG:blink_cam:refresh blink server info', 
            'INFO:blink_cam:saving blink foto', 
            'DEBUG:blink_cam:load blink config file', 
            'DEBUG:blink_cam:saved blink config file == running config', 
            'INFO:send_telegram_msg:send a foto: success'
            ]
        with self.assertLogs(level='DEBUG') as self.method_log:
            self.instance_TelegramMessages.handle_received_message(self.mocked_receive_msg)
        self.assertEqual(self.method_log.output, expected_method_log)
        self.assertEqual(self.instance_TelegramMessages.content_type,"text")
        self.assertEqual(str(self.instance_TelegramMessages.chat_id), self.instance_TelegramMessages.telegram_chat_nr)
        self.assertIn(str(self.instance_TelegramMessages.from_id), self.instance_TelegramMessages.allowed_user_ids)        
        self.blink.cameras.__getitem__.assert_called_with('Blink_Camera_name_here')
        self.blink.cameras.__getitem__().snap_picture.assert_called()
        self.blink.refresh.assert_called()
        self.blink.cameras.__getitem__().image_to_file.assert_called_with('/tmp/foto.jpg')
        self.bot.sendPhoto.assert_called()
Example #4
0
    def setUp(self):
        with open("test/expected_conf.json") as json_file:
            self.CONFIG_DICT = json.load(json_file)

        with open("test/mocked_received_msg.json") as json_file:
            self.mocked_received_msg = json.load(json_file)

        self.patcher_os_isfile = patch(
            "common.config_util.os.path.isfile", return_value=False
        )
        self.patcher_os_path = patch(
            "common.config_util.os.path.exists", return_value=False
        )

        self.bot = MagicMock()
        self.blink = MagicMock()
        self.auth = MagicMock()

        self.patch_verify_totp_code = patch(
            "messaging.otp.verify_totp_code", return_value=MagicMock()
        )
        self.mock_verify_totp_code = self.patch_verify_totp_code.start()

        self.patch_choose_camera = patch(
            "camera.cam_common.choose_camera", return_value=MagicMock()
        )
        self.mock_choose_camera = self.patch_choose_camera.start()
        self.patch_send_msg = patch(
            "messaging.send_msg.telegram_send_message", return_value=MagicMock()
        )
        self.mock_send_msg = self.patch_send_msg.start()

        self.patch_blink2FA = patch(
            "camera.blink_cam.add_2fa_blink_token", return_value=MagicMock()
        )
        self.mock_blink2FA = self.patch_blink2FA.start()
        self.patch_blink_compare_config = patch(
            "camera.blink_cam.blink_compare_config", return_value=MagicMock()
        )
        self.mock_blink_compare_config = self.patch_blink_compare_config.start()

        self.mock_os_isfile = self.patcher_os_isfile.start()

        with self.assertLogs("fdia_telegram", level="DEBUG") as self.log1:
            self.instance_TelegramMessages = TelegramMessages(
                self.bot, self.blink, self.auth
            )