fix: use print instead of logger for dev mode email output

Logger.info wasn't visible in gunicorn container logs. Using print
with flush=True ensures magic link URLs are visible in container
logs for QA testing.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-22 18:16:48 -07:00
parent 5a9f0d552a
commit 80f7926985
2 changed files with 30 additions and 31 deletions

View File

@@ -61,13 +61,13 @@ class EmailService:
Exception: If email sending fails Exception: If email sending fails
""" """
if self.dev_mode: if self.dev_mode:
# In dev mode, just log the email # In dev mode, print email details to stdout for QA testing
logger.info("=" * 80) print("=" * 80)
logger.info("DEV MODE: Email not sent") print("DEV MODE: Email not sent")
logger.info(f"To: {to}") print(f"To: {to}")
logger.info(f"Subject: {subject}") print(f"Subject: {subject}")
logger.info(f"Body preview: {html_body[:200]}...") print(f"Body preview: {html_body[:200]}...")
logger.info("=" * 80) print("=" * 80, flush=True)
# Return a mock success response # Return a mock success response
return {"id": f"dev-mode-{os.urandom(8).hex()}"} return {"id": f"dev-mode-{os.urandom(8).hex()}"}
@@ -99,7 +99,7 @@ class EmailService:
Response from email service Response from email service
""" """
if self.dev_mode: if self.dev_mode:
logger.info(f"DEV MODE: Magic link URL: {magic_link_url}") print(f"DEV MODE: Magic link URL: {magic_link_url}", flush=True)
subject = f"Access your {exchange_name} Secret Santa" subject = f"Access your {exchange_name} Secret Santa"
html_body = f""" html_body = f"""
@@ -143,7 +143,7 @@ class EmailService:
Response from email service Response from email service
""" """
if self.dev_mode: if self.dev_mode:
logger.info(f"DEV MODE: Magic link URL: {magic_link_url}") print(f"DEV MODE: Magic link URL: {magic_link_url}", flush=True)
subject = f"Welcome to {exchange_name}!" subject = f"Welcome to {exchange_name}!"

View File

@@ -1,6 +1,5 @@
"""Tests for email service.""" """Tests for email service."""
import logging
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
@@ -89,26 +88,26 @@ class TestEmailService:
assert call_args["from"] == "custom@example.com" assert call_args["from"] == "custom@example.com"
@patch("resend.Emails.send") @patch("resend.Emails.send")
def test_send_email_in_dev_mode_logs_instead(self, mock_send, app, caplog): def test_send_email_in_dev_mode_logs_instead(self, mock_send, app, capsys):
"""Test that email sending logs in dev mode instead of actually sending.""" """Test that email sending prints in dev mode instead of actually sending."""
with app.app_context(): with app.app_context():
app.config["FLASK_ENV"] = "development" app.config["FLASK_ENV"] = "development"
service = EmailService() service = EmailService()
with caplog.at_level(logging.INFO): result = service.send_email(
result = service.send_email( to="test@example.com",
to="test@example.com", subject="Test Subject",
subject="Test Subject", html_body="<p>Test body</p>",
html_body="<p>Test body</p>", )
)
# Should not actually send email # Should not actually send email
mock_send.assert_not_called() mock_send.assert_not_called()
# Should log the email details # Should print the email details
assert "DEV MODE: Email not sent" in caplog.text captured = capsys.readouterr()
assert "test@example.com" in caplog.text assert "DEV MODE: Email not sent" in captured.out
assert "Test Subject" in caplog.text assert "test@example.com" in captured.out
assert "Test Subject" in captured.out
# Should return a mock success response # Should return a mock success response
assert result["id"].startswith("dev-mode-") assert result["id"].startswith("dev-mode-")
@@ -182,20 +181,20 @@ class TestEmailService:
assert "$25-50" in call_args["html_body"] assert "$25-50" in call_args["html_body"]
assert "2025-12-20" in call_args["html_body"] assert "2025-12-20" in call_args["html_body"]
def test_dev_mode_logs_magic_link_url(self, app, caplog): def test_dev_mode_logs_magic_link_url(self, app, capsys):
"""Test that magic link URLs are logged in dev mode.""" """Test that magic link URLs are printed in dev mode."""
with app.app_context(): with app.app_context():
app.config["FLASK_ENV"] = "development" app.config["FLASK_ENV"] = "development"
service = EmailService() service = EmailService()
with caplog.at_level(logging.INFO): service.send_magic_link(
service.send_magic_link( to="test@example.com",
to="test@example.com", magic_link_url="https://example.com/magic/abc123",
magic_link_url="https://example.com/magic/abc123", exchange_name="Secret Santa 2025",
exchange_name="Secret Santa 2025", )
)
captured = capsys.readouterr()
assert ( assert (
"DEV MODE: Magic link URL: https://example.com/magic/abc123" "DEV MODE: Magic link URL: https://example.com/magic/abc123"
in caplog.text in captured.out
) )