Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4988 -- In the test client, Added tracking of the client and r…

…equest that caused a response so that the assertRedirects check can use the correct client when following a redirect. Well spotted, alex@gc-web.de.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6039 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3bf4ef0c77f80e2ed7fab298679ab0c5f28bc900 1 parent afc6985
Russell Keith-Magee authored September 03, 2007
4  django/test/client.py
@@ -182,6 +182,10 @@ def request(self, **request):
182 182
         if self.exc_info:
183 183
             raise self.exc_info[1], None, self.exc_info[2]
184 184
 
  185
+        # Save the client and request that stimulated the response
  186
+        response.client = self
  187
+        response.request = request
  188
+
185 189
         # Add any rendered template detail to the response
186 190
         # If there was only one template rendered (the most likely case),
187 191
         # flatten the list to a single element
4  django/test/testcases.py
@@ -80,7 +80,9 @@ def assertRedirects(self, response, expected_url, status_code=302, target_status
80 80
         self.assertEqual(url, expected_url, 
81 81
             "Response redirected to '%s', expected '%s'" % (url, expected_url))
82 82
         
83  
-        redirect_response = self.client.get(path, QueryDict(query))
  83
+        # Get the redirection page, using the same client that was used
  84
+        # to obtain the original response.
  85
+        redirect_response = response.client.get(path, QueryDict(query))
84 86
         self.assertEqual(redirect_response.status_code, target_status_code, 
85 87
             "Couldn't retrieve redirection page '%s': response code was %d (expected %d)" % 
86 88
                 (path, redirect_response.status_code, target_status_code))
25  docs/testing.txt
@@ -577,13 +577,25 @@ Specifically, a ``Response`` object has the following attributes:
577 577
     ===============  ==========================================================
578 578
     Attribute        Description
579 579
     ===============  ==========================================================
580  
-    ``status_code``  The HTTP status of the response, as an integer. See
581  
-                     RFC2616_ for a full list of HTTP status codes.
  580
+    ``client``       The test client that was used to make the request that
  581
+                     resulted in the response.
582 582
 
583 583
     ``content``      The body of the response, as a string. This is the final
584 584
                      page content as rendered by the view, or any error
585 585
                      message (such as the URL for a 302 redirect).
586 586
 
  587
+    ``context``      The template ``Context`` instance that was used to render
  588
+                     the template that produced the response content.
  589
+
  590
+                     If the rendered page used multiple templates, then
  591
+                     ``context`` will be a list of ``Context``
  592
+                     objects, in the order in which they were rendered.
  593
+
  594
+    ``request``      The request data that stimulated the response.
  595
+
  596
+    ``status_code``  The HTTP status of the response, as an integer. See
  597
+                     RFC2616_ for a full list of HTTP status codes.
  598
+
587 599
     ``template``     The ``Template`` instance that was used to render the
588 600
                      final content. Use ``template.name`` to get the
589 601
                      template's file name, if the template was loaded from a
@@ -594,13 +606,6 @@ Specifically, a ``Response`` object has the following attributes:
594 606
                      using `template inheritance`_ -- then ``template`` will
595 607
                      be a list of ``Template`` instances, in the order in
596 608
                      which they were rendered.
597  
-
598  
-    ``context``      The template ``Context`` instance that was used to render
599  
-                     the template that produced the response content.
600  
-
601  
-                     As with ``template``, if the rendered page used multiple
602  
-                     templates, then ``context`` will be a list of ``Context``
603  
-                     objects, in the order in which they were rendered.
604 609
     ===============  ==========================================================
605 610
 
606 611
 .. _RFC2616: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
@@ -828,7 +833,7 @@ useful for testing Web applications:
828 833
 
829 834
 ``assertRedirects(response, expected_url, status_code=302, target_status_code=200)``
830 835
     Asserts that the response return a ``status_code`` redirect status,
831  
-    it redirected to ``expected_url`` (including any GET data), and the subsequent 
  836
+    it redirected to ``expected_url`` (including any GET data), and the subsequent
832 837
     page was received with ``target_status_code``.
833 838
 
834 839
 ``assertTemplateUsed(response, template_name)``
3  tests/modeltests/test_client/models.py
@@ -234,7 +234,8 @@ def test_view_with_login(self):
234 234
         self.assertRedirects(response, '/accounts/login/?next=/test_client/login_protected_view/')
235 235
         
236 236
         # Log in
237  
-        self.client.login(username='testclient', password='password')
  237
+        login = self.client.login(username='testclient', password='password')
  238
+        self.assertTrue(login, 'Could not log in')
238 239
 
239 240
         # Request a page that requires a login
240 241
         response = self.client.get('/test_client/login_protected_view/')
21  tests/regressiontests/test_client_regress/models.py
@@ -212,7 +212,7 @@ def test_unknown_error(self):
212 212
         except AssertionError, e:
213 213
             self.assertEqual(str(e), "The field 'email' on form 'form' in context 0 does not contain the error 'Some error.' (actual errors: [u'Enter a valid e-mail address.'])")
214 214
 
215  
-class AssertFileUploadTests(TestCase):
  215
+class FileUploadTests(TestCase):
216 216
     def test_simple_upload(self):
217 217
         fd = open(os.path.join(os.path.dirname(__file__), "views.py"))
218 218
         post_data = {
@@ -221,3 +221,22 @@ def test_simple_upload(self):
221 221
         }
222 222
         response = self.client.post('/test_client_regress/file_upload/', post_data)
223 223
         self.assertEqual(response.status_code, 200)
  224
+
  225
+class LoginTests(TestCase):
  226
+    fixtures = ['testdata']
  227
+
  228
+    def test_login_different_client(self):
  229
+        "Check that using a different test client doesn't violate authentication"
  230
+
  231
+        # Create a second client, and log in.
  232
+        c = Client()
  233
+        login = c.login(username='testclient', password='password')
  234
+        self.assertTrue(login, 'Could not log in')
  235
+
  236
+        # Get a redirection page with the second client.
  237
+        response = c.get("/test_client_regress/login_protected_redirect_view/")
  238
+        
  239
+        # At this points, the self.client isn't logged in. 
  240
+        # Check that assertRedirects uses the original client, not the 
  241
+        # default client.
  242
+        self.assertRedirects(response, "/test_client_regress/get_view/")
2  tests/regressiontests/test_client_regress/urls.py
@@ -4,4 +4,6 @@
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'^get_view/$', views.get_view),
  8
+    (r'^login_protected_redirect_view/$', views.login_protected_redirect_view)
7 9
 )
12  tests/regressiontests/test_client_regress/views.py
... ...
@@ -1,5 +1,6 @@
  1
+from django.contrib.auth.decorators import login_required
1 2
 from django.core.mail import EmailMessage, SMTPConnection
2  
-from django.http import HttpResponse, HttpResponseServerError
  3
+from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError
3 4
 from django.shortcuts import render_to_response
4 5
 
5 6
 def no_template_view(request):
@@ -18,3 +19,12 @@ def file_upload_view(request):
18 19
     else:
19 20
         return HttpResponseServerError()
20 21
 
  22
+def get_view(request):
  23
+    "A simple login protected view"    
  24
+    return HttpResponse("Hello world")
  25
+get_view = login_required(get_view)
  26
+
  27
+def login_protected_redirect_view(request):
  28
+    "A view that redirects all requests to the GET view"
  29
+    return HttpResponseRedirect('/test_client_regress/get_view/')
  30
+login_protected_redirect_view = login_required(login_protected_redirect_view)

0 notes on commit 3bf4ef0

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