Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@freakboy3742 freakboy3742 authored
View
1  AUTHORS
@@ -367,6 +367,7 @@ answer newbie questions, and generally made Django that much better:
George Vilches <gav@thataddress.com>
Vlado <vlado@labath.org>
Milton Waddams
+ Chris Wagner <cw264701@ohio.edu>
wam-djangobug@wamber.net
Wang Chun <wangchun@exoweb.net>
Filip Wasilewski <filip.wasilewski@gmail.com>
View
11 django/test/client.py
@@ -179,10 +179,15 @@ def request(self, **request):
if e.args != ('500.html',):
raise
- # Look for a signalled exception and reraise it
+ # Look for a signalled exception, clear the current context
+ # exception data, then re-raise the signalled exception.
+ # Also make sure that the signalled exception is cleared from
+ # the local cache!
if self.exc_info:
- raise self.exc_info[1], None, self.exc_info[2]
-
+ exc_info = self.exc_info
+ self.exc_info = None
+ raise exc_info[1], None, exc_info[2]
+
# Save the client and request that stimulated the response
response.client = self
response.request = request
View
18 tests/modeltests/test_client/fixtures/testdata.json
@@ -34,5 +34,23 @@
"email": "testclient@example.com",
"date_joined": "2006-12-17 07:03:31"
}
+ },
+ {
+ "pk": "3",
+ "model": "auth.user",
+ "fields": {
+ "username": "staff",
+ "first_name": "Staff",
+ "last_name": "Member",
+ "is_active": true,
+ "is_superuser": false,
+ "is_staff": true,
+ "last_login": "2006-12-17 07:03:31",
+ "groups": [],
+ "user_permissions": [],
+ "password": "sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161",
+ "email": "testclient@example.com",
+ "date_joined": "2006-12-17 07:03:31"
+ }
}
]
View
36 tests/regressiontests/test_client_regress/fixtures/testdata.json
@@ -16,5 +16,41 @@
"email": "testclient@example.com",
"date_joined": "2006-12-17 07:03:31"
}
+ },
+ {
+ "pk": "2",
+ "model": "auth.user",
+ "fields": {
+ "username": "inactive",
+ "first_name": "Inactive",
+ "last_name": "User",
+ "is_active": false,
+ "is_superuser": false,
+ "is_staff": false,
+ "last_login": "2006-12-17 07:03:31",
+ "groups": [],
+ "user_permissions": [],
+ "password": "sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161",
+ "email": "testclient@example.com",
+ "date_joined": "2006-12-17 07:03:31"
+ }
+ },
+ {
+ "pk": "3",
+ "model": "auth.user",
+ "fields": {
+ "username": "staff",
+ "first_name": "Staff",
+ "last_name": "Member",
+ "is_active": true,
+ "is_superuser": false,
+ "is_staff": true,
+ "last_login": "2006-12-17 07:03:31",
+ "groups": [],
+ "user_permissions": [],
+ "password": "sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161",
+ "email": "testclient@example.com",
+ "date_joined": "2006-12-17 07:03:31"
+ }
}
]
View
23 tests/regressiontests/test_client_regress/models.py
@@ -4,6 +4,7 @@
"""
from django.test import Client, TestCase
from django.core.urlresolvers import reverse
+from django.core.exceptions import SuspiciousOperation
import os
class AssertContainsTests(TestCase):
@@ -294,4 +295,26 @@ def test_argument_with_space_post(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'Hi, Arthur')
+class ExceptionTests(TestCase):
+ fixtures = ['testdata.json']
+
+ def test_exception_cleared(self):
+ "#5836 - A stale user exception isn't re-raised by the test client."
+ login = self.client.login(username='testclient',password='password')
+ self.failUnless(login, 'Could not log in')
+ try:
+ response = self.client.get("/test_client_regress/staff_only/")
+ self.fail("General users should not be able to visit this page")
+ except SuspiciousOperation:
+ pass
+
+ # At this point, an exception has been raised, and should be cleared.
+
+ # This next operation should be successful; if it isn't we have a problem.
+ login = self.client.login(username='staff', password='password')
+ self.failUnless(login, 'Could not log in')
+ try:
+ self.client.get("/test_client_regress/staff_only/")
+ except SuspiciousOperation:
+ self.fail("Staff should be able to visit this page")
View
1  tests/regressiontests/test_client_regress/urls.py
@@ -4,6 +4,7 @@
urlpatterns = patterns('',
(r'^no_template_view/$', views.no_template_view),
(r'^file_upload/$', views.file_upload_view),
+ (r'^staff_only/$', views.staff_only_view),
(r'^get_view/$', views.get_view),
url(r'^arg_view/(?P<name>.+)/$', views.view_with_argument, name='arg_view'),
(r'^login_protected_redirect_view/$', views.login_protected_redirect_view)
View
8 tests/regressiontests/test_client_regress/views.py
@@ -2,6 +2,7 @@
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError
+from django.core.exceptions import SuspiciousOperation
def no_template_view(request):
"A simple view that expects a GET request, and returns a rendered template"
@@ -23,6 +24,13 @@ def file_upload_view(request):
else:
return HttpResponseServerError()
+def staff_only_view(request):
+ "A view that can only be visited by staff. Non staff members get an exception"
+ if request.user.is_staff:
+ return HttpResponse('')
+ else:
+ raise SuspiciousOperation()
+
def get_view(request):
"A simple login protected view"
return HttpResponse("Hello world")
Please sign in to comment.
Something went wrong with that request. Please try again.