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

View File

@@ -0,0 +1,81 @@
"""Tests for AuthService."""
from unittest.mock import Mock, patch
from app.services.auth_service import AuthService
class TestAuthService:
"""Test cases for AuthService."""
def test_init_without_app(self) -> None:
"""Test initializing AuthService without Flask app."""
auth_service = AuthService()
assert auth_service.oauth is not None
assert auth_service.google is None
assert auth_service.token_service is not None
@patch("app.services.auth_service.os.getenv")
def test_init_app(self, mock_getenv: Mock) -> None:
"""Test initializing AuthService with Flask app."""
mock_getenv.side_effect = lambda key: {
"GOOGLE_CLIENT_ID": "test_client_id",
"GOOGLE_CLIENT_SECRET": "test_client_secret"
}.get(key)
mock_app = Mock()
auth_service = AuthService()
auth_service.init_app(mock_app)
auth_service.oauth.init_app.assert_called_once_with(mock_app)
@patch("app.services.auth_service.request")
def test_get_current_user_no_token(self, mock_request: Mock) -> None:
"""Test getting current user when no token exists."""
mock_request.cookies.get.return_value = None
auth_service = AuthService()
user = auth_service.get_current_user()
assert user is None
@patch("app.services.auth_service.request")
def test_get_current_user_with_token(self, mock_request: Mock) -> None:
"""Test getting current user when valid token exists."""
mock_request.cookies.get.return_value = "valid.access.token"
auth_service = AuthService()
user_data = {"id": "123", "email": "test@example.com", "name": "Test User"}
with patch.object(auth_service.token_service, 'get_user_from_access_token', return_value=user_data):
user = auth_service.get_current_user()
assert user == user_data
@patch("app.services.auth_service.request")
def test_is_authenticated_false(self, mock_request: Mock) -> None:
"""Test authentication check when not authenticated."""
mock_request.cookies.get.return_value = None
auth_service = AuthService()
assert not auth_service.is_authenticated()
@patch("app.services.auth_service.request")
def test_is_authenticated_true(self, mock_request: Mock) -> None:
"""Test authentication check when authenticated."""
mock_request.cookies.get.return_value = "valid.access.token"
auth_service = AuthService()
user_data = {"id": "123", "email": "test@example.com", "name": "Test User"}
with patch.object(auth_service.token_service, 'get_user_from_access_token', return_value=user_data):
assert auth_service.is_authenticated()
@patch("app.services.auth_service.make_response")
def test_logout(self, mock_make_response: Mock) -> None:
"""Test logout functionality."""
mock_response = Mock()
mock_make_response.return_value = mock_response
auth_service = AuthService()
result = auth_service.logout()
assert result == mock_response
mock_response.set_cookie.assert_any_call("access_token", "", expires=0)
mock_response.set_cookie.assert_any_call("refresh_token", "", expires=0)