import pytest from httpx import AsyncClient from sqlalchemy.ext.asyncio import AsyncSession from app.core.security import create_access_token pytestmark = pytest.mark.asyncio async def test_login(client: AsyncClient, db_session: AsyncSession): # First register a test user register_response = await client.post( "/api/v1/auth/register", data={ "username": "testuser@example.com", "password": "testpass123" } ) assert register_response.status_code == 200 # Test login with correct credentials response = await client.post( "/api/v1/auth/login", data={ "username": "testuser@example.com", "password": "testpass123" } ) assert response.status_code == 200 assert "access_token" in response.json() assert response.json()["token_type"] == "bearer" # Test login with incorrect password response = await client.post( "/api/v1/auth/login", data={ "username": "testuser@example.com", "password": "wrongpass" } ) assert response.status_code == 401 # Test login with non-existent user response = await client.post( "/api/v1/auth/login", data={ "username": "nonexistent@example.com", "password": "testpass123" } ) assert response.status_code == 401 async def test_register(client: AsyncClient, db_session: AsyncSession): # Test successful registration response = await client.post( "/api/v1/auth/register", data={ "username": "newuser@example.com", "password": "newpass123" } ) assert response.status_code == 200 user_data = response.json() assert user_data["email"] == "newuser@example.com" assert "hashed_password" not in user_data # Test registration with existing email response = await client.post( "/api/v1/auth/register", data={ "username": "newuser@example.com", "password": "anotherpass" } ) assert response.status_code == 400 assert "Email already registered" in response.json()["detail"]