137 lines
4.5 KiB
Python
137 lines
4.5 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""
|
||
|
|
Test email sending functionality
|
||
|
|
"""
|
||
|
|
|
||
|
|
import asyncio
|
||
|
|
from email.mime.text import MIMEText
|
||
|
|
from email.mime.multipart import MIMEMultipart
|
||
|
|
|
||
|
|
def test_email_message_construction():
|
||
|
|
"""Test that email messages are constructed correctly"""
|
||
|
|
print("Testing Email Message Construction...")
|
||
|
|
|
||
|
|
# Create a test message like the email service does
|
||
|
|
message = MIMEMultipart('alternative')
|
||
|
|
message['Subject'] = 'Test Subject'
|
||
|
|
message['To'] = 'test@example.com'
|
||
|
|
message['From'] = 'noreply@bakeryforecast.es'
|
||
|
|
|
||
|
|
# Add text content
|
||
|
|
text_part = MIMEText('This is a test email', 'plain', 'utf-8')
|
||
|
|
message.attach(text_part)
|
||
|
|
|
||
|
|
# Verify message structure
|
||
|
|
assert message['Subject'] == 'Test Subject'
|
||
|
|
assert message['To'] == 'test@example.com'
|
||
|
|
assert message['From'] == 'noreply@bakeryforecast.es'
|
||
|
|
# Check that the message has the expected structure
|
||
|
|
assert len(message.get_payload()) == 1
|
||
|
|
assert message.get_payload()[0].get_content_type() == 'text/plain'
|
||
|
|
|
||
|
|
print("✓ Email message constructed correctly")
|
||
|
|
print(f" Subject: {message['Subject']}")
|
||
|
|
print(f" To: {message['To']}")
|
||
|
|
print(f" From: {message['From']}")
|
||
|
|
|
||
|
|
return True
|
||
|
|
|
||
|
|
async def test_aiosmtplib_compatibility():
|
||
|
|
"""Test aiosmtplib send_message compatibility"""
|
||
|
|
print("\nTesting aiosmtplib Compatibility...")
|
||
|
|
|
||
|
|
try:
|
||
|
|
import aiosmtplib
|
||
|
|
|
||
|
|
# Create a test message
|
||
|
|
message = MIMEMultipart('alternative')
|
||
|
|
message['Subject'] = 'Test Subject'
|
||
|
|
message['To'] = 'test@example.com'
|
||
|
|
message['From'] = 'noreply@bakeryforecast.es'
|
||
|
|
text_part = MIMEText('Test content', 'plain', 'utf-8')
|
||
|
|
message.attach(text_part)
|
||
|
|
|
||
|
|
# Test that send_message method exists and can be called
|
||
|
|
# (We won't actually send, just verify the method signature)
|
||
|
|
server = aiosmtplib.SMTP(hostname='localhost', port=1025)
|
||
|
|
|
||
|
|
# Check if send_message method accepts the message
|
||
|
|
import inspect
|
||
|
|
sig = inspect.signature(server.send_message)
|
||
|
|
params = list(sig.parameters.keys())
|
||
|
|
|
||
|
|
print(f" send_message parameters: {params}")
|
||
|
|
|
||
|
|
# The message should be the first parameter
|
||
|
|
assert 'message' in params or len(params) > 0
|
||
|
|
|
||
|
|
print("✓ aiosmtplib send_message method is compatible")
|
||
|
|
|
||
|
|
return True
|
||
|
|
|
||
|
|
except ImportError:
|
||
|
|
print("⚠ aiosmtplib not available for testing")
|
||
|
|
return True
|
||
|
|
except Exception as e:
|
||
|
|
print(f"✗ aiosmtplib test failed: {e}")
|
||
|
|
return False
|
||
|
|
|
||
|
|
def test_configuration_compatibility():
|
||
|
|
"""Test that the configuration is compatible with both modes"""
|
||
|
|
print("\nTesting Configuration Compatibility...")
|
||
|
|
|
||
|
|
# Test trusted relay configuration
|
||
|
|
config1 = {
|
||
|
|
'SMTP_HOST': 'mailu.local',
|
||
|
|
'SMTP_PORT': 587,
|
||
|
|
'SMTP_USER': '',
|
||
|
|
'SMTP_PASSWORD': '',
|
||
|
|
'SMTP_REQUIRE_AUTH': False
|
||
|
|
}
|
||
|
|
|
||
|
|
# Test authenticated configuration
|
||
|
|
config2 = {
|
||
|
|
'SMTP_HOST': 'smtp.sendgrid.net',
|
||
|
|
'SMTP_PORT': 587,
|
||
|
|
'SMTP_USER': 'apikey',
|
||
|
|
'SMTP_PASSWORD': 'test_key',
|
||
|
|
'SMTP_REQUIRE_AUTH': True
|
||
|
|
}
|
||
|
|
|
||
|
|
# Verify both configurations are valid
|
||
|
|
assert isinstance(config1['SMTP_REQUIRE_AUTH'], bool)
|
||
|
|
assert isinstance(config2['SMTP_REQUIRE_AUTH'], bool)
|
||
|
|
|
||
|
|
print("✓ Both configuration modes are valid")
|
||
|
|
print(f" Trusted relay: SMTP_REQUIRE_AUTH={config1['SMTP_REQUIRE_AUTH']}")
|
||
|
|
print(f" Authenticated: SMTP_REQUIRE_AUTH={config2['SMTP_REQUIRE_AUTH']}")
|
||
|
|
|
||
|
|
return True
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
print("Email Sending Functionality Test")
|
||
|
|
print("=" * 40)
|
||
|
|
|
||
|
|
try:
|
||
|
|
# Run tests
|
||
|
|
results = []
|
||
|
|
results.append(test_email_message_construction())
|
||
|
|
results.append(asyncio.run(test_aiosmtplib_compatibility()))
|
||
|
|
results.append(test_configuration_compatibility())
|
||
|
|
|
||
|
|
print("\n" + "=" * 40)
|
||
|
|
if all(results):
|
||
|
|
print("✓ All email sending tests passed!")
|
||
|
|
print("\nThe email service should now work correctly with:")
|
||
|
|
print("- Proper MIME message construction")
|
||
|
|
print("- Compatible aiosmtplib usage")
|
||
|
|
print("- Both trusted relay and authenticated modes")
|
||
|
|
else:
|
||
|
|
print("✗ Some tests failed!")
|
||
|
|
exit(1)
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"\n✗ Test failed with exception: {e}")
|
||
|
|
import traceback
|
||
|
|
traceback.print_exc()
|
||
|
|
exit(1)
|