Beispiel #1
0
    def test_should_raise_exception_if_message_text_is_not_provided(
            self, mock_get_conn):
        with pytest.raises(airflow.exceptions.AirflowException) as ctx:
            hook = TelegramHook(telegram_conn_id='telegram_default')
            hook.send_message({"chat_id": -420913222})

        assert "'text' must be provided for telegram message" == str(ctx.value)
Beispiel #2
0
    def test_should_retry_when_any_telegram_error_is_encountered(
            self, mock_get_conn):
        excepted_retry_count = 5
        mock_get_conn.return_value = mock.Mock(password="******")

        def side_effect(*args, **kwargs):
            raise telegram.error.TelegramError("cosmic rays caused bit flips")

        mock_get_conn.return_value.send_message.side_effect = side_effect

        with pytest.raises(Exception) as ctx:
            hook = TelegramHook(
                telegram_conn_id='telegram-webhook-with-chat_id')
            hook.send_message({"text": "test telegram message"})

        assert "RetryError" in str(ctx.value)
        assert "state=finished raised TelegramError" in str(ctx.value)

        mock_get_conn.assert_called_once()
        mock_get_conn.return_value.send_message.assert_called_with(
            **{
                'chat_id': "-420913222",
                'parse_mode': 'HTML',
                'disable_web_page_preview': True,
                'text': 'test telegram message',
            })
        assert excepted_retry_count == mock_get_conn.return_value.send_message.call_count
Beispiel #3
0
    def test_should_raise_exception_if_chat_id_is_not_provided_anywhere(
            self, mock_get_conn):
        with pytest.raises(airflow.exceptions.AirflowException) as ctx:
            hook = TelegramHook(telegram_conn_id='telegram_default')
            hook.send_message({"text": "test telegram message"})

        assert "'chat_id' must be provided for telegram message" == str(
            ctx.value)
Beispiel #4
0
 def execute(self, **kwargs) -> None:
     """Calls the TelegramHook to post the provided Telegram message"""
     telegram_hook = TelegramHook(
         telegram_conn_id=self.telegram_conn_id,
         token=self.token,
         chat_id=self.chat_id,
     )
     telegram_hook.send_message(self.telegram_kwargs)
    def test_should_raise_exception_if_message_text_is_not_provided(
            self, mock_get_conn):
        with self.assertRaises(airflow.exceptions.AirflowException) as e:
            hook = TelegramHook(telegram_conn_id='telegram_default')
            hook.send_message({"chat_id": -420913222})

        self.assertEqual("'text' must be provided for telegram message",
                         str(e.exception))
    def test_should_raise_exception_if_chat_id_is_not_provided_anywhere(
            self, mock_get_conn):
        with self.assertRaises(airflow.exceptions.AirflowException) as e:
            hook = TelegramHook(telegram_conn_id='telegram_default')
            hook.send_message({"text": "test telegram message"})

        self.assertEqual("'chat_id' must be provided for telegram message",
                         str(e.exception))
Beispiel #7
0
    def execute(self, context: Dict) -> None:
        """Calls the TelegramHook to post the provided Telegram message"""
        if self.text:
            self.telegram_kwargs['text'] = self.text

        telegram_hook = TelegramHook(
            telegram_conn_id=self.telegram_conn_id,
            token=self.token,
            chat_id=self.chat_id,
        )
        telegram_hook.send_message(self.telegram_kwargs)
Beispiel #8
0
    def test_should_raise_exception_if_both_connection_or_token_is_not_provided(
            self):
        with pytest.raises(airflow.exceptions.AirflowException) as ctx:
            TelegramHook()

        assert "Cannot get token: No valid Telegram connection supplied." == str(
            ctx.value)
Beispiel #9
0
    def test_should_send_message_if_token_is_provided(self, mock_get_conn):
        mock_get_conn.return_value = mock.Mock(password="******")

        hook = TelegramHook(token=TELEGRAM_TOKEN, chat_id=-420913222)
        hook.send_message({"text": "test telegram message"})

        mock_get_conn.return_value.send_message.return_value = "OK."

        mock_get_conn.assert_called_once()
        mock_get_conn.return_value.send_message.assert_called_once_with(
            **{
                'chat_id': -420913222,
                'parse_mode': 'HTML',
                'disable_web_page_preview': True,
                'text': 'test telegram message',
            })
Beispiel #10
0
    def test_should_send_message_if_chat_id_is_provided_in_connection(
            self, mock_get_conn):
        mock_get_conn.return_value = mock.Mock(password="******")

        hook = TelegramHook(telegram_conn_id='telegram-webhook-with-chat_id')
        hook.send_message({"text": "test telegram message"})

        mock_get_conn.return_value.send_message.return_value = "OK."

        mock_get_conn.assert_called_once()
        mock_get_conn.return_value.send_message.assert_called_once_with(
            **{
                'chat_id': "-420913222",
                'parse_mode': 'HTML',
                'disable_web_page_preview': True,
                'text': 'test telegram message',
            })
    def test_should_raise_exception_if_conn_id_doesnt_exist(self):
        with self.assertRaises(
                airflow.exceptions.AirflowNotFoundException) as e:
            TelegramHook(telegram_conn_id='telegram-webhook-non-existent')

        self.assertEqual(
            "The conn_id `telegram-webhook-non-existent` isn't defined",
            str(e.exception))
    def test_should_raise_exception_if_both_connection_or_token_is_not_provided(
            self):
        with self.assertRaises(airflow.exceptions.AirflowException) as e:
            TelegramHook()

        self.assertEqual(
            "Cannot get token: No valid Telegram connection supplied.",
            str(e.exception))
Beispiel #13
0
    def test_should_send_message_if_all_parameters_are_correctly_provided(
            self, mock_get_conn):
        mock_get_conn.return_value = mock.Mock(password="******")

        hook = TelegramHook(telegram_conn_id='telegram_default')
        hook.send_message({
            "chat_id": -420913222,
            "text": "test telegram message"
        })

        mock_get_conn.return_value.send_message.return_value = "OK."

        mock_get_conn.assert_called_once()
        mock_get_conn.return_value.send_message.assert_called_once_with(
            **{
                'chat_id': -420913222,
                'parse_mode': 'HTML',
                'disable_web_page_preview': True,
                'text': 'test telegram message',
            })
Beispiel #14
0
    def test_should_raise_exception_if_conn_id_doesnt_contain_token(self):
        with pytest.raises(airflow.exceptions.AirflowException) as ctx:
            TelegramHook(telegram_conn_id='telegram-webhook-without-token')

        assert "Missing token(password) in Telegram connection" == str(
            ctx.value)
Beispiel #15
0
    def test_should_raise_exception_if_conn_id_doesnt_exist(self):
        with pytest.raises(airflow.exceptions.AirflowNotFoundException) as ctx:
            TelegramHook(telegram_conn_id='telegram-webhook-non-existent')

        assert "The conn_id `telegram-webhook-non-existent` isn't defined" == str(
            ctx.value)
    def test_should_raise_exception_if_conn_id_doesnt_contain_token(self):
        with self.assertRaises(airflow.exceptions.AirflowException) as e:
            TelegramHook(telegram_conn_id='telegram-webhook-without-token')

        self.assertEqual("Missing token(password) in Telegram connection",
                         str(e.exception))