Skip to content

Commit

Permalink
Fix tests (#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dresdn authored Dec 5, 2023
1 parent 53ac872 commit 0b50ee6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
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

0 comments on commit 0b50ee6

Please sign in to comment.