mirror of
https://github.com/django/django.git
synced 2025-03-31 19:46:42 +00:00
Fixed #21357 -- Fixed test client session initialization.
The test client will now create a session when it is first accessed if no session already exists.
This commit is contained in:
parent
4dc4d12e27
commit
be88b062af
@ -393,6 +393,11 @@ class Client(RequestFactory):
|
|||||||
cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)
|
cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)
|
||||||
if cookie:
|
if cookie:
|
||||||
return engine.SessionStore(cookie.value)
|
return engine.SessionStore(cookie.value)
|
||||||
|
else:
|
||||||
|
s = engine.SessionStore()
|
||||||
|
s.save()
|
||||||
|
self.cookies[settings.SESSION_COOKIE_NAME] = s.session_key
|
||||||
|
return s
|
||||||
return {}
|
return {}
|
||||||
session = property(_session)
|
session = property(_session)
|
||||||
|
|
||||||
|
@ -1048,6 +1048,14 @@ class SessionTests(TestCase):
|
|||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(response.content, b'YES')
|
self.assertEqual(response.content, b'YES')
|
||||||
|
|
||||||
|
def test_session_initiated(self):
|
||||||
|
session = self.client.session
|
||||||
|
session['session_var'] = 'foo'
|
||||||
|
session.save()
|
||||||
|
|
||||||
|
response = self.client.get('/check_session/')
|
||||||
|
self.assertEqual(response.content, b'foo')
|
||||||
|
|
||||||
def test_logout(self):
|
def test_logout(self):
|
||||||
"""Logout should work whether the user is logged in or not (#9978)."""
|
"""Logout should work whether the user is logged in or not (#9978)."""
|
||||||
self.client.logout()
|
self.client.logout()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user