92 lines
3.5 KiB
Python
92 lines
3.5 KiB
Python
"""
|
|
Tests for email functionality.
|
|
"""
|
|
import pytest
|
|
from unittest.mock import patch, MagicMock
|
|
from flask_mail import Message
|
|
from app 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 patch('app.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 patch('app.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 patch('app.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 patch('app.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.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
|