test_minimal.py•2.67 kB
#!/usr/bin/env python3
"""
Minimal server to test generate-token endpoint
"""
import sys
import os
import asyncio
from fastapi import FastAPI
from fastapi import Request, HTTPException, Depends
from fastapi.routing import APIRouter
from typing import Dict, Any
import json
import time
# Simple router for testing
router = APIRouter()
# Test user data (for testing purposes)
test_users = {
"test@example.com": {
"user_id": 1,
"email": "test@example.com",
"username": "testuser",
"hashed_password": "fake_hash"
}
}
def get_current_user(request: Request):
"""Simple test auth function"""
auth_header = request.headers.get("Authorization", "")
if not auth_header.startswith("Bearer "):
raise HTTPException(status_code=401, detail="Authentication required")
# For testing, accept any token
return {"user_id": 1, "email": "test@example.com", "username": "testuser"}
@router.post("/generate-token")
async def generate_token(current_user: Dict[str, Any] = Depends(get_current_user)):
"""Generate JWT token for authenticated user"""
try:
# Create user payload for token
user_payload = {
"user_id": current_user.get("user_id"),
"email": current_user.get("email"),
"username": current_user.get("username")
}
# For testing, just return the payload as token
access_token = f"test_token_{int(time.time())}"
return {
"access_token": access_token,
"token_type": "bearer",
"user": user_payload
}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Token generation failed: {str(e)}")
@router.get("/health")
async def health_check():
"""Health check endpoint"""
return {"status": "ok", "message": "Server is running"}
# Create minimal FastAPI app
app = FastAPI(title="MCP Test Server", description="Test server for generate-token endpoint")
# Add CORS middleware
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include router
app.include_router(router, prefix="/auth", tags=["auth"])
if __name__ == "__main__":
import uvicorn
print("🚀 Starting Minimal Test Server on http://localhost:8000")
print("📍 Endpoints:")
print(" - GET /health")
print(" - POST /auth/generate-token")
print("🔑 Test with: curl -X POST http://localhost:8000/auth/generate-token -H 'Authorization: Bearer test'")
uvicorn.run(app, host="0.0.0.0", port=8001)