Files
application-form-7/tests/test_email.py
T
gurix f1d8bae6a1 refactor: split monolithic app.py into modular Flask application with factory pattern
- Created app/ package with 6 modules:
  - __init__.py: Application factory (create_app function)
  - routes.py: All route handlers (473 lines)
  - models.py: Data persistence layer (41 lines)
  - validators.py: Input validation functions (48 lines)
  - email_service.py: Email sending functions (85 lines)
  - utils.py: Utility functions (30 lines)

- Implemented Flask application factory pattern
- Created run.py as minimal entry point
- Updated all test imports to use new module structure
- Fixed template/static folder paths for package structure

- All 102 tests passing
- Improved maintainability with clear separation of concerns
- Follows Flask best practices for scalable applications
2025-12-27 22:36:05 +01:00

96 lines
3.8 KiB
Python

"""
Tests for email functionality.
"""
import pytest
from unittest.mock import patch, MagicMock
from flask_mail import Message
from app.email_service import send_resume_email
class TestEmailSending:
"""Tests for email sending functionality."""
def test_send_resume_email_success(self, app):
"""Test successfully sending a resume email."""
with app.app_context():
with patch('app.email_service.mail.send') as mock_send:
result = send_resume_email('test@example.com', 'test-session-123', 'Software Developer')
assert result is True
assert mock_send.called
def test_send_resume_email_failure(self, app):
"""Test handling email sending failure."""
with app.app_context():
with patch('app.email_service.mail.send') as mock_send:
mock_send.side_effect = Exception('SMTP error')
result = send_resume_email('test@example.com', 'test-session-123', 'Test Job')
assert result is False
def test_resume_email_content(self, app):
"""Test that resume email contains correct content."""
with app.app_context():
with patch('app.email_service.mail.send') as mock_send:
send_resume_email('applicant@example.com', 'abc-123', 'Marketing Manager')
# Get the Message object that was passed to send()
assert mock_send.called
call_args = mock_send.call_args
message = call_args[0][0] if call_args[0] else None
if message:
assert isinstance(message, Message)
assert 'applicant@example.com' in message.recipients
assert 'Bewerbung' in message.subject or 'Marketing Manager' in message.subject
def test_resume_link_in_email(self, app):
"""Test that resume link is included in email body."""
with app.app_context():
with patch('app.email_service.mail.send') as mock_send:
send_resume_email('test@example.com', 'session-xyz', 'Test Position')
if mock_send.called:
call_args = mock_send.call_args
message = call_args[0][0] if call_args[0] else None
if message and hasattr(message, 'body'):
assert 'session-xyz' in message.body or '/resume/' in message.body
class TestEmailIntegration:
"""Tests for email integration with application workflow."""
def test_email_sent_on_initial_submission(self, client, app):
"""Test that email is sent when user submits their email address."""
with patch('app.email_service.mail.send') as mock_send:
response = client.post('/apply/submit-email', data={
'email': 'newuser@example.com',
'job_name': 'Junior Developer'
}, follow_redirects=True)
assert response.status_code == 200
# Email should be sent
assert mock_send.called or app.config.get('MAIL_SUPPRESS_SEND') is True
def test_email_suppressed_in_testing(self, app):
"""Test that email is suppressed during testing."""
assert app.config['TESTING'] is True
assert app.config.get('MAIL_SUPPRESS_SEND') is True
class TestEmailConfiguration:
"""Tests for email configuration."""
def test_email_config_exists(self, app):
"""Test that email configuration is set."""
assert 'MAIL_SERVER' in app.config
assert 'MAIL_PORT' in app.config
assert 'MAIL_USE_TLS' in app.config
def test_mail_suppress_send_in_testing(self, app):
"""Test that email sending is suppressed in test mode."""
assert app.config['TESTING'] is True
assert app.config['MAIL_SUPPRESS_SEND'] is True