Files
bakery-ia/test_user_endpoint_changes.py
2026-01-16 15:19:34 +01:00

170 lines
6.5 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Test script to verify that the user endpoint changes work correctly.
This script tests the new user endpoint structure after removing /auth/me.
"""
import asyncio
import httpx
import json
from typing import Dict, Any
class UserEndpointTester:
"""Test the user endpoint changes"""
def __init__(self):
self.base_url = "http://localhost:8000" # Gateway URL
self.auth_service_url = "http://localhost:8001" # Auth service URL
self.timeout = 30.0
# Test data
self.test_user_id = "00000000-0000-0000-0000-000000000001" # Example UUID
self.test_auth_token = "test_token_12345"
async def test_auth_service_user_endpoint(self):
"""Test that the auth service user endpoint works correctly"""
print("🧪 Testing auth service user endpoint...")
try:
async with httpx.AsyncClient(timeout=self.timeout) as client:
# Test GET /api/v1/auth/users/{user_id}
url = f"{self.auth_service_url}/api/v1/auth/users/{self.test_user_id}"
headers = {
"Authorization": f"Bearer {self.test_auth_token}",
"Content-Type": "application/json"
}
print(f"📡 Requesting: GET {url}")
response = await client.get(url, headers=headers)
print(f"📤 Response status: {response.status_code}")
print(f"📦 Response headers: {dict(response.headers)}")
if response.status_code == 200:
data = response.json()
print(f"📋 Response data: {json.dumps(data, indent=2)}")
return True
elif response.status_code == 404:
print("⚠️ User not found (expected for test user)")
return True
else:
print(f"❌ Unexpected status code: {response.status_code}")
print(f"📋 Response: {response.text}")
return False
except Exception as e:
print(f"❌ Error testing auth service: {e}")
return False
async def test_gateway_user_endpoint(self):
"""Test that the gateway user endpoint works correctly"""
print("\n🧪 Testing gateway user endpoint...")
try:
async with httpx.AsyncClient(timeout=self.timeout) as client:
# Test GET /api/v1/users/{user_id}
url = f"{self.base_url}/api/v1/users/{self.test_user_id}"
headers = {
"Authorization": f"Bearer {self.test_auth_token}",
"Content-Type": "application/json"
}
print(f"📡 Requesting: GET {url}")
response = await client.get(url, headers=headers)
print(f"📤 Response status: {response.status_code}")
print(f"📦 Response headers: {dict(response.headers)}")
if response.status_code == 200:
data = response.json()
print(f"📋 Response data: {json.dumps(data, indent=2)}")
return True
elif response.status_code == 404:
print("⚠️ User not found (expected for test user)")
return True
else:
print(f"❌ Unexpected status code: {response.status_code}")
print(f"📋 Response: {response.text}")
return False
except Exception as e:
print(f"❌ Error testing gateway: {e}")
return False
async def test_auth_me_endpoint_removed(self):
"""Test that the /auth/me endpoint has been removed"""
print("\n🧪 Testing that /auth/me endpoint has been removed...")
try:
async with httpx.AsyncClient(timeout=self.timeout) as client:
# Test GET /api/v1/auth/me (should return 404)
url = f"{self.auth_service_url}/api/v1/auth/me"
headers = {
"Authorization": f"Bearer {self.test_auth_token}",
"Content-Type": "application/json"
}
print(f"📡 Requesting: GET {url}")
response = await client.get(url, headers=headers)
print(f"📤 Response status: {response.status_code}")
if response.status_code == 404:
print("✅ /auth/me endpoint correctly returns 404 (removed)")
return True
else:
print(f"❌ /auth/me endpoint still exists (status: {response.status_code})")
return False
except Exception as e:
print(f"❌ Error testing /auth/me removal: {e}")
return False
async def run_all_tests(self):
"""Run all tests"""
print("🚀 Starting user endpoint change tests...\n")
results = []
# Test 1: Auth service user endpoint
result1 = await self.test_auth_service_user_endpoint()
results.append(("Auth service user endpoint", result1))
# Test 2: Gateway user endpoint
result2 = await self.test_gateway_user_endpoint()
results.append(("Gateway user endpoint", result2))
# Test 3: /auth/me endpoint removed
result3 = await self.test_auth_me_endpoint_removed()
results.append(("/auth/me endpoint removed", result3))
# Print summary
print("\n" + "="*60)
print("📊 TEST SUMMARY")
print("="*60)
all_passed = True
for test_name, passed in results:
status = "✅ PASS" if passed else "❌ FAIL"
print(f"{status} {test_name}")
if not passed:
all_passed = False
print("="*60)
if all_passed:
print("🎉 All tests passed! User endpoint changes are working correctly.")
else:
print("⚠️ Some tests failed. Please check the implementation.")
return all_passed
if __name__ == "__main__":
tester = UserEndpointTester()
# Run tests
success = asyncio.run(tester.run_all_tests())
exit(0 if success else 1)