Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1712c00

Browse files
committed
update invalid token case
1 parent ff29fdd commit 1712c00

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

rest_framework/authentication.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,10 @@ def authenticate(self, request):
185185
return self.authenticate_credentials(token)
186186

187187
def authenticate_credentials(self, key):
188+
model = self.get_model()
188189
try:
189-
token = self.get_model().objects.select_related('user').get(key=key)
190-
except self.model.DoesNotExist:
190+
token = model.objects.select_related('user').get(key=key)
191+
except model.DoesNotExist:
191192
raise exceptions.AuthenticationFailed(_('Invalid token.'))
192193

193194
if not token.user.is_active:

tests/test_authentication.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ def test_post_form_passing_token_auth(self):
162162
response = self.csrf_client.post('/token/', {'example': 'example'}, HTTP_AUTHORIZATION=auth)
163163
self.assertEqual(response.status_code, status.HTTP_200_OK)
164164

165+
def test_fail_post_form_passing_nonexistent_token_auth(self):
166+
# use a nonexistent token key
167+
auth = 'Token wxyz6789'
168+
response = self.csrf_client.post('/token/', {'example': 'example'}, HTTP_AUTHORIZATION=auth)
169+
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
170+
165171
def test_fail_post_form_passing_invalid_token_auth(self):
166172
# add an 'invalid' unicode character
167173
auth = 'Token ' + self.key + "¸"

0 commit comments

Comments
 (0)