New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace deprecated utcnow() by now() #1711
base: main
Are you sure you want to change the base?
Conversation
Help is welcome to fix the broken tests. It's not directly clear to me how to resolve this. |
I think def token_valid(self, provided_token, max_hours=24):
"""Checks whether provided token is correct and still valid"""
# there must be a token and a timestamp
if bool(self.token) and bool(self.token_created_at):
now = dt.datetime.now(dt.timezone.utc)
diff = (now - self.token_created_at).total_seconds()
# return if token is correct and we are still before deadline
return self.token == provided_token and diff <= max_hours * 3600
else:
return False Because usually this will work fine, but for some reason |
def test_token_validity(setup_teardown, subtract_time):
subtract_hours, subtract_mins, validity = subtract_time
user = crud.create_user(DB)
user.set_token_data("secret", "salt")
assert user.token_created_at.tzinfo is not None and user.token_created_at.tzinfo.utcoffset(user.token_created_at) is not None, "token_created_at must be offset-aware. Location 1"
DB.session.commit()
assert user.token_created_at.tzinfo is not None and user.token_created_at.tzinfo.utcoffset(user.token_created_at) is not None, "token_created_at must be offset-aware. Location 2" FAILED asreview/webapp/tests/test_database_and_models/test_user_model.py::test_token_validity[subtract_time0] - AssertionError: token_created_at must be offset-aware. Location 2 So I think the database is stripping the timezone information. |
def token_valid(self, provided_token, max_hours=24):
"""Checks whether provided token is correct and still valid"""
# there must be a token and a timestamp
if bool(self.token) and bool(self.token_created_at):
now = dt.datetime.now(dt.timezone.utc)
if self.token_created_at.tzinfo is None:
self.token_created_at = self.token_created_at.replace(tzinfo=dt.timezone.utc)
diff = (now - self.token_created_at).total_seconds()
# return if token is correct and we are still before deadline
return self.token == provided_token and diff <= max_hours * 3600
else:
return False Adding |
|
An alternative could be to keep using offset native datetimes, by using >>> datetime.datetime.utcnow() - datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
datetime.timedelta(0) |
Note that if you are using Python 3.11 or newer, you can replace |
No description provided.