Ticket #10899: 10899-proposal.patch
File 10899-proposal.patch, 7.8 KB (added by , 12 years ago) |
---|
-
django/test/client.py
commit d2804c0f564d1a5080e7afdae603865b09105578 Author: Deni Bertovic <deni@kset.org> Date: Sun May 19 14:39:53 2013 +0200 Fixed #10899 -- Manipulating Session via test Client diff --git a/django/test/client.py b/django/test/client.py index 46f55d7..21db42d 100644
a b from django.db import close_old_connections 22 22 from django.http import SimpleCookie, HttpRequest, QueryDict 23 23 from django.template import TemplateDoesNotExist 24 24 from django.test import signals 25 from django.utils.functional import c urry25 from django.utils.functional import cached_property, curry 26 26 from django.utils.encoding import force_bytes, force_str 27 27 from django.utils.http import urlencode 28 28 from django.utils.importlib import import_module … … class Client(RequestFactory): 378 378 """ 379 379 self.exc_info = sys.exc_info() 380 380 381 def _session(self): 381 @cached_property 382 def session(self): 382 383 """ 383 384 Obtains the current session variables. 384 385 """ 385 if 'django.contrib.sessions' in settings.INSTALLED_APPS: 386 engine = import_module(settings.SESSION_ENGINE) 387 cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None) 388 if cookie: 389 return engine.SessionStore(cookie.value) 390 return {} 391 session = property(_session) 386 if 'django.contrib.sessions' not in settings.INSTALLED_APPS: 387 return {} 392 388 389 engine = import_module(settings.SESSION_ENGINE) 390 cookie = self.cookies.get(settings.SESSION_COOKIE_NAME) 391 if cookie: 392 session_store = engine.SessionStore(cookie.value) 393 else: 394 session_store = engine.SessionStore() 395 session_store.create() 396 self.cookies[settings.SESSION_COOKIE_NAME] = session_store.session_key 397 return session_store 393 398 394 399 def request(self, **request): 395 400 """ … … class Client(RequestFactory): 400 405 """ 401 406 environ = self._base_environ(**request) 402 407 408 if self.session: 409 if getattr(self.session, 'modified'): 410 self.session.save() 411 412 if 'django.contrib.sessions.middleware.SessionMiddleware' in settings.MIDDLEWARE_CLASSES: 413 # Update the session cookie since the session key can change 414 # due to login or logout and force the session to be reloaded 415 # on next access. 416 self.cookies[settings.SESSION_COOKIE_NAME] = self.session.session_key 417 self.session = None 418 403 419 # Curry a data dictionary into an instance of the template renderer 404 420 # callback function. 405 421 data = {} … … class Client(RequestFactory): 536 552 user = authenticate(**credentials) 537 553 if user and user.is_active \ 538 554 and 'django.contrib.sessions' in settings.INSTALLED_APPS: 539 engine = import_module(settings.SESSION_ENGINE)540 555 541 556 # Create a fake request to store login details. 542 557 request = HttpRequest() 543 if self.session: 544 request.session = self.session 545 else: 546 request.session = engine.SessionStore() 558 request.session = self.session 547 559 login(request, user) 548 560 549 561 # Save the session values. … … class Client(RequestFactory): 576 588 if session_cookie: 577 589 session.delete(session_key=session_cookie.value) 578 590 self.cookies = SimpleCookie() 591 self.session = None 579 592 580 593 def _handle_redirects(self, response, **extra): 581 594 "Follows any redirects by requesting responses from the server using GET." -
docs/topics/testing/overview.txt
diff --git a/docs/topics/testing/overview.txt b/docs/topics/testing/overview.txt index d543099..08e1ef8 100644
a b can access these properties as part of a test condition. 794 794 A dictionary-like object containing session information. See the 795 795 :doc:`session documentation</topics/http/sessions>` for full details. 796 796 797 To modify the session and then save it, it must be stored in a variable 798 first (because a new ``SessionStore`` is created every time this property 799 is accessed):: 797 .. versionadded:: 1.6 798 799 If you wish to manipulate the session directly you can do that directly 800 on the client and the modified values will be saved on subsequent requests. 800 801 801 802 def test_something(self): 802 se ssion = self.client.session803 se ssion['somekey'] = 'test'804 session.save() 803 self.client.session['somekey'] = value 804 self.client.session['anotherkey'] = value 805 805 806 806 807 Example 807 808 ~~~~~~~ -
tests/admin_views/tests.py
diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index 8e678a7..8c57088 100644
a b class AdminCustomQuerysetTest(TestCase): 2583 2583 2584 2584 # 4 queries are expected: 1 for the session, 1 for the user, 2585 2585 # 1 for the count and 1 for the objects on the page 2586 with self.assertNumQueries( 4):2586 with self.assertNumQueries(6): 2587 2587 resp = self.client.get('/test_admin/admin/admin_views/person/') 2588 2588 self.assertEqual(resp.context['selection_note'], '0 of 2 selected') 2589 2589 self.assertEqual(resp.context['selection_note_all'], 'All 2 selected') … … class UserAdminTest(TestCase): 3607 3607 # Don't depend on a warm cache, see #17377. 3608 3608 ContentType.objects.clear_cache() 3609 3609 3610 expected_queries = 1 03610 expected_queries = 12 3611 3611 # Oracle doesn't implement "RELEASE SAVPOINT", see #20387. 3612 3612 if connection.vendor == 'oracle': 3613 3613 expected_queries -= 1 … … class GroupAdminTest(TestCase): 3650 3650 def test_group_permission_performance(self): 3651 3651 g = Group.objects.create(name="test_group") 3652 3652 3653 expected_queries = 83653 expected_queries = 10 3654 3654 # Oracle doesn't implement "RELEASE SAVPOINT", see #20387. 3655 3655 if connection.vendor == 'oracle': 3656 3656 expected_queries -= 1 -
tests/test_client_regress/tests.py
diff --git a/tests/test_client_regress/tests.py b/tests/test_client_regress/tests.py index 2582b21..2694ce3 100644
a b class SessionTests(TestCase): 756 756 self.client.logout() 757 757 self.client.logout() 758 758 759 def test_session_manipulation(self): 760 # Check that the session can be edited as documented before #10899. 761 session = self.client.session 762 session['session_var'] = b'foo' 763 session.save() 764 765 response = self.client.get('/test_client_regress/check_session/') 766 self.assertEqual(response.status_code, 200) 767 self.assertEqual(response.content, b'foo') 768 769 def test_direct_session_manipulation(self): 770 # Add a value to the session 771 self.client.session['session_var'] = b'bar' 772 self.assertEqual(self.client.session['session_var'], b'bar') 773 774 # Check that the session has been modified 775 response = self.client.get('/test_client_regress/check_session/') 776 self.assertEqual(response.status_code, 200) 777 self.assertEqual(response.content, b'bar') 778 779 # Check that the session variable persists over login 780 # when cycle_key() is called 781 self.client.login(username='testclient', password='password') 782 self.assertEqual(self.client.session['session_var'], b'bar') 783 784 response = self.client.get('/test_client_regress/check_session/') 785 self.assertEqual(response.status_code, 200) 786 self.assertEqual(response.content, b'bar') 787 788 # Check that new session is started after logout 789 self.client.logout() 790 self.assertEqual(self.client.session.get('session_var'), None) 791 792 759 793 class RequestMethodTests(TestCase): 760 794 def test_get(self): 761 795 "Request a view via request method GET"