auth google + jwt

This commit is contained in:
JSC
2025-06-27 13:14:29 +02:00
commit 8e2dbd8723
21 changed files with 1107 additions and 0 deletions

106
tests/test_auth_routes.py Normal file
View File

@@ -0,0 +1,106 @@
"""Tests for authentication routes."""
from unittest.mock import Mock, patch
import pytest
from app import create_app
@pytest.fixture
def client():
"""Create a test client for the Flask application."""
app = create_app()
app.config["TESTING"] = True
with app.test_client() as client:
yield client
class TestAuthRoutes:
"""Test cases for authentication routes."""
@patch("app.routes.auth.auth_service.get_login_url")
def test_login_route(self, mock_get_login_url: Mock, client) -> None:
"""Test the login route."""
mock_get_login_url.return_value = "https://accounts.google.com/oauth/authorize?..."
response = client.get("/api/auth/login")
assert response.status_code == 200
data = response.get_json()
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
}
mock_handle_callback.return_value = (user_data, 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()
@patch("app.routes.auth.auth_service.handle_callback")
def test_callback_route_error(self, mock_handle_callback: Mock, client) -> None:
"""Test callback route with error."""
mock_handle_callback.side_effect = Exception("OAuth error")
response = client.get("/api/auth/callback?code=test_code")
assert response.status_code == 400
data = response.get_json()
assert data["error"] == "OAuth error"
@patch("app.routes.auth.auth_service.logout")
def test_logout_route(self, mock_logout: Mock, client) -> None:
"""Test logout route."""
mock_response = Mock()
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()
@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:
"""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"