|
| 1 | +"""Tests for authentication module.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from unittest.mock import Mock, patch, MagicMock |
| 5 | +from datetime import datetime, timedelta |
| 6 | + |
| 7 | +from atlas_sdk.auth import AuthManager |
| 8 | +from atlas_sdk.storage import TokenStorage |
| 9 | +from atlas_sdk.exceptions import AuthenticationError |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def auth_manager(): |
| 14 | + """Create an auth manager for testing.""" |
| 15 | + return AuthManager(base_url="http://localhost:8080/api") |
| 16 | + |
| 17 | + |
| 18 | +class TestAuthManager: |
| 19 | + """Test AuthManager class.""" |
| 20 | + |
| 21 | + @patch("requests.post") |
| 22 | + def test_login_request_magic_link(self, mock_post, auth_manager): |
| 23 | + """Test requesting a magic link.""" |
| 24 | + mock_response = Mock() |
| 25 | + mock_response.status_code = 200 |
| 26 | + mock_response.json.return_value = {"message": "Magic link sent"} |
| 27 | + mock_response.raise_for_status = Mock() |
| 28 | + mock_post.return_value = mock_response |
| 29 | + |
| 30 | + result = auth_manager.login("test@example.com", use_stored_token=False) |
| 31 | + |
| 32 | + assert result["message"] == "Magic link sent" |
| 33 | + assert auth_manager.email == "test@example.com" |
| 34 | + |
| 35 | + @patch("requests.get") |
| 36 | + @patch.object(TokenStorage, "get_token") |
| 37 | + def test_login_with_stored_token(self, mock_get_token, mock_get, auth_manager): |
| 38 | + """Test login with stored token.""" |
| 39 | + mock_get_token.return_value = "stored-token" |
| 40 | + |
| 41 | + mock_response = Mock() |
| 42 | + mock_response.status_code = 200 |
| 43 | + mock_get.return_value = mock_response |
| 44 | + |
| 45 | + result = auth_manager.login("test@example.com", use_stored_token=True) |
| 46 | + |
| 47 | + assert result["success"] is True |
| 48 | + assert auth_manager.jwt_token == "stored-token" |
| 49 | + |
| 50 | + @patch("requests.post") |
| 51 | + def test_validate_magic_link_success(self, mock_post, auth_manager): |
| 52 | + """Test successful magic link validation.""" |
| 53 | + mock_response = Mock() |
| 54 | + mock_response.status_code = 200 |
| 55 | + mock_response.json.return_value = { |
| 56 | + "message": "Validated", |
| 57 | + "email": "test@example.com", |
| 58 | + "credits": 5000, |
| 59 | + } |
| 60 | + mock_response.cookies = {"jwt": "new-token"} |
| 61 | + mock_response.raise_for_status = Mock() |
| 62 | + mock_post.return_value = mock_response |
| 63 | + |
| 64 | + auth_manager.email = "test@example.com" |
| 65 | + |
| 66 | + with patch.object(auth_manager.storage, "save_token") as mock_save: |
| 67 | + result = auth_manager.validate_magic_link("magic-link-123") |
| 68 | + |
| 69 | + assert result["email"] == "test@example.com" |
| 70 | + assert auth_manager.jwt_token == "new-token" |
| 71 | + mock_save.assert_called_once() |
| 72 | + |
| 73 | + def test_validate_magic_link_no_email(self, auth_manager): |
| 74 | + """Test validation without email.""" |
| 75 | + with pytest.raises(AuthenticationError) as exc_info: |
| 76 | + auth_manager.validate_magic_link("magic-link-123") |
| 77 | + |
| 78 | + assert "Email is required" in str(exc_info.value) |
| 79 | + |
| 80 | + @patch("requests.get") |
| 81 | + def test_check_auth_valid(self, mock_get, auth_manager): |
| 82 | + """Test checking valid authentication.""" |
| 83 | + auth_manager.jwt_token = "valid-token" |
| 84 | + auth_manager.cookies = {"jwt": "valid-token"} |
| 85 | + |
| 86 | + mock_response = Mock() |
| 87 | + mock_response.status_code = 200 |
| 88 | + mock_get.return_value = mock_response |
| 89 | + |
| 90 | + assert auth_manager.check_auth() is True |
| 91 | + |
| 92 | + def test_check_auth_no_token(self, auth_manager): |
| 93 | + """Test checking auth with no token.""" |
| 94 | + assert auth_manager.check_auth() is False |
| 95 | + |
| 96 | + @patch("requests.post") |
| 97 | + def test_logout(self, mock_post, auth_manager): |
| 98 | + """Test logout.""" |
| 99 | + auth_manager.jwt_token = "token" |
| 100 | + auth_manager.email = "test@example.com" |
| 101 | + |
| 102 | + mock_response = Mock() |
| 103 | + mock_response.status_code = 200 |
| 104 | + mock_response.json.return_value = {"message": "Logged out"} |
| 105 | + mock_response.raise_for_status = Mock() |
| 106 | + mock_post.return_value = mock_response |
| 107 | + |
| 108 | + with patch.object(auth_manager.storage, "delete_token") as mock_delete: |
| 109 | + result = auth_manager.logout() |
| 110 | + |
| 111 | + assert result["message"] == "Logged out" |
| 112 | + assert auth_manager.jwt_token is None |
| 113 | + assert auth_manager.email is None |
| 114 | + mock_delete.assert_called_once_with("test@example.com") |
0 commit comments