refactor: Address technical debt in auth refresh implementation

This commit is contained in:
Yunxiao Xu
2026-02-18 14:36:10 -08:00
parent 341bd08176
commit 6131f27142
4 changed files with 49 additions and 25 deletions

View File

@@ -18,11 +18,17 @@ def test_refresh_token_success(client):
# 2. Set the cookie manually in the client
client.cookies.set("refresh_token", refresh_token)
import time
time.sleep(1.1) # Wait to ensure iat is different
# 3. Call the refresh endpoint with mock
with patch("ea_chatbot.api.routers.auth.history_manager") as mock_hm:
with patch("ea_chatbot.api.routers.auth.history_manager") as mock_hm, \
patch("ea_chatbot.api.utils.datetime") as mock_datetime:
# Mock datetime to ensure the second token has a different timestamp
from datetime import datetime, timezone, timedelta
base_now = datetime.now(timezone.utc)
# First call (inside refresh) gets base_now + 1 second
mock_datetime.now.return_value = base_now + timedelta(seconds=1)
mock_datetime.side_effect = lambda *args, **kwargs: datetime(*args, **kwargs)
mock_hm.get_user_by_id.return_value = User(id=user_id, username="test@test.com")
response = client.post("/api/v1/auth/refresh")