Files
OSIT-AE-API-FastAPI/tests/unit/test_unit_email.py
Scott Idem b2384f2869 Tests: Reorganize test suite into functional subdirectories
- Categorized scripts into tests/unit/, tests/integration/, tests/e2e/, and tests/tools/.
- Adopted consistent naming prefixes (test_unit_*, test_int_*, test_e2e_*, tool_*).
- Renamed conftest_mock.py to mock_config_helper.py for clarity.
- Updated test_int_boot_diagnosis.py with sys.path setup for root-level execution.
2026-01-16 10:46:19 -05:00

103 lines
3.2 KiB
Python

import unittest
from unittest.mock import MagicMock, patch
import sys
import os
# Add project root to path
sys.path.append(os.getcwd())
# --- Mocking Dependencies BEFORE Import ---
# 1. Mock html2text
mock_html2text = MagicMock()
mock_html2text.html2text.return_value = "Mock Text Content"
sys.modules['html2text'] = mock_html2text
# 2. Mock app.config
# We need to create a mock module and assign it to sys.modules['app.config']
mock_config = MagicMock()
# Create a Mock Settings object with an SMTP attribute
mock_settings = MagicMock()
mock_settings.SMTP = {}
mock_config.settings = mock_settings
sys.modules['app.config'] = mock_config
# 3. Mock app.log with a functional decorator
mock_log = MagicMock()
def simple_decorator(func):
"""Pass-through decorator."""
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
mock_log.logger_reset = simple_decorator
sys.modules['app.log'] = mock_log
# Now we can import the function to test
from app.lib_email import send_email
class TestEmailConfiguration(unittest.TestCase):
@patch('app.lib_email.smtplib')
def test_send_email_missing_server_config(self, mock_smtplib):
"""Test that send_email returns False gracefully if server/port are missing."""
print("\n--- Testing send_email with missing server/port ---")
# Setup the mock settings for this test
sys.modules['app.config'].settings.SMTP = {
'server': '',
'port': '',
'username': 'user',
'password': 'password'
}
result = send_email(
from_email="test@example.com",
to_email="recipient@example.com",
subject="Test",
body_html="<p>Body</p>",
test=True
)
print(f"Result: {result}")
self.assertFalse(result)
# Verify SMTP_SSL was NOT called
mock_smtplib.SMTP_SSL.assert_not_called()
@patch('app.lib_email.smtplib')
def test_send_email_valid_config(self, mock_smtplib):
"""Test that send_email attempts to connect if config is valid."""
print("\n--- Testing send_email with VALID config ---")
# Setup the mock settings for this test
sys.modules['app.config'].settings.SMTP = {
'server': 'smtp.example.com',
'port': '465',
'username': 'user',
'password': 'password'
}
# Mock successful connection context manager
mock_server_instance = MagicMock()
mock_smtplib.SMTP_SSL.return_value.__enter__.return_value = mock_server_instance
result = send_email(
from_email="test@example.com",
to_email="recipient@example.com",
subject="Test",
body_html="<p>Body</p>",
test=False # Attempt real send (mocked)
)
print(f"Result: {result}")
self.assertTrue(result)
# Verify connection and login were called
mock_smtplib.SMTP_SSL.assert_called_with('smtp.example.com', 465, context=unittest.mock.ANY)
mock_server_instance.login.assert_called_with('user', 'password')
mock_server_instance.send_message.assert_called()
if __name__ == '__main__':
unittest.main()