#!/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)