51 lines
1.8 KiB
Python
51 lines
1.8 KiB
Python
import httpx
|
|
import asyncio
|
|
|
|
BASE_URL = "http://localhost:8000"
|
|
|
|
async def test_search():
|
|
async with httpx.AsyncClient() as client:
|
|
# 1. Login to get token
|
|
login_data = {
|
|
"username": "testuser",
|
|
"password": "testpassword"
|
|
}
|
|
# First, try to register if user doesn't exist (assuming test env)
|
|
try:
|
|
await client.post(f"{BASE_URL}/register", json={
|
|
"username": "testuser",
|
|
"email": "test@example.com",
|
|
"password": "testpassword"
|
|
})
|
|
except:
|
|
pass
|
|
|
|
response = await client.post(f"{BASE_URL}/login", json=login_data)
|
|
if response.status_code != 200:
|
|
print(f"Login failed: {response.text}")
|
|
return
|
|
|
|
token = response.json()["access_token"]
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
|
|
# 2. Test search by username
|
|
print("Testing search by username 'test'...")
|
|
response = await client.get(f"{BASE_URL}/users/search?query=test", headers=headers)
|
|
print(f"Status: {response.status_code}")
|
|
print(f"Body: {response.json()}")
|
|
|
|
# 3. Test search by email
|
|
print("\nTesting search by email 'example'...")
|
|
response = await client.get(f"{BASE_URL}/users/search?query=example", headers=headers)
|
|
print(f"Status: {response.status_code}")
|
|
print(f"Body: {response.json()}")
|
|
|
|
# 4. Test search with no results
|
|
print("\nTesting search with no results 'nonexistent'...")
|
|
response = await client.get(f"{BASE_URL}/users/search?query=nonexistent", headers=headers)
|
|
print(f"Status: {response.status_code}")
|
|
print(f"Body: {response.json()}")
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(test_search())
|