Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

95 bugfix/form not hyperlinked #96

Merged
merged 9 commits into from
Oct 26, 2023
21 changes: 11 additions & 10 deletions node_monitor/load_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +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', '')
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
Expand Down
1 change: 1 addition & 0 deletions node_monitor/node_monitor_helpers/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
19 changes: 12 additions & 7 deletions tests/test_bot_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,21 @@ def test_send_emails_network():
## 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 = ['[email protected]']
subject1, message1 = messages.nodes_down_message([fakenode], fakelabel)
subject1 = str(f'{time.time()} - {subject1}')
email_bot.send_emails(recipients, subject1, message1)

subject2, message2 = messages.nodes_status_message([fakenode], fakelabel)
## 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
Expand Down
13 changes: 7 additions & 6 deletions tests/test_bot_slack.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -24,13 +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)

slack_channel_name = "node-monitor"
message = "🔬 Hello from test_send_message_slack()"
message = "🔬 This is a test message from Node Monitor"

# 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.
## 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.
err = slack_bot.send_message(slack_channel_name, message)
if err is not None:
raise err
5 changes: 4 additions & 1 deletion tests/test_bot_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -25,8 +27,9 @@ def test_send_message(mock_get):
def test_send_live_message():
telegram_bot = TelegramBot(c.TOKEN_TELEGRAM)
chat_id = "-1001925583150"
message = "Test message"
message = "🔬 This is a test message from Node Monitor"

err = telegram_bot.send_message(chat_id, message)
if err is not None:
raise err

Loading