Skip to content

Commit fe3b1f0

Browse files
committed
add tests
1 parent 6c54f85 commit fe3b1f0

7 files changed

Lines changed: 774 additions & 0 deletions

File tree

.coveragerc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[run]
2+
source = atlas_sdk
3+
omit =
4+
*/tests/*
5+
*/test_*.py
6+
*/__init__.py
7+
8+
[report]
9+
exclude_lines =
10+
pragma: no cover
11+
def __repr__
12+
raise AssertionError
13+
raise NotImplementedError
14+
if __name__ == .__main__.:
15+
if TYPE_CHECKING:
16+
@abstractmethod

pytest.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[pytest]
2+
testpaths = tests
3+
python_files = test_*.py
4+
python_classes = Test*
5+
python_functions = test_*
6+
addopts = -v --tb=short --strict-markers
7+
markers =
8+
unit: Unit tests
9+
integration: Integration tests

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Tests for Atlas SDK."""

tests/test_auth.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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

Comments
 (0)