diff options
| author | Tom Christie | 2013-02-06 00:53:41 -0800 |
|---|---|---|
| committer | Tom Christie | 2013-02-06 00:53:41 -0800 |
| commit | 72fe686623f63b79fa2b7f7de177c6d98bc1da3f (patch) | |
| tree | 5d0834d4eb2caf21ed54a4dcba9a4a3a40e6fa99 /rest_framework/tests/authentication.py | |
| parent | 221e77d3575c182eb49d50546f844f392a5f7ba6 (diff) | |
| parent | bdc97c561147130b59c8cd8cc6bc735eab8b223d (diff) | |
| download | django-rest-framework-72fe686623f63b79fa2b7f7de177c6d98bc1da3f.tar.bz2 | |
Merge pull request #636 from tomchristie/2.2
2.2 Release
Diffstat (limited to 'rest_framework/tests/authentication.py')
| -rw-r--r-- | rest_framework/tests/authentication.py | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/rest_framework/tests/authentication.py b/rest_framework/tests/authentication.py index 1f17e8d2..c9df1733 100644 --- a/rest_framework/tests/authentication.py +++ b/rest_framework/tests/authentication.py @@ -1,13 +1,13 @@ +from __future__ import unicode_literals from django.contrib.auth.models import User from django.http import HttpResponse from django.test import Client, TestCase - +from rest_framework import HTTP_HEADER_ENCODING from rest_framework import permissions from rest_framework.authtoken.models import Token from rest_framework.authentication import TokenAuthentication, BasicAuthentication, SessionAuthentication from rest_framework.compat import patterns from rest_framework.views import APIView - import json import base64 @@ -42,13 +42,17 @@ class BasicAuthTests(TestCase): def test_post_form_passing_basic_auth(self): """Ensure POSTing json over basic auth with correct credentials passes and does not require CSRF""" - auth = 'Basic %s' % base64.encodestring('%s:%s' % (self.username, self.password)).strip() + credentials = ('%s:%s' % (self.username, self.password)) + base64_credentials = base64.b64encode(credentials.encode(HTTP_HEADER_ENCODING)).decode(HTTP_HEADER_ENCODING) + auth = 'Basic %s' % base64_credentials response = self.csrf_client.post('/basic/', {'example': 'example'}, HTTP_AUTHORIZATION=auth) self.assertEqual(response.status_code, 200) def test_post_json_passing_basic_auth(self): """Ensure POSTing form over basic auth with correct credentials passes and does not require CSRF""" - auth = 'Basic %s' % base64.encodestring('%s:%s' % (self.username, self.password)).strip() + credentials = ('%s:%s' % (self.username, self.password)) + base64_credentials = base64.b64encode(credentials.encode(HTTP_HEADER_ENCODING)).decode(HTTP_HEADER_ENCODING) + auth = 'Basic %s' % base64_credentials response = self.csrf_client.post('/basic/', json.dumps({'example': 'example'}), 'application/json', HTTP_AUTHORIZATION=auth) self.assertEqual(response.status_code, 200) @@ -159,7 +163,7 @@ class TokenAuthTests(TestCase): response = client.post('/auth-token/', json.dumps({'username': self.username, 'password': self.password}), 'application/json') self.assertEqual(response.status_code, 200) - self.assertEqual(json.loads(response.content)['token'], self.key) + self.assertEqual(json.loads(response.content.decode('ascii'))['token'], self.key) def test_token_login_json_bad_creds(self): """Ensure token login view using JSON POST fails if bad credentials are used.""" @@ -181,4 +185,4 @@ class TokenAuthTests(TestCase): response = client.post('/auth-token/', {'username': self.username, 'password': self.password}) self.assertEqual(response.status_code, 200) - self.assertEqual(json.loads(response.content)['token'], self.key) + self.assertEqual(json.loads(response.content.decode('ascii'))['token'], self.key) |
