Ticket #10899: easy_session_manipulation_against_14099_with_tests.diff
File easy_session_manipulation_against_14099_with_tests.diff, 7.1 KB (added by , 14 years ago) |
---|
-
django/test/client.py
diff --git a/django/test/client.py b/django/test/client.py index 08e3ff6..41e0db5 100644
a b class Client(object): 179 179 self.cookies = SimpleCookie() 180 180 self.exc_info = None 181 181 self.errors = StringIO() 182 self._session_store = None 182 183 183 184 def store_exc_info(self, **kwargs): 184 185 """ … … class Client(object): 188 189 189 190 def _session(self): 190 191 """ 191 Obtains the current session variables.192 Obtains a SessionStore containing all the current session variables. 192 193 """ 193 if 'django.contrib.sessions' in settings.INSTALLED_APPS: 194 engine = import_module(settings.SESSION_ENGINE) 195 cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None) 196 if cookie: 197 return engine.SessionStore(cookie.value) 198 return {} 194 if self._session_store == None: 195 if 'django.contrib.sessions' in settings.INSTALLED_APPS: 196 cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None) 197 engine = import_module(settings.SESSION_ENGINE) 198 if cookie: 199 self._session_store = engine.SessionStore(cookie.value) 200 else: 201 self._session_store = engine.SessionStore() 202 # if load() fails the cookie changes, so save() first 203 self._session_store.save() 204 self.cookies[settings.SESSION_COOKIE_NAME] = \ 205 self._session_store.session_key 206 else: 207 self._session_store = {} 208 return self._session_store 199 209 session = property(_session) 200 210 211 def _session_save(self): 212 """ 213 Push the session store to the database then set the current 214 session to None so that it is reloaded on subsquent requests. 215 """ 216 if hasattr(self._session_store, 'modified'): 217 if self._session_store.modified: 218 self._session_store.save() 219 # update the cookie in case cycle_key() was called 220 self.cookies[settings.SESSION_COOKIE_NAME] = \ 221 self._session_store.session_key 222 self._session_store = None 223 201 224 def request(self, **request): 202 225 """ 203 226 The master request method. Composes the environment dictionary … … class Client(object): 285 308 """ 286 309 Requests a response from the server using GET. 287 310 """ 311 self._session_save() 288 312 parsed = urlparse(path) 289 313 r = { 290 314 'CONTENT_TYPE': 'text/html; charset=utf-8', … … class Client(object): 305 329 """ 306 330 Requests a response from the server using POST. 307 331 """ 332 self._session_save() 308 333 if content_type is MULTIPART_CONTENT: 309 334 post_data = encode_multipart(BOUNDARY, data) 310 335 else: … … class Client(object): 336 361 """ 337 362 Request a response from the server using HEAD. 338 363 """ 364 self._session_save() 339 365 parsed = urlparse(path) 340 366 r = { 341 367 'CONTENT_TYPE': 'text/html; charset=utf-8', … … class Client(object): 355 381 """ 356 382 Request a response from the server using OPTIONS. 357 383 """ 384 self._session_save() 358 385 parsed = urlparse(path) 359 386 r = { 360 387 'PATH_INFO': urllib.unquote(parsed[2]), … … class Client(object): 374 401 """ 375 402 Send a resource to the server using PUT. 376 403 """ 404 self._session_save() 377 405 if content_type is MULTIPART_CONTENT: 378 406 post_data = encode_multipart(BOUNDARY, data) 379 407 else: … … class Client(object): 405 433 """ 406 434 Send a DELETE request to the server. 407 435 """ 436 self._session_save() 408 437 parsed = urlparse(path) 409 438 r = { 410 439 'PATH_INFO': urllib.unquote(parsed[2]), … … class Client(object): 430 459 user = authenticate(**credentials) 431 460 if user and user.is_active \ 432 461 and 'django.contrib.sessions' in settings.INSTALLED_APPS: 433 engine = import_module(settings.SESSION_ENGINE)434 462 435 463 # Create a fake request to store login details. 436 464 request = HttpRequest() 437 if self.session: 438 request.session = self.session 439 else: 440 request.session = engine.SessionStore() 465 request.session = self.session 441 466 login(request, user) 442 467 443 # Save the session values.444 request.session.save()445 446 # Set the cookie to represent the session.447 session_cookie = settings.SESSION_COOKIE_NAME448 self.cookies[session_cookie] = request.session.session_key449 cookie_data = {450 'max-age': None,451 'path': '/',452 'domain': settings.SESSION_COOKIE_DOMAIN,453 'secure': settings.SESSION_COOKIE_SECURE or None,454 'expires': None,455 }456 self.cookies[session_cookie].update(cookie_data)457 458 468 return True 459 469 else: 460 470 return False … … class Client(object): 465 475 466 476 Causes the authenticated user to be logged out. 467 477 """ 478 self._session_store = None 468 479 session = import_module(settings.SESSION_ENGINE).SessionStore() 469 480 session_cookie = self.cookies.get(settings.SESSION_COOKIE_NAME) 470 481 if session_cookie: -
tests/regressiontests/test_client_regress/models.py
diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py index 22b59e5..9bd9134 100644
a b class SessionTests(TestCase): 677 677 self.assertEqual(response.status_code, 200) 678 678 self.assertEqual(response.content, 'YES') 679 679 680 def test_session_manipulation(self): 681 # Check that the existing documented behavior works 682 session = self.client.session 683 session["session_var"] = "foo" 684 session.save() 685 686 response = self.client.get('/test_client_regress/check_session/') 687 self.assertEqual(response.status_code, 200) 688 self.assertEqual(response.content, 'foo') 689 690 def test_direct_session_manipulation(self): 691 self.client.session['session_var'] = 'bar' 692 693 # Check that the session has been modified 694 response = self.client.get('/test_client_regress/check_session/') 695 self.assertEqual(response.status_code, 200) 696 self.assertEqual(response.content, 'bar') 697 698 # Check that the session variable persists over login 699 # when cycle_key() is called 700 login = self.client.login(username='testclient', password='password') 701 response = self.client.get('/test_client_regress/check_session/') 702 self.assertEqual(response.status_code, 200) 703 self.assertEqual(response.content, 'bar') 704 705 self.assertEqual(self.client.session.pop('session_var'), 'bar') 706 680 707 def test_logout(self): 681 708 """Logout should work whether the user is logged in or not (#9978).""" 682 709 self.client.logout()