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

Fix tests #769

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions rest_framework_simplejwt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from django.conf import settings
from django.utils.functional import lazy
from django.utils.timezone import is_naive, make_aware


def get_md5_hash_password(password: str) -> str:
Expand All @@ -16,8 +15,8 @@ def get_md5_hash_password(password: str) -> str:


def make_utc(dt: datetime) -> datetime:
if settings.USE_TZ and is_naive(dt):
return make_aware(dt, timezone=timezone.utc)
if settings.USE_TZ and dt.tzinfo is None:
return dt.replace(tzinfo=timezone.utc)

return dt

Expand Down
5 changes: 2 additions & 3 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
from importlib import reload
from importlib.metadata import PackageNotFoundError
from unittest.mock import Mock, patch

from django.test import SimpleTestCase
from pkg_resources import DistributionNotFound


class TestInit(SimpleTestCase):
def test_package_is_not_installed(self):
with patch(
"pkg_resources.get_distribution", Mock(side_effect=DistributionNotFound)
"importlib.metadata.version", Mock(side_effect=PackageNotFoundError)
):
# Import package mock package is not installed
import rest_framework_simplejwt.__init__

self.assertEqual(rest_framework_simplejwt.__init__.__version__, None)
Expand Down
11 changes: 4 additions & 7 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone

from django.test import TestCase
from django.utils import timezone
from freezegun import freeze_time

from rest_framework_simplejwt.utils import (
Expand All @@ -24,11 +23,11 @@ def test_it_should_return_the_correct_values(self):

with self.settings(USE_TZ=False):
dt = make_utc(dt)
self.assertTrue(timezone.is_naive(dt))
self.assertTrue(dt.tzinfo is None)

with self.settings(USE_TZ=True):
dt = make_utc(dt)
self.assertTrue(timezone.is_aware(dt))
self.assertTrue(dt.tzinfo is not None)
self.assertEqual(dt.utcoffset(), timedelta(seconds=0))


Expand All @@ -39,9 +38,7 @@ def test_it_should_return_the_correct_value(self):
with freeze_time(now):
# Should return aware utcnow if USE_TZ == True
with self.settings(USE_TZ=True):
self.assertEqual(
timezone.make_aware(now, timezone=timezone.utc), aware_utcnow()
)
self.assertEqual(now.replace(tzinfo=timezone.utc), aware_utcnow())

# Should return naive utcnow if USE_TZ == False
with self.settings(USE_TZ=False):
Expand Down
Loading