Files
sdb-back/tests/test_auth_service.py
JSC 171dbb9b63 Refactor code for improved readability and consistency
- Cleaned up whitespace and formatting across multiple files for better readability.
2025-07-02 10:37:48 +02:00

51 lines
1.7 KiB
Python

"""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 TestAuthServiceJWTExtended:
"""Test cases for AuthService with Flask-JWT-Extended."""
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)
app = create_app()
auth_service = AuthService()
auth_service.init_app(app)
# Verify OAuth was initialized
assert auth_service.google is not 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."""
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"}
)