217 lines
7.0 KiB
Python
217 lines
7.0 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
Test script to verify the Add Checklist Item functionality
|
|
"""
|
|
import os
|
|
import django
|
|
|
|
# Setup Django
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'PX360.settings')
|
|
django.setup()
|
|
|
|
from django.test import Client, TestCase
|
|
from django.contrib.auth import get_user_model
|
|
from apps.accounts.models import AcknowledgementChecklistItem, AcknowledgementContent, Role
|
|
from apps.organizations.models import Hospital, Department
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
def test_checklist_item_api():
|
|
"""Test the API endpoint for creating checklist items"""
|
|
print("Testing Checklist Item API...")
|
|
|
|
# Create a test user with PX Admin role
|
|
try:
|
|
user = User.objects.get(email='test_admin@example.com')
|
|
except User.DoesNotExist:
|
|
user = User.objects.create_user(
|
|
email='test_admin@example.com',
|
|
password='testpass123',
|
|
first_name='Test',
|
|
last_name='Admin'
|
|
)
|
|
# Add PX Admin role
|
|
px_admin_role, _ = Role.objects.get_or_create(
|
|
name='px_admin',
|
|
defaults={
|
|
'display_name': 'PX Admin',
|
|
'description': 'System administrator'
|
|
}
|
|
)
|
|
user.groups.add(px_admin_role.group)
|
|
|
|
# Create test client
|
|
client = Client()
|
|
|
|
# Login
|
|
client.login(email='test_admin@example.com', password='testpass123')
|
|
|
|
# Test data
|
|
test_data = {
|
|
'code': 'TEST_ITEM_001',
|
|
'role': 'staff',
|
|
'text_en': 'Test Checklist Item',
|
|
'text_ar': 'عنصر قائمة الاختبار',
|
|
'description_en': 'This is a test description',
|
|
'description_ar': 'هذا وصف اختبار',
|
|
'is_required': True,
|
|
'is_active': True,
|
|
'order': 1
|
|
}
|
|
|
|
# Try to create checklist item via API
|
|
response = client.post(
|
|
'/api/accounts/onboarding/checklist/',
|
|
data=test_data,
|
|
content_type='application/json'
|
|
)
|
|
|
|
print(f"Response Status: {response.status_code}")
|
|
print(f"Response Content: {response.content.decode()}")
|
|
|
|
if response.status_code in [200, 201]:
|
|
print("✅ API endpoint is working!")
|
|
|
|
# Verify item was created
|
|
item = AcknowledgementChecklistItem.objects.filter(code='TEST_ITEM_001').first()
|
|
if item:
|
|
print(f"✅ Checklist item created: {item.text_en}")
|
|
print(f" - Role: {item.role}")
|
|
print(f" - Required: {item.is_required}")
|
|
print(f" - Active: {item.is_active}")
|
|
print(f" - Order: {item.order}")
|
|
|
|
# Clean up
|
|
item.delete()
|
|
print("✅ Test item cleaned up")
|
|
else:
|
|
print("❌ Item was not found in database")
|
|
else:
|
|
print(f"❌ API endpoint failed with status {response.status_code}")
|
|
|
|
# Clean up test user
|
|
user.delete()
|
|
|
|
return response.status_code in [200, 201]
|
|
|
|
|
|
def test_template_rendering():
|
|
"""Test that the checklist list template renders correctly"""
|
|
print("\nTesting Template Rendering...")
|
|
|
|
# Create test user
|
|
try:
|
|
user = User.objects.get(email='test_admin2@example.com')
|
|
except User.DoesNotExist:
|
|
user = User.objects.create_user(
|
|
email='test_admin2@example.com',
|
|
password='testpass123',
|
|
first_name='Test',
|
|
last_name='Admin2'
|
|
)
|
|
px_admin_role, _ = Role.objects.get_or_create(
|
|
name='px_admin',
|
|
defaults={
|
|
'display_name': 'PX Admin',
|
|
'description': 'System administrator'
|
|
}
|
|
)
|
|
user.groups.add(px_admin_role.group)
|
|
|
|
# Create test content
|
|
try:
|
|
content = AcknowledgementContent.objects.get(code='TEST_CONTENT')
|
|
except AcknowledgementContent.DoesNotExist:
|
|
content = AcknowledgementContent.objects.create(
|
|
code='TEST_CONTENT',
|
|
title_en='Test Content',
|
|
title_ar='محتوى الاختبار',
|
|
description_en='Test description',
|
|
description_ar='وصف الاختبار',
|
|
role='staff',
|
|
is_active=True
|
|
)
|
|
|
|
# Create test checklist item
|
|
try:
|
|
item = AcknowledgementChecklistItem.objects.get(code='TEST_ITEM_002')
|
|
except AcknowledgementChecklistItem.DoesNotExist:
|
|
item = AcknowledgementChecklistItem.objects.create(
|
|
code='TEST_ITEM_002',
|
|
role='staff',
|
|
text_en='Test Item for Template',
|
|
text_ar='عنصر لقالب الاختبار',
|
|
description_en='Test description for template',
|
|
description_ar='وصف اختبار للقالب',
|
|
is_required=True,
|
|
is_active=True,
|
|
order=1,
|
|
content=content
|
|
)
|
|
|
|
# Create client and login
|
|
client = Client()
|
|
client.login(email='test_admin2@example.com', password='testpass123')
|
|
|
|
# Get the page
|
|
response = client.get('/accounts/onboarding/checklist/')
|
|
|
|
print(f"Response Status: {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
content = response.content.decode()
|
|
|
|
# Check for key elements
|
|
checks = [
|
|
('Add Checklist Item button', 'Add Checklist Item' in content or 'data-bs-target="#createChecklistItemModal"' in content),
|
|
('Modal form', 'createChecklistItemModal' in content),
|
|
('Code field', 'name="code"' in content),
|
|
('Role dropdown', 'name="role"' in content),
|
|
('Text fields', 'name="text_en"' in content and 'name="text_ar"' in content),
|
|
('Required toggle', 'name="is_required"' in content),
|
|
('Active toggle', 'name="is_active"' in content),
|
|
('Order field', 'name="order"' in content),
|
|
('JavaScript function', 'saveChecklistItem' in content),
|
|
('API endpoint', '/api/accounts/onboarding/checklist/' in content)
|
|
]
|
|
|
|
all_passed = True
|
|
for check_name, result in checks:
|
|
status = "✅" if result else "❌"
|
|
print(f"{status} {check_name}")
|
|
if not result:
|
|
all_passed = False
|
|
|
|
if all_passed:
|
|
print("\n✅ All template checks passed!")
|
|
|
|
else:
|
|
print(f"❌ Failed to render template with status {response.status_code}")
|
|
|
|
# Clean up
|
|
item.delete()
|
|
content.delete()
|
|
user.delete()
|
|
|
|
return response.status_code == 200
|
|
|
|
|
|
if __name__ == '__main__':
|
|
print("=" * 60)
|
|
print("CHECKLIST ITEM CREATION TESTS")
|
|
print("=" * 60)
|
|
|
|
api_test = test_checklist_item_api()
|
|
template_test = test_template_rendering()
|
|
|
|
print("\n" + "=" * 60)
|
|
print("TEST SUMMARY")
|
|
print("=" * 60)
|
|
print(f"API Test: {'✅ PASSED' if api_test else '❌ FAILED'}")
|
|
print(f"Template Test: {'✅ PASSED' if template_test else '❌ FAILED'}")
|
|
|
|
if api_test and template_test:
|
|
print("\n🎉 All tests passed! The Add Checklist Item feature is working.")
|
|
else:
|
|
print("\n⚠️ Some tests failed. Please review the output above.") |