fix: Address second code review findings (test isolation, frontend type safety)
This commit is contained in:
@@ -7,7 +7,9 @@ from ea_chatbot.history.models import User
|
||||
# We will need to mock HistoryManager and get_db dependencies later
|
||||
# For now, we define the expected behavior of the auth endpoints.
|
||||
|
||||
client = TestClient(app)
|
||||
@pytest.fixture
|
||||
def client():
|
||||
return TestClient(app)
|
||||
|
||||
@pytest.fixture
|
||||
def mock_user():
|
||||
@@ -19,7 +21,7 @@ def mock_user():
|
||||
theme_preference="light"
|
||||
)
|
||||
|
||||
def test_register_user_success():
|
||||
def test_register_user_success(client):
|
||||
"""Test successful user registration."""
|
||||
# We mock it where it is used in the router
|
||||
with patch("ea_chatbot.api.routers.auth.history_manager") as mock_hm:
|
||||
@@ -34,7 +36,7 @@ def test_register_user_success():
|
||||
assert response.status_code == 201
|
||||
assert response.json()["email"] == "new@example.com"
|
||||
|
||||
def test_login_success():
|
||||
def test_login_success(client):
|
||||
"""Test successful login and JWT return."""
|
||||
with patch("ea_chatbot.api.routers.auth.history_manager") as mock_hm:
|
||||
mock_hm.authenticate_user.return_value = User(id="1", username="test@example.com")
|
||||
@@ -48,7 +50,7 @@ def test_login_success():
|
||||
assert "access_token" in response.json()
|
||||
assert response.json()["token_type"] == "bearer"
|
||||
|
||||
def test_login_invalid_credentials():
|
||||
def test_login_invalid_credentials(client):
|
||||
"""Test login with wrong password."""
|
||||
with patch("ea_chatbot.api.routers.auth.history_manager") as mock_hm:
|
||||
mock_hm.authenticate_user.return_value = None
|
||||
@@ -61,12 +63,12 @@ def test_login_invalid_credentials():
|
||||
assert response.status_code == 401
|
||||
assert "detail" in response.json()
|
||||
|
||||
def test_protected_route_without_token():
|
||||
def test_protected_route_without_token(client):
|
||||
"""Test that protected routes require a token."""
|
||||
response = client.get("/api/v1/auth/me")
|
||||
assert response.status_code == 401
|
||||
|
||||
def test_oidc_login_redirect():
|
||||
def test_oidc_login_redirect(client):
|
||||
"""Test that OIDC login returns a redirect URL and sets session cookie."""
|
||||
with patch("ea_chatbot.api.routers.auth.oidc_client") as mock_oidc:
|
||||
mock_oidc.get_auth_data.return_value = {
|
||||
@@ -81,7 +83,7 @@ def test_oidc_login_redirect():
|
||||
assert response.json()["url"] == "https://oidc-provider.com/auth"
|
||||
assert "oidc_session" in response.cookies
|
||||
|
||||
def test_oidc_callback_success():
|
||||
def test_oidc_callback_success(client):
|
||||
"""Test successful OIDC callback and JWT issuance."""
|
||||
with patch("ea_chatbot.api.routers.auth.oidc_client") as mock_oidc, \
|
||||
patch("ea_chatbot.api.routers.auth.OIDCSession.decrypt") as mock_decrypt, \
|
||||
@@ -105,7 +107,7 @@ def test_oidc_callback_success():
|
||||
assert response.status_code == 302
|
||||
assert "access_token" in response.cookies
|
||||
|
||||
def test_get_me_success():
|
||||
def test_get_me_success(client):
|
||||
"""Test getting current user with a valid token."""
|
||||
from ea_chatbot.api.utils import create_access_token
|
||||
token = create_access_token(data={"sub": "123"})
|
||||
|
||||
Reference in New Issue
Block a user