219 lines
6.8 KiB
Python
219 lines
6.8 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script to verify email composition functionality
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import django
|
|
|
|
# Setup Django environment
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'NorahUniversity.settings')
|
|
django.setup()
|
|
|
|
from django.test import TestCase, Client
|
|
from django.urls import reverse
|
|
from django.contrib.auth.models import User
|
|
from django.utils import timezone
|
|
from recruitment.models import JobPosting, Candidate
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
def test_email_composition_view():
|
|
"""Test the email composition view"""
|
|
print("Testing email composition view...")
|
|
|
|
# Create test user (delete if exists)
|
|
User.objects.filter(username='testuser').delete()
|
|
user = User.objects.create_user(
|
|
username='testuser',
|
|
email='test@example.com',
|
|
password='testpass123'
|
|
)
|
|
|
|
# Create test job
|
|
job = JobPosting.objects.create(
|
|
title='Test Job',
|
|
internal_job_id='TEST001',
|
|
description='Test job description',
|
|
status='active',
|
|
application_deadline=timezone.now() + timezone.timedelta(days=30)
|
|
)
|
|
|
|
# Add user to job participants so they appear in recipient choices
|
|
job.users.add(user)
|
|
|
|
# Create test candidate
|
|
candidate = Candidate.objects.create(
|
|
first_name='Test Candidate',
|
|
last_name='',
|
|
email='candidate@example.com',
|
|
phone='1234567890',
|
|
job=job
|
|
)
|
|
|
|
# Create client and login
|
|
client = Client()
|
|
client.login(username='testuser', password='testpass123')
|
|
|
|
# Test GET request to email composition view
|
|
url = reverse('compose_candidate_email', kwargs={
|
|
'job_slug': job.slug,
|
|
'candidate_slug': candidate.slug
|
|
})
|
|
|
|
try:
|
|
response = client.get(url)
|
|
print(f"✓ GET request successful: {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
print("✓ Email composition form rendered successfully")
|
|
|
|
# Check if form contains expected fields
|
|
content = response.content.decode('utf-8')
|
|
if 'subject' in content.lower():
|
|
print("✓ Subject field found in form")
|
|
if 'message' in content.lower():
|
|
print("✓ Message field found in form")
|
|
if 'recipients' in content.lower():
|
|
print("✓ Recipients field found in form")
|
|
|
|
else:
|
|
print(f"✗ Unexpected status code: {response.status_code}")
|
|
|
|
except Exception as e:
|
|
print(f"✗ Error testing GET request: {e}")
|
|
|
|
# Test POST request with mock email sending
|
|
post_data = {
|
|
'subject': 'Test Subject',
|
|
'message': 'Test message content',
|
|
'recipients': ['candidate@example.com'],
|
|
'include_candidate_info': True,
|
|
'include_meeting_details': False
|
|
}
|
|
|
|
with patch('django.core.mail.send_mass_mail') as mock_send_mail:
|
|
mock_send_mail.return_value = 1
|
|
|
|
try:
|
|
response = client.post(url, data=post_data)
|
|
print(f"✓ POST request successful: {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
# Check if JSON response is correct
|
|
try:
|
|
json_data = response.json()
|
|
if json_data.get('success'):
|
|
print("✓ Email sent successfully")
|
|
print(f"✓ Success message: {json_data.get('message')}")
|
|
else:
|
|
print(f"✗ Email send failed: {json_data.get('error')}")
|
|
except:
|
|
print("✗ Invalid JSON response")
|
|
else:
|
|
print(f"✗ Unexpected status code: {response.status_code}")
|
|
|
|
except Exception as e:
|
|
print(f"✗ Error testing POST request: {e}")
|
|
|
|
# Clean up
|
|
user.delete()
|
|
job.delete()
|
|
candidate.delete()
|
|
|
|
print("Email composition test completed!")
|
|
|
|
def test_email_form():
|
|
"""Test the CandidateEmailForm"""
|
|
print("\nTesting CandidateEmailForm...")
|
|
|
|
from recruitment.forms import CandidateEmailForm
|
|
|
|
# Create test user for form (delete if exists)
|
|
User.objects.filter(username='formuser').delete()
|
|
form_user = User.objects.create_user(
|
|
username='formuser',
|
|
email='form@example.com',
|
|
password='formpass123'
|
|
)
|
|
|
|
# Create test job and candidate for form
|
|
job = JobPosting.objects.create(
|
|
title='Test Job Form',
|
|
internal_job_id='TEST002',
|
|
description='Test job description for form',
|
|
status='active',
|
|
application_deadline=timezone.now() + timezone.timedelta(days=30)
|
|
)
|
|
|
|
# Add user to job participants so they appear in recipient choices
|
|
job.users.add(form_user)
|
|
|
|
candidate = Candidate.objects.create(
|
|
first_name='Test Candidate',
|
|
last_name='Form',
|
|
email='candidate_form@example.com',
|
|
phone='1234567890',
|
|
job=job
|
|
)
|
|
|
|
try:
|
|
# Test valid form data - get available choices from form
|
|
form = CandidateEmailForm(job, candidate)
|
|
available_choices = [choice[0] for choice in form.fields['recipients'].choices]
|
|
|
|
# Use first available choice for testing
|
|
test_recipient = available_choices[0] if available_choices else None
|
|
|
|
if test_recipient:
|
|
form = CandidateEmailForm(job, candidate, data={
|
|
'subject': 'Test Subject',
|
|
'message': 'Test message content',
|
|
'recipients': [test_recipient],
|
|
'include_candidate_info': True,
|
|
'include_meeting_details': False
|
|
})
|
|
|
|
if form.is_valid():
|
|
print("✓ Form validation passed")
|
|
print(f"✓ Cleaned recipients: {form.cleaned_data['recipients']}")
|
|
else:
|
|
print(f"✗ Form validation failed: {form.errors}")
|
|
else:
|
|
print("✗ No recipient choices available for testing")
|
|
except Exception as e:
|
|
print(f"✗ Error testing form: {e}")
|
|
|
|
try:
|
|
# Test invalid form data (empty subject)
|
|
form = CandidateEmailForm(job, candidate, data={
|
|
'subject': '',
|
|
'message': 'Test message content',
|
|
'recipients': [],
|
|
'include_candidate_info': True,
|
|
'include_meeting_details': False
|
|
})
|
|
|
|
if not form.is_valid():
|
|
print("✓ Form correctly rejected empty subject")
|
|
if 'subject' in form.errors:
|
|
print("✓ Subject field has validation error")
|
|
else:
|
|
print("✗ Form should have failed validation")
|
|
except Exception as e:
|
|
print(f"✗ Error testing invalid form: {e}")
|
|
|
|
# Clean up
|
|
job.delete()
|
|
candidate.delete()
|
|
|
|
if __name__ == '__main__':
|
|
print("Running Email Composition Tests")
|
|
print("=" * 50)
|
|
|
|
test_email_form()
|
|
test_email_composition_view()
|
|
|
|
print("\n" + "=" * 50)
|
|
print("All tests completed!")
|