2013-02-01 18:03:28 +04:00
|
|
|
from __future__ import unicode_literals
|
2012-09-20 16:06:27 +04:00
|
|
|
from django.contrib.auth.models import User
|
2012-12-20 03:12:27 +04:00
|
|
|
from django.http import HttpResponse
|
2012-09-20 16:06:27 +04:00
|
|
|
from django.test import Client, TestCase
|
2013-02-01 18:03:28 +04:00
|
|
|
from rest_framework import HTTP_HEADER_ENCODING
|
2013-02-28 21:58:58 +04:00
|
|
|
from rest_framework import exceptions
|
2012-09-20 16:06:27 +04:00
|
|
|
from rest_framework import permissions
|
2013-02-25 17:59:40 +04:00
|
|
|
from rest_framework import status
|
2012-09-20 16:06:27 +04:00
|
|
|
from rest_framework.authtoken.models import Token
|
2013-02-28 21:58:58 +04:00
|
|
|
from rest_framework.authentication import (
|
|
|
|
BaseAuthentication,
|
|
|
|
TokenAuthentication,
|
|
|
|
BasicAuthentication,
|
|
|
|
SessionAuthentication
|
|
|
|
)
|
2012-12-20 03:12:27 +04:00
|
|
|
from rest_framework.compat import patterns
|
2013-02-28 21:58:58 +04:00
|
|
|
from rest_framework.tests.utils import RequestFactory
|
2012-12-20 03:12:27 +04:00
|
|
|
from rest_framework.views import APIView
|
2013-01-05 16:40:02 +04:00
|
|
|
import json
|
2012-09-20 16:06:27 +04:00
|
|
|
import base64
|
|
|
|
|
|
|
|
|
2013-02-28 21:58:58 +04:00
|
|
|
factory = RequestFactory()
|
|
|
|
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
class MockView(APIView):
|
|
|
|
permission_classes = (permissions.IsAuthenticated,)
|
|
|
|
|
2013-02-28 21:58:58 +04:00
|
|
|
def get(self, request):
|
|
|
|
return HttpResponse({'a': 1, 'b': 2, 'c': 3})
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
def post(self, request):
|
|
|
|
return HttpResponse({'a': 1, 'b': 2, 'c': 3})
|
|
|
|
|
|
|
|
def put(self, request):
|
|
|
|
return HttpResponse({'a': 1, 'b': 2, 'c': 3})
|
|
|
|
|
2013-02-28 21:58:58 +04:00
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
urlpatterns = patterns('',
|
2013-01-22 01:29:49 +04:00
|
|
|
(r'^session/$', MockView.as_view(authentication_classes=[SessionAuthentication])),
|
|
|
|
(r'^basic/$', MockView.as_view(authentication_classes=[BasicAuthentication])),
|
|
|
|
(r'^token/$', MockView.as_view(authentication_classes=[TokenAuthentication])),
|
2012-12-08 02:25:16 +04:00
|
|
|
(r'^auth-token/$', 'rest_framework.authtoken.views.obtain_auth_token'),
|
2012-09-20 16:06:27 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class BasicAuthTests(TestCase):
|
|
|
|
"""Basic authentication"""
|
|
|
|
urls = 'rest_framework.tests.authentication'
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.csrf_client = Client(enforce_csrf_checks=True)
|
|
|
|
self.username = 'john'
|
|
|
|
self.email = 'lennon@thebeatles.com'
|
|
|
|
self.password = 'password'
|
|
|
|
self.user = User.objects.create_user(self.username, self.email, self.password)
|
|
|
|
|
|
|
|
def test_post_form_passing_basic_auth(self):
|
|
|
|
"""Ensure POSTing json over basic auth with correct credentials passes and does not require CSRF"""
|
2013-02-01 18:03:28 +04:00
|
|
|
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
|
2013-01-22 01:29:49 +04:00
|
|
|
response = self.csrf_client.post('/basic/', {'example': 'example'}, HTTP_AUTHORIZATION=auth)
|
2013-02-25 17:59:40 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_post_json_passing_basic_auth(self):
|
|
|
|
"""Ensure POSTing form over basic auth with correct credentials passes and does not require CSRF"""
|
2013-02-01 18:03:28 +04:00
|
|
|
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
|
2013-01-22 01:29:49 +04:00
|
|
|
response = self.csrf_client.post('/basic/', json.dumps({'example': 'example'}), 'application/json', HTTP_AUTHORIZATION=auth)
|
2013-02-25 17:59:40 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_post_form_failing_basic_auth(self):
|
|
|
|
"""Ensure POSTing form over basic auth without correct credentials fails"""
|
2013-01-22 01:29:49 +04:00
|
|
|
response = self.csrf_client.post('/basic/', {'example': 'example'})
|
2013-02-25 17:59:40 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_post_json_failing_basic_auth(self):
|
|
|
|
"""Ensure POSTing json over basic auth without correct credentials fails"""
|
2013-01-22 01:29:49 +04:00
|
|
|
response = self.csrf_client.post('/basic/', json.dumps({'example': 'example'}), 'application/json')
|
2013-02-25 17:59:40 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
2013-01-22 01:29:49 +04:00
|
|
|
self.assertEqual(response['WWW-Authenticate'], 'Basic realm="api"')
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
class SessionAuthTests(TestCase):
|
|
|
|
"""User session authentication"""
|
|
|
|
urls = 'rest_framework.tests.authentication'
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.csrf_client = Client(enforce_csrf_checks=True)
|
|
|
|
self.non_csrf_client = Client(enforce_csrf_checks=False)
|
|
|
|
self.username = 'john'
|
|
|
|
self.email = 'lennon@thebeatles.com'
|
|
|
|
self.password = 'password'
|
|
|
|
self.user = User.objects.create_user(self.username, self.email, self.password)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.csrf_client.logout()
|
|
|
|
|
|
|
|
def test_post_form_session_auth_failing_csrf(self):
|
|
|
|
"""
|
|
|
|
Ensure POSTing form over session authentication without CSRF token fails.
|
|
|
|
"""
|
|
|
|
self.csrf_client.login(username=self.username, password=self.password)
|
2013-01-22 01:29:49 +04:00
|
|
|
response = self.csrf_client.post('/session/', {'example': 'example'})
|
2013-02-25 17:59:40 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_post_form_session_auth_passing(self):
|
|
|
|
"""
|
|
|
|
Ensure POSTing form over session authentication with logged in user and CSRF token passes.
|
|
|
|
"""
|
|
|
|
self.non_csrf_client.login(username=self.username, password=self.password)
|
2013-01-22 01:29:49 +04:00
|
|
|
response = self.non_csrf_client.post('/session/', {'example': 'example'})
|
2013-02-25 17:59:40 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_put_form_session_auth_passing(self):
|
|
|
|
"""
|
|
|
|
Ensure PUTting form over session authentication with logged in user and CSRF token passes.
|
|
|
|
"""
|
|
|
|
self.non_csrf_client.login(username=self.username, password=self.password)
|
2013-01-22 01:29:49 +04:00
|
|
|
response = self.non_csrf_client.put('/session/', {'example': 'example'})
|
2013-02-25 17:59:40 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_post_form_session_auth_failing(self):
|
|
|
|
"""
|
|
|
|
Ensure POSTing form over session authentication without logged in user fails.
|
|
|
|
"""
|
2013-01-22 01:29:49 +04:00
|
|
|
response = self.csrf_client.post('/session/', {'example': 'example'})
|
2013-02-25 17:59:40 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TokenAuthTests(TestCase):
|
|
|
|
"""Token authentication"""
|
|
|
|
urls = 'rest_framework.tests.authentication'
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.csrf_client = Client(enforce_csrf_checks=True)
|
|
|
|
self.username = 'john'
|
|
|
|
self.email = 'lennon@thebeatles.com'
|
|
|
|
self.password = 'password'
|
|
|
|
self.user = User.objects.create_user(self.username, self.email, self.password)
|
|
|
|
|
|
|
|
self.key = 'abcd1234'
|
|
|
|
self.token = Token.objects.create(key=self.key, user=self.user)
|
|
|
|
|
|
|
|
def test_post_form_passing_token_auth(self):
|
|
|
|
"""Ensure POSTing json over token auth with correct credentials passes and does not require CSRF"""
|
|
|
|
auth = "Token " + self.key
|
2013-01-22 01:29:49 +04:00
|
|
|
response = self.csrf_client.post('/token/', {'example': 'example'}, HTTP_AUTHORIZATION=auth)
|
2013-02-25 17:59:40 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_post_json_passing_token_auth(self):
|
|
|
|
"""Ensure POSTing form over token auth with correct credentials passes and does not require CSRF"""
|
|
|
|
auth = "Token " + self.key
|
2013-01-22 01:29:49 +04:00
|
|
|
response = self.csrf_client.post('/token/', json.dumps({'example': 'example'}), 'application/json', HTTP_AUTHORIZATION=auth)
|
2013-02-25 17:59:40 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_post_form_failing_token_auth(self):
|
|
|
|
"""Ensure POSTing form over token auth without correct credentials fails"""
|
2013-01-22 01:29:49 +04:00
|
|
|
response = self.csrf_client.post('/token/', {'example': 'example'})
|
2013-02-25 17:59:40 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_post_json_failing_token_auth(self):
|
|
|
|
"""Ensure POSTing json over token auth without correct credentials fails"""
|
2013-01-22 01:29:49 +04:00
|
|
|
response = self.csrf_client.post('/token/', json.dumps({'example': 'example'}), 'application/json')
|
2013-02-25 17:59:40 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_token_has_auto_assigned_key_if_none_provided(self):
|
|
|
|
"""Ensure creating a token with no key will auto-assign a key"""
|
2012-10-09 12:57:31 +04:00
|
|
|
self.token.delete()
|
2012-09-20 16:06:27 +04:00
|
|
|
token = Token.objects.create(user=self.user)
|
|
|
|
self.assertTrue(bool(token.key))
|
2012-11-11 04:17:50 +04:00
|
|
|
|
|
|
|
def test_token_login_json(self):
|
|
|
|
"""Ensure token login view using JSON POST works."""
|
|
|
|
client = Client(enforce_csrf_checks=True)
|
2012-12-08 02:25:16 +04:00
|
|
|
response = client.post('/auth-token/',
|
2012-11-13 03:16:53 +04:00
|
|
|
json.dumps({'username': self.username, 'password': self.password}), 'application/json')
|
2013-02-25 17:59:40 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
2012-11-25 02:19:03 +04:00
|
|
|
self.assertEqual(json.loads(response.content.decode('ascii'))['token'], self.key)
|
2012-11-11 04:17:50 +04:00
|
|
|
|
|
|
|
def test_token_login_json_bad_creds(self):
|
|
|
|
"""Ensure token login view using JSON POST fails if bad credentials are used."""
|
|
|
|
client = Client(enforce_csrf_checks=True)
|
2012-12-08 02:25:16 +04:00
|
|
|
response = client.post('/auth-token/',
|
2012-11-13 03:16:53 +04:00
|
|
|
json.dumps({'username': self.username, 'password': "badpass"}), 'application/json')
|
2012-12-08 02:25:16 +04:00
|
|
|
self.assertEqual(response.status_code, 400)
|
2012-11-11 04:17:50 +04:00
|
|
|
|
|
|
|
def test_token_login_json_missing_fields(self):
|
|
|
|
"""Ensure token login view using JSON POST fails if missing fields."""
|
|
|
|
client = Client(enforce_csrf_checks=True)
|
2012-12-08 02:25:16 +04:00
|
|
|
response = client.post('/auth-token/',
|
2012-11-13 03:16:53 +04:00
|
|
|
json.dumps({'username': self.username}), 'application/json')
|
2012-12-08 02:25:16 +04:00
|
|
|
self.assertEqual(response.status_code, 400)
|
2012-11-11 04:17:50 +04:00
|
|
|
|
|
|
|
def test_token_login_form(self):
|
|
|
|
"""Ensure token login view using form POST works."""
|
|
|
|
client = Client(enforce_csrf_checks=True)
|
2012-12-08 02:25:16 +04:00
|
|
|
response = client.post('/auth-token/',
|
2012-11-13 03:16:53 +04:00
|
|
|
{'username': self.username, 'password': self.password})
|
2013-02-25 17:59:40 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
2012-11-25 02:19:03 +04:00
|
|
|
self.assertEqual(json.loads(response.content.decode('ascii'))['token'], self.key)
|
2013-02-28 21:58:58 +04:00
|
|
|
|
|
|
|
|
|
|
|
class IncorrectCredentialsTests(TestCase):
|
|
|
|
def test_incorrect_credentials(self):
|
|
|
|
"""
|
|
|
|
If a request contains bad authentication credentials, then
|
|
|
|
authentication should run and error, even if no permissions
|
|
|
|
are set on the view.
|
|
|
|
"""
|
|
|
|
class IncorrectCredentialsAuth(BaseAuthentication):
|
|
|
|
def authenticate(self, request):
|
|
|
|
raise exceptions.AuthenticationFailed('Bad credentials')
|
|
|
|
|
|
|
|
request = factory.get('/')
|
|
|
|
view = MockView.as_view(
|
|
|
|
authentication_classes=(IncorrectCredentialsAuth,),
|
|
|
|
permission_classes=()
|
|
|
|
)
|
|
|
|
response = view(request)
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
|
|
|
self.assertEqual(response.data, {'detail': 'Bad credentials'})
|