"""Integration tests for authentication endpoints.""" import pytest from fastapi.testclient import TestClient def test_register_success(client: TestClient): """Test successful user registration.""" response = client.post( "/auth/register", json={ "email": "newuser@test.com", "password": "password123", "name": "New User", }, ) assert response.status_code == 201 data = response.json() assert data["success"] is True assert "token" in data["data"] assert data["data"]["token"]["token_type"] == "bearer" assert "user" in data["data"] assert data["data"]["user"]["email"] == "newuser@test.com" def test_register_duplicate_email(client: TestClient): """Test registration with duplicate email fails.""" # Register first user client.post( "/auth/register", json={ "email": "duplicate@test.com", "password": "password123", "name": "First User", }, ) # Try to register again with same email response = client.post( "/auth/register", json={ "email": "duplicate@test.com", "password": "password456", "name": "Second User", }, ) assert response.status_code == 400 def test_register_short_password(client: TestClient): """Test registration with password < 8 characters fails.""" response = client.post( "/auth/register", json={ "email": "short@test.com", "password": "short", "name": "Short Password", }, ) assert response.status_code == 422 # Validation error def test_login_success(client: TestClient): """Test successful login.""" # Register user first client.post( "/auth/register", json={ "email": "login@test.com", "password": "password123", "name": "Login User", }, ) # Login response = client.post( "/auth/login", json={"email": "login@test.com", "password": "password123"}, ) assert response.status_code == 200 data = response.json() assert data["success"] is True assert "token" in data["data"] assert data["message"] == "Login successful" def test_login_wrong_password(client: TestClient): """Test login with wrong password fails.""" # Register user client.post( "/auth/register", json={ "email": "wrongpass@test.com", "password": "correctpass", "name": "User", }, ) # Login with wrong password response = client.post( "/auth/login", json={"email": "wrongpass@test.com", "password": "wrongpass"}, ) assert response.status_code == 401 def test_login_nonexistent_user(client: TestClient): """Test login with non-existent user fails.""" response = client.post( "/auth/login", json={"email": "noexist@test.com", "password": "password123"}, ) assert response.status_code == 401 def test_logout_success(client: TestClient, auth_token_factory): """Test logout with valid token.""" # Create a mock user_id and token token = auth_token_factory(1) response = client.post( "/auth/logout", headers={"Authorization": f"Bearer {token}"}, ) # Logout returns 200 even though JWT is stateless assert response.status_code == 200 data = response.json() assert data["success"] is True def test_logout_without_token(client: TestClient): """Test logout without token fails.""" response = client.post("/auth/logout") assert response.status_code == 403 # Forbidden (no auth header)