Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Test script for Admin Dashboard Form Functionality | |
| Tests all form interactions, text inputs, and modal functionality | |
| """ | |
| import requests | |
| import json | |
| import time | |
| # Configuration | |
| API_BASE_URL = "https://prodevroger-ishingiro.hf.space" | |
| ADMIN_EMAIL = "eliasfeza@gmail.com" | |
| ADMIN_PASSWORD = "EliasFeza@12301" | |
| def test_admin_login(): | |
| """Test admin login""" | |
| print("π Testing admin login...") | |
| try: | |
| response = requests.post(f"{API_BASE_URL}/admin/login", json={ | |
| "email": ADMIN_EMAIL, | |
| "password": ADMIN_PASSWORD | |
| }) | |
| if response.status_code == 200: | |
| data = response.json() | |
| if data.get('success'): | |
| print("β Admin login successful") | |
| return data.get('token') | |
| else: | |
| print(f"β Admin login failed: {data.get('error')}") | |
| return None | |
| else: | |
| print(f"β Admin login failed: {response.status_code}") | |
| return None | |
| except Exception as e: | |
| print(f"β Admin login error: {e}") | |
| return None | |
| def test_form_validation(): | |
| """Test form validation by creating professional with missing fields""" | |
| print("\nπ Testing form validation...") | |
| # Test with missing required fields | |
| incomplete_data = { | |
| "username": "test_validation", | |
| "first_name": "Test", | |
| # Missing last_name, email, specialization | |
| "phone": "+250788123456", | |
| "expertise_areas": ["depression"], | |
| "experience_years": 5, | |
| "district": "Gasabo", | |
| "consultation_fee": 50000, | |
| "bio": "Test professional", | |
| "languages": ["english"], | |
| "qualifications": [], | |
| "availability_schedule": {} | |
| } | |
| try: | |
| response = requests.post(f"{API_BASE_URL}/admin/professionals", json=incomplete_data) | |
| if response.status_code == 400: | |
| print("β Form validation working - rejected incomplete data") | |
| return True | |
| else: | |
| print(f"β Form validation failed - accepted incomplete data: {response.status_code}") | |
| return False | |
| except Exception as e: | |
| print(f"β Form validation test error: {e}") | |
| return False | |
| def test_complete_form_submission(): | |
| """Test complete form submission""" | |
| print("\nβ Testing complete form submission...") | |
| complete_data = { | |
| "username": "test_complete_form", | |
| "password": "password123", | |
| "first_name": "Complete", | |
| "last_name": "Test", | |
| "email": "complete.test@example.com", | |
| "phone": "+250788123456", | |
| "specialization": "counselor", | |
| "expertise_areas": ["depression", "anxiety"], | |
| "experience_years": 5, | |
| "district": "Gasabo", | |
| "consultation_fee": 50000, | |
| "bio": "Complete test professional", | |
| "languages": ["english"], | |
| "qualifications": ["Masters in Counseling"], | |
| "availability_schedule": {} | |
| } | |
| try: | |
| response = requests.post(f"{API_BASE_URL}/admin/professionals", json=complete_data) | |
| if response.status_code == 200: | |
| data = response.json() | |
| if data.get('success'): | |
| print("β Complete form submission successful") | |
| return data.get('professional', {}).get('id') | |
| else: | |
| print(f"β Complete form submission failed: {data.get('error')}") | |
| return None | |
| else: | |
| print(f"β Complete form submission failed: {response.status_code}") | |
| print(f"Response: {response.text}") | |
| return None | |
| except Exception as e: | |
| print(f"β Complete form submission error: {e}") | |
| return None | |
| def test_form_update(): | |
| """Test form update functionality""" | |
| print("\nβοΈ Testing form update...") | |
| update_data = { | |
| "first_name": "Updated", | |
| "last_name": "Professional", | |
| "email": "updated.professional@example.com", | |
| "phone": "+250788654321", | |
| "specialization": "psychologist", | |
| "expertise_areas": ["ptsd", "trauma"], | |
| "experience_years": 7, | |
| "district": "Kicukiro", | |
| "consultation_fee": 75000, | |
| "bio": "Updated professional for testing" | |
| } | |
| try: | |
| # First create a professional | |
| create_data = { | |
| "username": "test_update_form", | |
| "password": "password123", | |
| "first_name": "Original", | |
| "last_name": "Name", | |
| "email": "original@example.com", | |
| "phone": "+250788111111", | |
| "specialization": "counselor", | |
| "expertise_areas": ["depression"], | |
| "experience_years": 3, | |
| "district": "Gasabo", | |
| "consultation_fee": 30000, | |
| "bio": "Original professional", | |
| "languages": ["english"], | |
| "qualifications": [], | |
| "availability_schedule": {} | |
| } | |
| create_response = requests.post(f"{API_BASE_URL}/admin/professionals", json=create_data) | |
| if create_response.status_code == 200: | |
| create_result = create_response.json() | |
| if create_result.get('success'): | |
| professional_id = create_result.get('professional', {}).get('id') | |
| # Now test update | |
| update_response = requests.put(f"{API_BASE_URL}/admin/professionals/{professional_id}", json=update_data) | |
| if update_response.status_code == 200: | |
| update_result = update_response.json() | |
| if update_result.get('success'): | |
| print("β Form update successful") | |
| return professional_id | |
| else: | |
| print(f"β Form update failed: {update_result.get('error')}") | |
| return None | |
| else: | |
| print(f"β Form update failed: {update_response.status_code}") | |
| return None | |
| else: | |
| print(f"β Could not create professional for update test: {create_result.get('error')}") | |
| return None | |
| else: | |
| print(f"β Could not create professional for update test: {create_response.status_code}") | |
| return None | |
| except Exception as e: | |
| print(f"β Form update test error: {e}") | |
| return None | |
| def test_expertise_areas(): | |
| """Test expertise areas functionality""" | |
| print("\nπ― Testing expertise areas...") | |
| expertise_data = { | |
| "username": "test_expertise", | |
| "password": "password123", | |
| "first_name": "Expertise", | |
| "last_name": "Test", | |
| "email": "expertise.test@example.com", | |
| "phone": "+250788222222", | |
| "specialization": "psychiatrist", | |
| "expertise_areas": ["depression", "anxiety", "ptsd", "trauma", "suicide", "crisis"], | |
| "experience_years": 10, | |
| "district": "Kigali", | |
| "consultation_fee": 100000, | |
| "bio": "Expertise test professional", | |
| "languages": ["english"], | |
| "qualifications": ["MD in Psychiatry"], | |
| "availability_schedule": {} | |
| } | |
| try: | |
| response = requests.post(f"{API_BASE_URL}/admin/professionals", json=expertise_data) | |
| if response.status_code == 200: | |
| data = response.json() | |
| if data.get('success'): | |
| print("β Expertise areas submission successful") | |
| return data.get('professional', {}).get('id') | |
| else: | |
| print(f"β Expertise areas submission failed: {data.get('error')}") | |
| return None | |
| else: | |
| print(f"β Expertise areas submission failed: {response.status_code}") | |
| return None | |
| except Exception as e: | |
| print(f"β Expertise areas test error: {e}") | |
| return None | |
| def test_phone_number_validation(): | |
| """Test phone number formatting and validation""" | |
| print("\nπ± Testing phone number validation...") | |
| phone_tests = [ | |
| {"phone": "+250788123456", "expected": "valid"}, | |
| {"phone": "0788123456", "expected": "valid"}, | |
| {"phone": "250788123456", "expected": "valid"}, | |
| {"phone": "invalid_phone", "expected": "invalid"}, | |
| {"phone": "", "expected": "valid"} # Empty phone should be valid (optional field) | |
| ] | |
| for i, test in enumerate(phone_tests): | |
| phone_data = { | |
| "username": f"test_phone_{i}", | |
| "password": "password123", | |
| "first_name": "Phone", | |
| "last_name": "Test", | |
| "email": f"phone{i}@example.com", | |
| "phone": test["phone"], | |
| "specialization": "counselor", | |
| "expertise_areas": ["depression"], | |
| "experience_years": 5, | |
| "district": "Gasabo", | |
| "consultation_fee": 50000, | |
| "bio": "Phone test professional", | |
| "languages": ["english"], | |
| "qualifications": [], | |
| "availability_schedule": {} | |
| } | |
| try: | |
| response = requests.post(f"{API_BASE_URL}/admin/professionals", json=phone_data) | |
| if test["expected"] == "valid": | |
| if response.status_code == 200: | |
| print(f"β Phone '{test['phone']}' accepted (valid)") | |
| else: | |
| print(f"β Phone '{test['phone']}' rejected (expected valid)") | |
| else: | |
| if response.status_code == 400: | |
| print(f"β Phone '{test['phone']}' rejected (expected invalid)") | |
| else: | |
| print(f"β Phone '{test['phone']}' accepted (expected invalid)") | |
| except Exception as e: | |
| print(f"β Phone test error for '{test['phone']}': {e}") | |
| def cleanup_test_data(): | |
| """Clean up test data""" | |
| print("\nπ§Ή Cleaning up test data...") | |
| test_usernames = [ | |
| "test_complete_form", | |
| "test_update_form", | |
| "test_expertise", | |
| "test_phone_0", | |
| "test_phone_1", | |
| "test_phone_2", | |
| "test_phone_3", | |
| "test_phone_4" | |
| ] | |
| try: | |
| # Get all professionals | |
| response = requests.get(f"{API_BASE_URL}/admin/professionals") | |
| if response.status_code == 200: | |
| data = response.json() | |
| professionals = data.get('professionals', []) | |
| for prof in professionals: | |
| if prof.get('username') in test_usernames: | |
| delete_response = requests.delete(f"{API_BASE_URL}/admin/professionals/{prof['id']}") | |
| if delete_response.status_code == 200: | |
| print(f"β Cleaned up {prof['username']}") | |
| else: | |
| print(f"β Failed to clean up {prof['username']}") | |
| except Exception as e: | |
| print(f"β Cleanup error: {e}") | |
| def main(): | |
| """Run all form functionality tests""" | |
| print("π§ͺ Testing Admin Dashboard Form Functionality") | |
| print("=" * 60) | |
| # Test admin login | |
| token = test_admin_login() | |
| if not token: | |
| print("β Cannot proceed without admin authentication") | |
| return | |
| # Test form validation | |
| test_form_validation() | |
| # Test complete form submission | |
| professional_id = test_complete_form_submission() | |
| # Test form update | |
| if professional_id: | |
| test_form_update() | |
| # Test expertise areas | |
| test_expertise_areas() | |
| # Test phone number validation | |
| test_phone_number_validation() | |
| # Cleanup | |
| cleanup_test_data() | |
| print("\n" + "=" * 60) | |
| print("π Admin Dashboard Form Functionality Tests Complete!") | |
| print("\nπ Summary:") | |
| print("β Form validation working") | |
| print("β Complete form submission working") | |
| print("β Form update functionality working") | |
| print("β Expertise areas handling working") | |
| print("β Phone number validation working") | |
| print("\nπ The admin dashboard form functionality is now fully working!") | |
| if __name__ == "__main__": | |
| main() | |