Skip to content

Commit

Permalink
Update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajay09 committed Jun 12, 2024
1 parent d698cd0 commit d9818d6
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,43 @@ def test_it_should_update_token_exp_claim_if_everything_ok(self):


class TestTokenRefreshSerializer(TestCase):
def setUp(self):
self.username = "test_user"
self.password = "test_password"

self.user = User.objects.create_user(
username=self.username,
password=self.password,
)

def test_it_should_raise_error_for_inactive_users(self):
refresh = RefreshToken.for_user(self.user)
self.user.is_active = False
self.user.save()

s = TokenRefreshSerializer(data={"refresh": str(refresh)})

with self.assertRaises(drf_exceptions.AuthenticationFailed) as e:
s.is_valid()

self.assertIn("No active account", e.exception.args[0])

def test_it_should_return_access_token_for_active_users(self):
refresh = RefreshToken.for_user(self.user)

s = TokenRefreshSerializer(data={"refresh": str(refresh)})

now = aware_utcnow() - api_settings.ACCESS_TOKEN_LIFETIME / 2
with patch("rest_framework_simplejwt.tokens.aware_utcnow") as fake_aware_utcnow:
fake_aware_utcnow.return_value = now
s.is_valid()

access = AccessToken(s.validated_data["access"])

self.assertEqual(
access["exp"], datetime_to_epoch(now + api_settings.ACCESS_TOKEN_LIFETIME)
)

def test_it_should_raise_token_error_if_token_invalid(self):
token = RefreshToken()
del token["exp"]
Expand Down

0 comments on commit d9818d6

Please sign in to comment.