"""Test authentication endpoints.""" import pytest from httpx import AsyncClient class TestAuthEndpoints: """Tests for /api/v1/auth/* endpoints.""" async def test_register_user(self, client: AsyncClient): """Test user registration.""" response = await client.post( "/api/v1/auth/register", json={ "email": "newuser@example.com", "username": "newuser", "password": "password123", }, ) assert response.status_code == 200 data = response.json() assert "access_token" in data assert "refresh_token" in data assert data["token_type"] == "bearer" async def test_register_duplicate_email(self, client: AsyncClient): """Test registration with duplicate email.""" # First registration await client.post( "/api/v1/auth/register", json={ "email": "duplicate@example.com", "username": "user1", "password": "password123", }, ) # Second registration with same email response = await client.post( "/api/v1/auth/register", json={ "email": "duplicate@example.com", "username": "user2", "password": "password123", }, ) assert response.status_code == 400 async def test_login_success(self, client: AsyncClient): """Test successful login.""" # Register first await client.post( "/api/v1/auth/register", json={ "email": "login@example.com", "username": "loginuser", "password": "password123", }, ) # Login response = await client.post( "/api/v1/auth/login", json={ "email": "login@example.com", "password": "password123", }, ) assert response.status_code == 200 data = response.json() assert "access_token" in data assert "refresh_token" in data async def test_login_wrong_password(self, client: AsyncClient): """Test login with wrong password.""" # Register first await client.post( "/api/v1/auth/register", json={ "email": "wrongpass@example.com", "username": "wronguser", "password": "password123", }, ) # Login with wrong password response = await client.post( "/api/v1/auth/login", json={ "email": "wrongpass@example.com", "password": "wrongpassword", }, ) assert response.status_code == 401 async def test_get_current_user(self, client: AsyncClient, auth_headers: dict): """Test getting current user info.""" response = await client.get("/api/v1/auth/me", headers=auth_headers) assert response.status_code == 200 data = response.json() assert data["email"] == "test@example.com" assert data["username"] == "testuser" async def test_get_current_user_unauthorized(self, client: AsyncClient): """Test getting current user without auth.""" response = await client.get("/api/v1/auth/me") assert response.status_code == 401