Beispiel #1
0
class TelegramMessagesTestCase(unittest.TestCase):
    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_receive_msg_totp_code = json.load(json_file)

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

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

    def tearDown(self):
        self.patcher_os_isfile.stop()
        # self.patch_verify_totp_code.stop()

    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,
        )
Beispiel #2
0
class TelegramMessagesTestCase(unittest.TestCase):
    def setUp(self):
        with open('test/expected_conf.json') as json_file:
            self.CONFIG_DICT = json.load(json_file)

        with open('test/mocked_send_msg.json') as json_file:
            self.send_msg_return = json.load(json_file)

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

        self.patcher_os_isfile = patch('common.config_util.os.path.isfile', 
            return_value = False)
        self.mock_os_isfile = self.patcher_os_isfile.start()

        self.bot = MagicMock() 
        self.bot.sendMessage.return_value = self.send_msg_return
        self.bot.sendPhoto.return_value = self.send_msg_return

        self.blink = MagicMock()
        self.blink.cameras.snap_picture.return_value = True

        self.blink.refresh.return_value = True
        self.blink.setup_post_verify = MagicMock()
        self.blink.save = MagicMock()

        self.auth = MagicMock()
        self.auth.login_attributes = True

        self.auth.send_auth_key = MagicMock()
        
        self.patch_open_file1 = patch('messaging.send_msg.open', return_value=MagicMock())
        self.mock_open_send_msg = self.patch_open_file1.start()
        self.patch_open_file2 = patch('camera.blink_cam.open', return_value=MagicMock())
        self.mock_open_blink_cam = self.patch_open_file2.start()
        self.patch_json_load = patch('camera.blink_cam.json', return_value=MagicMock())
        self.mock_json_load = self.patch_json_load.start()
        self.mock_json_load.load.return_value = True


    def tearDown(self):
        self.patcher_os_isfile.stop()
        self.patch_open_file1.stop()
        self.patch_open_file2.stop()
        self.patch_json_load.stop()
        
    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()
Beispiel #3
0
class TelegramMessagesTestCase(unittest.TestCase):
    def setUp(self):
        with open("test/expected_conf.json") as json_file:
            self.CONFIG_DICT = json.load(json_file)

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

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

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

        self.bot = MagicMock()
        self.bot.sendMessage.return_value = self.send_msg_return
        self.bot.sendPhoto.return_value = self.send_msg_return

        self.blink = MagicMock()
        self.blink.cameras.snap_picture.return_value = True
        self.blink.refresh.return_value = True
        self.blink.setup_post_verify = MagicMock()
        self.blink.save = MagicMock()

        self.auth = MagicMock()
        self.auth.login_attributes = False
        self.auth.send_auth_key = MagicMock()

        self.patch_open_file1 = patch("messaging.send_msg.open",
                                      return_value=MagicMock())
        self.mock_open_send_msg = self.patch_open_file1.start()
        self.patch_open_file2 = patch("camera.blink_cam.open",
                                      return_value=MagicMock())
        self.mock_open_blink_cam = self.patch_open_file2.start()
        self.patch_json_load = patch("camera.blink_cam.json",
                                     return_value=MagicMock())
        self.mock_json_load = self.patch_json_load.start()
        self.mock_json_load.load.return_value = True

    def tearDown(self):
        self.patcher_os_isfile.stop()
        self.patch_open_file1.stop()
        self.patch_open_file2.stop()
        self.patch_json_load.stop()

    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:search_key: blink in message: blink 123456",
            "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()
Beispiel #4
0
class TelegramMessagesTestCase(unittest.TestCase):
    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
            )

    def tearDown(self):
        self.patcher_os_isfile.stop()
        self.patch_verify_totp_code.stop()
        self.patch_send_msg.stop()
        self.patch_choose_camera.stop()
        self.patch_blink2FA.stop()
        self.patch_blink_compare_config.stop()

    def test_telegram_messages_config(self):
        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_log1 = ["DEBUG:fdia_telegram:reading config"]
        self.assertEqual(self.log1.output, expected_log1)

    def test_receive_msg_text_allowed_user_and_group_unwanted_text(self):
        with open("test/mocked_received_msg.json") as json_file:
            self.mocked_received_msg = json.load(json_file)
        expected_log2 = [
            "INFO:fdia_telegram:received a telegram message",
            "DEBUG:fdia_telegram:receiving a message text in chat id -4321",
            "INFO:fdia_telegram:received message = test4",
            "INFO:fdia_telegram:chat msg allowed: chat_group_id -4321 is in config",
            "INFO:fdia_telegram:chat msg allowed: user FirstName with from_id 123456789 is in config",
            "DEBUG:fdia_telegram:text not matched checking for totp code",
            "DEBUG:fdia_telegram:regex search string",
            "DEBUG:fdia_telegram:no code number detected",
        ]
        with self.assertLogs("fdia_telegram", level="DEBUG") as self.log2:
            self.instance_TelegramMessages.handle_received_message(
                self.mocked_received_msg
            )
        self.assertEqual(self.log2.output, expected_log2)
        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.mock_verify_totp_code.assert_not_called()

    def test_receive_msg_text_allowed_user_and_group_with_foto_text(self):
        with open("test/mocked_received_msg.json") as json_file:
            self.mocked_received_msg = json.load(json_file)
        self.mocked_received_msg["text"] = "foto"
        expected_log3 = [
            "INFO:fdia_telegram:received a telegram message",
            "DEBUG:fdia_telegram:receiving a message text in chat id -4321",
            "INFO:fdia_telegram:received message = foto",
            "INFO:fdia_telegram:chat msg allowed: chat_group_id -4321 is in config",
            "INFO:fdia_telegram:chat msg allowed: user FirstName with from_id 123456789 is in config",
            "DEBUG:fdia_telegram:search_key: foto in message: foto",
            "DEBUG:fdia_telegram:text match foto found",
        ]
        with self.assertLogs("fdia_telegram", level="DEBUG") as self.log3:
            self.instance_TelegramMessages.handle_received_message(
                self.mocked_received_msg
            )
        self.assertEqual(self.log3.output, expected_log3)
        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.mock_send_msg.assert_called()
        self.mock_choose_camera.assert_called()

    def test_receive_msg_unallowed_user(self):
        with open("test/mocked_received_msg.json") as json_file:
            self.mocked_received_msg = json.load(json_file)
        self.mocked_received_msg["from"]["id"] = 2342356
        expected_log4 = [
            "INFO:fdia_telegram:received a telegram message",
            "DEBUG:fdia_telegram:receiving a message text in chat id -4321",
            "INFO:fdia_telegram:received message = test4",
            "INFO:fdia_telegram:chat msg allowed: chat_group_id -4321 is in config",
            "INFO:fdia_telegram:chat msg denied: from user FirstName with from_id 2342356 is NOT in config",
        ]
        with self.assertLogs("fdia_telegram", level="DEBUG") as self.log4:
            self.instance_TelegramMessages.handle_received_message(
                self.mocked_received_msg
            )
        self.assertEqual(self.log4.output, expected_log4)
        self.assertNotIn(
            str(self.instance_TelegramMessages.from_id),
            self.instance_TelegramMessages.allowed_user_ids,
        )

    def test_receive_msg_unallowed_group(self):
        with open("test/mocked_received_msg.json") as json_file:
            self.mocked_received_msg = json.load(json_file)
        self.mocked_received_msg["chat"]["id"] = 3566
        expected_log5 = [
            "INFO:fdia_telegram:received a telegram message",
            "DEBUG:fdia_telegram:receiving a message text in chat id 3566",
            "INFO:fdia_telegram:received message = test4",
            "INFO:fdia_telegram:chat msg denied: chat_id 3566 is not in config",
        ]
        with self.assertLogs("fdia_telegram", level="DEBUG") as self.log5:
            self.instance_TelegramMessages.handle_received_message(
                self.mocked_received_msg
            )
        self.assertEqual(self.log5.output, expected_log5)
        self.assertNotEqual(
            str(self.instance_TelegramMessages.chat_id),
            self.instance_TelegramMessages.telegram_chat_nr,
        )

    def test_receive_msg_text_allowed_user_and_group_with_blink_2FA_code_text(self):
        with open("test/mocked_received_msg.json") as json_file:
            self.mocked_received_msg = json.load(json_file)
        self.mocked_received_msg["text"] = "blink 356632"
        expected_log6 = [
            "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 356632",
            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:search_key: blink in message: blink 356632",
            "DEBUG:fdia_telegram:text match blink found",
            "INFO:fdia_telegram:blink token received - will save config",
        ]
        with self.assertLogs("fdia_telegram", level="DEBUG") as self.log6:
            self.instance_TelegramMessages.handle_received_message(
                self.mocked_received_msg
            )
        self.assertEqual(self.log6.output, expected_log6)
        self.assertEqual(
            str(self.instance_TelegramMessages.chat_id),
            self.instance_TelegramMessages.telegram_chat_nr,
        )
        self.mock_blink2FA.assert_called()
        self.mock_blink_compare_config.assert_called()