feat(api): Implement authentication router and secure dependencies

This commit is contained in:
Yunxiao Xu
2026-02-10 12:37:35 -08:00
parent 979e1ad2d6
commit ff27dee366
3 changed files with 73 additions and 30 deletions

View File

@@ -20,7 +20,7 @@ def mock_user():
def test_register_user_success():
"""Test successful user registration."""
# We'll need to mock history_manager.get_user and create_user
# 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")
@@ -30,7 +30,6 @@ def test_register_user_success():
json={"email": "new@example.com", "password": "password123", "display_name": "New"}
)
# This will fail now because the router doesn't exist
assert response.status_code == 201
assert response.json()["email"] == "new@example.com"
@@ -74,3 +73,20 @@ def test_oidc_login_redirect():
response = client.get("/auth/oidc/login")
assert response.status_code == 200
assert response.json()["url"] == "https://oidc-provider.com/auth"
def test_get_me_success():
"""Test getting current user with a valid token."""
from ea_chatbot.api.utils import create_access_token
token = create_access_token(data={"sub": "test@example.com", "user_id": "123"})
with patch("ea_chatbot.api.dependencies.history_manager") as mock_hm:
mock_hm.get_user.return_value = User(id="123", username="test@example.com", display_name="Test")
response = client.get(
"/auth/me",
headers={"Authorization": f"Bearer {token}"}
)
assert response.status_code == 200
assert response.json()["email"] == "test@example.com"
assert response.json()["id"] == "123"