def setUp(self) -> None: self.test_bot_token = 'test_bot_token' self.test_bot_chat_id = 'test_bot_chat_id' self.test_base_url = \ "https://api.telegram.org/bot" + self.test_bot_token self.test_message = "This is a test message" self.test_telegram_bot_api = TelegramBotApi(self.test_bot_token, self.test_bot_chat_id)
def setUp(self) -> None: self.test_channel_name = 'test_telegram_channel' self.test_channel_id = 'test_telegram_id12345' self.dummy_logger = logging.getLogger('Dummy') self.dummy_logger.disabled = True self.test_bot_token = 'test_bot_token' self.test_bot_chat_id = 'test_bot_chat_id' self.test_base_url = \ "https://api.telegram.org/bot" + self.test_bot_token self.test_telegram_bot_api = TelegramBotApi(self.test_bot_token, self.test_bot_chat_id) self.test_telegram_channel = TelegramChannel( self.test_channel_name, self.test_channel_id, self.dummy_logger, self.test_telegram_bot_api) self.test_system_name = 'test_system' self.test_percentage_usage = 50 self.test_panic_severity = 'WARNING' self.test_last_monitored = 45 self.test_parent_id = 'parent_1234' self.test_system_id = 'system_id32423' self.test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert( self.test_system_name, self.test_percentage_usage, self.test_panic_severity, self.test_last_monitored, self.test_panic_severity, self.test_parent_id, self.test_system_id )
def _initialise_telegram_commands_handler( bot_token: str, bot_chat_id: str, channel_id: str, channel_name: str, associated_chains: Dict) -> TelegramCommandsHandler: # Handler display name based on channel name handler_display_name = TELEGRAM_COMMANDS_HANDLER_NAME_TEMPLATE.format( channel_name) handler_logger = _initialise_channel_handler_logger( handler_display_name, TelegramCommandsHandler.__name__) # Try initialising handler until successful while True: try: telegram_bot = TelegramBotApi(bot_token, bot_chat_id) telegram_channel = TelegramChannel( channel_name, channel_id, handler_logger.getChild(TelegramChannel.__name__), telegram_bot) cmd_handlers_logger = handler_logger.getChild( TelegramCommandHandlers.__name__) cmd_handlers_rabbitmq = RabbitMQApi( logger=cmd_handlers_logger.getChild(RabbitMQApi.__name__), host=env.RABBIT_IP) cmd_handlers_redis = RedisApi( logger=cmd_handlers_logger.getChild(RedisApi.__name__), host=env.REDIS_IP, db=env.REDIS_DB, port=env.REDIS_PORT, namespace=env.UNIQUE_ALERTER_IDENTIFIER) cmd_handlers_mongo = MongoApi(logger=cmd_handlers_logger.getChild( MongoApi.__name__), host=env.DB_IP, db_name=env.DB_NAME, port=env.DB_PORT) cmd_handlers = TelegramCommandHandlers( TELEGRAM_COMMAND_HANDLERS_NAME, cmd_handlers_logger, associated_chains, telegram_channel, cmd_handlers_rabbitmq, cmd_handlers_redis, cmd_handlers_mongo) handler_rabbitmq = RabbitMQApi(logger=handler_logger.getChild( RabbitMQApi.__name__), host=env.RABBIT_IP) telegram_commands_handler = TelegramCommandsHandler( handler_display_name, handler_logger, handler_rabbitmq, telegram_channel, cmd_handlers) log_and_print( "Successfully initialised {}".format(handler_display_name), handler_logger) break except Exception as e: msg = get_initialisation_error_message(handler_display_name, e) log_and_print(msg, handler_logger) # sleep before trying again time.sleep(RE_INITIALISE_SLEEPING_PERIOD) return telegram_commands_handler
class TestTelegramBotApi(unittest.TestCase): def setUp(self) -> None: self.test_bot_token = 'test_bot_token' self.test_bot_chat_id = 'test_bot_chat_id' self.test_base_url = \ "https://api.telegram.org/bot" + self.test_bot_token self.test_message = "This is a test message" self.test_telegram_bot_api = TelegramBotApi(self.test_bot_token, self.test_bot_chat_id) def tearDown(self) -> None: self.test_telegram_bot_api = None def test_bot_token_returns_bot_token(self) -> None: self.assertEqual(self.test_bot_token, self.test_telegram_bot_api.bot_token) def test_bot_chat_id_returns_bot_chat_id(self) -> None: self.assertEqual(self.test_bot_chat_id, self.test_telegram_bot_api.bot_chat_id) def test_base_url_initialised_correctly(self) -> None: self.assertEqual(self.test_base_url, self.test_telegram_bot_api._base_url) ''' In the tests below we will check that each request is performed correctly by checking that the correct parameters are passed to the request. Note, we cannot check that the request was actually successful because we would have to expose infrastructure details. ''' @mock.patch.object(requests, "get") def test_send_message_sends_a_message_correctly(self, mock_get) -> None: data_dict = { 'chat_id': self.test_bot_chat_id, 'text': self.test_message, 'parse_mode': "Markdown" } self.test_telegram_bot_api.send_message(self.test_message) mock_get.assert_called_once_with(self.test_base_url + "/sendMessage", data=data_dict, timeout=10) @mock.patch.object(requests, "get") def test_send_get_updates_sends_request_correctly(self, mock_get) -> None: self.test_telegram_bot_api.get_updates() mock_get.assert_called_once_with(self.test_base_url + "/getUpdates", timeout=10) @mock.patch.object(requests, "get") def test_send_get_me_sends_request_correctly(self, mock_get) -> None: self.test_telegram_bot_api.get_me() mock_get.assert_called_once_with(self.test_base_url + "/getMe", timeout=10)
def setUp(self) -> None: self.test_handler_name = 'test_telegram_alerts_handler' self.dummy_logger = logging.getLogger('Dummy') self.dummy_logger.disabled = True self.connection_check_time_interval = timedelta(seconds=0) self.rabbit_ip = env.RABBIT_IP self.rabbitmq = RabbitMQApi( self.dummy_logger, self.rabbit_ip, connection_check_time_interval=self.connection_check_time_interval) self.test_channel_name = 'test_telegram_channel' self.test_channel_id = 'test_telegram_id12345' self.test_channel_logger = self.dummy_logger.getChild('dummy_channel') self.test_bot_token = 'test_bot_token' self.test_bot_chat_id = 'test_bot_chat_id' self.test_base_url = \ "https://api.telegram.org/bot" + self.test_bot_token self.test_api = TelegramBotApi(self.test_bot_token, self.test_bot_chat_id) self.test_channel = TelegramChannel(self.test_channel_name, self.test_channel_id, self.test_channel_logger, self.test_api) self.test_queue_size = 1000 self.test_max_attempts = 5 self.test_alert_validity_threshold = 300 self.test_telegram_alerts_handler = TelegramAlertsHandler( self.test_handler_name, self.dummy_logger, self.rabbitmq, self.test_channel, self.test_queue_size, self.test_max_attempts, self.test_alert_validity_threshold) self.test_data_str = "this is a test string" self.test_rabbit_queue_name = 'Test Queue' self.test_timestamp = 45676565.556 self.test_heartbeat = { 'component_name': 'Test Component', 'is_alive': True, 'timestamp': self.test_timestamp, } self.test_system_name = 'test_system' self.test_percentage_usage = 50 self.test_panic_severity = 'WARNING' self.test_parent_id = 'parent_1234' self.test_system_id = 'system_id32423' self.test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert( self.test_system_name, self.test_percentage_usage, self.test_panic_severity, self.test_timestamp, self.test_panic_severity, self.test_parent_id, self.test_system_id) self.test_alerts_queue = Queue(self.test_queue_size)
def _initialise_telegram_alerts_handler( bot_token: str, bot_chat_id: str, channel_id: str, channel_name: str) -> TelegramAlertsHandler: # Handler display name based on channel name handler_display_name = TELEGRAM_ALERTS_HANDLER_NAME_TEMPLATE.format( channel_name) handler_logger = _initialise_channel_handler_logger( handler_display_name, TelegramAlertsHandler.__name__) # Try initialising handler until successful while True: try: telegram_bot = TelegramBotApi(bot_token, bot_chat_id) telegram_channel = TelegramChannel( channel_name, channel_id, handler_logger.getChild(TelegramChannel.__name__), telegram_bot) rabbitmq = RabbitMQApi(logger=handler_logger.getChild( RabbitMQApi.__name__), host=env.RABBIT_IP) telegram_alerts_handler = TelegramAlertsHandler( handler_display_name, handler_logger, rabbitmq, telegram_channel, env.CHANNELS_MANAGER_PUBLISHING_QUEUE_SIZE) log_and_print( "Successfully initialised {}".format(handler_display_name), handler_logger) break except Exception as e: msg = get_initialisation_error_message(handler_display_name, e) log_and_print(msg, handler_logger) # sleep before trying again time.sleep(RE_INITIALISE_SLEEPING_PERIOD) return telegram_alerts_handler
def setUp(self) -> None: self.test_handler_name = 'test_telegram_commands_handler' self.dummy_logger = logging.getLogger('Dummy') self.dummy_logger.disabled = True self.connection_check_time_interval = timedelta(seconds=0) self.rabbit_ip = env.RABBIT_IP self.rabbitmq = RabbitMQApi( self.dummy_logger, self.rabbit_ip, connection_check_time_interval=self.connection_check_time_interval) self.test_channel_name = 'test_telegram_channel' self.test_channel_id = 'test_telegram_id12345' self.test_channel_logger = self.dummy_logger.getChild('dummy_channel') self.test_bot_token = '1234567891:ABC-67ABCrfZFdddqRT5Gh837T2rtUFHgTY' self.test_bot_chat_id = 'test_bot_chat_id' self.test_base_url = \ "https://api.telegram.org/bot" + self.test_bot_token self.test_api = TelegramBotApi(self.test_bot_token, self.test_bot_chat_id) self.test_channel = TelegramChannel(self.test_channel_name, self.test_channel_id, self.test_channel_logger, self.test_api) self.cmd_handlers_rabbit = RabbitMQApi( logger=self.dummy_logger.getChild(RabbitMQApi.__name__), host=self.rabbit_ip, connection_check_time_interval=self.connection_check_time_interval) self.redis = RedisApi(logger=self.dummy_logger.getChild( RedisApi.__name__), host=env.REDIS_IP, db=env.REDIS_DB, port=env.REDIS_PORT, namespace=env.UNIQUE_ALERTER_IDENTIFIER) self.mongo = MongoApi(logger=self.dummy_logger.getChild( MongoApi.__name__), host=env.DB_IP, db_name=env.DB_NAME, port=env.DB_PORT) self.test_command_handlers_logger = self.dummy_logger.getChild( 'command_handlers') self.test_chain_1 = 'Kusama' self.test_chain_2 = 'Cosmos' self.test_chain_3 = 'Test_Chain' self.test_chain1_id = 'kusama1234' self.test_chain2_id = 'cosmos1234' self.test_chain3_id = 'test_chain11123' self.test_associated_chains = { self.test_chain1_id: self.test_chain_1, self.test_chain2_id: self.test_chain_2, self.test_chain3_id: self.test_chain_3 } self.test_telegram_command_handlers = TelegramCommandHandlers( self.test_handler_name, self.test_command_handlers_logger, self.test_associated_chains, self.test_channel, self.cmd_handlers_rabbit, self.redis, self.mongo) self.test_telegram_commands_handler = TelegramCommandsHandler( self.test_handler_name, self.dummy_logger, self.rabbitmq, self.test_channel, self.test_telegram_command_handlers) self.test_data_str = "this is a test string" self.test_rabbit_queue_name = 'Test Queue' self.test_timestamp = 45676565.556 self.test_heartbeat = { 'component_name': 'Test Component', 'timestamp': self.test_timestamp, } self.test_system_name = 'test_system' self.test_percentage_usage = 50 self.test_panic_severity = 'WARNING' self.test_parent_id = 'parent_1234' self.test_system_id = 'system_id32423' self.test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert( self.test_system_name, self.test_percentage_usage, self.test_panic_severity, self.test_timestamp, self.test_panic_severity, self.test_parent_id, self.test_system_id)
def setUp(self) -> None: self.dummy_logger = logging.getLogger('Dummy') self.dummy_logger.disabled = True self.handler_display_name = 'Test Channel Handler' self.handler_module_name = 'TestChannelHandler' self.connection_check_time_interval = timedelta(seconds=0) self.rabbit_ip = env.RABBIT_IP self.rabbitmq = RabbitMQApi( self.dummy_logger, self.rabbit_ip, connection_check_time_interval=self.connection_check_time_interval) self.telegram_channel_name = 'test_telegram_channel' self.telegram_channel_id = 'test_telegram_id12345' self.telegram_channel_logger = self.dummy_logger.getChild( 'telegram_channel_logger') self.bot_token = '1234567891:ABC-67ABCrfZFdddqRT5Gh837T2rtUFHgTY' self.bot_chat_id = 'test_bot_chat_id' self.telegram_base_url = "https://api.telegram.org/bot" + self.bot_token self.telegram_bot_api = TelegramBotApi(self.bot_token, self.bot_chat_id) self.telegram_channel = TelegramChannel(self.telegram_channel_name, self.telegram_channel_id, self.telegram_channel_logger, self.telegram_bot_api) self.test_queue_size = 1000 self.test_max_attempts = 5 self.test_alert_validity_threshold = 300 self.telegram_alerts_handler = TelegramAlertsHandler( self.handler_display_name, self.dummy_logger, self.rabbitmq, self.telegram_channel, self.test_queue_size, self.test_max_attempts, self.test_alert_validity_threshold) self.cmd_handlers_rabbit = RabbitMQApi( logger=self.dummy_logger.getChild(RabbitMQApi.__name__), host=self.rabbit_ip, connection_check_time_interval=self.connection_check_time_interval) self.redis = RedisApi(logger=self.dummy_logger.getChild( RedisApi.__name__), host=env.REDIS_IP, db=env.REDIS_DB, port=env.REDIS_PORT, namespace=env.UNIQUE_ALERTER_IDENTIFIER) self.mongo = MongoApi(logger=self.dummy_logger.getChild( MongoApi.__name__), host=env.DB_IP, db_name=env.DB_NAME, port=env.DB_PORT) self.command_handlers_logger = self.dummy_logger.getChild( TelegramCommandHandlers.__name__) self.test_chain_1 = 'Kusama' self.test_chain_2 = 'Cosmos' self.test_chain_3 = 'Test_Chain' self.test_chain1_id = 'kusama1234' self.test_chain2_id = 'cosmos1234' self.test_chain3_id = 'test_chain11123' self.test_associated_chains = { self.test_chain1_id: self.test_chain_1, self.test_chain2_id: self.test_chain_2, self.test_chain3_id: self.test_chain_3 } self.telegram_command_handlers = TelegramCommandHandlers( self.handler_display_name, self.command_handlers_logger, self.test_associated_chains, self.telegram_channel, self.cmd_handlers_rabbit, self.redis, self.mongo) self.telegram_commands_handler = TelegramCommandsHandler( self.handler_display_name, self.dummy_logger, self.rabbitmq, self.telegram_channel, self.telegram_command_handlers) self.twilio_channel_name = 'test_twilio_channel' self.twilio_channel_id = 'test_twilio_id12345' self.twilio_channel_logger = self.dummy_logger.getChild( 'twilio_channel') self.account_sid = 'test_account_sid' self.auth_token = 'test_auth_token' self.call_from = '+35699999999' self.call_to = ['+35611111111', '+35644545454', '+35634343434'] self.twiml = '<Response><Reject/></Response>' self.twiml_is_url = False self.twilio_api = TwilioApi(self.account_sid, self.auth_token) self.twilio_channel = TwilioChannel(self.twilio_channel_name, self.twilio_channel_id, self.twilio_channel_logger, self.twilio_api) self.twilio_alerts_handler = TwilioAlertsHandler( self.handler_display_name, self.dummy_logger, self.rabbitmq, self.twilio_channel, self.call_from, self.call_to, self.twiml, self.twiml_is_url, self.test_max_attempts, self.test_alert_validity_threshold) self.integration_key = 'test_integration_key' self.pagerduty_channel_name = 'test_pagerduty_channel' self.pagerduty_channel_id = 'test_pagerduty_id12345' self.pagerduty_channel_logger = self.dummy_logger.getChild('pagerduty') self.pagerduty_api = PagerDutyApi(self.integration_key) self.pagerduty_channel = PagerDutyChannel( self.pagerduty_channel_name, self.pagerduty_channel_id, self.pagerduty_channel_logger, self.pagerduty_api) self.pagerduty_alerts_handler = PagerDutyAlertsHandler( self.handler_display_name, self.dummy_logger, self.rabbitmq, self.pagerduty_channel, self.test_queue_size, self.test_max_attempts, self.test_alert_validity_threshold) self.email_channel_name = 'test_email_channel' self.email_channel_id = 'test_email1234' self.email_channel_logger = self.dummy_logger.getChild('email_channel') self.emails_to = [ '*****@*****.**', '*****@*****.**', '*****@*****.**' ] self.smtp = 'test smtp server' self.sender = 'test sender' self.username = '******' self.password = '******' self.port = 10 self.email_api = EmailApi(self.smtp, self.sender, self.username, self.password, self.port) self.email_channel = EmailChannel(self.email_channel_name, self.email_channel_id, self.email_channel_logger, self.emails_to, self.email_api) self.email_alerts_handler = EmailAlertsHandler( self.handler_display_name, self.dummy_logger, self.rabbitmq, self.email_channel, self.test_queue_size, self.test_max_attempts, self.test_alert_validity_threshold) self.api_key = 'test api key' self.opsgenie_channel_name = 'test_opgenie_channel' self.opsgenie_channel_id = 'test_opsgenie_id12345' self.opsgenie_channel_logger = self.dummy_logger.getChild( 'opsgenie_channel') self.eu_host = True self.opsgenie_api = OpsgenieApi(self.api_key, self.eu_host) self.opsgenie_channel = OpsgenieChannel(self.opsgenie_channel_name, self.opsgenie_channel_id, self.opsgenie_channel_logger, self.opsgenie_api) self.opsgenie_alerts_handler = OpsgenieAlertsHandler( self.handler_display_name, self.dummy_logger, self.rabbitmq, self.opsgenie_channel, self.test_queue_size, self.test_max_attempts, self.test_alert_validity_threshold) self.console_channel_name = 'test_console_channel' self.console_channel_id = 'test_console1234' self.console_channel_logger = self.dummy_logger.getChild( 'console_channel') self.console_channel = ConsoleChannel(self.console_channel_name, self.console_channel_id, self.console_channel_logger) self.console_alerts_handler = ConsoleAlertsHandler( self.handler_display_name, self.dummy_logger, self.rabbitmq, self.console_channel) self.log_channel_name = 'test_logger_channel' self.log_channel_id = 'test_logger1234' self.log_channel_logger = self.dummy_logger.getChild('log_channel') self.alerts_logger = self.dummy_logger.getChild('alerts_logger') self.log_channel = LogChannel(self.log_channel_name, self.log_channel_id, self.log_channel_logger, self.alerts_logger) self.log_alerts_handler = LogAlertsHandler(self.handler_display_name, self.dummy_logger, self.rabbitmq, self.log_channel)