From aaab91f82bb81c74f8df99c274983f5be27cfefc Mon Sep 17 00:00:00 2001 From: Marc LaBelle Date: Wed, 1 Apr 2020 21:28:02 -0400 Subject: [PATCH] updated exceptions to raise error if not not handled --- dj_rest_auth/tests/test_api.py | 11 +++++++++++ dj_rest_auth/views.py | 14 +++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/dj_rest_auth/tests/test_api.py b/dj_rest_auth/tests/test_api.py index 0134560..f373dcc 100644 --- a/dj_rest_auth/tests/test_api.py +++ b/dj_rest_auth/tests/test_api.py @@ -555,3 +555,14 @@ class APIBasicTests(TestsMixin, TestCase): self.assertEqual(['jwt-auth'], list(resp.cookies.keys())) resp = self.get('/protected-view/') 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 diff --git a/dj_rest_auth/views.py b/dj_rest_auth/views.py index 365545e..c79423f 100644 --- a/dj_rest_auth/views.py +++ b/dj_rest_auth/views.py @@ -147,18 +147,26 @@ class LogoutView(APIView): try: token = RefreshToken(request.data['refresh']) token.blacklist() + 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) + 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.")}, status=status.HTTP_404_NOT_FOUND) + else: + raise + except AttributeError as e: # 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.")}, status=status.HTTP_501_NOT_IMPLEMENTED) + else: + raise + return response