Skip to content

Commit 6cf0635

Browse files
committed
Avoid DoesNotExist exception in TokenRefreshSerializer
1 parent a9ee40d commit 6cf0635

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

rest_framework_simplejwt/serializers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,12 @@ def validate(self, attrs: dict[str, Any]) -> dict[str, str]:
112112
refresh = self.token_class(attrs["refresh"])
113113

114114
user_id = refresh.payload.get(api_settings.USER_ID_CLAIM, None)
115-
if user_id and (
116-
user := get_user_model().objects.get(
117-
**{api_settings.USER_ID_FIELD: user_id}
115+
if user_id:
116+
user = (
117+
get_user_model()
118+
.objects.filter(**{api_settings.USER_ID_FIELD: user_id})
119+
.first()
118120
)
119-
):
120121
if not api_settings.USER_AUTHENTICATION_RULE(user):
121122
raise AuthenticationFailed(
122123
self.error_messages["no_active_account"],

tests/test_serializers.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from django.conf import settings
66
from django.contrib.auth import get_user_model
7-
from django.core import exceptions as django_exceptions
87
from django.test import TestCase, override_settings
98
from rest_framework import exceptions as drf_exceptions
109

@@ -286,10 +285,10 @@ def test_it_should_raise_error_for_deleted_users(self):
286285

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

289-
with self.assertRaises(django_exceptions.ObjectDoesNotExist) as e:
288+
with self.assertRaises(drf_exceptions.AuthenticationFailed) as e:
290289
s.is_valid()
291290

292-
self.assertIn("does not exist", str(e.exception))
291+
self.assertIn("No active account", str(e.exception))
293292

294293
def test_it_should_raise_error_for_inactive_users(self):
295294
refresh = RefreshToken.for_user(self.user)

0 commit comments

Comments
 (0)