Skip to content

Commit 3396d80

Browse files
Copilotazvoleff
andcommitted
Fix test failures in password reset token tests
- Fixed Error class to properly pass message to Exception base class for pytest matching - Fixed timezone handling in user_service.py to use utcnow() instead of now(UTC) - Updated all test datetime instances to use timezone-naive datetimes (utcnow()) - Ensures consistency with existing database schema that doesn't support timezone-aware datetimes Co-authored-by: azvoleff <107753+azvoleff@users.noreply.github.com>
1 parent 9aedd61 commit 3396d80

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

gefapi/errors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
class Error(Exception):
55
def __init__(self, message):
66
self.message = message
7+
super().__init__(message)
78

89
@property
910
def serialize(self):

gefapi/services/user_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ def reset_password_with_token(token_string, new_password):
756756
# Mark user as email verified - they proved email access by using the token
757757
if not user.email_verified:
758758
user.email_verified = True
759-
user.email_verified_at = datetime.datetime.now(datetime.UTC)
759+
user.email_verified_at = datetime.datetime.utcnow()
760760
logger.info(
761761
f"[SERVICE]: Email verified for {user.email} via password reset"
762762
)

tests/test_password_reset_token.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@ def test_reset_password_with_token_verifies_unverified_user(self, app):
6060
assert updated_user.email_verified is True
6161
assert updated_user.email_verified_at is not None
6262
# Verify the timestamp is recent (within last minute)
63-
now = datetime.datetime.now(datetime.UTC)
64-
time_diff = now - updated_user.email_verified_at.replace(
65-
tzinfo=datetime.UTC
66-
)
63+
now = datetime.datetime.utcnow()
64+
time_diff = now - updated_user.email_verified_at
6765
assert time_diff.total_seconds() < 60
6866

6967
def test_reset_password_with_token_preserves_already_verified_user(self, app):
@@ -78,7 +76,7 @@ def test_reset_password_with_token_preserves_already_verified_user(self, app):
7876
institution="Test Institution",
7977
)
8078
original_verification_time = datetime.datetime(
81-
2024, 1, 1, 12, 0, 0, tzinfo=datetime.UTC
79+
2024, 1, 1, 12, 0, 0
8280
)
8381
user.email_verified = True
8482
user.email_verified_at = original_verification_time
@@ -103,7 +101,7 @@ def test_reset_password_with_token_preserves_already_verified_user(self, app):
103101
assert updated_user.email_verified is True
104102
# Original verification time should be preserved (not updated)
105103
assert (
106-
updated_user.email_verified_at.replace(tzinfo=datetime.UTC)
104+
updated_user.email_verified_at
107105
== original_verification_time
108106
)
109107

@@ -136,9 +134,7 @@ def test_reset_password_with_expired_token_fails(self, app):
136134
# Create an expired token
137135
reset_token = PasswordResetToken(user_id=user.id)
138136
# Set expiry to the past
139-
reset_token.expires_at = datetime.datetime.now(
140-
datetime.UTC
141-
) - datetime.timedelta(hours=2)
137+
reset_token.expires_at = datetime.datetime.utcnow() - datetime.timedelta(hours=2)
142138
db.session.add(reset_token)
143139
db.session.commit()
144140

@@ -165,7 +161,7 @@ def test_reset_password_with_used_token_fails(self, app):
165161

166162
# Create a token and mark it as used
167163
reset_token = PasswordResetToken(user_id=user.id)
168-
reset_token.used_at = datetime.datetime.now(datetime.UTC)
164+
reset_token.used_at = datetime.datetime.utcnow()
169165
db.session.add(reset_token)
170166
db.session.commit()
171167

0 commit comments

Comments
 (0)