1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Converted sessions_tests to use assertIs() rather than assertTrue/False().

This commit is contained in:
Tim Graham 2017-05-03 20:21:39 -04:00 committed by GitHub
parent 578e576c31
commit 67702f3505

View File

@ -54,15 +54,15 @@ class SessionTestsMixin:
self.session.delete() self.session.delete()
def test_new_session(self): def test_new_session(self):
self.assertFalse(self.session.modified) self.assertIs(self.session.modified, False)
self.assertFalse(self.session.accessed) self.assertIs(self.session.accessed, False)
def test_get_empty(self): def test_get_empty(self):
self.assertIsNone(self.session.get('cat')) self.assertIsNone(self.session.get('cat'))
def test_store(self): def test_store(self):
self.session['cat'] = "dog" self.session['cat'] = "dog"
self.assertTrue(self.session.modified) self.assertIs(self.session.modified, True)
self.assertEqual(self.session.pop('cat'), 'dog') self.assertEqual(self.session.pop('cat'), 'dog')
def test_pop(self): def test_pop(self):
@ -72,20 +72,20 @@ class SessionTestsMixin:
self.modified = False self.modified = False
self.assertEqual(self.session.pop('some key'), 'exists') self.assertEqual(self.session.pop('some key'), 'exists')
self.assertTrue(self.session.accessed) self.assertIs(self.session.accessed, True)
self.assertTrue(self.session.modified) self.assertIs(self.session.modified, True)
self.assertIsNone(self.session.get('some key')) self.assertIsNone(self.session.get('some key'))
def test_pop_default(self): def test_pop_default(self):
self.assertEqual(self.session.pop('some key', 'does not exist'), self.assertEqual(self.session.pop('some key', 'does not exist'),
'does not exist') 'does not exist')
self.assertTrue(self.session.accessed) self.assertIs(self.session.accessed, True)
self.assertFalse(self.session.modified) self.assertIs(self.session.modified, False)
def test_pop_default_named_argument(self): def test_pop_default_named_argument(self):
self.assertEqual(self.session.pop('some key', default='does not exist'), 'does not exist') self.assertEqual(self.session.pop('some key', default='does not exist'), 'does not exist')
self.assertTrue(self.session.accessed) self.assertIs(self.session.accessed, True)
self.assertFalse(self.session.modified) self.assertIs(self.session.modified, False)
def test_pop_no_default_keyerror_raised(self): def test_pop_no_default_keyerror_raised(self):
with self.assertRaises(KeyError): with self.assertRaises(KeyError):
@ -94,13 +94,13 @@ class SessionTestsMixin:
def test_setdefault(self): def test_setdefault(self):
self.assertEqual(self.session.setdefault('foo', 'bar'), 'bar') self.assertEqual(self.session.setdefault('foo', 'bar'), 'bar')
self.assertEqual(self.session.setdefault('foo', 'baz'), 'bar') self.assertEqual(self.session.setdefault('foo', 'baz'), 'bar')
self.assertTrue(self.session.accessed) self.assertIs(self.session.accessed, True)
self.assertTrue(self.session.modified) self.assertIs(self.session.modified, True)
def test_update(self): def test_update(self):
self.session.update({'update key': 1}) self.session.update({'update key': 1})
self.assertTrue(self.session.accessed) self.assertIs(self.session.accessed, True)
self.assertTrue(self.session.modified) self.assertIs(self.session.modified, True)
self.assertEqual(self.session.get('update key', None), 1) self.assertEqual(self.session.get('update key', None), 1)
def test_has_key(self): def test_has_key(self):
@ -108,34 +108,34 @@ class SessionTestsMixin:
self.session.modified = False self.session.modified = False
self.session.accessed = False self.session.accessed = False
self.assertIn('some key', self.session) self.assertIn('some key', self.session)
self.assertTrue(self.session.accessed) self.assertIs(self.session.accessed, True)
self.assertFalse(self.session.modified) self.assertIs(self.session.modified, False)
def test_values(self): def test_values(self):
self.assertEqual(list(self.session.values()), []) self.assertEqual(list(self.session.values()), [])
self.assertTrue(self.session.accessed) self.assertIs(self.session.accessed, True)
self.session['some key'] = 1 self.session['some key'] = 1
self.session.modified = False self.session.modified = False
self.session.accessed = False self.session.accessed = False
self.assertEqual(list(self.session.values()), [1]) self.assertEqual(list(self.session.values()), [1])
self.assertTrue(self.session.accessed) self.assertIs(self.session.accessed, True)
self.assertFalse(self.session.modified) self.assertIs(self.session.modified, False)
def test_keys(self): def test_keys(self):
self.session['x'] = 1 self.session['x'] = 1
self.session.modified = False self.session.modified = False
self.session.accessed = False self.session.accessed = False
self.assertEqual(list(self.session.keys()), ['x']) self.assertEqual(list(self.session.keys()), ['x'])
self.assertTrue(self.session.accessed) self.assertIs(self.session.accessed, True)
self.assertFalse(self.session.modified) self.assertIs(self.session.modified, False)
def test_items(self): def test_items(self):
self.session['x'] = 1 self.session['x'] = 1
self.session.modified = False self.session.modified = False
self.session.accessed = False self.session.accessed = False
self.assertEqual(list(self.session.items()), [('x', 1)]) self.assertEqual(list(self.session.items()), [('x', 1)])
self.assertTrue(self.session.accessed) self.assertIs(self.session.accessed, True)
self.assertFalse(self.session.modified) self.assertIs(self.session.modified, False)
def test_clear(self): def test_clear(self):
self.session['x'] = 1 self.session['x'] = 1
@ -144,28 +144,28 @@ class SessionTestsMixin:
self.assertEqual(list(self.session.items()), [('x', 1)]) self.assertEqual(list(self.session.items()), [('x', 1)])
self.session.clear() self.session.clear()
self.assertEqual(list(self.session.items()), []) self.assertEqual(list(self.session.items()), [])
self.assertTrue(self.session.accessed) self.assertIs(self.session.accessed, True)
self.assertTrue(self.session.modified) self.assertIs(self.session.modified, True)
def test_save(self): def test_save(self):
self.session.save() self.session.save()
self.assertTrue(self.session.exists(self.session.session_key)) self.assertIs(self.session.exists(self.session.session_key), True)
def test_delete(self): def test_delete(self):
self.session.save() self.session.save()
self.session.delete(self.session.session_key) self.session.delete(self.session.session_key)
self.assertFalse(self.session.exists(self.session.session_key)) self.assertIs(self.session.exists(self.session.session_key), False)
def test_flush(self): def test_flush(self):
self.session['foo'] = 'bar' self.session['foo'] = 'bar'
self.session.save() self.session.save()
prev_key = self.session.session_key prev_key = self.session.session_key
self.session.flush() self.session.flush()
self.assertFalse(self.session.exists(prev_key)) self.assertIs(self.session.exists(prev_key), False)
self.assertNotEqual(self.session.session_key, prev_key) self.assertNotEqual(self.session.session_key, prev_key)
self.assertIsNone(self.session.session_key) self.assertIsNone(self.session.session_key)
self.assertTrue(self.session.modified) self.assertIs(self.session.modified, True)
self.assertTrue(self.session.accessed) self.assertIs(self.session.accessed, True)
def test_cycle(self): def test_cycle(self):
self.session['a'], self.session['b'] = 'c', 'd' self.session['a'], self.session['b'] = 'c', 'd'
@ -173,7 +173,7 @@ class SessionTestsMixin:
prev_key = self.session.session_key prev_key = self.session.session_key
prev_data = list(self.session.items()) prev_data = list(self.session.items())
self.session.cycle_key() self.session.cycle_key()
self.assertFalse(self.session.exists(prev_key)) self.assertIs(self.session.exists(prev_key), False)
self.assertNotEqual(self.session.session_key, prev_key) self.assertNotEqual(self.session.session_key, prev_key)
self.assertEqual(list(self.session.items()), prev_data) self.assertEqual(list(self.session.items()), prev_data)
@ -182,7 +182,7 @@ class SessionTestsMixin:
self.session.save() self.session.save()
prev_data = self.session.items() prev_data = self.session.items()
self.session = self.backend(self.session.session_key) self.session = self.backend(self.session.session_key)
self.assertFalse(hasattr(self.session, '_session_cache')) self.assertIs(hasattr(self.session, '_session_cache'), False)
self.session.cycle_key() self.session.cycle_key()
self.assertCountEqual(self.session.items(), prev_data) self.assertCountEqual(self.session.items(), prev_data)
@ -286,23 +286,23 @@ class SessionTestsMixin:
# set_expiry calls # set_expiry calls
with override_settings(SESSION_EXPIRE_AT_BROWSER_CLOSE=False): with override_settings(SESSION_EXPIRE_AT_BROWSER_CLOSE=False):
self.session.set_expiry(10) self.session.set_expiry(10)
self.assertFalse(self.session.get_expire_at_browser_close()) self.assertIs(self.session.get_expire_at_browser_close(), False)
self.session.set_expiry(0) self.session.set_expiry(0)
self.assertTrue(self.session.get_expire_at_browser_close()) self.assertIs(self.session.get_expire_at_browser_close(), True)
self.session.set_expiry(None) self.session.set_expiry(None)
self.assertFalse(self.session.get_expire_at_browser_close()) self.assertIs(self.session.get_expire_at_browser_close(), False)
with override_settings(SESSION_EXPIRE_AT_BROWSER_CLOSE=True): with override_settings(SESSION_EXPIRE_AT_BROWSER_CLOSE=True):
self.session.set_expiry(10) self.session.set_expiry(10)
self.assertFalse(self.session.get_expire_at_browser_close()) self.assertIs(self.session.get_expire_at_browser_close(), False)
self.session.set_expiry(0) self.session.set_expiry(0)
self.assertTrue(self.session.get_expire_at_browser_close()) self.assertIs(self.session.get_expire_at_browser_close(), True)
self.session.set_expiry(None) self.session.set_expiry(None)
self.assertTrue(self.session.get_expire_at_browser_close()) self.assertIs(self.session.get_expire_at_browser_close(), True)
def test_decode(self): def test_decode(self):
# Ensure we can decode what we encode # Ensure we can decode what we encode
@ -482,7 +482,7 @@ class CacheDBSessionTests(SessionTestsMixin, TestCase):
def test_exists_searches_cache_first(self): def test_exists_searches_cache_first(self):
self.session.save() self.session.save()
with self.assertNumQueries(0): with self.assertNumQueries(0):
self.assertTrue(self.session.exists(self.session.session_key)) self.assertIs(self.session.exists(self.session.session_key), True)
# Some backends might issue a warning # Some backends might issue a warning
@ignore_warnings(module="django.core.cache.backends.base") @ignore_warnings(module="django.core.cache.backends.base")
@ -638,8 +638,7 @@ class SessionMiddlewareTests(TestCase):
# Handle the response through the middleware # Handle the response through the middleware
response = middleware.process_response(request, response) response = middleware.process_response(request, response)
self.assertTrue( self.assertIs(response.cookies[settings.SESSION_COOKIE_NAME]['secure'], True)
response.cookies[settings.SESSION_COOKIE_NAME]['secure'])
@override_settings(SESSION_COOKIE_HTTPONLY=True) @override_settings(SESSION_COOKIE_HTTPONLY=True)
def test_httponly_session_cookie(self): def test_httponly_session_cookie(self):
@ -653,8 +652,7 @@ class SessionMiddlewareTests(TestCase):
# Handle the response through the middleware # Handle the response through the middleware
response = middleware.process_response(request, response) response = middleware.process_response(request, response)
self.assertTrue( self.assertIs(response.cookies[settings.SESSION_COOKIE_NAME]['httponly'], True)
response.cookies[settings.SESSION_COOKIE_NAME]['httponly'])
self.assertIn( self.assertIn(
cookies.Morsel._reserved['httponly'], cookies.Morsel._reserved['httponly'],
str(response.cookies[settings.SESSION_COOKIE_NAME]) str(response.cookies[settings.SESSION_COOKIE_NAME])
@ -672,10 +670,11 @@ class SessionMiddlewareTests(TestCase):
# Handle the response through the middleware # Handle the response through the middleware
response = middleware.process_response(request, response) response = middleware.process_response(request, response)
self.assertFalse(response.cookies[settings.SESSION_COOKIE_NAME]['httponly']) self.assertEqual(response.cookies[settings.SESSION_COOKIE_NAME]['httponly'], '')
self.assertNotIn(
self.assertNotIn(cookies.Morsel._reserved['httponly'], cookies.Morsel._reserved['httponly'],
str(response.cookies[settings.SESSION_COOKIE_NAME])) str(response.cookies[settings.SESSION_COOKIE_NAME])
)
def test_session_save_on_500(self): def test_session_save_on_500(self):
request = RequestFactory().get('/') request = RequestFactory().get('/')