Django

Code

Changeset 7583

Show
Ignore:
Timestamp:
06/07/08 01:25:59 (4 months ago)
Author:
russellm
Message:

Fixed #5836 -- Corrected the logic in the Test Client when an exception raised by a view is caught and re-raised. Thanks for the report, test case, and fix, Chris Wagner.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r7579 r7583  
    368368    Vlado <vlado@labath.org> 
    369369    Milton Waddams 
     370    Chris Wagner <cw264701@ohio.edu> 
    370371    wam-djangobug@wamber.net 
    371372    Wang Chun <wangchun@exoweb.net> 
  • django/trunk/django/test/client.py

    r7577 r7583  
    180180                raise 
    181181 
    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! 
    183186        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             
    186191        # Save the client and request that stimulated the response 
    187192        response.client = self 
  • django/trunk/tests/modeltests/test_client/fixtures/testdata.json

    r5677 r7583  
    3535            "date_joined": "2006-12-17 07:03:31" 
    3636        } 
     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        } 
    3755    } 
    3856] 
  • django/trunk/tests/regressiontests/test_client_regress/fixtures/testdata.json

    r6042 r7583  
    1717            "date_joined": "2006-12-17 07:03:31" 
    1818        } 
     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        } 
    1955    } 
    2056] 
  • django/trunk/tests/regressiontests/test_client_regress/models.py

    r7578 r7583  
    55from django.test import Client, TestCase 
    66from django.core.urlresolvers import reverse 
     7from django.core.exceptions import SuspiciousOperation 
    78import os 
    89 
     
    295296        self.assertEqual(response.content, 'Hi, Arthur') 
    296297 
    297  
     298class 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." 
     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") 
  • django/trunk/tests/regressiontests/test_client_regress/urls.py

    r7330 r7583  
    55    (r'^no_template_view/$', views.no_template_view), 
    66    (r'^file_upload/$', views.file_upload_view), 
     7    (r'^staff_only/$', views.staff_only_view), 
    78    (r'^get_view/$', views.get_view), 
    89    url(r'^arg_view/(?P<name>.+)/$', views.view_with_argument, name='arg_view'), 
  • django/trunk/tests/regressiontests/test_client_regress/views.py

    r7577 r7583  
    33from django.contrib.auth.decorators import login_required 
    44from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError 
     5from django.core.exceptions import SuspiciousOperation 
    56 
    67def no_template_view(request): 
     
    2425        return HttpResponseServerError() 
    2526 
     27def 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     
    2634def get_view(request): 
    2735    "A simple login protected view"