diff --git a/jwt_auth/compat.py b/jwt_auth/compat.py index d43e697..e3a279e 100644 --- a/jwt_auth/compat.py +++ b/jwt_auth/compat.py @@ -16,3 +16,10 @@ from django.utils.encoding import smart_text except ImportError: from django.utils.encoding import smart_unicode as smart_text + +try: + # In 1.5 the test client uses force_bytes + from django.utils.encoding import force_bytes as force_bytes_or_smart_bytes +except ImportError: + # In 1.4 the test client just uses smart_str + from django.utils.encoding import smart_str as force_bytes_or_smart_bytes diff --git a/jwt_auth/views.py b/jwt_auth/views.py index 04f8076..69e015d 100644 --- a/jwt_auth/views.py +++ b/jwt_auth/views.py @@ -2,7 +2,7 @@ from django.views.generic import View from django.core.serializers.json import DjangoJSONEncoder -from jwt_auth.compat import json +from jwt_auth.compat import json, smart_text from jwt_auth.forms import JSONWebTokenForm @@ -13,7 +13,7 @@ class ObtainJSONWebToken(View): def post(self, request, *args, **kwargs): try: - request_json = json.loads(request.body) + request_json = json.loads(smart_text(request.body)) except ValueError: return self.render_bad_request_response() diff --git a/tests/test_mixins.py b/tests/test_mixins.py index fa2b9d8..7a801d6 100644 --- a/tests/test_mixins.py +++ b/tests/test_mixins.py @@ -2,12 +2,10 @@ from django.test.client import Client from jwt_auth import utils -from jwt_auth.compat import User, json +from jwt_auth.compat import User, json, smart_text class JSONWebTokenAuthMixinTestCase(TestCase): - # urls = 'tests.test_mixins' - def setUp(self): self.email = 'jpueblo@example.com' self.username = 'jpueblo' @@ -37,7 +35,7 @@ def test_post_json_passing_jwt_auth(self): HTTP_AUTHORIZATION=auth ) - response_content = json.loads(response.content) + response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 200) self.assertEqual(response_content['username'], self.username) @@ -48,7 +46,7 @@ def test_post_json_failing_jwt_auth(self): """ response = self.client.post('/jwt/', content_type='application/json') - response_content = json.loads(response.content) + response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 401) self.assertEqual(response['WWW-Authenticate'], 'JWT realm="api"') @@ -67,7 +65,7 @@ def test_post_no_jwt_header_failing_jwt_auth(self): HTTP_AUTHORIZATION=auth, ) - response_content = json.loads(response.content) + response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 401) self.assertEqual(response['WWW-Authenticate'], 'JWT realm="api"') @@ -86,7 +84,7 @@ def test_post_invalid_jwt_header_failing_jwt_auth(self): HTTP_AUTHORIZATION=auth ) - response_content = json.loads(response.content) + response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 401) self.assertEqual(response['WWW-Authenticate'], 'JWT realm="api"') @@ -109,7 +107,7 @@ def test_post_expired_token_failing_jwt_auth(self): HTTP_AUTHORIZATION=auth ) - response_content = json.loads(response.content) + response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 401) self.assertEqual(response['WWW-Authenticate'], 'JWT realm="api"') @@ -128,7 +126,7 @@ def test_post_invalid_token_failing_jwt_auth(self): HTTP_AUTHORIZATION=auth ) - response_content = json.loads(response.content) + response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 401) self.assertEqual(response['WWW-Authenticate'], 'JWT realm="api"') diff --git a/tests/test_views.py b/tests/test_views.py index 292b06f..b35c749 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -2,12 +2,10 @@ from django.test.client import Client from jwt_auth import utils -from jwt_auth.compat import json, User +from jwt_auth.compat import json, User, smart_text class ObtainJSONWebTokenTestCase(TestCase): - # urls = 'tests.test_views' - def setUp(self): self.email = 'jpueblo@example.com' self.username = 'jpueblo' @@ -32,7 +30,7 @@ def test_jwt_login_json(self): content_type='application/json' ) - response_content = json.loads(response.content) + response_content = json.loads(smart_text(response.content)) decoded_payload = utils.jwt_decode_handler(response_content['token']) @@ -83,7 +81,7 @@ def test_jwt_login_with_expired_token(self): HTTP_AUTHORIZATION=auth ) - response_content = json.loads(response.content) + response_content = json.loads(smart_text(response.content)) decoded_payload = utils.jwt_decode_handler(response_content['token']) diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..6b928c3 --- /dev/null +++ b/tox.ini @@ -0,0 +1,86 @@ +[tox] +downloadcache = {toxworkdir}/cache/ +envlist = + py3.4-django1.6,py3.3-django1.6,py3.2-django1.6,py2.7-django1.6,py2.6-django1.6, + py3.4-django1.5,py3.3-django1.5,py3.2-django1.5,py2.7-django1.5,py2.6-django1.5, + py2.7-django1.4,py2.6-django1.4 + +[testenv] +commands = ./runtests.py --fast + +[testenv:flake8] +basepython = python2.7 +deps = Django + -rrequirements.txt + -rrequirements-test.txt +commands = ./runtests.py --lintonly + +[testenv:py3.4-django1.6] +basepython = python3.4 +deps = Django==1.6.3 + +[testenv:py3.3-django1.6] +basepython = python3.3 +deps = Django==1.6.3 + -rrequirements.txt + -rrequirements-test.txt + +[testenv:py3.2-django1.6] +basepython = python3.2 +deps = Django==1.6.3 + -rrequirements.txt + -rrequirements-test.txt + +[testenv:py2.7-django1.6] +basepython = python2.7 +deps = Django==1.6.3 + -rrequirements.txt + -rrequirements-test.txt + +[testenv:py2.6-django1.6] +basepython = python2.6 +deps = Django==1.6.3 + -rrequirements.txt + -rrequirements-test.txt + +[testenv:py3.4-django1.5] +basepython = python3.4 +deps = Django==1.5.6 + -rrequirements.txt + -rrequirements-test.txt + +[testenv:py3.3-django1.5] +basepython = python3.3 +deps = Django==1.5.6 + -rrequirements.txt + -rrequirements-test.txt + +[testenv:py3.2-django1.5] +basepython = python3.2 +deps = Django==1.5.6 + -rrequirements.txt + -rrequirements-test.txt + +[testenv:py2.7-django1.5] +basepython = python2.7 +deps = Django==1.5.6 + -rrequirements.txt + -rrequirements-test.txt + +[testenv:py2.6-django1.5] +basepython = python2.6 +deps = Django==1.5.6 + -rrequirements.txt + -rrequirements-test.txt + +[testenv:py2.7-django1.4] +basepython = python2.7 +deps = Django==1.4.11 + -rrequirements.txt + -rrequirements-test.txt + +[testenv:py2.6-django1.4] +basepython = python2.6 +deps = Django==1.4.11 + -rrequirements.txt + -rrequirements-test.txt