#!/usr/bin/env python3 """ Helper script to get an existing API key for testing purposes. This script connects to the database and retrieves an active API key that can be used for E2E testing. Usage: python scripts/get_test_api_key.py """ import asyncio import sys import os # Add the src directory to the path sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src')) from src.db.repositories.api_key_repository import api_key_repository from src.db.repositories.user_repository import user_repository from src.db.repositories.team_repository import team_repository async def get_test_api_key(): """Get an existing API key for testing""" try: # Get all API keys api_keys = await api_key_repository.get_all() if not api_keys: print("❌ No API keys found in the database") return None # Find an active API key active_keys = [key for key in api_keys if key.is_active] if not active_keys: print("❌ No active API keys found in the database") return None # Get the first active key test_key = active_keys[0] # Get user and team info user = await user_repository.get_by_id(test_key.user_id) team = await team_repository.get_by_id(test_key.team_id) print("✅ Found test API key:") print(f" Key ID: {test_key.id}") print(f" Key Name: {test_key.name}") print(f" User: {user.name} ({user.email})" if user else " User: Not found") print(f" Team: {team.name}" if team else " Team: Not found") print(f" Created: {test_key.created_at}") print(f" Is Admin: {user.is_admin}" if user else " Is Admin: Unknown") # Note: We can't return the actual key value since it's hashed print("\n⚠️ Note: The actual API key value is hashed in the database.") print(" You'll need to use an API key you have access to for testing.") return { "key_id": str(test_key.id), "key_name": test_key.name, "user_id": str(test_key.user_id), "team_id": str(test_key.team_id), "user_name": user.name if user else None, "user_email": user.email if user else None, "team_name": team.name if team else None, "is_admin": user.is_admin if user else None } except Exception as e: print(f"❌ Error getting API key: {e}") return None async def main(): """Main function""" print("🔍 Looking for existing API keys in the database...") result = await get_test_api_key() if result: print("\n💡 To run E2E tests, you can:") print(" 1. Use an API key you have access to") print(" 2. Create a new API key using the bootstrap endpoint (if not already done)") print(" 3. Set the API key in the test environment") else: print("\n💡 To run E2E tests, you may need to:") print(" 1. Run the bootstrap endpoint to create initial data") print(" 2. Create API keys manually") if __name__ == "__main__": asyncio.run(main())