import pytest from httpx import AsyncClient class TestUserService: """Test cases for User Service""" async def test_register_user(self, client: AsyncClient, user_data): """Test user registration""" response = await client.post("/api/v1/register", json=user_data) assert response.status_code == 200 data = response.json() assert data["email"] == user_data["email"] assert data["first_name"] == user_data["first_name"] assert "id" in data async def test_register_duplicate_email(self, client: AsyncClient, user_data): """Test registration with duplicate email""" # First registration await client.post("/api/v1/register", json=user_data) # Second registration with same email response = await client.post("/api/v1/register", json=user_data) assert response.status_code == 400 assert "already registered" in response.json()["detail"] async def test_login(self, client: AsyncClient, user_data): """Test user login""" # Register user first await client.post("/api/v1/register", json=user_data) # Login login_data = {"email": user_data["email"], "password": user_data["password"]} response = await client.post("/api/v1/login", json=login_data) assert response.status_code == 200 data = response.json() assert "access_token" in data assert data["token_type"] == "bearer" async def test_login_invalid_credentials(self, client: AsyncClient): """Test login with invalid credentials""" login_data = {"email": "wrong@example.com", "password": "wrongpassword"} response = await client.post("/api/v1/login", json=login_data) assert response.status_code == 401 async def test_get_profile(self, client: AsyncClient, user_data): """Test getting user profile""" # Register and login await client.post("/api/v1/register", json=user_data) login_response = await client.post( "/api/v1/login", json={"email": user_data["email"], "password": user_data["password"]}, ) token = login_response.json()["access_token"] # Get profile headers = {"Authorization": f"Bearer {token}"} response = await client.get("/api/v1/profile", headers=headers) assert response.status_code == 200 data = response.json() assert data["email"] == user_data["email"] async def test_update_profile(self, client: AsyncClient, user_data): """Test updating user profile""" # Register and login await client.post("/api/v1/register", json=user_data) login_response = await client.post( "/api/v1/login", json={"email": user_data["email"], "password": user_data["password"]}, ) token = login_response.json()["access_token"] # Update profile update_data = {"bio": "Updated bio text"} headers = {"Authorization": f"Bearer {token}"} response = await client.put( "/api/v1/profile", json=update_data, headers=headers ) assert response.status_code == 200 data = response.json() assert data["bio"] == "Updated bio text"