#!/usr/bin/env python """ Test script for HTML email template functionality """ import os import sys import django # Setup Django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'NorahUniversity.settings') django.setup() from django.template.loader import render_to_string from django.utils.html import strip_tags from recruitment.models import Candidate, JobPosting from recruitment.email_service import send_interview_invitation_email def test_html_template(): """Test the HTML email template rendering""" print("Testing HTML email template...") # Create test context context = { 'candidate_name': 'John Doe', 'candidate_email': 'john.doe@example.com', 'candidate_phone': '+966 50 123 4567', 'job_title': 'Senior Software Developer', 'department': 'Information Technology', 'company_name': 'Norah University', 'meeting_topic': 'Interview for Senior Software Developer', 'meeting_date_time': 'November 15, 2025 at 2:00 PM', 'meeting_duration': '60 minutes', 'join_url': 'https://zoom.us/j/123456789', } try: # Test template rendering html_content = render_to_string('emails/interview_invitation.html', context) plain_content = strip_tags(html_content) print("✅ HTML template rendered successfully!") print(f"HTML content length: {len(html_content)} characters") print(f"Plain text length: {len(plain_content)} characters") # Save rendered HTML to file for inspection with open('test_interview_email.html', 'w', encoding='utf-8') as f: f.write(html_content) print("✅ HTML content saved to 'test_interview_email.html'") # Save plain text to file for inspection with open('test_interview_email.txt', 'w', encoding='utf-8') as f: f.write(plain_content) print("✅ Plain text content saved to 'test_interview_email.txt'") return True except Exception as e: print(f"❌ Error rendering template: {e}") return False def test_email_service_function(): """Test the email service function with mock data""" print("\nTesting email service function...") try: # Get a real candidate and job for testing candidate = Candidate.objects.first() job = JobPosting.objects.first() if not candidate: print("❌ No candidates found in database") return False if not job: print("❌ No jobs found in database") return False print(f"Using candidate: {candidate.name}") print(f"Using job: {job.title}") # Test meeting details meeting_details = { 'topic': f'Interview for {job.title}', 'date_time': 'November 15, 2025 at 2:00 PM', 'duration': '60 minutes', 'join_url': 'https://zoom.us/j/test123456', } # Test the email function (without actually sending) result = send_interview_invitation_email( candidate=candidate, job=job, meeting_details=meeting_details, recipient_list=['test@example.com'] ) if result['success']: print("✅ Email service function executed successfully!") print(f"Recipients: {result.get('recipients_count', 'N/A')}") print(f"Message: {result.get('message', 'N/A')}") else: print(f"❌ Email service function failed: {result.get('error', 'Unknown error')}") return result['success'] except Exception as e: print(f"❌ Error testing email service: {e}") return False def test_template_variables(): """Test all template variables""" print("\nTesting template variables...") # Test with minimal data minimal_context = { 'candidate_name': 'Test Candidate', 'candidate_email': 'test@example.com', 'job_title': 'Test Position', } try: html_content = render_to_string('emails/interview_invitation.html', minimal_context) print("✅ Template works with minimal data") # Check for required variables required_vars = ['candidate_name', 'candidate_email', 'job_title'] missing_vars = [] for var in required_vars: if f'{{ {var} }}' in html_content: missing_vars.append(var) if missing_vars: print(f"⚠️ Missing variables: {missing_vars}") else: print("✅ All required variables are present") return True except Exception as e: print(f"❌ Error with minimal data: {e}") return False def main(): """Run all tests""" print("🧪 Testing HTML Email Template System") print("=" * 50) # Test 1: Template rendering test1_passed = test_html_template() # Test 2: Template variables test2_passed = test_template_variables() # Test 3: Email service function test3_passed = test_email_service_function() # Summary print("\n" + "=" * 50) print("📊 TEST SUMMARY") print(f"Template Rendering: {'✅ PASS' if test1_passed else '❌ FAIL'}") print(f"Template Variables: {'✅ PASS' if test2_passed else '❌ FAIL'}") print(f"Email Service: {'✅ PASS' if test3_passed else '❌ FAIL'}") overall_success = test1_passed and test2_passed and test3_passed print(f"\nOverall Result: {'✅ ALL TESTS PASSED' if overall_success else '❌ SOME TESTS FAILED'}") if overall_success: print("\n🎉 HTML email template system is ready!") print("You can now send professional interview invitations using the new template.") else: print("\n🔧 Please fix the issues before using the template system.") if __name__ == '__main__': main()