mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-27 00:19:53 +03:00
Added tests for PATCH form in the Browsable API
* Added the `tests.utils.Client` (`django.test.client.Client` subclass with patch method support) * Added tests for PATCH form in the Browsable API.
This commit is contained in:
parent
c28a5d9a00
commit
a1c322c346
|
@ -1,12 +1,13 @@
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.test import Client, TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from rest_framework import permissions
|
from rest_framework import permissions
|
||||||
from rest_framework.authtoken.models import Token
|
from rest_framework.authtoken.models import Token
|
||||||
from rest_framework.authentication import TokenAuthentication
|
from rest_framework.authentication import TokenAuthentication
|
||||||
from rest_framework.compat import patterns
|
from rest_framework.compat import patterns
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
from rest_framework.tests.utils import Client
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import base64
|
import base64
|
||||||
|
@ -18,6 +19,9 @@ class MockView(APIView):
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
return HttpResponse({'a': 1, 'b': 2, 'c': 3})
|
return HttpResponse({'a': 1, 'b': 2, 'c': 3})
|
||||||
|
|
||||||
|
def patch(self, request):
|
||||||
|
return HttpResponse({'a': 1, 'b': 2, 'c': 3})
|
||||||
|
|
||||||
def put(self, request):
|
def put(self, request):
|
||||||
return HttpResponse({'a': 1, 'b': 2, 'c': 3})
|
return HttpResponse({'a': 1, 'b': 2, 'c': 3})
|
||||||
|
|
||||||
|
@ -102,6 +106,14 @@ class SessionAuthTests(TestCase):
|
||||||
response = self.non_csrf_client.put('/', {'example': 'example'})
|
response = self.non_csrf_client.put('/', {'example': 'example'})
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
def test_patch_form_session_auth_passing(self):
|
||||||
|
"""
|
||||||
|
Ensure PATCHting form over session authentication with logged in user and CSRF token passes.
|
||||||
|
"""
|
||||||
|
self.non_csrf_client.login(username=self.username, password=self.password)
|
||||||
|
response = self.non_csrf_client.patch('/', {'example': 'example'})
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
def test_post_form_session_auth_failing(self):
|
def test_post_form_session_auth_failing(self):
|
||||||
"""
|
"""
|
||||||
Ensure POSTing form over session authentication without logged in user fails.
|
Ensure POSTing form over session authentication without logged in user fails.
|
||||||
|
|
|
@ -111,6 +111,9 @@ class POSTDeniedView(APIView):
|
||||||
def put(self, request):
|
def put(self, request):
|
||||||
return Response()
|
return Response()
|
||||||
|
|
||||||
|
def patch(self, request):
|
||||||
|
return Response()
|
||||||
|
|
||||||
|
|
||||||
class DocumentingRendererTests(TestCase):
|
class DocumentingRendererTests(TestCase):
|
||||||
def test_only_permitted_forms_are_displayed(self):
|
def test_only_permitted_forms_are_displayed(self):
|
||||||
|
@ -119,6 +122,7 @@ class DocumentingRendererTests(TestCase):
|
||||||
response = view(request).render()
|
response = view(request).render()
|
||||||
self.assertNotContains(response, '>POST<')
|
self.assertNotContains(response, '>POST<')
|
||||||
self.assertContains(response, '>PUT<')
|
self.assertContains(response, '>PUT<')
|
||||||
|
self.assertContains(response, '>PATCH<')
|
||||||
|
|
||||||
|
|
||||||
class RendererEndToEndTests(TestCase):
|
class RendererEndToEndTests(TestCase):
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
from django.test.client import RequestFactory, FakePayload
|
from django.test.client import Client as _Client, RequestFactory as _RequestFactory, FakePayload
|
||||||
from django.test.client import MULTIPART_CONTENT
|
from django.test.client import MULTIPART_CONTENT
|
||||||
from urlparse import urlparse
|
from urlparse import urlparse
|
||||||
|
|
||||||
|
|
||||||
class RequestFactory(RequestFactory):
|
class RequestFactory(_RequestFactory):
|
||||||
|
|
||||||
def __init__(self, **defaults):
|
def __init__(self, **defaults):
|
||||||
super(RequestFactory, self).__init__(**defaults)
|
super(RequestFactory, self).__init__(**defaults)
|
||||||
|
@ -25,3 +25,15 @@ class RequestFactory(RequestFactory):
|
||||||
}
|
}
|
||||||
r.update(extra)
|
r.update(extra)
|
||||||
return self.request(**r)
|
return self.request(**r)
|
||||||
|
|
||||||
|
|
||||||
|
class Client(_Client, RequestFactory):
|
||||||
|
def patch(self, path, data={}, content_type=MULTIPART_CONTENT,
|
||||||
|
follow=False, **extra):
|
||||||
|
"""
|
||||||
|
Send a resource to the server using PATCH.
|
||||||
|
"""
|
||||||
|
response = super(Client, self).patch(path, data=data, content_type=content_type, **extra)
|
||||||
|
if follow:
|
||||||
|
response = self._handle_redirects(response, **extra)
|
||||||
|
return response
|
||||||
|
|
Loading…
Reference in New Issue
Block a user