XYHLF / tests /test_users.py
Mbonea's picture
initial commit
9d4bd7c
import pytest
import httpx
pytest_plugins = ["pytest_asyncio"]
@pytest.mark.asyncio
async def test_register_user(client, initialize_tests):
async with httpx.AsyncClient() as async_client:
response = await async_client.post("http://localhost:8001/users/register", json={
"username": "testwuser",
"email": "[email protected]",
"password": "testpassword123"
})
assert response.status_code == 200
data = response.json()
assert data["success"] == True
assert data["data"]["username"] == "testuser"
assert data["data"]["email"] == "[email protected]"
@pytest.mark.asyncio
async def test_register_duplicate_email(client, initialize_tests):
# First registration
async with httpx.AsyncClient() as async_client:
await async_client.post("http://localhost:8001/users/register", json={
"username": "existing",
"email": "[email protected]",
"password": "password123"
})
# Attempt duplicate registration
response = await async_client.post("http://localhost:8001/users/register", json={
"username": "testwuser",
"email": "[email protected]",
"password": "testpassword123"
})
assert response.status_code == 400
data = response.json()
print(data)
assert data["success"] == False
assert "Email already registered" in data["message"]
@pytest.mark.asyncio
async def test_get_portfolio(client, initialize_tests):
async with httpx.AsyncClient() as async_client:
# First create a user
register_response = await async_client.post("http://localhost:8001/users/register", json={
"username": "portfoliouser",
"email": "[email protected]",
"password": "password123"
})
user_id = register_response.json()["data"]["id"]
# Create a portfolio for the user
portfolio_response = await async_client.post(f"http://localhost:8001/users/{user_id}/portfolio", json={
"name": "Test Portfolio"
})
assert portfolio_response.status_code == 200
assert portfolio_response.json()["success"] == True
# Get the portfolio
get_response = await async_client.get(f"http://localhost:8001/users/{user_id}/portfolio")
assert get_response.status_code == 200
data = get_response.json()
assert data["success"] == True
assert "data" in data