import pytest from fastapi.testclient import TestClient from unittest.mock import patch from ea_chatbot.api.main import app from ea_chatbot.history.models import User from ea_chatbot.api.utils import create_access_token @pytest.fixture def client(): return TestClient(app) @pytest.fixture def test_user(): return User( id="user-123", username="test@example.com", display_name="Test User", theme_preference="light" ) @pytest.fixture def auth_token(): return create_access_token(data={"sub": "user-123"}) def test_get_me_includes_theme(client, test_user, auth_token): """Test that /auth/me returns the theme_preference.""" with patch("ea_chatbot.api.dependencies.history_manager") as mock_hm: mock_hm.get_user_by_id.return_value = test_user client.cookies.set("access_token", auth_token) response = client.get("/api/v1/auth/me") assert response.status_code == 200 data = response.json() assert "theme_preference" in data assert data["theme_preference"] == "light" def test_update_theme_success(client, test_user, auth_token): """Test successful theme update via PATCH /auth/theme.""" updated_user = User( id="user-123", username="test@example.com", display_name="Test User", theme_preference="dark" ) with patch("ea_chatbot.api.dependencies.history_manager") as mock_hm_dep, \ patch("ea_chatbot.api.routers.auth.history_manager") as mock_hm_router: # Dependency injection uses the one from dependencies mock_hm_dep.get_user_by_id.return_value = test_user # The router uses its own reference to history_manager mock_hm_router.update_user_theme.return_value = updated_user client.cookies.set("access_token", auth_token) response = client.patch( "/api/v1/auth/theme", json={"theme": "dark"} ) assert response.status_code == 200 data = response.json() assert data["theme_preference"] == "dark" mock_hm_router.update_user_theme.assert_called_once_with("user-123", "dark") def test_update_theme_unauthorized(client): """Test that theme update requires authentication.""" response = client.patch( "/api/v1/auth/theme", json={"theme": "dark"} ) assert response.status_code == 401