Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5836 -- Corrected the logic in the Test Client when an excepti…

…on raised by a view is caught and re-raised. Thanks for the report, test case, and fix, Chris Wagner.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7583 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 8a7bbc3c3426beacdb4286960fb1a731a7d88d25 1 parent 5b9d907
Russell Keith-Magee authored June 07, 2008
1  AUTHORS
@@ -367,6 +367,7 @@ answer newbie questions, and generally made Django that much better:
367 367
     George Vilches <gav@thataddress.com>
368 368
     Vlado <vlado@labath.org>
369 369
     Milton Waddams
  370
+    Chris Wagner <cw264701@ohio.edu>
370 371
     wam-djangobug@wamber.net
371 372
     Wang Chun <wangchun@exoweb.net>
372 373
     Filip Wasilewski <filip.wasilewski@gmail.com>
11  django/test/client.py
@@ -179,10 +179,15 @@ def request(self, **request):
179 179
             if e.args != ('500.html',):
180 180
                 raise
181 181
 
182  
-        # Look for a signalled exception and reraise it
  182
+        # Look for a signalled exception, clear the current context
  183
+        # exception data, then re-raise the signalled exception.
  184
+        # Also make sure that the signalled exception is cleared from
  185
+        # the local cache!
183 186
         if self.exc_info:
184  
-            raise self.exc_info[1], None, self.exc_info[2]
185  
-
  187
+            exc_info = self.exc_info
  188
+            self.exc_info = None
  189
+            raise exc_info[1], None, exc_info[2]
  190
+            
186 191
         # Save the client and request that stimulated the response
187 192
         response.client = self
188 193
         response.request = request
18  tests/modeltests/test_client/fixtures/testdata.json
@@ -34,5 +34,23 @@
34 34
             "email": "testclient@example.com", 
35 35
             "date_joined": "2006-12-17 07:03:31"
36 36
         }
  37
+    },
  38
+    {
  39
+        "pk": "3", 
  40
+        "model": "auth.user", 
  41
+        "fields": {
  42
+            "username": "staff", 
  43
+            "first_name": "Staff", 
  44
+            "last_name": "Member", 
  45
+            "is_active": true, 
  46
+            "is_superuser": false, 
  47
+            "is_staff": true, 
  48
+            "last_login": "2006-12-17 07:03:31", 
  49
+            "groups": [], 
  50
+            "user_permissions": [], 
  51
+            "password": "sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161", 
  52
+            "email": "testclient@example.com", 
  53
+            "date_joined": "2006-12-17 07:03:31"
  54
+        }
37 55
     }
38 56
 ]
36  tests/regressiontests/test_client_regress/fixtures/testdata.json
@@ -16,5 +16,41 @@
16 16
             "email": "testclient@example.com", 
17 17
             "date_joined": "2006-12-17 07:03:31"
18 18
         }
  19
+    },
  20
+    {
  21
+        "pk": "2", 
  22
+        "model": "auth.user", 
  23
+        "fields": {
  24
+            "username": "inactive", 
  25
+            "first_name": "Inactive", 
  26
+            "last_name": "User", 
  27
+            "is_active": false, 
  28
+            "is_superuser": false, 
  29
+            "is_staff": false, 
  30
+            "last_login": "2006-12-17 07:03:31", 
  31
+            "groups": [], 
  32
+            "user_permissions": [], 
  33
+            "password": "sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161", 
  34
+            "email": "testclient@example.com", 
  35
+            "date_joined": "2006-12-17 07:03:31"
  36
+        }
  37
+    },
  38
+    {
  39
+        "pk": "3", 
  40
+        "model": "auth.user", 
  41
+        "fields": {
  42
+            "username": "staff", 
  43
+            "first_name": "Staff", 
  44
+            "last_name": "Member", 
  45
+            "is_active": true, 
  46
+            "is_superuser": false, 
  47
+            "is_staff": true, 
  48
+            "last_login": "2006-12-17 07:03:31", 
  49
+            "groups": [], 
  50
+            "user_permissions": [], 
  51
+            "password": "sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161", 
  52
+            "email": "testclient@example.com", 
  53
+            "date_joined": "2006-12-17 07:03:31"
  54
+        }
19 55
     }
20 56
 ]
23  tests/regressiontests/test_client_regress/models.py
@@ -4,6 +4,7 @@
4 4
 """
5 5
 from django.test import Client, TestCase
6 6
 from django.core.urlresolvers import reverse
  7
+from django.core.exceptions import SuspiciousOperation
7 8
 import os
8 9
 
9 10
 class AssertContainsTests(TestCase):
@@ -294,4 +295,26 @@ def test_argument_with_space_post(self):
294 295
         self.assertEqual(response.status_code, 200)
295 296
         self.assertEqual(response.content, 'Hi, Arthur')
296 297
 
  298
+class ExceptionTests(TestCase):
  299
+    fixtures = ['testdata.json']
  300
+    
  301
+    def test_exception_cleared(self):
  302
+        "#5836 - A stale user exception isn't re-raised by the test client."
297 303
 
  304
+        login = self.client.login(username='testclient',password='password')
  305
+        self.failUnless(login, 'Could not log in')
  306
+        try:
  307
+            response = self.client.get("/test_client_regress/staff_only/")
  308
+            self.fail("General users should not be able to visit this page")
  309
+        except SuspiciousOperation:
  310
+            pass
  311
+
  312
+        # At this point, an exception has been raised, and should be cleared.
  313
+        
  314
+        # This next operation should be successful; if it isn't we have a problem.
  315
+        login = self.client.login(username='staff', password='password')
  316
+        self.failUnless(login, 'Could not log in')
  317
+        try:
  318
+            self.client.get("/test_client_regress/staff_only/")
  319
+        except SuspiciousOperation:
  320
+            self.fail("Staff should be able to visit this page")
1  tests/regressiontests/test_client_regress/urls.py
@@ -4,6 +4,7 @@
4 4
 urlpatterns = patterns('',
5 5
     (r'^no_template_view/$', views.no_template_view),
6 6
     (r'^file_upload/$', views.file_upload_view),
  7
+    (r'^staff_only/$', views.staff_only_view),
7 8
     (r'^get_view/$', views.get_view),
8 9
     url(r'^arg_view/(?P<name>.+)/$', views.view_with_argument, name='arg_view'),
9 10
     (r'^login_protected_redirect_view/$', views.login_protected_redirect_view)
8  tests/regressiontests/test_client_regress/views.py
@@ -2,6 +2,7 @@
2 2
 
3 3
 from django.contrib.auth.decorators import login_required
4 4
 from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError
  5
+from django.core.exceptions import SuspiciousOperation
5 6
 
6 7
 def no_template_view(request):
7 8
     "A simple view that expects a GET request, and returns a rendered template"
@@ -23,6 +24,13 @@ def file_upload_view(request):
23 24
     else:
24 25
         return HttpResponseServerError()
25 26
 
  27
+def staff_only_view(request):
  28
+    "A view that can only be visited by staff. Non staff members get an exception"
  29
+    if request.user.is_staff:
  30
+        return HttpResponse('')
  31
+    else:
  32
+        raise SuspiciousOperation()
  33
+    
26 34
 def get_view(request):
27 35
     "A simple login protected view"
28 36
     return HttpResponse("Hello world")

0 notes on commit 8a7bbc3

Please sign in to comment.
Something went wrong with that request. Please try again.