#!/usr/bin/env python3
"""
Test script to check if the collections API endpoint works after fixing enum issues.
"""
import asyncio
import sys
import os
# Add the backend path to sys.path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'backend'))
from app.core.database import get_db
from app.models.user import User
from app.models.project import ProjectRole
from app.middleware.rbac import ProjectAccessControl
async def test_enum_fix():
"""Test if the enum casting issue is fixed."""
print("Testing ProjectAccessControl.get_user_accessible_projects...")
try:
# Create a fake database session (we'll use None for now to test import)
db = None
# Create a fake user object
class FakeUser:
def __init__(self):
self.id = "test-user-id"
self.is_admin = False
fake_user = FakeUser()
# Test if we can at least import and call the method without enum casting errors
# (This will fail with a database error, but should not fail with enum casting)
try:
result = await ProjectAccessControl.get_user_accessible_projects(
fake_user, db, ProjectRole.VIEWER
)
print("✗ Method call should have failed with DB error but didn't")
except AttributeError as e:
if "NoneType" in str(e):
print("✓ Method call failed with expected DB error (no enum casting issue)")
else:
print(f"✗ Unexpected AttributeError: {e}")
except Exception as e:
if "projectrole" in str(e).lower() or "enum" in str(e).lower():
print(f"✗ Still has enum casting issue: {e}")
else:
print(f"✓ Method failed with expected non-enum error: {type(e).__name__}")
print("✓ Successfully imported and called method without enum casting errors!")
return True
except Exception as e:
if "projectrole" in str(e).lower() or "enum" in str(e).lower():
print(f"✗ Enum casting issue still exists: {e}")
return False
else:
print(f"✗ Unexpected error: {e}")
return False
if __name__ == "__main__":
try:
result = asyncio.run(test_enum_fix())
if result:
print("\n🎉 Enum casting issues appear to be resolved!")
sys.exit(0)
else:
print("\n❌ Enum casting issues still exist!")
sys.exit(1)
except Exception as e:
print(f"\n❌ Test failed with error: {e}")
sys.exit(1)