22
33import django .test
44from django import http
5- from django .conf .urls . defaults import patterns
5+ from django .conf .urls import patterns
66from django .contrib .auth import logout
77from django .contrib .auth .middleware import AuthenticationMiddleware
88from django .contrib .auth .models import User
1111from django .core import signals
1212from django .core .cache import cache
1313from django .core .handlers .wsgi import WSGIRequest
14- from django .db import close_connection
15- from django .template import context
14+ from django .core .exceptions import ImproperlyConfigured
1615
1716import mock
1817
@@ -46,33 +45,33 @@ def login(self):
4645 def test_csrftoken_unauthenticated (self ):
4746 # request.csrf_token is '' for anonymous users.
4847 response = self .client .get ('/' , follow = True )
49- self .assertEqual (response ._request .csrf_token , '' )
48+ self .assertEqual (response .wsgi_request .csrf_token , '' )
5049
5150 def test_csrftoken_authenticated (self ):
5251 # request.csrf_token is a random non-empty string for authed users.
5352 self .login ()
5453 response = self .client .get ('/' , follow = True )
5554 # The CSRF token is a 32-character MD5 string.
56- self .assertEqual (len (response ._request .csrf_token ), 32 )
55+ self .assertEqual (len (response .wsgi_request .csrf_token ), 32 )
5756
5857 def test_csrftoken_new_session (self ):
5958 # The csrf_token is added to request.session the first time.
6059 self .login ()
6160 response = self .client .get ('/' , follow = True )
6261 # The CSRF token is a 32-character MD5 string.
63- token = response ._request .session ['csrf_token' ]
62+ token = response .wsgi_request .session ['csrf_token' ]
6463 self .assertEqual (len (token ), 32 )
65- self .assertEqual (token , response ._request .csrf_token )
64+ self .assertEqual (token , response .wsgi_request .csrf_token )
6665
6766 def test_csrftoken_existing_session (self ):
6867 # The csrf_token in request.session is reused on subsequent requests.
6968 self .login ()
7069 r1 = self .client .get ('/' , follow = True )
71- token = r1 ._request .session ['csrf_token' ]
70+ token = r1 .wsgi_request .session ['csrf_token' ]
7271
7372 r2 = self .client .get ('/' , follow = True )
74- self .assertEqual (r1 ._request .csrf_token , r2 ._request .csrf_token )
75- self .assertEqual (token , r2 ._request .csrf_token )
73+ self .assertEqual (r1 .wsgi_request .csrf_token , r2 .wsgi_request .csrf_token )
74+ self .assertEqual (token , r2 .wsgi_request .csrf_token )
7675
7776
7877class TestCsrfMiddleware (django .test .TestCase ):
@@ -160,7 +159,7 @@ def test_csrf_token_context_processor(self):
160159 request .csrf_token = self .token
161160 request .groups = []
162161 ctx = {}
163- for processor in context . get_standard_processors ():
162+ for processor in get_context_processors ():
164163 ctx .update (processor (request ))
165164 self .assertEqual (ctx ['csrf_token' ], self .token )
166165
@@ -209,7 +208,7 @@ def test_new_anon_token_on_request(self):
209208 response = self .client .get ('/anon' )
210209 # Get the key from the cookie and find the token in the cache.
211210 key = response .cookies ['anoncsrf' ].value
212- self .assertEqual (response ._request .csrf_token , cache .get (prep_key (key )))
211+ self .assertEqual (response .wsgi_request .csrf_token , cache .get (prep_key (key )))
213212
214213 def test_existing_anon_cookie_on_request (self ):
215214 # We reuse an existing anon cookie key+token.
@@ -218,7 +217,7 @@ def test_existing_anon_cookie_on_request(self):
218217 # Now check that subsequent requests use that cookie.
219218 response = self .client .get ('/anon' )
220219 self .assertEqual (response .cookies ['anoncsrf' ].value , key )
221- self .assertEqual (response ._request .csrf_token , cache .get (prep_key (key )))
220+ self .assertEqual (response .wsgi_request .csrf_token , cache .get (prep_key (key )))
222221
223222 def test_new_anon_token_on_response (self ):
224223 # The anon cookie is sent and we vary on Cookie.
@@ -244,12 +243,12 @@ def test_anon_csrf_logout(self):
244243
245244 def test_existing_anon_cookie_not_in_cache (self ):
246245 response = self .client .get ('/anon' )
247- self .assertEqual (len (response ._request .csrf_token ), 32 )
246+ self .assertEqual (len (response .wsgi_request .csrf_token ), 32 )
248247
249248 # Clear cache and make sure we still get a token
250249 cache .clear ()
251250 response = self .client .get ('/anon' )
252- self .assertEqual (len (response ._request .csrf_token ), 32 )
251+ self .assertEqual (len (response .wsgi_request .csrf_token ), 32 )
253252
254253 def test_anonymous_csrf_exempt (self ):
255254 response = self .client .post ('/no-anon-csrf' )
@@ -283,7 +282,7 @@ def test_csrftoken_unauthenticated(self):
283282 # when ANON_ALWAYS is enabled.
284283 response = self .client .get ('/' , follow = True )
285284 # The CSRF token is a 32-character MD5 string.
286- self .assertEqual (len (response ._request .csrf_token ), 32 )
285+ self .assertEqual (len (response .wsgi_request .csrf_token ), 32 )
287286
288287 def test_authenticated_request (self ):
289288 # Nothing special happens, nothing breaks.
@@ -307,7 +306,7 @@ def test_new_anon_token_on_request(self):
307306 response = self .client .get ('/' )
308307 # Get the key from the cookie and find the token in the cache.
309308 key = response .cookies ['anoncsrf' ].value
310- self .assertEqual (response ._request .csrf_token , cache .get (prep_key (key )))
309+ self .assertEqual (response .wsgi_request .csrf_token , cache .get (prep_key (key )))
311310
312311 def test_existing_anon_cookie_on_request (self ):
313312 # We reuse an existing anon cookie key+token.
@@ -317,7 +316,7 @@ def test_existing_anon_cookie_on_request(self):
317316 # Now check that subsequent requests use that cookie.
318317 response = self .client .get ('/' )
319318 self .assertEqual (response .cookies ['anoncsrf' ].value , key )
320- self .assertEqual (response ._request .csrf_token , cache .get (prep_key (key )))
319+ self .assertEqual (response .wsgi_request .csrf_token , cache .get (prep_key (key )))
321320 self .assertEqual (response ['Vary' ], 'Cookie' )
322321
323322 def test_anon_csrf_logout (self ):
@@ -328,12 +327,12 @@ def test_anon_csrf_logout(self):
328327
329328 def test_existing_anon_cookie_not_in_cache (self ):
330329 response = self .client .get ('/' )
331- self .assertEqual (len (response ._request .csrf_token ), 32 )
330+ self .assertEqual (len (response .wsgi_request .csrf_token ), 32 )
332331
333332 # Clear cache and make sure we still get a token
334333 cache .clear ()
335334 response = self .client .get ('/' )
336- self .assertEqual (len (response ._request .csrf_token ), 32 )
335+ self .assertEqual (len (response .wsgi_request .csrf_token ), 32 )
337336
338337 def test_massive_anon_cookie (self ):
339338 # if the key + PREFIX + setting prefix is greater than 250
@@ -352,33 +351,24 @@ def test_surprising_characters(self):
352351 self .assertEqual (warner .call_count , 0 )
353352
354353
355- class ClientHandler (django .test .client .ClientHandler ):
356- """
357- Handler that stores the real request object on the response.
358-
359- Almost all the code comes from the parent class.
360- """
354+ def get_context_processors ():
355+ """Get context processors in a way that works for Django 1.7 and 1.8+"""
356+ try :
357+ # 1.7
358+ from django .template .context import get_standard_processors
359+ return get_standard_processors ()
360+ except ImportError :
361+ # 1.8+
362+ try :
363+ from django .template .engine import Engine
364+ engine = Engine .get_default ()
365+ except ImproperlyConfigured :
366+ return []
367+ return engine .template_context_processors
361368
362- def __call__ (self , environ ):
363- # Set up middleware if needed. We couldn't do this earlier, because
364- # settings weren't available.
365- if self ._request_middleware is None :
366- self .load_middleware ()
367369
368- signals .request_started .send (sender = self .__class__ )
369- try :
370- request = WSGIRequest (environ )
371- # sneaky little hack so that we can easily get round
372- # CsrfViewMiddleware. This makes life easier, and is probably
373- # required for backwards compatibility with external tests against
374- # admin views.
375- request ._dont_enforce_csrf_checks = not self .enforce_csrf_checks
376- response = self .get_response (request )
377- finally :
378- signals .request_finished .disconnect (close_connection )
379- signals .request_finished .send (sender = self .__class__ )
380- signals .request_finished .connect (close_connection )
381-
382- # Store the request object.
383- response ._request = request
384- return response
370+ # for 1.7 support
371+ class ClientHandler (django .test .client .ClientHandler ):
372+ @property
373+ def wsgi_request_middleware (self ):
374+ return self ._request_middleware
0 commit comments