mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-13 18:11:05 +03:00
update invalid token case
This commit is contained in:
parent
ff29fdd875
commit
1712c00001
|
@ -185,9 +185,10 @@ class TokenAuthentication(BaseAuthentication):
|
||||||
return self.authenticate_credentials(token)
|
return self.authenticate_credentials(token)
|
||||||
|
|
||||||
def authenticate_credentials(self, key):
|
def authenticate_credentials(self, key):
|
||||||
|
model = self.get_model()
|
||||||
try:
|
try:
|
||||||
token = self.get_model().objects.select_related('user').get(key=key)
|
token = model.objects.select_related('user').get(key=key)
|
||||||
except self.model.DoesNotExist:
|
except model.DoesNotExist:
|
||||||
raise exceptions.AuthenticationFailed(_('Invalid token.'))
|
raise exceptions.AuthenticationFailed(_('Invalid token.'))
|
||||||
|
|
||||||
if not token.user.is_active:
|
if not token.user.is_active:
|
||||||
|
|
|
@ -162,6 +162,12 @@ class TokenAuthTests(TestCase):
|
||||||
response = self.csrf_client.post('/token/', {'example': 'example'}, HTTP_AUTHORIZATION=auth)
|
response = self.csrf_client.post('/token/', {'example': 'example'}, HTTP_AUTHORIZATION=auth)
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
|
def test_fail_post_form_passing_nonexistent_token_auth(self):
|
||||||
|
# use a nonexistent token key
|
||||||
|
auth = 'Token wxyz6789'
|
||||||
|
response = self.csrf_client.post('/token/', {'example': 'example'}, HTTP_AUTHORIZATION=auth)
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||||
|
|
||||||
def test_fail_post_form_passing_invalid_token_auth(self):
|
def test_fail_post_form_passing_invalid_token_auth(self):
|
||||||
# add an 'invalid' unicode character
|
# add an 'invalid' unicode character
|
||||||
auth = 'Token ' + self.key + "¸"
|
auth = 'Token ' + self.key + "¸"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user