143 lines
4.7 KiB
Python
143 lines
4.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Debug the 500 Internal Server Error on variant approval - fixed CSRF.
|
|
"""
|
|
|
|
import re
|
|
|
|
import httpx
|
|
|
|
BASE_URL = "http://localhost:8000"
|
|
|
|
|
|
def login(client: httpx.Client) -> bool:
|
|
"""Login and maintain session."""
|
|
response = client.get("/admin/login")
|
|
if response.status_code != 200:
|
|
return False
|
|
|
|
match = re.search(r'name="csrf_token" value="([^"]+)"', response.text)
|
|
csrf_token = match.group(1) if match else ""
|
|
|
|
if not csrf_token:
|
|
return False
|
|
|
|
response = client.post(
|
|
"/admin/login",
|
|
data={
|
|
"username": "admin",
|
|
"password": "admin123",
|
|
"csrf_token": csrf_token,
|
|
},
|
|
follow_redirects=True,
|
|
)
|
|
|
|
return response.status_code == 200 and "/admin/dashboard" in str(response.url)
|
|
|
|
|
|
def get_csrf_from_page(client: httpx.Client, page_url: str) -> tuple:
|
|
"""Get CSRF token from a specific page and return both token and response."""
|
|
response = client.get(page_url, follow_redirects=True)
|
|
if response.status_code == 200:
|
|
match = re.search(r'name="csrf_token" value="([^"]+)"', response.text)
|
|
if match:
|
|
return match.group(1), response
|
|
return "", response
|
|
|
|
|
|
def main():
|
|
print("=" * 80)
|
|
print("Debugging 500 Internal Server Error on Variant Approval")
|
|
print("=" * 80)
|
|
|
|
with httpx.Client(base_url=BASE_URL, timeout=60.0) as client:
|
|
print("\n1. Logging in...")
|
|
if not login(client):
|
|
print(" ❌ Login failed")
|
|
return
|
|
print(" ✅ Login successful")
|
|
|
|
# Test 1: Variant approval - get CSRF from the actual review page
|
|
print("\n2. Testing variant approval...")
|
|
|
|
# First access the review page to get the CSRF token
|
|
csrf_token, page_response = get_csrf_from_page(
|
|
client, "/admin/questions/4/generate?tab=review"
|
|
)
|
|
print(f" Page URL: {page_response.url}")
|
|
print(f" Page status: {page_response.status_code}")
|
|
print(f" CSRF token: {csrf_token[:30] if csrf_token else 'None'}...")
|
|
|
|
# If we got redirected, we can't test this endpoint
|
|
if "/generate" not in str(page_response.url):
|
|
print(
|
|
" ⚠️ Redirected away from AI playground - item may not exist or not be AI-generated"
|
|
)
|
|
print(" Skipping this test...")
|
|
else:
|
|
# Submit the form
|
|
response = client.post(
|
|
"/admin/questions/4/generate/review-bulk",
|
|
data={
|
|
"item_ids": "4",
|
|
"action": "approved",
|
|
"tab": "review",
|
|
"csrf_token": csrf_token,
|
|
},
|
|
follow_redirects=True,
|
|
)
|
|
|
|
print(f" Response status: {response.status_code}")
|
|
|
|
# Extract and print the full traceback
|
|
if "Traceback" in response.text:
|
|
idx = response.text.find("Traceback")
|
|
print("\n" + "=" * 80)
|
|
print("FULL TRACEBACK:")
|
|
print("=" * 80)
|
|
print(response.text[idx:])
|
|
print("=" * 80)
|
|
elif response.status_code == 500:
|
|
print("\n ⚠️ Got 500 error but no traceback in response")
|
|
print(f" Response preview: {response.text[:500]}")
|
|
else:
|
|
print(f" Response preview: {response.text[:500]}")
|
|
|
|
# Test 2: Generate variants
|
|
print("\n3. Testing generate variants...")
|
|
|
|
csrf_token, page_response = get_csrf_from_page(
|
|
client, "/admin/questions/4/generate?tab=generate"
|
|
)
|
|
print(f" Page URL: {page_response.url}")
|
|
print(f" Page status: {page_response.status_code}")
|
|
|
|
if "/generate" not in str(page_response.url):
|
|
print(" ⚠️ Redirected away from AI playground")
|
|
else:
|
|
response = client.post(
|
|
"/admin/questions/4/generate",
|
|
data={
|
|
"target_level": "mudah",
|
|
"ai_model": "meta-llama/llama-4-maverick:free",
|
|
"generation_count": "1",
|
|
"operator_notes": "",
|
|
"csrf_token": csrf_token,
|
|
},
|
|
follow_redirects=True,
|
|
)
|
|
|
|
print(f" Response status: {response.status_code}")
|
|
|
|
if "Traceback" in response.text:
|
|
idx = response.text.find("Traceback")
|
|
print("\n" + "=" * 80)
|
|
print("FULL TRACEBACK:")
|
|
print("=" * 80)
|
|
print(response.text[idx:])
|
|
print("=" * 80)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|