#!/usr/bin/env python3
"""
Test script to verify the authentication fix works correctly
"""
import json
import requests
import time
# Configuration - using the new port
BASE_URL = "http://localhost:8082"
BEARER_TOKEN = "K32SfHHiOdaoMEde4r7cvBd7gYfdY3UPQccGHkh5gMyMwcrjfHMETV8RqzeXdrRg0dDrbMZ"
def test_health_endpoint():
"""Test the health endpoint (should always work)"""
print("๐ฅ Testing health endpoint...")
try:
response = requests.get(f"{BASE_URL}/health", timeout=10)
print(f" Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f" Response: {data}")
print(" โ
Health check passed")
return True
else:
print(f" โ Health check failed: {response.text}")
return False
except Exception as e:
print(f" โ Health check error: {e}")
return False
def test_mcp_initialization():
"""Test MCP initialization (should work without auth based on our fix)"""
print("\n๐ค Testing MCP initialization...")
headers = {
"Content-Type": "application/json",
"Accept": "application/json, text/event-stream"
}
payload = {
"jsonrpc": "2.0",
"method": "initialize",
"id": "test-init-001",
"params": {
"protocolVersion": "2024-11-05"
}
}
try:
response = requests.post(f"{BASE_URL}/mcp",
headers=headers,
json=payload,
timeout=15)
print(f" Status: {response.status_code}")
print(f" Response: {response.text[:500]}...")
if response.status_code == 200:
data = response.json()
if "result" in data:
print(" โ
MCP initialization successful!")
return True
else:
print(f" โ Unexpected response format: {data}")
return False
else:
print(f" โ MCP initialization failed")
return False
except Exception as e:
print(f" โ MCP initialization error: {e}")
return False
def test_ping_tool_with_auth():
"""Test calling the ping tool with authentication"""
print("\n๐ Testing ping tool with authentication...")
headers = {
"Content-Type": "application/json",
"Accept": "application/json, text/event-stream",
"Authorization": f"Bearer {BEARER_TOKEN}"
}
payload = {
"jsonrpc": "2.0",
"method": "tools/call",
"id": "test-ping-001",
"params": {
"name": "ping",
"arguments": {}
}
}
try:
response = requests.post(f"{BASE_URL}/mcp",
headers=headers,
json=payload,
timeout=15)
print(f" Status: {response.status_code}")
print(f" Response: {response.text}")
if response.status_code == 200:
data = response.json()
if "result" in data:
print(f" โ
Ping successful: {data['result']}")
return True
else:
print(f" โ Unexpected ping response: {data}")
return False
else:
print(f" โ Ping failed: {response.text}")
return False
except Exception as e:
print(f" โ Ping error: {e}")
return False
def main():
"""Run authentication fix test suite"""
print("๐งช Authentication Fix Test Suite")
print("=" * 50)
# Test 1: Health check
health_ok = test_health_endpoint()
# Test 2: MCP Initialization (should work without auth)
init_ok = test_mcp_initialization()
# Test 3: Tool call with auth (this is where the fix should work)
ping_ok = test_ping_tool_with_auth()
# Summary
print("\n" + "=" * 50)
print("๐ Test Results Summary:")
print(f" Health Endpoint: {'โ
' if health_ok else 'โ'}")
print(f" MCP Initialization: {'โ
' if init_ok else 'โ'}")
print(f" Tool Call with Auth: {'โ
' if ping_ok else 'โ'}")
if all([health_ok, init_ok, ping_ok]):
print("\n๐ ALL TESTS PASSED! Authentication fix is working!")
return True
else:
print(f"\nโ ๏ธ Some tests failed. The invalid_token error may still be present.")
return False
if __name__ == "__main__":
main()