aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/tests
diff options
context:
space:
mode:
authorTom Drummond2011-07-15 16:14:35 +0100
committerTom Drummond2011-07-15 16:14:35 +0100
commit6bbfbf77e29c05c3aa382fe9100ef6dc755d43d6 (patch)
treeda1de0c21c19bbfab4dde08282dbbff60f78742d /djangorestframework/tests
parent73ad2e5054c85df30e5281ecf241e5aee4b18302 (diff)
downloaddjango-rest-framework-6bbfbf77e29c05c3aa382fe9100ef6dc755d43d6.tar.bz2
Added tests to ensure you can access request.POST with UserLoggedInAuthentication
Diffstat (limited to 'djangorestframework/tests')
-rw-r--r--djangorestframework/tests/content.py53
1 files changed, 52 insertions, 1 deletions
diff --git a/djangorestframework/tests/content.py b/djangorestframework/tests/content.py
index 83ad72d0..8f1de3fd 100644
--- a/djangorestframework/tests/content.py
+++ b/djangorestframework/tests/content.py
@@ -1,10 +1,28 @@
"""
Tests for content parsing, and form-overloaded content parsing.
"""
-from django.test import TestCase
+from django.conf.urls.defaults import patterns
+from django.contrib.auth.models import User
+from django.test import TestCase, Client
+from djangorestframework import status
+from djangorestframework.authentication import UserLoggedInAuthentication
from djangorestframework.compat import RequestFactory
from djangorestframework.mixins import RequestMixin
from djangorestframework.parsers import FormParser, MultiPartParser, PlainTextParser
+from djangorestframework.response import Response
+from djangorestframework.views import View
+
+class MockView(View):
+ authentication = (UserLoggedInAuthentication,)
+ def post(self, request):
+ if request.POST.get('example') is not None:
+ return Response(status.OK)
+
+ return Response(status.INTERNAL_SERVER_ERROR)
+
+urlpatterns = patterns('',
+ (r'^$', MockView.as_view()),
+)
class TestContentParsing(TestCase):
def setUp(self):
@@ -84,3 +102,36 @@ class TestContentParsing(TestCase):
view.request = self.req.post('/', form_data)
view.parsers = (PlainTextParser,)
self.assertEqual(view.DATA, content)
+
+class TestContentParsingWithAuthentication(TestCase):
+ urls = 'djangorestframework.tests.content'
+
+ 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.req = RequestFactory()
+
+ def test_user_logged_in_authentication_has_post_when_not_logged_in(self):
+ """Ensures request.POST exists after UserLoggedInAuthentication when user doesn't log in"""
+ content = {'example': 'example'}
+
+ response = self.client.post('/', content)
+ self.assertEqual(status.OK, response.status_code)
+
+ response = self.csrf_client.post('/', content)
+ self.assertEqual(status.OK, response.status_code)
+
+ def test_user_logged_in_authentication_has_post_when_logged_in(self):
+ """Ensures request.POST exists after UserLoggedInAuthentication when user does log in"""
+ self.client.login(username='john', password='password')
+ self.csrf_client.login(username='john', password='password')
+ content = {'example': 'example'}
+
+ response = self.client.post('/', content)
+ self.assertEqual(status.OK, response.status_code, "POST data")
+
+ response = self.csrf_client.post('/', content)
+ self.assertEqual(status.OK, response.status_code)