[snowy: 1/8] Partially fix PUT and POST requests in BadMethods unit tests



commit 93b27ce1308822ad8e66e94d21d485787241dd16
Author: Leon Handreke <leon handreke gmail com>
Date:   Tue Feb 9 22:37:32 2010 +0100

    Partially fix PUT and POST requests in BadMethods unit tests
    
    Dummy JSON data added to PUT and POST requests in the unit tests to work
    around a misdetection of the MIME type by piston.
    
    All piston resources marked csrf_exempt to prevent django's CsrfMiddleware from
    interfering with them.
    
    For 3 of the 4 BadMethods tests still fail because with an OAuth error when
    using POST.

 api/tests.py |   22 +++++++++++++---------
 api/urls.py  |    8 ++++++++
 2 files changed, 21 insertions(+), 9 deletions(-)
---
diff --git a/api/tests.py b/api/tests.py
index 6f9b15c..7116a4f 100644
--- a/api/tests.py
+++ b/api/tests.py
@@ -179,10 +179,11 @@ class ApiTestCase(TestCase):
 
     def testUserBadMethods(self):
         # PUT/POST/DELETE are not allowed
-        response = self.admin_requester.put('/api/1.0/admin/', '')
+        # POST and PUT need some dummy data, otherwise piston replies "Bad Request"
+        response = self.admin_requester.put('/api/1.0/admin/', '{ "test" : "test" }')
         self.assertEqual(response.status_code, 405)
         self.assertEqual(response['Allow'], 'GET')
-        response = self.admin_requester.post('/api/1.0/admin/', '')
+        response = self.admin_requester.post('/api/1.0/admin/', '{ "test" : "test" }')
         self.assertEqual(response.status_code, 405)
         self.assertEqual(response['Allow'], 'GET')
         response = self.admin_requester.delete('/api/1.0/admin/')
@@ -236,16 +237,17 @@ class ApiTestCase(TestCase):
 
     def testRootBadMethods(self):
         # PUT/POST/DELETE are not allowed
-        response = self.admin_requester.put('/api/1.0/', '')
+        # POST and PUT need some dummy data, otherwise piston replies "Bad Request"
+        response = self.admin_requester.put('/api/1.0/', '{ "test" : "test" }')
         self.assertEqual(response.status_code, 405)
         self.assertEqual(response['Allow'], 'GET')
-        response = self.admin_requester.post('/api/1.0/', '')
+        response = self.admin_requester.post('/api/1.0/', '{ "test" : "test" }')
         self.assertEqual(response.status_code, 405)
         self.assertEqual(response['Allow'], 'GET')
         response = self.admin_requester.delete('/api/1.0/')
         self.assertEqual(response.status_code, 405)
         self.assertEqual(response['Allow'], 'GET')
-
+        
     def testNotes(self):
         noteJson = '{"guid": "002e91a2-2e34-4e2d-bf88-21def49a7705",' + \
                    '"title" :"New Note 6",' + \
@@ -305,7 +307,8 @@ class ApiTestCase(TestCase):
 
     def testNotesBadMethods(self):
         # POST/DELETE are not allowed
-        response = self.admin_requester.post('/api/1.0/admin/notes/', '')
+        # POST and PUT need some dummy data, otherwise piston replies "Bad Request"
+        response = self.admin_requester.post('/api/1.0/admin/notes/',  '{ "test" : "test" }')
         self.assertEqual(response.status_code, 405)
         self.assertEqual(response['Allow'], 'GET, PUT')
         response = self.admin_requester.delete('/api/1.0/admin/notes/')
@@ -318,12 +321,13 @@ class ApiTestCase(TestCase):
 
     def testNoteBadMethods(self):
         # PUT/POST/DELETE are not allowed
-        response = self.admin_requester.put('/api/1.0/admin/notes/1/', '')
+        # POST and PUT need some dummy data, otherwise piston replies "Bad Request"
+        response = self.admin_requester.put('/api/1.0/admin/notes/1/',  '{ "test" : "test" }')
         self.assertEqual(response.status_code, 405)
         self.assertEqual(response['Allow'], 'GET')
-        response = self.admin_requester.post('/api/1.0/admin/notes/1/', '')
+        response = self.admin_requester.post('/api/1.0/admin/notes/1/',  '{ "test" : "test" }')
         self.assertEqual(response.status_code, 405)
         self.assertEqual(response['Allow'], 'GET')
         response = self.admin_requester.delete('/api/1.0/admin/notes/1/')
         self.assertEqual(response.status_code, 405)
-        self.assertEqual(response['Allow'], 'GET')
\ No newline at end of file
+        self.assertEqual(response['Allow'], 'GET')
diff --git a/api/urls.py b/api/urls.py
index 524beb8..5bd2377 100644
--- a/api/urls.py
+++ b/api/urls.py
@@ -26,10 +26,18 @@ auth = HttpBasicAuthentication(realm='Snowy')
 authoauth = OAuthAuthentication(realm='Snowy')
 ad = {'authentication': authoauth}
 
+""" piston resources are marked csrf_exempt to ensure the the django
+CsrfMiddleware doesn't interfere with POST requests
+http://bitbucket.org/jespern/django-piston/issue/82/post-requests-fail-when-using-django-trunk """
+
 root_handler = Resource(handler=RootHandler, **ad)
+root_handler.csrf_exempt = getattr(root_handler.handler, 'csrf_exempt', True)
 user_handler = Resource(UserHandler)
+user_handler.csrf_exempt = getattr(user_handler.handler, 'csrf_exempt', True)
 notes_handler = Resource(handler=NotesHandler, **ad)
+notes_handler.csrf_exempt = getattr(notes_handler.handler, 'csrf_exempt', True)
 note_handler = Resource(handler=NoteHandler, **ad)
+note_handler.csrf_exempt = getattr(note_handler.handler, 'csrf_exempt', True)
 
 urlpatterns = patterns('',
     # 1.0 API methods



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]