Fix timezone-aware datetime comparisons in PasswordResetToken#88
Fix timezone-aware datetime comparisons in PasswordResetToken#88
Conversation
Co-authored-by: azvoleff <107753+azvoleff@users.noreply.github.com>
|
@copilot commit 5b26969 made the changes that led to these test failures. Those changes were to address this error: trends-earth-prod_api.2.g31s371ucn4n@ip-172-40-1-210 | 2026-01-26 21:58:49,176 - root - INFO - [ROUTER]: Reset password with token Will these changes lead to that error recurring? If so make a proper fix. |
No, these changes will not cause that error to recur. The production error occurred because # Lines 70-74: Convert naive to aware if needed
expires_at = (
self.expires_at.replace(tzinfo=datetime.UTC)
if self.expires_at.tzinfo is None
else self.expires_at
)This matches the pattern in |
Test
test_expired_token_is_invalidfails withTypeError: can't compare offset-naive and offset-aware datetimes. The test usesdatetime.datetime.now(datetime.UTC)but the model usesdatetime.datetime.utcnow().Changes
Updated
gefapi/models/password_reset_token.pyto use timezone-aware datetimes:created_atcolumn default:datetime.datetime.now(datetime.UTC)__init__(): Initialize with timezone-aware datetimeis_valid(): Handle both timezone-aware and naive datetimes for backward compatibility with existing database recordsmark_used(): Record usage with timezone-aware datetimeinvalidate_user_tokens(): Use timezone-aware datetime for queriescleanup_expired_tokens(): Use timezone-aware datetime for cutoff calculationFollows the same pattern as
RefreshTokenmodel which already handles timezone-aware datetimes correctly.Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.