Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/utils/email_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ def send_email_alert(subject: str, body: str) -> None:
return

sender_email = EMAIL_SENDER
receiver_emails = EMAIL_RECIPIENTS.split(",")
receiver_emails = [
email.strip()
for email in EMAIL_RECIPIENTS.split(",")
if email.strip()
]

if not receiver_emails:
logging.info("No email recipients configured; skipping email alert.")
return

message = MIMEMultipart()
message["From"] = sender_email
Expand Down
20 changes: 20 additions & 0 deletions tests/test_email_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ def test_send_email_alert_enabled(self):
self.assertIn("Subject: Subject", args[2])
self.assertIn("Body", args[2])

def test_send_email_alert_no_recipients(self):
with (
patch("smtplib.SMTP") as mock_smtp,
patch("app.utils.email_alerts.EMAIL_ENABLED", "True"),
patch("app.utils.email_alerts.EMAIL_SENDER", "sender@example.com"),
patch("app.utils.email_alerts.EMAIL_RECIPIENTS", " , , "),
patch("app.utils.email_alerts.EMAIL_SMTP_SERVER", "smtp.example.com"),
patch("app.utils.email_alerts.EMAIL_SMTP_PORT", "587"),
patch("app.utils.email_alerts.EMAIL_SMTP_TIMEOUT", 5),
patch("app.utils.email_alerts.EMAIL_USE_TLS", "true"),
patch("app.utils.email_alerts.EMAIL_USERNAME", "user"),
patch("app.utils.email_alerts.EMAIL_PASSWORD", "pass"),
patch("logging.info") as mock_log_info,
):
send_email_alert("Subject", "Body")
mock_smtp.assert_not_called()
mock_log_info.assert_called_once_with(
"No email recipients configured; skipping email alert."
)

def test_send_email_alert_timeout(self):
smtp_mock = MagicMock()
mock_cm = MagicMock()
Expand Down
Loading