diff options
| author | Tom Christie | 2013-06-29 21:02:58 +0100 | 
|---|---|---|
| committer | Tom Christie | 2013-06-29 21:02:58 +0100 | 
| commit | 664f8c63655770cd90bdbd510b315bcd045b380a (patch) | |
| tree | 2145a39de36701bc67cad67f2b303594a76d23e9 /rest_framework/tests | |
| parent | 35022ca9213939a2f40c82facffa908a818efe0b (diff) | |
| download | django-rest-framework-664f8c63655770cd90bdbd510b315bcd045b380a.tar.bz2 | |
Added APIClient.authenticate()
Diffstat (limited to 'rest_framework/tests')
| -rw-r--r-- | rest_framework/tests/test_testing.py | 42 | 
1 files changed, 39 insertions, 3 deletions
diff --git a/rest_framework/tests/test_testing.py b/rest_framework/tests/test_testing.py index 71dacd38..a8398b9a 100644 --- a/rest_framework/tests/test_testing.py +++ b/rest_framework/tests/test_testing.py @@ -1,6 +1,7 @@  # -- coding: utf-8 --  from __future__ import unicode_literals +from django.contrib.auth.models import User  from django.test import TestCase  from rest_framework.compat import patterns, url  from rest_framework.decorators import api_view @@ -8,10 +9,11 @@ from rest_framework.response import Response  from rest_framework.test import APIClient -@api_view(['GET']) +@api_view(['GET', 'POST'])  def mirror(request):      return Response({ -        'auth': request.META.get('HTTP_AUTHORIZATION', b'') +        'auth': request.META.get('HTTP_AUTHORIZATION', b''), +        'user': request.user.username      }) @@ -27,6 +29,40 @@ class CheckTestClient(TestCase):          self.client = APIClient()      def test_credentials(self): +        """ +        Setting `.credentials()` adds the required headers to each request. +        """          self.client.credentials(HTTP_AUTHORIZATION='example') +        for _ in range(0, 3): +            response = self.client.get('/view/') +            self.assertEqual(response.data['auth'], 'example') + +    def test_authenticate(self): +        """ +        Setting `.authenticate()` forcibly authenticates each request. +        """ +        user = User.objects.create_user('example', 'example@example.com') +        self.client.authenticate(user)          response = self.client.get('/view/') -        self.assertEqual(response.data['auth'], 'example') +        self.assertEqual(response.data['user'], 'example') + +    def test_csrf_exempt_by_default(self): +        """ +        By default, the test client is CSRF exempt. +        """ +        User.objects.create_user('example', 'example@example.com', 'password') +        self.client.login(username='example', password='password') +        response = self.client.post('/view/') +        self.assertEqual(response.status_code, 200) + +    def test_explicitly_enforce_csrf_checks(self): +        """ +        The test client can enforce CSRF checks. +        """ +        client = APIClient(enforce_csrf_checks=True) +        User.objects.create_user('example', 'example@example.com', 'password') +        client.login(username='example', password='password') +        response = client.post('/view/') +        expected = {'detail': 'CSRF Failed: CSRF cookie not set.'} +        self.assertEqual(response.status_code, 403) +        self.assertEqual(response.data, expected)  | 
