Skip to content

Commit 489239d

Browse files
agusmdevclaude
andcommitted
refactor: rename misleading methods in AuthService
- session_id() -> generate_session_id(): was named as accessor but generates a new token via secrets.token_urlsafe(64) - check_session() -> validate_session(): returns UserResponse not a bool; the name suggested a boolean check when it actually retrieves the user Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c8f33b5 commit 489239d

4 files changed

Lines changed: 18 additions & 18 deletions

File tree

template/backend/app/user/auth/permissions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async def get_user(
5555
) -> User:
5656
try:
5757
if not getattr(request.state, "user", None):
58-
user = await auth_service.check_session(http_auth.credentials)
58+
user = await auth_service.validate_session(http_auth.credentials)
5959
cast("Any", request.state).user = user
6060
req_ctx: RequestContext = get_request_context()
6161
req_ctx.user_id = str(user.id)

template/backend/app/user/auth/service.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def __init__(
9090
}
9191

9292
@staticmethod
93-
def session_id() -> str:
93+
def generate_session_id() -> str:
9494
return f"s_{secrets.token_urlsafe(64)}"
9595

9696
@staticmethod
@@ -103,7 +103,7 @@ async def authenticate(self, email: str, password: str) -> SessionResponse:
103103

104104
created_session = await self.repo.create(
105105
SessionCreate(
106-
id=self.session_id(),
106+
id=self.generate_session_id(),
107107
user_id=user.id,
108108
expires_at=datetime.now() + timedelta(days=365),
109109
)
@@ -139,7 +139,7 @@ async def oauth_login(
139139

140140
created_session = await self.repo.create(
141141
SessionCreate(
142-
id=self.session_id(),
142+
id=self.generate_session_id(),
143143
user_id=user.id,
144144
expires_at=datetime.now() + timedelta(days=365),
145145
)
@@ -150,7 +150,7 @@ async def oauth_login(
150150
expires_at=created_session.expires_at,
151151
)
152152

153-
async def check_session(self, session_id: str) -> UserResponse:
153+
async def validate_session(self, session_id: str) -> UserResponse:
154154
session = cast(
155155
"UserSessionResponse",
156156
await self.repo.get(

template/backend/tests/unit/services/test_auth_service.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ class TestAuthServiceSessionId:
2424

2525
def test_session_id_format(self):
2626
"""Test session_id format."""
27-
session_id = AuthService.session_id()
27+
session_id = AuthService.generate_session_id()
2828
assert session_id.startswith("s_")
2929
assert len(session_id) > 10
3030

3131
def test_session_id_unique(self):
3232
"""Test session_id generates unique values."""
33-
ids = [AuthService.session_id() for _ in range(100)]
33+
ids = [AuthService.generate_session_id() for _ in range(100)]
3434
assert len(set(ids)) == 100
3535

3636
def test_generate_token_format(self):
@@ -142,7 +142,7 @@ async def test_register_success(
142142

143143

144144
class TestAuthServiceCheckSession:
145-
"""Tests for AuthService.check_session method."""
145+
"""Tests for AuthService.validate_session method."""
146146

147147
@pytest.fixture
148148
def auth_service(
@@ -157,7 +157,7 @@ def auth_service(
157157
email_verification_repo=mock_email_verification_repository,
158158
)
159159

160-
async def test_check_session_valid(self, auth_service, mock_session_repository):
160+
async def test_validate_session_valid(self, auth_service, mock_session_repository):
161161
"""Test checking valid session."""
162162
mock_session = MagicMock()
163163
mock_session.expires_at = datetime.now() + timedelta(days=1)
@@ -166,19 +166,19 @@ async def test_check_session_valid(self, auth_service, mock_session_repository):
166166
)
167167
mock_session_repository.get.return_value = mock_session
168168

169-
result = await auth_service.check_session("s_valid_session")
169+
result = await auth_service.validate_session("s_valid_session")
170170

171171
assert result == mock_session.user
172172

173-
async def test_check_session_expired(self, auth_service, mock_session_repository):
173+
async def test_validate_session_expired(self, auth_service, mock_session_repository):
174174
"""Test checking expired session."""
175175
mock_session = MagicMock()
176176
mock_session.expires_at = datetime.now() - timedelta(days=1)
177177
mock_session.user = MagicMock()
178178
mock_session_repository.get.return_value = mock_session
179179

180180
with pytest.raises(SessionExpiredError):
181-
await auth_service.check_session("s_expired_session")
181+
await auth_service.validate_session("s_expired_session")
182182

183183

184184
class TestAuthServiceLogout:

template/backend/tests/unit/services/test_authenticated_user.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ async def test_get_user_success(
5858
self, mock_request, mock_http_auth, mock_auth_service, sample_user_response
5959
):
6060
"""Test successful user retrieval."""
61-
mock_auth_service.check_session = AsyncMock(return_value=sample_user_response)
61+
mock_auth_service.validate_session = AsyncMock(return_value=sample_user_response)
6262

6363
with patch("app.user.auth.permissions.get_request_context") as mock_ctx, \
6464
patch("app.user.auth.permissions.log_user"):
@@ -68,13 +68,13 @@ async def test_get_user_success(
6868
)
6969

7070
assert result is sample_user_response
71-
mock_auth_service.check_session.assert_called_once_with("s_test_session_token")
71+
mock_auth_service.validate_session.assert_called_once_with("s_test_session_token")
7272

7373
async def test_get_user_raises_401_on_exception(
7474
self, mock_request, mock_http_auth, mock_auth_service
7575
):
7676
"""Test that any exception raises 401 HTTPException."""
77-
mock_auth_service.check_session = AsyncMock(side_effect=SessionExpiredError())
77+
mock_auth_service.validate_session = AsyncMock(side_effect=SessionExpiredError())
7878

7979
with pytest.raises(HTTPException) as exc_info:
8080
await AuthenticatedUser.get_user(
@@ -93,7 +93,7 @@ async def test_get_user_returns_cached_user(
9393
mock_request, mock_http_auth, mock_auth_service
9494
)
9595

96-
mock_auth_service.check_session.assert_not_called()
96+
mock_auth_service.validate_session.assert_not_called()
9797
assert result is sample_user_response
9898

9999

@@ -104,7 +104,7 @@ async def test_returns_user_id(
104104
self, mock_request, mock_http_auth, mock_auth_service, sample_user_response
105105
):
106106
"""Test returns user UUID."""
107-
mock_auth_service.check_session = AsyncMock(return_value=sample_user_response)
107+
mock_auth_service.validate_session = AsyncMock(return_value=sample_user_response)
108108

109109
with patch("app.user.auth.permissions.get_request_context") as mock_ctx, \
110110
patch("app.user.auth.permissions.log_user"):
@@ -124,7 +124,7 @@ async def test_returns_user_email(
124124
self, mock_request, mock_http_auth, mock_auth_service, sample_user_response
125125
):
126126
"""Test returns user email."""
127-
mock_auth_service.check_session = AsyncMock(return_value=sample_user_response)
127+
mock_auth_service.validate_session = AsyncMock(return_value=sample_user_response)
128128

129129
with patch("app.user.auth.permissions.get_request_context") as mock_ctx, \
130130
patch("app.user.auth.permissions.log_user"):

0 commit comments

Comments
 (0)