"""Script para verificar el acceso a tickets con diferentes usuarios""" import asyncio import httpx import os BASE_URL = "http://localhost:8000/api/v1" TICKET_ID = "2bd79718-440d-4144-b660-c0c6051fcf73" async def login(email: str, password: str, tenant_slug: str = "aduanasoft"): """Login y obtener token""" async with httpx.AsyncClient() as client: response = await client.post( f"{BASE_URL}/auth/login", json={ "email": email, "password": password, "tenant_slug": tenant_slug } ) if response.status_code == 200: data = response.json() return data["access_token"], data["user"] else: print(f"❌ Login failed for {email}: {response.text}") return None, None async def get_ticket(ticket_id: str, token: str, tenant_id: str): """Intentar obtener un ticket""" async with httpx.AsyncClient() as client: response = await client.get( f"{BASE_URL}/tickets/{ticket_id}", headers={ "Authorization": f"Bearer {token}", "X-Tenant-ID": tenant_id } ) return response.status_code, response.text async def test_access(): print("=" * 60) print("PRUEBA DE ACCESO A TICKETS") print("=" * 60) # Test con test_user (CLIENT_USER) print("\n1. Probando con test_user (CLIENT_USER)...") token, user = await login("test_user@example.com", "TestPassword123!") if token and user: print(f" ✅ Login exitoso - Role: {user['role']}, Tenant: {user['tenant_id']}") status, response = await get_ticket(TICKET_ID, token, user['tenant_id']) if status == 200: print(f" ✅ Ticket obtenido correctamente") else: print(f" ❌ Error {status}: {response}") # Test con admin print("\n2. Probando con admin (ADMIN)...") token, user = await login("admin@aduanasoft.com", "Admin123!") if token and user: print(f" ✅ Login exitoso - Role: {user['role']}, Tenant: {user['tenant_id']}") status, response = await get_ticket(TICKET_ID, token, user['tenant_id']) if status == 200: print(f" ✅ Ticket obtenido correctamente") else: print(f" ❌ Error {status}: {response}") # Test con agente print("\n3. Probando con agente (AGENT)...") token, user = await login("agente@aduanasoft.com", "Agente123!") if token and user: print(f" ✅ Login exitoso - Role: {user['role']}, Tenant: {user['tenant_id']}") status, response = await get_ticket(TICKET_ID, token, user['tenant_id']) if status == 200: print(f" ✅ Ticket obtenido correctamente") else: print(f" ❌ Error {status}: {response}") print("\n" + "=" * 60) print("Nota: Este ticket fue creado por test_user@example.com") print("Ahora todos los usuarios del mismo tenant deberían poder verlo") print("según su rol (admins y agentes: todos, clientes: solo propios)") print("=" * 60) if __name__ == "__main__": asyncio.run(test_access())