From 65ae13b53d25bb6da1c7de7b2642e7cc40439285 Mon Sep 17 00:00:00 2001 From: louisevelayo Date: Mon, 23 Oct 2023 12:17:04 +0200 Subject: [PATCH 1/8] add form url to message template --- node_monitor/load_config.py | 1 + node_monitor/node_monitor.py | 7 +++++-- node_monitor/node_monitor_helpers/messages.py | 11 +++++++---- tests/test_bot_email.py | 7 +++++-- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/node_monitor/load_config.py b/node_monitor/load_config.py index a56c3c95..41c9141b 100644 --- a/node_monitor/load_config.py +++ b/node_monitor/load_config.py @@ -17,6 +17,7 @@ DB_PASSWORD = os.environ.get('DB_PASSWORD', '') DB_NAME = os.environ.get('DB_NAME', '') DB_PORT = os.environ.get('DB_PORT', '') +FEEDBACK_FORM = os.environ.get('FEEDBACK_FORM', '') ## Pre-flight check diff --git a/node_monitor/node_monitor.py b/node_monitor/node_monitor.py index 24a3d9a8..3b2b498f 100644 --- a/node_monitor/node_monitor.py +++ b/node_monitor/node_monitor.py @@ -12,6 +12,7 @@ from node_monitor.node_monitor_helpers.get_compromised_nodes import \ get_compromised_nodes import node_monitor.node_monitor_helpers.messages as messages +import node_monitor.load_config as c Seconds = int Principal = str @@ -107,7 +108,8 @@ def broadcast_alerts(self) -> None: channels = self.node_provider_db.get_channels_as_dict() for node_provider_id, nodes in self.actionables.items(): preferences = subscribers[node_provider_id] - subject, message = messages.nodes_down_message(nodes, node_labels) + subject, message = messages.nodes_down_message( + nodes, node_labels, c.FEEDBACK_FORM) # - - - - - - - - - - - - - - - - - if preferences['notify_email'] == True: recipients = email_recipients[node_provider_id] @@ -142,7 +144,8 @@ def broadcast_status_report(self) -> None: # - - - - - - - - - - - - - - - - - for node_provider_id, nodes in reportable_nodes.items(): preferences = subscribers[node_provider_id] - subject, message = messages.nodes_status_message(nodes, node_labels) + subject, message = messages.nodes_status_message( + nodes, node_labels, c.FEEDBACK_FORM) # - - - - - - - - - - - - - - - - - if preferences['notify_email'] == True: recipients = email_recipients[node_provider_id] diff --git a/node_monitor/node_monitor_helpers/messages.py b/node_monitor/node_monitor_helpers/messages.py index dd55a68d..e82c7a05 100644 --- a/node_monitor/node_monitor_helpers/messages.py +++ b/node_monitor/node_monitor_helpers/messages.py @@ -2,6 +2,7 @@ from typing import List, Dict, Tuple import node_monitor.ic_api as ic_api +import node_monitor.load_config as c # Forgive me Lord Guido, for I have broken PEP8. Principal = str @@ -53,7 +54,8 @@ def detailnodes(nodes: List[ic_api.Node], def nodes_down_message(nodes: List[ic_api.Node], - labels: Dict[Principal, str]) -> Tuple[str, str]: + labels: Dict[Principal, str], + feedback_form: str) -> Tuple[str, str]: """Returns a message that describes the nodes that are down, in the format of an email or message for a comprable communication channel. """ @@ -74,13 +76,14 @@ def _make_subject() -> str: f"\n" f"Node Monitor by Aviate Labs\n" f"Report Generated: {datetime.utcnow().isoformat()} UTC\n" - f"Help us serve you better! Provide your feedback!\n") + f"Help us serve you better! Provide your feedback here: {feedback_form}\n") return (subject, message) def nodes_status_message(nodes: List[ic_api.Node], - labels: Dict[Principal, str]) -> Tuple[str, str]: + labels: Dict[Principal, str], + feedback_form: str) -> Tuple[str, str]: """Returns a message that describes the status of all nodes, in the format of an email or message for a comprable communication channel. """ @@ -125,5 +128,5 @@ def _render_frac(numerator: int, denominator: int) -> str: f"Thanks for reviewing today's report. We'll be back tomorrow!\n" f"Node Monitor by Aviate Labs.\n" f"Report generated: {datetime.utcnow().isoformat()} UTC\n" - f"Help us serve you better! Provide your feedback!\n") + f"Help us serve you better! Provide your feedback here: {feedback_form} \n") return (subject, message) diff --git a/tests/test_bot_email.py b/tests/test_bot_email.py index 3ac76491..7f2b704b 100644 --- a/tests/test_bot_email.py +++ b/tests/test_bot_email.py @@ -55,6 +55,7 @@ def test_send_emails_network(): subnet_id = 'fake_subnet_id', ) fakelabel = {'fake_node_id': 'fake_label'} + fakeurl = 'https://forms.gle/thisisfake' ## Init the authenticated email bot instance email_bot = EmailBot(c.EMAIL_USERNAME, c.EMAIL_PASSWORD) @@ -63,11 +64,13 @@ def test_send_emails_network(): ## Send out nodes_down_message and nodes_status_message. We test both. ## We append time to the subject to act as an identifier for the test. recipients = ['nodemonitortest@mailnesia.com'] - subject1, message1 = messages.nodes_down_message([fakenode], fakelabel) + subject1, message1 = messages.nodes_down_message( + [fakenode], fakelabel, fakeurl) subject1 = str(f'{time.time()} - {subject1}') email_bot.send_emails(recipients, subject1, message1) - subject2, message2 = messages.nodes_status_message([fakenode], fakelabel) + subject2, message2 = messages.nodes_status_message( + [fakenode], fakelabel, fakeurl) subject2 = str(f'{time.time()} - {subject2}') email_bot.send_emails(recipients, subject2, message2) From 8499e1fc3fc35bb936395678cc5ab44ea81fc4d4 Mon Sep 17 00:00:00 2001 From: louisevelayo Date: Mon, 23 Oct 2023 13:23:04 +0200 Subject: [PATCH 2/8] use message templates in live slack message tests --- tests/test_bot_slack.py | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/tests/test_bot_slack.py b/tests/test_bot_slack.py index 11529657..83a88b6f 100644 --- a/tests/test_bot_slack.py +++ b/tests/test_bot_slack.py @@ -1,8 +1,10 @@ import pytest from unittest.mock import patch -import node_monitor.load_config as c from node_monitor.bot_slack import SlackBot +import node_monitor.load_config as c +import node_monitor.node_monitor_helpers.messages as messages +import node_monitor.ic_api as ic_api @patch("slack_sdk.WebClient") @@ -24,13 +26,35 @@ def test_send_message(mock_web_client): def test_send_message_slack(): """Send a real test message to a Slack workspace""" slack_bot = SlackBot(c.TOKEN_SLACK) + + ## Create a fake node model + fakenode = ic_api.Node( + dc_id = 'fake_dc_id', + dc_name = 'fake_dc_name', + node_id = 'fake_node_id', + node_operator_id = 'fake_node_operator_id', + node_provider_id = 'fake_node_provider_id', + node_provider_name = 'fake_node_provider_name', + owner = 'fake_owner', + region = 'fake_region', + status = 'DOWN', + subnet_id = 'fake_subnet_id', + ) + fakelabel = {'fake_node_id': 'fake_label'} + fakeurl = 'https://forms.gle/thisisfake' slack_channel_name = "node-monitor" - message = "🔬 Hello from test_send_message_slack()" + subject1, message1 = messages.nodes_down_message( + [fakenode], fakelabel, fakeurl) + subject2, message2 = messages.nodes_status_message( + [fakenode], fakelabel, fakeurl) # SlackBot.send_message() returns an error without raising an exception # to prevent NodeMonitor from crashing if the message fails to send. # Instead, we raise it here. - err = slack_bot.send_message(slack_channel_name, message) - if err is not None: - raise err + err1 = slack_bot.send_message(slack_channel_name, message1) + err2 = slack_bot.send_message(slack_channel_name, message2) + if err1 is not None: + raise err1 + if err2 is not None: + raise err2 From 16630b2a0a50c5a9aa12cd28f8b2d45ea5fed80a Mon Sep 17 00:00:00 2001 From: louisevelayo Date: Mon, 23 Oct 2023 13:31:50 +0200 Subject: [PATCH 3/8] use message templates in live telegram message testing --- tests/test_bot_telegram.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/tests/test_bot_telegram.py b/tests/test_bot_telegram.py index 96216af5..964bb6d6 100644 --- a/tests/test_bot_telegram.py +++ b/tests/test_bot_telegram.py @@ -2,6 +2,8 @@ from unittest.mock import patch import node_monitor.load_config as c +import node_monitor.node_monitor_helpers.messages as messages +import node_monitor.ic_api as ic_api from node_monitor.bot_telegram import TelegramBot @patch("requests.get") @@ -25,8 +27,32 @@ def test_send_message(mock_get): def test_send_live_message(): telegram_bot = TelegramBot(c.TOKEN_TELEGRAM) chat_id = "-1001925583150" - message = "Test message" - err = telegram_bot.send_message(chat_id, message) - if err is not None: - raise err + ## Create a fake node model + fakenode = ic_api.Node( + dc_id = 'fake_dc_id', + dc_name = 'fake_dc_name', + node_id = 'fake_node_id', + node_operator_id = 'fake_node_operator_id', + node_provider_id = 'fake_node_provider_id', + node_provider_name = 'fake_node_provider_name', + owner = 'fake_owner', + region = 'fake_region', + status = 'DOWN', + subnet_id = 'fake_subnet_id', + ) + fakelabel = {'fake_node_id': 'fake_label'} + fakeurl = 'https://forms.gle/thisisfake' + + subject1, message1 = messages.nodes_down_message( + [fakenode], fakelabel, fakeurl) + subject2, message2 = messages.nodes_status_message( + [fakenode], fakelabel, fakeurl) + + err1 = telegram_bot.send_message(chat_id, message1) + err2 = telegram_bot.send_message(chat_id, message2) + if err1 is not None: + raise err1 + if err2 is not None: + raise err2 + From ff9ce40d4046a0dc36d8b2111654015d6c8e153f Mon Sep 17 00:00:00 2001 From: louisevelayo Date: Tue, 24 Oct 2023 11:24:08 +0200 Subject: [PATCH 4/8] use 'FEEDBACK_FORM_URL' instead of 'FEEDBACK_FORM' --- node_monitor/load_config.py | 22 +++++++++---------- node_monitor/node_monitor.py | 4 ++-- node_monitor/node_monitor_helpers/messages.py | 8 +++---- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/node_monitor/load_config.py b/node_monitor/load_config.py index 41c9141b..1fdbadf8 100644 --- a/node_monitor/load_config.py +++ b/node_monitor/load_config.py @@ -7,17 +7,17 @@ ############################################## ## Secrets -EMAIL_USERNAME = os.environ.get('EMAIL_USERNAME', '') -EMAIL_PASSWORD = os.environ.get('EMAIL_PASSWORD', '') -TOKEN_DISCORD = os.environ.get('TOKEN_DISCORD', '') # Not implemented -TOKEN_SLACK = os.environ.get('TOKEN_SLACK', '') -TOKEN_TELEGRAM = os.environ.get('TOKEN_TELEGRAM', '') -DB_HOST = os.environ.get('DB_HOST', '') -DB_USERNAME = os.environ.get('DB_USERNAME', '') -DB_PASSWORD = os.environ.get('DB_PASSWORD', '') -DB_NAME = os.environ.get('DB_NAME', '') -DB_PORT = os.environ.get('DB_PORT', '') -FEEDBACK_FORM = os.environ.get('FEEDBACK_FORM', '') +EMAIL_USERNAME = os.environ.get('EMAIL_USERNAME', '') +EMAIL_PASSWORD = os.environ.get('EMAIL_PASSWORD', '') +TOKEN_DISCORD = os.environ.get('TOKEN_DISCORD', '') # Not implemented +TOKEN_SLACK = os.environ.get('TOKEN_SLACK', '') +TOKEN_TELEGRAM = os.environ.get('TOKEN_TELEGRAM', '') +DB_HOST = os.environ.get('DB_HOST', '') +DB_USERNAME = os.environ.get('DB_USERNAME', '') +DB_PASSWORD = os.environ.get('DB_PASSWORD', '') +DB_NAME = os.environ.get('DB_NAME', '') +DB_PORT = os.environ.get('DB_PORT', '') +FEEDBACK_FORM_URL = os.environ.get('FEEDBACK_FORM_URL', '') ## Pre-flight check diff --git a/node_monitor/node_monitor.py b/node_monitor/node_monitor.py index 3b2b498f..f932ae1f 100644 --- a/node_monitor/node_monitor.py +++ b/node_monitor/node_monitor.py @@ -109,7 +109,7 @@ def broadcast_alerts(self) -> None: for node_provider_id, nodes in self.actionables.items(): preferences = subscribers[node_provider_id] subject, message = messages.nodes_down_message( - nodes, node_labels, c.FEEDBACK_FORM) + nodes, node_labels, c.FEEDBACK_FORM_URL) # - - - - - - - - - - - - - - - - - if preferences['notify_email'] == True: recipients = email_recipients[node_provider_id] @@ -145,7 +145,7 @@ def broadcast_status_report(self) -> None: for node_provider_id, nodes in reportable_nodes.items(): preferences = subscribers[node_provider_id] subject, message = messages.nodes_status_message( - nodes, node_labels, c.FEEDBACK_FORM) + nodes, node_labels, c.FEEDBACK_FORM_URL) # - - - - - - - - - - - - - - - - - if preferences['notify_email'] == True: recipients = email_recipients[node_provider_id] diff --git a/node_monitor/node_monitor_helpers/messages.py b/node_monitor/node_monitor_helpers/messages.py index e82c7a05..4dc7e952 100644 --- a/node_monitor/node_monitor_helpers/messages.py +++ b/node_monitor/node_monitor_helpers/messages.py @@ -55,7 +55,7 @@ def detailnodes(nodes: List[ic_api.Node], def nodes_down_message(nodes: List[ic_api.Node], labels: Dict[Principal, str], - feedback_form: str) -> Tuple[str, str]: + feedback_form_url: str) -> Tuple[str, str]: """Returns a message that describes the nodes that are down, in the format of an email or message for a comprable communication channel. """ @@ -76,14 +76,14 @@ def _make_subject() -> str: f"\n" f"Node Monitor by Aviate Labs\n" f"Report Generated: {datetime.utcnow().isoformat()} UTC\n" - f"Help us serve you better! Provide your feedback here: {feedback_form}\n") + f"Help us serve you better! Provide your feedback here: {feedback_form_url}\n") return (subject, message) def nodes_status_message(nodes: List[ic_api.Node], labels: Dict[Principal, str], - feedback_form: str) -> Tuple[str, str]: + feedback_form_url: str) -> Tuple[str, str]: """Returns a message that describes the status of all nodes, in the format of an email or message for a comprable communication channel. """ @@ -128,5 +128,5 @@ def _render_frac(numerator: int, denominator: int) -> str: f"Thanks for reviewing today's report. We'll be back tomorrow!\n" f"Node Monitor by Aviate Labs.\n" f"Report generated: {datetime.utcnow().isoformat()} UTC\n" - f"Help us serve you better! Provide your feedback here: {feedback_form} \n") + f"Help us serve you better! Provide your feedback here: {feedback_form_url} \n") return (subject, message) From bed6492812d249c182da2b5552f95cfcb088ea39 Mon Sep 17 00:00:00 2001 From: Ioannis Mourginakis Date: Tue, 24 Oct 2023 12:19:11 -0700 Subject: [PATCH 5/8] use unittest.mock.patch to redact email rather than passing email in as a parameter --- node_monitor/node_monitor.py | 7 ++---- node_monitor/node_monitor_helpers/messages.py | 10 ++++----- tests/test_bot_email.py | 22 ++++++++++--------- tests/test_bot_slack.py | 16 ++++++-------- tests/test_bot_telegram.py | 10 ++++----- 5 files changed, 29 insertions(+), 36 deletions(-) diff --git a/node_monitor/node_monitor.py b/node_monitor/node_monitor.py index f932ae1f..24a3d9a8 100644 --- a/node_monitor/node_monitor.py +++ b/node_monitor/node_monitor.py @@ -12,7 +12,6 @@ from node_monitor.node_monitor_helpers.get_compromised_nodes import \ get_compromised_nodes import node_monitor.node_monitor_helpers.messages as messages -import node_monitor.load_config as c Seconds = int Principal = str @@ -108,8 +107,7 @@ def broadcast_alerts(self) -> None: channels = self.node_provider_db.get_channels_as_dict() for node_provider_id, nodes in self.actionables.items(): preferences = subscribers[node_provider_id] - subject, message = messages.nodes_down_message( - nodes, node_labels, c.FEEDBACK_FORM_URL) + subject, message = messages.nodes_down_message(nodes, node_labels) # - - - - - - - - - - - - - - - - - if preferences['notify_email'] == True: recipients = email_recipients[node_provider_id] @@ -144,8 +142,7 @@ def broadcast_status_report(self) -> None: # - - - - - - - - - - - - - - - - - for node_provider_id, nodes in reportable_nodes.items(): preferences = subscribers[node_provider_id] - subject, message = messages.nodes_status_message( - nodes, node_labels, c.FEEDBACK_FORM_URL) + subject, message = messages.nodes_status_message(nodes, node_labels) # - - - - - - - - - - - - - - - - - if preferences['notify_email'] == True: recipients = email_recipients[node_provider_id] diff --git a/node_monitor/node_monitor_helpers/messages.py b/node_monitor/node_monitor_helpers/messages.py index 4dc7e952..063a4f7e 100644 --- a/node_monitor/node_monitor_helpers/messages.py +++ b/node_monitor/node_monitor_helpers/messages.py @@ -54,8 +54,7 @@ def detailnodes(nodes: List[ic_api.Node], def nodes_down_message(nodes: List[ic_api.Node], - labels: Dict[Principal, str], - feedback_form_url: str) -> Tuple[str, str]: + labels: Dict[Principal, str]) -> Tuple[str, str]: """Returns a message that describes the nodes that are down, in the format of an email or message for a comprable communication channel. """ @@ -76,14 +75,13 @@ def _make_subject() -> str: f"\n" f"Node Monitor by Aviate Labs\n" f"Report Generated: {datetime.utcnow().isoformat()} UTC\n" - f"Help us serve you better! Provide your feedback here: {feedback_form_url}\n") + f"Help us serve you better! Provide your feedback here: {c.FEEDBACK_FORM_URL}\n") return (subject, message) def nodes_status_message(nodes: List[ic_api.Node], - labels: Dict[Principal, str], - feedback_form_url: str) -> Tuple[str, str]: + labels: Dict[Principal, str]) -> Tuple[str, str]: """Returns a message that describes the status of all nodes, in the format of an email or message for a comprable communication channel. """ @@ -128,5 +126,5 @@ def _render_frac(numerator: int, denominator: int) -> str: f"Thanks for reviewing today's report. We'll be back tomorrow!\n" f"Node Monitor by Aviate Labs.\n" f"Report generated: {datetime.utcnow().isoformat()} UTC\n" - f"Help us serve you better! Provide your feedback here: {feedback_form_url} \n") + f"Help us serve you better! Provide your feedback here: {c.FEEDBACK_FORM_URL}\n") return (subject, message) diff --git a/tests/test_bot_email.py b/tests/test_bot_email.py index 7f2b704b..1c484132 100644 --- a/tests/test_bot_email.py +++ b/tests/test_bot_email.py @@ -55,23 +55,25 @@ def test_send_emails_network(): subnet_id = 'fake_subnet_id', ) fakelabel = {'fake_node_id': 'fake_label'} - fakeurl = 'https://forms.gle/thisisfake' ## Init the authenticated email bot instance email_bot = EmailBot(c.EMAIL_USERNAME, c.EMAIL_PASSWORD) - ## Set paramaters (uses an anonymous email inbox for testing) - ## Send out nodes_down_message and nodes_status_message. We test both. - ## We append time to the subject to act as an identifier for the test. + ## Set recipient (we use an anonymous email inbox for testing) recipients = ['nodemonitortest@mailnesia.com'] - subject1, message1 = messages.nodes_down_message( - [fakenode], fakelabel, fakeurl) - subject1 = str(f'{time.time()} - {subject1}') - email_bot.send_emails(recipients, subject1, message1) - subject2, message2 = messages.nodes_status_message( - [fakenode], fakelabel, fakeurl) + ## Create the messages. We use unittest.mock.patch to remove the private URL. + with patch.object(c, 'FEEDBACK_FORM_URL', 'https://url-has-been-redacted.ninja'): + subject1, message1 = messages.nodes_down_message([fakenode], fakelabel) + subject2, message2 = messages.nodes_status_message([fakenode], fakelabel) + + ## Append the time to the subject to act as an identifier for the test, + ## making it easy to do a regex search to validate the email. + subject1 = str(f'{time.time()} - {subject1}') subject2 = str(f'{time.time()} - {subject2}') + + ## Send both nodes_down_message and nodes_status_message as emails. + email_bot.send_emails(recipients, subject1, message1) email_bot.send_emails(recipients, subject2, message2) ## Automatically check the email inbox diff --git a/tests/test_bot_slack.py b/tests/test_bot_slack.py index 83a88b6f..de36cddb 100644 --- a/tests/test_bot_slack.py +++ b/tests/test_bot_slack.py @@ -41,17 +41,15 @@ def test_send_message_slack(): subnet_id = 'fake_subnet_id', ) fakelabel = {'fake_node_id': 'fake_label'} - fakeurl = 'https://forms.gle/thisisfake' slack_channel_name = "node-monitor" - subject1, message1 = messages.nodes_down_message( - [fakenode], fakelabel, fakeurl) - subject2, message2 = messages.nodes_status_message( - [fakenode], fakelabel, fakeurl) - - # SlackBot.send_message() returns an error without raising an exception - # to prevent NodeMonitor from crashing if the message fails to send. - # Instead, we raise it here. + with patch.object(c, 'FEEDBACK_FORM_URL', 'https://url-has-been-redacted.ninja'): + subject1, message1 = messages.nodes_down_message([fakenode], fakelabel) + subject2, message2 = messages.nodes_status_message([fakenode], fakelabel) + + ## SlackBot.send_message() normally returns an error without raising + ## an exception to prevent NodeMonitor from crashing if the message + ## fails to send. We make sure to raise it here to purposely fail the test. err1 = slack_bot.send_message(slack_channel_name, message1) err2 = slack_bot.send_message(slack_channel_name, message2) if err1 is not None: diff --git a/tests/test_bot_telegram.py b/tests/test_bot_telegram.py index 964bb6d6..6387abc7 100644 --- a/tests/test_bot_telegram.py +++ b/tests/test_bot_telegram.py @@ -42,12 +42,10 @@ def test_send_live_message(): subnet_id = 'fake_subnet_id', ) fakelabel = {'fake_node_id': 'fake_label'} - fakeurl = 'https://forms.gle/thisisfake' - - subject1, message1 = messages.nodes_down_message( - [fakenode], fakelabel, fakeurl) - subject2, message2 = messages.nodes_status_message( - [fakenode], fakelabel, fakeurl) + + with patch.object(c, 'FEEDBACK_FORM_URL', 'https://url-has-been-redacted.ninja'): + subject1, message1 = messages.nodes_down_message([fakenode], fakelabel) + subject2, message2 = messages.nodes_status_message([fakenode], fakelabel) err1 = telegram_bot.send_message(chat_id, message1) err2 = telegram_bot.send_message(chat_id, message2) From 7a6039a3f8b56b4786b02cb7428ca4f4a9198857 Mon Sep 17 00:00:00 2001 From: Ioannis Mourginakis Date: Tue, 24 Oct 2023 12:24:02 -0700 Subject: [PATCH 6/8] add example to call telegram tests --- tests/conftest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/conftest.py b/tests/conftest.py index 013c9b72..f88566a2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,6 +9,7 @@ ## --db is a custom flag to test CRUD operations on the database ## example: pytest -s --send_emails tests/test_bot_email.py ## example: pytest -s --send_slack tests/test_bot_slack.py +## example: pytest -s --send_telegram tests/test_bot_telegram.py ## example: pytest -s --db tests/test_node_provider_db.py From 3f9898ac0beffb428d9bb68240ae590c3888074e Mon Sep 17 00:00:00 2001 From: louisevelayo Date: Wed, 25 Oct 2023 11:29:52 +0200 Subject: [PATCH 7/8] use short string for slack and telegram live message testing --- tests/test_bot_slack.py | 29 ++++------------------------- tests/test_bot_telegram.py | 29 ++++------------------------- 2 files changed, 8 insertions(+), 50 deletions(-) diff --git a/tests/test_bot_slack.py b/tests/test_bot_slack.py index de36cddb..0592cb19 100644 --- a/tests/test_bot_slack.py +++ b/tests/test_bot_slack.py @@ -26,33 +26,12 @@ def test_send_message(mock_web_client): def test_send_message_slack(): """Send a real test message to a Slack workspace""" slack_bot = SlackBot(c.TOKEN_SLACK) - - ## Create a fake node model - fakenode = ic_api.Node( - dc_id = 'fake_dc_id', - dc_name = 'fake_dc_name', - node_id = 'fake_node_id', - node_operator_id = 'fake_node_operator_id', - node_provider_id = 'fake_node_provider_id', - node_provider_name = 'fake_node_provider_name', - owner = 'fake_owner', - region = 'fake_region', - status = 'DOWN', - subnet_id = 'fake_subnet_id', - ) - fakelabel = {'fake_node_id': 'fake_label'} - slack_channel_name = "node-monitor" - with patch.object(c, 'FEEDBACK_FORM_URL', 'https://url-has-been-redacted.ninja'): - subject1, message1 = messages.nodes_down_message([fakenode], fakelabel) - subject2, message2 = messages.nodes_status_message([fakenode], fakelabel) + message = "🔬 This is a test message from Node Monitor" ## SlackBot.send_message() normally returns an error without raising ## an exception to prevent NodeMonitor from crashing if the message ## fails to send. We make sure to raise it here to purposely fail the test. - err1 = slack_bot.send_message(slack_channel_name, message1) - err2 = slack_bot.send_message(slack_channel_name, message2) - if err1 is not None: - raise err1 - if err2 is not None: - raise err2 + err = slack_bot.send_message(slack_channel_name, message) + if err is not None: + raise err diff --git a/tests/test_bot_telegram.py b/tests/test_bot_telegram.py index 6387abc7..faa022ab 100644 --- a/tests/test_bot_telegram.py +++ b/tests/test_bot_telegram.py @@ -27,30 +27,9 @@ def test_send_message(mock_get): def test_send_live_message(): telegram_bot = TelegramBot(c.TOKEN_TELEGRAM) chat_id = "-1001925583150" + message = "🔬 This is a test message from Node Monitor" - ## Create a fake node model - fakenode = ic_api.Node( - dc_id = 'fake_dc_id', - dc_name = 'fake_dc_name', - node_id = 'fake_node_id', - node_operator_id = 'fake_node_operator_id', - node_provider_id = 'fake_node_provider_id', - node_provider_name = 'fake_node_provider_name', - owner = 'fake_owner', - region = 'fake_region', - status = 'DOWN', - subnet_id = 'fake_subnet_id', - ) - fakelabel = {'fake_node_id': 'fake_label'} - - with patch.object(c, 'FEEDBACK_FORM_URL', 'https://url-has-been-redacted.ninja'): - subject1, message1 = messages.nodes_down_message([fakenode], fakelabel) - subject2, message2 = messages.nodes_status_message([fakenode], fakelabel) - - err1 = telegram_bot.send_message(chat_id, message1) - err2 = telegram_bot.send_message(chat_id, message2) - if err1 is not None: - raise err1 - if err2 is not None: - raise err2 + err = telegram_bot.send_message(chat_id, message) + if err is not None: + raise err From 9c17e85e2ee01ec4655c6b33f044ef6840400cf1 Mon Sep 17 00:00:00 2001 From: Ioannis Mourginakis Date: Thu, 26 Oct 2023 10:10:49 -0700 Subject: [PATCH 8/8] remove unused imports --- tests/test_bot_slack.py | 2 -- tests/test_bot_telegram.py | 2 -- 2 files changed, 4 deletions(-) diff --git a/tests/test_bot_slack.py b/tests/test_bot_slack.py index 0592cb19..2ac64f73 100644 --- a/tests/test_bot_slack.py +++ b/tests/test_bot_slack.py @@ -3,8 +3,6 @@ from node_monitor.bot_slack import SlackBot import node_monitor.load_config as c -import node_monitor.node_monitor_helpers.messages as messages -import node_monitor.ic_api as ic_api @patch("slack_sdk.WebClient") diff --git a/tests/test_bot_telegram.py b/tests/test_bot_telegram.py index faa022ab..3f7aae6c 100644 --- a/tests/test_bot_telegram.py +++ b/tests/test_bot_telegram.py @@ -2,8 +2,6 @@ from unittest.mock import patch import node_monitor.load_config as c -import node_monitor.node_monitor_helpers.messages as messages -import node_monitor.ic_api as ic_api from node_monitor.bot_telegram import TelegramBot @patch("requests.get")