updated exceptions to raise error if not not handled

This commit is contained in:
Marc LaBelle 2020-04-01 21:28:02 -04:00
parent 241011a353
commit aaab91f82b
2 changed files with 22 additions and 3 deletions

View File

@ -555,3 +555,14 @@ class APIBasicTests(TestsMixin, TestCase):
self.assertEqual(['jwt-auth'], list(resp.cookies.keys())) self.assertEqual(['jwt-auth'], list(resp.cookies.keys()))
resp = self.get('/protected-view/') resp = self.get('/protected-view/')
self.assertEquals(resp.status_code, 200) self.assertEquals(resp.status_code, 200)
@override_settings(REST_USE_JWT=True)
def test_blacklisting(self):
payload = {
"username": self.USERNAME,
"password": self.PASS
}
get_user_model().objects.create_user(self.USERNAME, '', self.PASS)
self.post(self.login_url, data=payload, status_code=200)
resp = self.post(self.logout_url, status=200)
pass

View File

@ -147,18 +147,26 @@ class LogoutView(APIView):
try: try:
token = RefreshToken(request.data['refresh']) token = RefreshToken(request.data['refresh'])
token.blacklist() token.blacklist()
except KeyError: except KeyError:
response = Response({"detail": _("Refresh token was not included.")}, response = Response({"detail": _("Refresh token was not included in request data.")},
status=status.HTTP_401_UNAUTHORIZED) status=status.HTTP_401_UNAUTHORIZED)
except TokenError as e: except TokenError as e:
if e.args[0] == 'Token is blacklisted': if hasattr(e, 'args') and 'Token is blacklisted' in e.args:
response = Response({"detail": _("Token is already blacklisted.")}, response = Response({"detail": _("Token is already blacklisted.")},
status=status.HTTP_404_NOT_FOUND) status=status.HTTP_404_NOT_FOUND)
else:
raise
except AttributeError as e: except AttributeError as e:
# warn user blacklist is not enabled # warn user blacklist is not enabled
if e.args[0] == "'RefreshToken' object has no attribute 'blacklist'": if hasattr(e, 'args') and "'RefreshToken' object has no attribute 'blacklist'" in e.args:
response = Response({"detail": _("Blacklist is not enabled in INSTALLED_APPS.")}, response = Response({"detail": _("Blacklist is not enabled in INSTALLED_APPS.")},
status=status.HTTP_501_NOT_IMPLEMENTED) status=status.HTTP_501_NOT_IMPLEMENTED)
else:
raise
return response return response