127 lines
3.4 KiB
Python
127 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script for Crumbforest Roles API
|
|
Tests the new role system endpoints
|
|
"""
|
|
import requests
|
|
import json
|
|
import sys
|
|
import os
|
|
|
|
# Configuration
|
|
BASE_URL = "http://localhost:8000"
|
|
LOGIN_URL = f"{BASE_URL}/de/login"
|
|
ASK_URL_TEMPLATE = f"{BASE_URL}/crumbforest/roles/{{role_id}}/ask"
|
|
|
|
# Test Credentials (must match setup_demo_user.py)
|
|
TEST_USER = "demo@crumb.local"
|
|
TEST_PASS = "demo123"
|
|
|
|
def get_session():
|
|
"""Login and return a session with cookies."""
|
|
session = requests.Session()
|
|
|
|
# 1. Get login page to get CSRF token if needed (not needed here but good practice)
|
|
# 2. Post login
|
|
print(f"🔑 Logging in as {TEST_USER}...")
|
|
try:
|
|
response = session.post(
|
|
LOGIN_URL,
|
|
data={"email": TEST_USER, "password": TEST_PASS},
|
|
allow_redirects=True
|
|
)
|
|
if response.status_code != 200:
|
|
print(f"❌ Login failed: Status {response.status_code}")
|
|
return None
|
|
|
|
# Check if we are redirected to admin or dashboard (success)
|
|
if "/login" in response.url:
|
|
print("❌ Login failed: Still on login page")
|
|
return None
|
|
|
|
print("✅ Login successful!")
|
|
return session
|
|
except Exception as e:
|
|
print(f"❌ Login error: {e}")
|
|
return None
|
|
|
|
def test_role(session, role_id, question):
|
|
"""Test a specific role."""
|
|
url = ASK_URL_TEMPLATE.format(role_id=role_id)
|
|
print(f"\n{'='*60}")
|
|
print(f"🧪 Testing Role: {role_id.upper()}")
|
|
print(f"📝 Question: {question}")
|
|
print(f"{'='*60}")
|
|
|
|
try:
|
|
response = session.post(
|
|
url,
|
|
data={"question": question},
|
|
timeout=60
|
|
)
|
|
|
|
if response.status_code != 200:
|
|
print(f"❌ Request failed: {response.status_code}")
|
|
print(response.text)
|
|
return False
|
|
|
|
data = response.json()
|
|
|
|
print(f"✅ Response received!")
|
|
print(f"🤖 Role: {data.get('role')}")
|
|
print(f"\n💬 Answer:\n")
|
|
print(data.get('answer'))
|
|
|
|
if 'usage' in data:
|
|
print(f"\n📊 Usage: {data['usage']}")
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error: {e}")
|
|
return False
|
|
|
|
def main():
|
|
print("🌲 Crumbforest Roles API Test Suite")
|
|
print("=" * 60)
|
|
|
|
session = get_session()
|
|
if not session:
|
|
sys.exit(1)
|
|
|
|
# Test Cases
|
|
tests = [
|
|
("dumbo", "Was ist ein Primary Key? Erklär es mir einfach."),
|
|
("snakepy", "Wie schreibe ich eine Hello World Funktion in Python?"),
|
|
("funkfox", "Was ist der Unterschied zwischen let und const?")
|
|
]
|
|
|
|
results = []
|
|
for role_id, question in tests:
|
|
success = test_role(session, role_id, question)
|
|
results.append((role_id, success))
|
|
|
|
# Summary
|
|
print(f"\n{'='*60}")
|
|
print("📊 TEST SUMMARY")
|
|
print(f"{'='*60}")
|
|
|
|
all_passed = True
|
|
for role_id, success in results:
|
|
status = "✅ PASS" if success else "❌ FAIL"
|
|
print(f"{role_id:<15} {status}")
|
|
if not success:
|
|
all_passed = False
|
|
|
|
print(f"{'='*60}\n")
|
|
|
|
if all_passed:
|
|
print("🎉 All role tests passed! 🚀")
|
|
sys.exit(0)
|
|
else:
|
|
print("⚠️ Some tests failed.")
|
|
sys.exit(1)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|