#!/usr/bin/env python3
"""
Test script to verify the dashboard FieldError fix
"""

import requests
import sys

def test_dashboard_access():
    """Test that dashboard redirects properly without FieldError"""
    try:
        # Test dashboard endpoint
        response = requests.get('http://localhost:8002/dashboard/', allow_redirects=False)
        
        if response.status_code == 302:
            print("✅ Dashboard endpoint working - redirects to login as expected")
            print(f"   Status: {response.status_code}")
            print(f"   Redirect location: {response.headers.get('Location', 'N/A')}")
            return True
        else:
            print(f"❌ Unexpected status code: {response.status_code}")
            return False
            
    except Exception as e:
        print(f"❌ Error accessing dashboard: {e}")
        return False

def test_login_page():
    """Test that login page loads properly"""
    try:
        response = requests.get('http://localhost:8002/auth/login/')
        
        if response.status_code == 200:
            print("✅ Login page accessible")
            print(f"   Status: {response.status_code}")
            return True
        else:
            print(f"❌ Login page error: {response.status_code}")
            return False
            
    except Exception as e:
        print(f"❌ Error accessing login page: {e}")
        return False

def main():
    print("Testing Adtlas Dashboard Fix...\n")
    
    tests_passed = 0
    total_tests = 2
    
    if test_dashboard_access():
        tests_passed += 1
    
    if test_login_page():
        tests_passed += 1
    
    print(f"\nTest Results: {tests_passed}/{total_tests} passed")
    
    if tests_passed == total_tests:
        print("🎉 All tests passed! The FieldError fix is working correctly.")
        sys.exit(0)
    else:
        print("❌ Some tests failed.")
        sys.exit(1)

if __name__ == '__main__':
    main()