204 lines
6.9 KiB
Python
204 lines
6.9 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
Test script for EDNX settings and session cookie configuration.
|
|
Run this in the LMS container to verify the implementation.
|
|
|
|
Usage:
|
|
cd /openedx/edx-platform
|
|
python /path/to/openedx-tenant-api/test_ednx_settings.py
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import django
|
|
|
|
# Setup Django
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lms.envs.development')
|
|
sys.path.insert(0, '/openedx/edx-platform')
|
|
sys.path.insert(0, '/openedx/tenant-api')
|
|
|
|
django.setup()
|
|
|
|
from openedx_tenant_api.views import generate_mfe_config
|
|
from openedx_tenant_api.serializers import CreateTenantSerializer
|
|
|
|
|
|
def test_default_ednx_settings():
|
|
"""Test that default EDNX settings are all enabled."""
|
|
print("\n=== Test 1: Default EDNX Settings ===")
|
|
config = generate_mfe_config(
|
|
tenant_name='testtenant',
|
|
platform_name='Test Tenant',
|
|
theme_name='indigo'
|
|
)
|
|
|
|
assert config['EDNX_TENANT_RESTRICT_USERS'] == True, "EDNX_TENANT_RESTRICT_USERS should default to True"
|
|
assert config['EDNX_TENANT_USER_FILTER_ENABLED'] == True, "EDNX_TENANT_USER_FILTER_ENABLED should default to True"
|
|
assert config['EDNX_USE_SIGNAL'] == True, "EDNX_USE_SIGNAL should default to True"
|
|
assert config['EDNX_ACCOUNT_REGISTRATION_SOURCES'] == ['testtenant.local.openedx.io:8000'], \
|
|
f"Expected ['testtenant.local.openedx.io:8000'], got {config['EDNX_ACCOUNT_REGISTRATION_SOURCES']}"
|
|
|
|
print("✓ All default EDNX settings are correct")
|
|
return True
|
|
|
|
|
|
def test_custom_ednx_settings():
|
|
"""Test that custom EDNX settings are applied."""
|
|
print("\n=== Test 2: Custom EDNX Settings ===")
|
|
config = generate_mfe_config(
|
|
tenant_name='customtenant',
|
|
platform_name='Custom Tenant',
|
|
theme_name='indigo',
|
|
ednx_tenant_restrict_users=False,
|
|
ednx_tenant_user_filter_enabled=False,
|
|
ednx_use_signal=False,
|
|
ednx_account_registration_sources=['custom.example.com']
|
|
)
|
|
|
|
assert config['EDNX_TENANT_RESTRICT_USERS'] == False, "EDNX_TENANT_RESTRICT_USERS should be False"
|
|
assert config['EDNX_TENANT_USER_FILTER_ENABLED'] == False, "EDNX_TENANT_USER_FILTER_ENABLED should be False"
|
|
assert config['EDNX_USE_SIGNAL'] == False, "EDNX_USE_SIGNAL should be False"
|
|
assert config['EDNX_ACCOUNT_REGISTRATION_SOURCES'] == ['custom.example.com'], \
|
|
f"Expected ['custom.example.com'], got {config['EDNX_ACCOUNT_REGISTRATION_SOURCES']}"
|
|
|
|
print("✓ Custom EDNX settings are applied correctly")
|
|
return True
|
|
|
|
|
|
def test_session_cookie_name():
|
|
"""Test session cookie name generation."""
|
|
print("\n=== Test 3: Session Cookie Name ===")
|
|
|
|
# Test without hyphen
|
|
config1 = generate_mfe_config('sitea', 'Site A', 'indigo')
|
|
assert config1['SESSION_COOKIE_NAME'] == 'sessionid_sitea', \
|
|
f"Expected 'sessionid_sitea', got {config1['SESSION_COOKIE_NAME']}"
|
|
|
|
# Test with hyphen
|
|
config2 = generate_mfe_config('my-tenant', 'My Tenant', 'indigo')
|
|
assert config2['SESSION_COOKIE_NAME'] == 'sessionid_mytenant', \
|
|
f"Expected 'sessionid_mytenant', got {config2['SESSION_COOKIE_NAME']}"
|
|
|
|
print("✓ Session cookie names are generated correctly")
|
|
return True
|
|
|
|
|
|
def test_session_cookie_domain():
|
|
"""Test session cookie domain matches LMS domain."""
|
|
print("\n=== Test 4: Session Cookie Domain ===")
|
|
config = generate_mfe_config('testsite', 'Test Site', 'indigo')
|
|
|
|
assert config['SESSION_COOKIE_DOMAIN'] == 'testsite.local.openedx.io', \
|
|
f"Expected 'testsite.local.openedx.io', got {config['SESSION_COOKIE_DOMAIN']}"
|
|
|
|
print("✓ Session cookie domain is correct")
|
|
return True
|
|
|
|
|
|
def test_serializer_with_ednx_fields():
|
|
"""Test that serializer accepts EDNX fields."""
|
|
print("\n=== Test 5: Serializer EDNX Fields ===")
|
|
|
|
# Test with all EDNX fields
|
|
data = {
|
|
'tenant_name': 'testtenant',
|
|
'platform_name': 'Test Tenant',
|
|
'theme_name': 'indigo',
|
|
'ednx_tenant_restrict_users': True,
|
|
'ednx_tenant_user_filter_enabled': False,
|
|
'ednx_use_signal': True,
|
|
'ednx_account_registration_sources': ['custom.source.com']
|
|
}
|
|
serializer = CreateTenantSerializer(data=data)
|
|
assert serializer.is_valid(), f"Serializer errors: {serializer.errors}"
|
|
|
|
assert serializer.validated_data['ednx_tenant_restrict_users'] == True
|
|
assert serializer.validated_data['ednx_tenant_user_filter_enabled'] == False
|
|
assert serializer.validated_data['ednx_use_signal'] == True
|
|
assert serializer.validated_data['ednx_account_registration_sources'] == ['custom.source.com']
|
|
|
|
print("✓ Serializer accepts and validates EDNX fields")
|
|
return True
|
|
|
|
|
|
def test_serializer_defaults():
|
|
"""Test that serializer provides correct defaults."""
|
|
print("\n=== Test 6: Serializer Defaults ===")
|
|
|
|
data = {'tenant_name': 'testtenant'}
|
|
serializer = CreateTenantSerializer(data=data)
|
|
assert serializer.is_valid(), f"Serializer errors: {serializer.errors}"
|
|
|
|
# Check defaults
|
|
assert serializer.validated_data.get('ednx_tenant_restrict_users', True) == True
|
|
assert serializer.validated_data.get('ednx_tenant_user_filter_enabled', True) == True
|
|
assert serializer.validated_data.get('ednx_use_signal', True) == True
|
|
assert serializer.validated_data.get('ednx_account_registration_sources') is None
|
|
|
|
print("✓ Serializer defaults are correct")
|
|
return True
|
|
|
|
|
|
def test_backward_compatibility():
|
|
"""Test that old API calls without EDNX fields still work."""
|
|
print("\n=== Test 7: Backward Compatibility ===")
|
|
|
|
# Old-style API call
|
|
data = {
|
|
'tenant_name': 'legacytenant',
|
|
'platform_name': 'Legacy Tenant',
|
|
'theme_name': 'indigo',
|
|
'org_filter': ['org1', 'org2']
|
|
}
|
|
serializer = CreateTenantSerializer(data=data)
|
|
assert serializer.is_valid(), f"Serializer errors: {serializer.errors}"
|
|
|
|
# Should have default values for new fields
|
|
assert serializer.validated_data['tenant_name'] == 'legacytenant'
|
|
assert serializer.validated_data['platform_name'] == 'Legacy Tenant'
|
|
|
|
print("✓ Backward compatibility maintained")
|
|
return True
|
|
|
|
|
|
def run_all_tests():
|
|
"""Run all tests."""
|
|
print("="*60)
|
|
print("Testing EDNX Settings and Session Cookie Configuration")
|
|
print("="*60)
|
|
|
|
tests = [
|
|
test_default_ednx_settings,
|
|
test_custom_ednx_settings,
|
|
test_session_cookie_name,
|
|
test_session_cookie_domain,
|
|
test_serializer_with_ednx_fields,
|
|
test_serializer_defaults,
|
|
test_backward_compatibility,
|
|
]
|
|
|
|
passed = 0
|
|
failed = 0
|
|
|
|
for test in tests:
|
|
try:
|
|
if test():
|
|
passed += 1
|
|
except AssertionError as e:
|
|
print(f"✗ FAILED: {e}")
|
|
failed += 1
|
|
except Exception as e:
|
|
print(f"✗ ERROR: {e}")
|
|
failed += 1
|
|
|
|
print("\n" + "="*60)
|
|
print(f"Results: {passed} passed, {failed} failed")
|
|
print("="*60)
|
|
|
|
return failed == 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
success = run_all_tests()
|
|
sys.exit(0 if success else 1)
|