auth email/password

This commit is contained in:
JSC
2025-06-28 18:30:30 +02:00
parent 8e2dbd8723
commit ceafed9108
25 changed files with 1694 additions and 314 deletions

View File

@@ -1,4 +1,4 @@
"""Tests for authentication routes."""
"""Tests for authentication routes with Flask-JWT-Extended."""
from unittest.mock import Mock, patch
@@ -12,12 +12,13 @@ def client():
"""Create a test client for the Flask application."""
app = create_app()
app.config["TESTING"] = True
app.config["JWT_COOKIE_SECURE"] = False # Allow cookies in testing
with app.test_client() as client:
yield client
class TestAuthRoutes:
"""Test cases for authentication routes."""
class TestAuthRoutesJWTExtended:
"""Test cases for authentication routes with Flask-JWT-Extended."""
@patch("app.routes.auth.auth_service.get_login_url")
def test_login_route(self, mock_get_login_url: Mock, client) -> None:
@@ -30,33 +31,18 @@ class TestAuthRoutes:
assert "login_url" in data
assert data["login_url"] == "https://accounts.google.com/oauth/authorize?..."
def test_callback_route_no_code(self, client) -> None:
"""Test callback route without authorization code."""
response = client.get("/api/auth/callback")
assert response.status_code == 400
data = response.get_json()
assert data["error"] == "Authorization code not found"
@patch("app.routes.auth.auth_service.handle_callback")
def test_callback_route_success(self, mock_handle_callback: Mock, client) -> None:
"""Test successful callback route."""
user_data = {
"id": "123",
"email": "test@example.com",
"name": "Test User"
}
mock_response = Mock()
mock_response.get_json.return_value = {
"message": "Login successful",
"user": user_data
"user": {"id": "123", "email": "test@example.com", "name": "Test User"}
}
mock_handle_callback.return_value = (user_data, mock_response)
mock_handle_callback.return_value = mock_response
with patch("app.routes.auth.client.get") as mock_get:
mock_get.return_value = mock_response
response = client.get("/api/auth/callback?code=test_code")
# Since we're returning the mock response directly, we need to verify differently
mock_handle_callback.assert_called_once()
response = client.get("/api/auth/callback?code=test_code")
mock_handle_callback.assert_called_once()
@patch("app.routes.auth.auth_service.handle_callback")
def test_callback_route_error(self, mock_handle_callback: Mock, client) -> None:
@@ -75,32 +61,19 @@ class TestAuthRoutes:
mock_response.get_json.return_value = {"message": "Logged out successfully"}
mock_logout.return_value = mock_response
with patch("app.routes.auth.client.get") as mock_get:
mock_get.return_value = mock_response
response = client.get("/api/auth/logout")
mock_logout.assert_called_once()
response = client.get("/api/auth/logout")
mock_logout.assert_called_once()
@patch("app.routes.auth.auth_service.get_current_user")
def test_me_route_authenticated(self, mock_get_current_user: Mock, client) -> None:
"""Test /me route when authenticated."""
user_data = {
"id": "123",
"email": "test@example.com",
"name": "Test User"
}
mock_get_current_user.return_value = user_data
response = client.get("/api/auth/me")
assert response.status_code == 200
data = response.get_json()
assert data["user"] == user_data
@patch("app.routes.auth.auth_service.get_current_user")
def test_me_route_not_authenticated(self, mock_get_current_user: Mock, client) -> None:
def test_me_route_not_authenticated(self, client) -> None:
"""Test /me route when not authenticated."""
mock_get_current_user.return_value = None
response = client.get("/api/auth/me")
assert response.status_code == 401
data = response.get_json()
assert data["error"] == "Not authenticated"
assert "msg" in data # Flask-JWT-Extended error format
def test_refresh_route_not_authenticated(self, client) -> None:
"""Test /refresh route when not authenticated."""
response = client.post("/api/auth/refresh")
assert response.status_code == 401
data = response.get_json()
assert "msg" in data # Flask-JWT-Extended error format