diff --git a/django/test/client.py b/django/test/client.py index 060fabd238..4d57f2d2ab 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -299,7 +299,10 @@ class Client: # Create a fake request to store login details. request = HttpRequest() - request.session = engine.SessionStore() + if self.session: + request.session = self.session + else: + request.session = engine.SessionStore() login(request, user) # Set the cookie to represent the session. diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py index 3518df3b9f..14552b7e63 100644 --- a/tests/regressiontests/test_client_regress/models.py +++ b/tests/regressiontests/test_client_regress/models.py @@ -325,3 +325,33 @@ class zzUrlconfSubstitutionTests(TestCase): "URLconf is reverted to original value after modification in a TestCase" url = reverse('arg_view', args=['somename']) self.assertEquals(url, '/test_client_regress/arg_view/somename/') + +class SessionTests(TestCase): + fixtures = ['testdata.json'] + + def test_session(self): + "The session isn't lost if a user logs in" + # The session doesn't exist to start. + response = self.client.get('/test_client_regress/check_session/') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.content, 'NO') + + # This request sets a session variable. + response = self.client.get('/test_client_regress/set_session/') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.content, 'set_session') + + # Check that the session has been modified + response = self.client.get('/test_client_regress/check_session/') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.content, 'YES') + + # Log in + login = self.client.login(username='testclient',password='password') + self.failUnless(login, 'Could not log in') + + # Session should still contain the modified value + response = self.client.get('/test_client_regress/check_session/') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.content, 'YES') + \ No newline at end of file diff --git a/tests/regressiontests/test_client_regress/urls.py b/tests/regressiontests/test_client_regress/urls.py index 12f6afacf3..ca21e18817 100644 --- a/tests/regressiontests/test_client_regress/urls.py +++ b/tests/regressiontests/test_client_regress/urls.py @@ -6,5 +6,7 @@ urlpatterns = patterns('', (r'^staff_only/$', views.staff_only_view), (r'^get_view/$', views.get_view), url(r'^arg_view/(?P.+)/$', views.view_with_argument, name='arg_view'), - (r'^login_protected_redirect_view/$', views.login_protected_redirect_view) + (r'^login_protected_redirect_view/$', views.login_protected_redirect_view), + (r'^set_session/$', views.set_session_view), + (r'^check_session/$', views.check_session_view), ) diff --git a/tests/regressiontests/test_client_regress/views.py b/tests/regressiontests/test_client_regress/views.py index d703c82124..9cd786d86b 100644 --- a/tests/regressiontests/test_client_regress/views.py +++ b/tests/regressiontests/test_client_regress/views.py @@ -34,3 +34,12 @@ def login_protected_redirect_view(request): "A view that redirects all requests to the GET view" return HttpResponseRedirect('/test_client_regress/get_view/') login_protected_redirect_view = login_required(login_protected_redirect_view) + +def set_session_view(request): + "A view that sets a session variable" + request.session['session_var'] = 'YES' + return HttpResponse('set_session') + +def check_session_view(request): + "A view that reads a session variable" + return HttpResponse(request.session.get('session_var', 'NO'))