feat: Add light/dark mode support with backend persistence
This commit is contained in:
@@ -15,7 +15,8 @@ def mock_user():
|
||||
id="user-123",
|
||||
username="test@example.com",
|
||||
display_name="Test User",
|
||||
password_hash="hashed_password"
|
||||
password_hash="hashed_password",
|
||||
theme_preference="light"
|
||||
)
|
||||
|
||||
def test_register_user_success():
|
||||
@@ -23,7 +24,7 @@ def test_register_user_success():
|
||||
# We mock it where it is used in the router
|
||||
with patch("ea_chatbot.api.routers.auth.history_manager") as mock_hm:
|
||||
mock_hm.get_user.return_value = None
|
||||
mock_hm.create_user.return_value = User(id="1", username="new@example.com", display_name="New")
|
||||
mock_hm.create_user.return_value = User(id="1", username="new@example.com", display_name="New", theme_preference="light")
|
||||
|
||||
response = client.post(
|
||||
"/api/v1/auth/register",
|
||||
@@ -93,7 +94,7 @@ def test_oidc_callback_success():
|
||||
}
|
||||
mock_oidc.exchange_code_for_token.return_value = {"id_token": "fake_id_token"}
|
||||
mock_oidc.validate_id_token.return_value = {"email": "sso@example.com", "name": "SSO User"}
|
||||
mock_hm.sync_user_from_oidc.return_value = User(id="sso-123", username="sso@example.com", display_name="SSO User")
|
||||
mock_hm.sync_user_from_oidc.return_value = User(id="sso-123", username="sso@example.com", display_name="SSO User", theme_preference="light")
|
||||
|
||||
client.cookies.set("oidc_session", "fake_token")
|
||||
response = client.get(
|
||||
@@ -110,7 +111,7 @@ def test_get_me_success():
|
||||
token = create_access_token(data={"sub": "123"})
|
||||
|
||||
with patch("ea_chatbot.api.dependencies.history_manager") as mock_hm:
|
||||
mock_hm.get_user_by_id.return_value = User(id="123", username="test@example.com", display_name="Test")
|
||||
mock_hm.get_user_by_id.return_value = User(id="123", username="test@example.com", display_name="Test", theme_preference="light")
|
||||
|
||||
response = client.get(
|
||||
"/api/v1/auth/me",
|
||||
|
||||
73
backend/tests/api/test_theme.py
Normal file
73
backend/tests/api/test_theme.py
Normal file
@@ -0,0 +1,73 @@
|
||||
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
|
||||
|
||||
client = 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(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
|
||||
|
||||
response = client.get(
|
||||
"/api/v1/auth/me",
|
||||
cookies={"access_token": auth_token}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "theme_preference" in data
|
||||
assert data["theme_preference"] == "light"
|
||||
|
||||
def test_update_theme_success(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
|
||||
|
||||
response = client.patch(
|
||||
"/api/v1/auth/theme",
|
||||
json={"theme": "dark"},
|
||||
cookies={"access_token": auth_token}
|
||||
)
|
||||
|
||||
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():
|
||||
"""Test that theme update requires authentication."""
|
||||
response = client.patch(
|
||||
"/api/v1/auth/theme",
|
||||
json={"theme": "dark"}
|
||||
)
|
||||
assert response.status_code == 401
|
||||
Reference in New Issue
Block a user