#!/usr/bin/env python """ Test script for async email functionality """ import os import sys import django # Setup Django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'NorahUniversity.settings') django.setup() from django.contrib.auth.models import User from recruitment.email_service import send_bulk_email from recruitment.models import JobPosting, Candidate def test_async_email(): """Test async email sending functionality""" print("๐Ÿงช Testing Async Email Functionality") print("=" * 50) try: # Get a test user test_user = User.objects.first() if not test_user: print("โŒ No users found in database. Please create a user first.") return # Get a test job and candidate test_job = JobPosting.objects.first() test_candidate = Candidate.objects.first() if not test_job or not test_candidate: print("โŒ No test job or candidate found. Please create some test data first.") return print(f"๐Ÿ“ง Test User: {test_user.email}") print(f"๐Ÿ’ผ Test Job: {test_job.title}") print(f"๐Ÿ‘ค Test Candidate: {test_candidate.name}") # Test synchronous email sending print("\n1. Testing Synchronous Email Sending...") try: sync_result = send_bulk_email( subject="Test Synchronous Email", message="This is a test synchronous email from the ATS system.", recipient_list=[test_user.email], async_task=False ) print(f" โœ… Sync result: {sync_result}") except Exception as e: print(f" โŒ Sync error: {e}") # Test asynchronous email sending print("\n2. Testing Asynchronous Email Sending...") try: async_result = send_bulk_email( subject="Test Asynchronous Email", message="This is a test asynchronous email from the ATS system.", recipient_list=[test_user.email], async_task=True ) print(f" โœ… Async result: {async_result}") except Exception as e: print(f" โŒ Async error: {e}") print("\n3. Testing Email Service Status...") # Check Django Q configuration try: import django_q from django_q.models import Task pending_tasks = Task.objects.count() print(f" ๐Ÿ“Š Django Q Status: Installed, {pending_tasks} tasks in queue") except ImportError: print(" โš ๏ธ Django Q not installed") except Exception as e: print(f" ๐Ÿ“Š Django Q Status: Installed but error checking status: {e}") # Check email backend configuration from django.conf import settings email_backend = getattr(settings, 'EMAIL_BACKEND', 'not configured') print(f" ๐Ÿ“ง Email Backend: {email_backend}") email_host = getattr(settings, 'EMAIL_HOST', 'not configured') print(f" ๐ŸŒ Email Host: {email_host}") email_port = getattr(settings, 'EMAIL_PORT', 'not configured') print(f" ๐Ÿ”Œ Email Port: {email_port}") print("\nโœ… Async email functionality test completed!") print("๐Ÿ’ก If emails are not being received, check:") print(" - Email server configuration in settings.py") print(" - Django Q cluster status (python manage.py qmonitor)") print(" - Email logs and spam folders") except Exception as e: print(f"โŒ Test failed with error: {e}") import traceback traceback.print_exc() if __name__ == "__main__": test_async_email()