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,12 +1,13 @@
"""Tests for AuthService."""
"""Tests for AuthService with Flask-JWT-Extended."""
from unittest.mock import Mock, patch
from app import create_app
from app.services.auth_service import AuthService
class TestAuthService:
"""Test cases for AuthService."""
class TestAuthServiceJWTExtended:
"""Test cases for AuthService with Flask-JWT-Extended."""
def test_init_without_app(self) -> None:
"""Test initializing AuthService without Flask app."""
@@ -23,59 +24,25 @@ class TestAuthService:
"GOOGLE_CLIENT_SECRET": "test_client_secret"
}.get(key)
mock_app = Mock()
app = create_app()
auth_service = AuthService()
auth_service.init_app(mock_app)
auth_service.init_app(app)
auth_service.oauth.init_app.assert_called_once_with(mock_app)
# Verify OAuth was initialized
assert auth_service.google is not None
@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:
@patch("app.services.auth_service.unset_jwt_cookies")
@patch("app.services.auth_service.jsonify")
def test_logout(self, mock_jsonify: Mock, mock_unset: 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)
app = create_app()
with app.app_context():
mock_response = Mock()
mock_jsonify.return_value = mock_response
auth_service = AuthService()
result = auth_service.logout()
assert result == mock_response
mock_unset.assert_called_once_with(mock_response)
mock_jsonify.assert_called_once_with({"message": "Logged out successfully"})