Files
application-form-7/tests/test_hr_notifications.py
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

262 lines
9.6 KiB
Python

"""
Tests for HR email notifications functionality.
"""
import pytest
from unittest.mock import patch, MagicMock
from app.email_service import send_hr_notification
class TestHRNotifications:
"""Test HR notification email functionality."""
def test_hr_notification_sent_on_submission(self, app, client, create_test_application, mock_mail):
"""Test that HR receives email when application is submitted."""
# Set HR_EMAIL in config
app.config['HR_EMAIL'] = 'hr@example.com'
app.config['APPLICATION_URL_BASE'] = 'http://localhost:5000'
# Create a test application with all required data
session_id = 'test-session-123'
create_test_application(
session_id=session_id,
email='applicant@example.com',
job_name='Software Developer',
current_page=4,
personal_info={
'name': 'Müller',
'firstname': 'Anna',
'address': 'Hauptstrasse 123',
'zip_code': '8001',
'city': 'Zürich',
'phone': '+41 79 123 45 67',
'birth_year': '1990',
'civil_status': 'ledig'
},
motivation_answers={
'current_job': 'Developer',
'motivation': 'Great company',
'qualifications': 'Python expert',
'salary': '100000'
}
)
# Submit the application
response = client.post(f'/apply/{session_id}/submit-application')
# Verify redirect to confirmation page
assert response.status_code == 302
assert f'/apply/{session_id}/confirmation' in response.location
# Verify HR email was sent
assert len(mock_mail.sent_messages) == 1
hr_email = mock_mail.sent_messages[0]
assert 'hr@example.com' in hr_email.recipients
assert 'Neue Bewerbung eingegangen: Software Developer' == hr_email.subject
assert 'Anna Müller' in hr_email.body
assert 'applicant@example.com' in hr_email.body
assert f'/application/{session_id}/' in hr_email.body
def test_hr_notification_not_sent_when_hr_email_not_configured(
self, app, client, create_test_application, mock_mail, caplog
):
"""Test that HR notification is skipped when HR_EMAIL is not configured."""
# Ensure HR_EMAIL is not set
app.config['HR_EMAIL'] = None
# Create a test application
session_id = 'test-session-456'
create_test_application(
session_id=session_id,
email='applicant@example.com',
job_name='Data Analyst',
current_page=4,
personal_info={
'name': 'Schmidt',
'firstname': 'Max',
'address': 'Bahnhofstrasse 1',
'zip_code': '8000',
'city': 'Zürich',
'phone': '+41 79 999 88 77',
'birth_year': '1985',
'civil_status': 'verheiratet'
}
)
# Submit the application
response = client.post(f'/apply/{session_id}/submit-application')
# Verify redirect to confirmation page
assert response.status_code == 302
# Verify no HR email was sent
assert len(mock_mail.sent_messages) == 0
# Verify warning was logged
assert 'HR_EMAIL not configured' in caplog.text
def test_hr_notification_includes_file_count(self, app, client, create_test_application, mock_mail):
"""Test that HR notification includes the count of uploaded files."""
app.config['HR_EMAIL'] = 'hr@example.com'
app.config['APPLICATION_URL_BASE'] = 'http://localhost:5000'
# Create application with uploaded files
session_id = 'test-session-789'
create_test_application(
session_id=session_id,
email='applicant@example.com',
job_name='Project Manager',
current_page=4,
personal_info={
'name': 'Weber',
'firstname': 'Lisa',
'address': 'Seestrasse 45',
'zip_code': '8002',
'city': 'Zürich',
'phone': '+41 79 555 44 33',
'birth_year': '1992',
'civil_status': 'ledig'
},
uploaded_files=[
{
'original_name': 'cv.pdf',
'stored_name': '20250101_120000_cv.pdf',
'size': 102400,
'uploaded_at': '2025-01-01T12:00:00'
},
{
'original_name': 'certificate.pdf',
'stored_name': '20250101_120100_certificate.pdf',
'size': 204800,
'uploaded_at': '2025-01-01T12:01:00'
}
]
)
# Submit the application
response = client.post(f'/apply/{session_id}/submit-application')
assert response.status_code == 302
# Verify HR email contains file count
assert len(mock_mail.sent_messages) == 1
hr_email = mock_mail.sent_messages[0]
assert 'Anzahl der hochgeladenen Dokumente: 2' in hr_email.body
def test_hr_notification_with_zero_files(self, app, client, create_test_application, mock_mail):
"""Test that HR notification works even when no files are uploaded."""
app.config['HR_EMAIL'] = 'hr@example.com'
app.config['APPLICATION_URL_BASE'] = 'http://localhost:5000'
# Create application without uploaded files
session_id = 'test-session-000'
create_test_application(
session_id=session_id,
email='applicant@example.com',
job_name='Sales Representative',
current_page=4,
personal_info={
'name': 'Fischer',
'firstname': 'Tom',
'address': 'Hauptplatz 10',
'zip_code': '8003',
'city': 'Zürich',
'phone': '+41 79 111 22 33',
'birth_year': '1988',
'civil_status': 'ledig'
},
uploaded_files=[]
)
# Submit the application
response = client.post(f'/apply/{session_id}/submit-application')
assert response.status_code == 302
# Verify HR email was sent with 0 file count
assert len(mock_mail.sent_messages) == 1
hr_email = mock_mail.sent_messages[0]
assert 'Anzahl der hochgeladenen Dokumente: 0' in hr_email.body
def test_send_hr_notification_function_directly(self, app):
"""Test the send_hr_notification function directly."""
app.config['HR_EMAIL'] = 'hr@example.com'
app.config['APPLICATION_URL_BASE'] = 'http://localhost:5000'
session_id = 'test-123'
app_data = {
'email': 'applicant@example.com',
'job_name': 'Test Job',
'submitted_at': '2025-01-01T12:00:00',
'personal_info': {
'firstname': 'John',
'name': 'Doe'
},
'uploaded_files': [
{'original_name': 'cv.pdf', 'stored_name': 'cv_stored.pdf', 'size': 1024}
]
}
with app.app_context():
# Mock mail.send to prevent actual sending
with patch('app.email_service.mail.send') as mock_send:
result = send_hr_notification(session_id, app_data)
assert result is True
assert mock_send.called
def test_send_hr_notification_handles_email_failure(self, app, caplog):
"""Test that send_hr_notification handles email sending failures gracefully."""
app.config['HR_EMAIL'] = 'hr@example.com'
app.config['APPLICATION_URL_BASE'] = 'http://localhost:5000'
session_id = 'test-456'
app_data = {
'email': 'applicant@example.com',
'job_name': 'Test Job',
'submitted_at': '2025-01-01T12:00:00',
'personal_info': {
'firstname': 'Jane',
'name': 'Smith'
},
'uploaded_files': []
}
with app.app_context():
# Mock mail.send to raise an exception
with patch('app.email_service.mail.send', side_effect=Exception('SMTP error')):
result = send_hr_notification(session_id, app_data)
assert result is False
assert 'Failed to send HR notification email' in caplog.text
def test_hr_notification_url_format(self, app, client, create_test_application, mock_mail):
"""Test that the application URL in HR notification is correctly formatted."""
app.config['HR_EMAIL'] = 'hr@example.com'
app.config['APPLICATION_URL_BASE'] = 'https://example.com'
session_id = 'test-url-format'
create_test_application(
session_id=session_id,
email='applicant@example.com',
job_name='Test Position',
current_page=4,
personal_info={
'name': 'Test',
'firstname': 'User',
'address': 'Street 1',
'zip_code': '8000',
'city': 'City',
'phone': '+41 79 123 45 67',
'birth_year': '1990',
'civil_status': 'ledig'
}
)
# Submit the application
client.post(f'/apply/{session_id}/submit-application')
# Verify URL format in email
assert len(mock_mail.sent_messages) == 1
hr_email = mock_mail.sent_messages[0]
expected_url = f'https://example.com/application/{session_id}/'
assert expected_url in hr_email.body