Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

J535D165
Copy link
Member

No description provided.

@J535D165
Copy link
Member Author

Help is welcome to fix the broken tests. It's not directly clear to me how to resolve this.

@jteijema
Copy link
Member

jteijema commented Apr 17, 2024

I think token_created_at changes from offset-aware to offset-naive (somewhere?) and fails. Maybe it's converted somewhere in the database.

    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 self.token_created_at is converted from offset-aware to offset-native.

@jteijema
Copy link
Member

jteijema commented Apr 17, 2024

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
FAILED asreview/webapp/tests/test_database_and_models/test_user_model.py::test_token_validity[subtract_time1] - AssertionError: 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_time2] - AssertionError: 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_time3] - AssertionError: token_created_at must be offset-aware. Location 2


So I think the database is stripping the timezone information.

@jteijema
Copy link
Member

jteijema commented Apr 17, 2024

    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 self.token_created_at = self.token_created_at.replace(tzinfo=dt.timezone.utc) in models.py will fix all 8 failing test, but that doesn't solve the issue. Can't guarantee there aren't timezone issues now.

@jteijema
Copy link
Member

token_created_at = Column(DateTime(timezone=True)) sadly doesn't do the trick.

@jteijema
Copy link
Member

jteijema commented Apr 17, 2024

An alternative could be to keep using offset native datetimes, by using datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None), however, this is discouraged.

>>> datetime.datetime.utcnow() - datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
datetime.timedelta(0)

@jteijema
Copy link
Member

Note that if you are using Python 3.11 or newer, you can replace datetime.timezone.utc with a shorter datetime.UTC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants