Converted sessions tests from doctest to unittest.
Also made the FileSession backend consistent with other backends in one corner case uncovered by the conversion, namely that the backend should create a new key if the one passed in is invalid. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13482 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
c5e83a9e0a
commit
5eece23296
|
@ -58,7 +58,7 @@ class SessionStore(SessionBase):
|
||||||
finally:
|
finally:
|
||||||
session_file.close()
|
session_file.close()
|
||||||
except IOError:
|
except IOError:
|
||||||
pass
|
self.create()
|
||||||
return session_data
|
return session_data
|
||||||
|
|
||||||
def create(self):
|
def create(self):
|
||||||
|
|
|
@ -1,388 +1,273 @@
|
||||||
r"""
|
from datetime import datetime, timedelta
|
||||||
|
from django.conf import settings
|
||||||
>>> from django.conf import settings
|
from django.contrib.sessions.backends.db import SessionStore as DatabaseSession
|
||||||
>>> from django.contrib.sessions.backends.db import SessionStore as DatabaseSession
|
from django.contrib.sessions.backends.cache import SessionStore as CacheSession
|
||||||
>>> from django.contrib.sessions.backends.cache import SessionStore as CacheSession
|
from django.contrib.sessions.backends.cached_db import SessionStore as CacheDBSession
|
||||||
>>> from django.contrib.sessions.backends.cached_db import SessionStore as CacheDBSession
|
from django.contrib.sessions.backends.file import SessionStore as FileSession
|
||||||
>>> from django.contrib.sessions.backends.file import SessionStore as FileSession
|
from django.contrib.sessions.backends.base import SessionBase
|
||||||
>>> from django.contrib.sessions.backends.base import SessionBase
|
from django.contrib.sessions.models import Session
|
||||||
>>> from django.contrib.sessions.models import Session
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
|
from django.test import TestCase
|
||||||
>>> db_session = DatabaseSession()
|
import shutil
|
||||||
>>> db_session.modified
|
import tempfile
|
||||||
False
|
import unittest
|
||||||
>>> db_session.get('cat')
|
|
||||||
>>> db_session['cat'] = "dog"
|
|
||||||
>>> db_session.modified
|
|
||||||
True
|
|
||||||
>>> db_session.pop('cat')
|
|
||||||
'dog'
|
|
||||||
>>> db_session.pop('some key', 'does not exist')
|
|
||||||
'does not exist'
|
|
||||||
>>> db_session.save()
|
|
||||||
>>> db_session.exists(db_session.session_key)
|
|
||||||
True
|
|
||||||
>>> db_session.delete(db_session.session_key)
|
|
||||||
>>> db_session.exists(db_session.session_key)
|
|
||||||
False
|
|
||||||
|
|
||||||
>>> db_session['foo'] = 'bar'
|
|
||||||
>>> db_session.save()
|
|
||||||
>>> db_session.exists(db_session.session_key)
|
|
||||||
True
|
|
||||||
>>> prev_key = db_session.session_key
|
|
||||||
>>> db_session.flush()
|
|
||||||
>>> db_session.exists(prev_key)
|
|
||||||
False
|
|
||||||
>>> db_session.session_key == prev_key
|
|
||||||
False
|
|
||||||
>>> db_session.modified, db_session.accessed
|
|
||||||
(True, True)
|
|
||||||
>>> db_session['a'], db_session['b'] = 'c', 'd'
|
|
||||||
>>> db_session.save()
|
|
||||||
>>> prev_key = db_session.session_key
|
|
||||||
>>> prev_data = db_session.items()
|
|
||||||
>>> db_session.cycle_key()
|
|
||||||
>>> db_session.session_key == prev_key
|
|
||||||
False
|
|
||||||
>>> db_session.items() == prev_data
|
|
||||||
True
|
|
||||||
|
|
||||||
# Submitting an invalid session key (either by guessing, or if the db has
|
|
||||||
# removed the key) results in a new key being generated.
|
|
||||||
>>> Session.objects.filter(pk=db_session.session_key).delete()
|
|
||||||
>>> db_session = DatabaseSession(db_session.session_key)
|
|
||||||
>>> db_session.save()
|
|
||||||
>>> DatabaseSession('1').get('cat')
|
|
||||||
|
|
||||||
#
|
|
||||||
# Cached DB session tests
|
|
||||||
#
|
|
||||||
|
|
||||||
>>> cdb_session = CacheDBSession()
|
|
||||||
>>> cdb_session.modified
|
|
||||||
False
|
|
||||||
>>> cdb_session['cat'] = "dog"
|
|
||||||
>>> cdb_session.modified
|
|
||||||
True
|
|
||||||
>>> cdb_session.pop('cat')
|
|
||||||
'dog'
|
|
||||||
>>> cdb_session.pop('some key', 'does not exist')
|
|
||||||
'does not exist'
|
|
||||||
>>> cdb_session.save()
|
|
||||||
>>> cdb_session.exists(cdb_session.session_key)
|
|
||||||
True
|
|
||||||
>>> cdb_session.delete(cdb_session.session_key)
|
|
||||||
>>> cdb_session.exists(cdb_session.session_key)
|
|
||||||
False
|
|
||||||
|
|
||||||
#
|
|
||||||
# File session tests.
|
|
||||||
#
|
|
||||||
|
|
||||||
# Do file session tests in an isolated directory, and kill it after we're done.
|
|
||||||
>>> original_session_file_path = settings.SESSION_FILE_PATH
|
|
||||||
>>> import tempfile
|
|
||||||
>>> temp_session_store = settings.SESSION_FILE_PATH = tempfile.mkdtemp()
|
|
||||||
|
|
||||||
>>> file_session = FileSession()
|
|
||||||
>>> file_session.modified
|
|
||||||
False
|
|
||||||
>>> file_session['cat'] = "dog"
|
|
||||||
>>> file_session.modified
|
|
||||||
True
|
|
||||||
>>> file_session.pop('cat')
|
|
||||||
'dog'
|
|
||||||
>>> file_session.pop('some key', 'does not exist')
|
|
||||||
'does not exist'
|
|
||||||
>>> file_session.save()
|
|
||||||
>>> file_session.exists(file_session.session_key)
|
|
||||||
True
|
|
||||||
>>> file_session.delete(file_session.session_key)
|
|
||||||
>>> file_session.exists(file_session.session_key)
|
|
||||||
False
|
|
||||||
>>> FileSession('1').get('cat')
|
|
||||||
|
|
||||||
>>> file_session['foo'] = 'bar'
|
|
||||||
>>> file_session.save()
|
|
||||||
>>> file_session.exists(file_session.session_key)
|
|
||||||
True
|
|
||||||
>>> prev_key = file_session.session_key
|
|
||||||
>>> file_session.flush()
|
|
||||||
>>> file_session.exists(prev_key)
|
|
||||||
False
|
|
||||||
>>> file_session.session_key == prev_key
|
|
||||||
False
|
|
||||||
>>> file_session.modified, file_session.accessed
|
|
||||||
(True, True)
|
|
||||||
>>> file_session['a'], file_session['b'] = 'c', 'd'
|
|
||||||
>>> file_session.save()
|
|
||||||
>>> prev_key = file_session.session_key
|
|
||||||
>>> prev_data = file_session.items()
|
|
||||||
>>> file_session.cycle_key()
|
|
||||||
>>> file_session.session_key == prev_key
|
|
||||||
False
|
|
||||||
>>> file_session.items() == prev_data
|
|
||||||
True
|
|
||||||
|
|
||||||
>>> Session.objects.filter(pk=file_session.session_key).delete()
|
|
||||||
>>> file_session = FileSession(file_session.session_key)
|
|
||||||
>>> file_session.save()
|
|
||||||
|
|
||||||
# Make sure the file backend checks for a good storage dir
|
|
||||||
>>> settings.SESSION_FILE_PATH = "/if/this/directory/exists/you/have/a/weird/computer"
|
|
||||||
>>> FileSession()
|
|
||||||
Traceback (innermost last):
|
|
||||||
...
|
|
||||||
ImproperlyConfigured: The session storage path '/if/this/directory/exists/you/have/a/weird/computer' doesn't exist. Please set your SESSION_FILE_PATH setting to an existing directory in which Django can store session data.
|
|
||||||
|
|
||||||
# Clean up after the file tests
|
|
||||||
>>> settings.SESSION_FILE_PATH = original_session_file_path
|
|
||||||
>>> import shutil
|
|
||||||
>>> shutil.rmtree(temp_session_store)
|
|
||||||
|
|
||||||
#
|
|
||||||
# Cache-based tests
|
|
||||||
# NB: be careful to delete any sessions created; stale sessions fill up the
|
|
||||||
# /tmp and eventually overwhelm it after lots of runs (think buildbots)
|
|
||||||
#
|
|
||||||
|
|
||||||
>>> cache_session = CacheSession()
|
|
||||||
>>> cache_session.modified
|
|
||||||
False
|
|
||||||
>>> cache_session['cat'] = "dog"
|
|
||||||
>>> cache_session.modified
|
|
||||||
True
|
|
||||||
>>> cache_session.pop('cat')
|
|
||||||
'dog'
|
|
||||||
>>> cache_session.pop('some key', 'does not exist')
|
|
||||||
'does not exist'
|
|
||||||
>>> cache_session.save()
|
|
||||||
>>> cache_session.delete(cache_session.session_key)
|
|
||||||
>>> cache_session.exists(cache_session.session_key)
|
|
||||||
False
|
|
||||||
>>> cache_session['foo'] = 'bar'
|
|
||||||
>>> cache_session.save()
|
|
||||||
>>> cache_session.exists(cache_session.session_key)
|
|
||||||
True
|
|
||||||
>>> prev_key = cache_session.session_key
|
|
||||||
>>> cache_session.flush()
|
|
||||||
>>> cache_session.exists(prev_key)
|
|
||||||
False
|
|
||||||
>>> cache_session.session_key == prev_key
|
|
||||||
False
|
|
||||||
>>> cache_session.modified, cache_session.accessed
|
|
||||||
(True, True)
|
|
||||||
>>> cache_session['a'], cache_session['b'] = 'c', 'd'
|
|
||||||
>>> cache_session.save()
|
|
||||||
>>> prev_key = cache_session.session_key
|
|
||||||
>>> prev_data = cache_session.items()
|
|
||||||
>>> cache_session.cycle_key()
|
|
||||||
>>> cache_session.session_key == prev_key
|
|
||||||
False
|
|
||||||
>>> cache_session.items() == prev_data
|
|
||||||
True
|
|
||||||
>>> cache_session = CacheSession()
|
|
||||||
>>> cache_session.save()
|
|
||||||
>>> key = cache_session.session_key
|
|
||||||
>>> cache_session.exists(key)
|
|
||||||
True
|
|
||||||
|
|
||||||
>>> Session.objects.filter(pk=cache_session.session_key).delete()
|
|
||||||
>>> cache_session = CacheSession(cache_session.session_key)
|
|
||||||
>>> cache_session.save()
|
|
||||||
>>> cache_session.delete(cache_session.session_key)
|
|
||||||
|
|
||||||
>>> s = SessionBase()
|
|
||||||
>>> s._session['some key'] = 'exists' # Pre-populate the session with some data
|
|
||||||
>>> s.accessed = False # Reset to pretend this wasn't accessed previously
|
|
||||||
|
|
||||||
>>> s.accessed, s.modified
|
|
||||||
(False, False)
|
|
||||||
|
|
||||||
>>> s.pop('non existant key', 'does not exist')
|
|
||||||
'does not exist'
|
|
||||||
>>> s.accessed, s.modified
|
|
||||||
(True, False)
|
|
||||||
|
|
||||||
>>> s.setdefault('foo', 'bar')
|
|
||||||
'bar'
|
|
||||||
>>> s.setdefault('foo', 'baz')
|
|
||||||
'bar'
|
|
||||||
|
|
||||||
>>> s.accessed = False # Reset the accessed flag
|
|
||||||
|
|
||||||
>>> s.pop('some key')
|
|
||||||
'exists'
|
|
||||||
>>> s.accessed, s.modified
|
|
||||||
(True, True)
|
|
||||||
|
|
||||||
>>> s.pop('some key', 'does not exist')
|
|
||||||
'does not exist'
|
|
||||||
|
|
||||||
|
|
||||||
>>> s.get('update key', None)
|
class SessionTestsMixin(object):
|
||||||
|
# This does not inherit from TestCase to avoid any tests being run with this
|
||||||
|
# class, which wouldn't work, and to allow different TestCase subclasses to
|
||||||
|
# be used.
|
||||||
|
|
||||||
# test .update()
|
backend = None # subclasses must specify
|
||||||
>>> s.modified = s.accessed = False # Reset to pretend this wasn't accessed previously
|
|
||||||
>>> s.update({'update key':1})
|
|
||||||
>>> s.accessed, s.modified
|
|
||||||
(True, True)
|
|
||||||
>>> s.get('update key', None)
|
|
||||||
1
|
|
||||||
|
|
||||||
# test .has_key()
|
def setUp(self):
|
||||||
>>> s.modified = s.accessed = False # Reset to pretend this wasn't accessed previously
|
self.session = self.backend()
|
||||||
>>> s.has_key('update key')
|
|
||||||
True
|
|
||||||
>>> s.accessed, s.modified
|
|
||||||
(True, False)
|
|
||||||
|
|
||||||
# test .values()
|
def tearDown(self):
|
||||||
>>> s = SessionBase()
|
# NB: be careful to delete any sessions created; stale sessions fill up
|
||||||
>>> s.values()
|
# the /tmp (with some backends) and eventually overwhelm it after lots
|
||||||
[]
|
# of runs (think buildbots)
|
||||||
>>> s.accessed
|
self.session.delete()
|
||||||
True
|
|
||||||
>>> s['x'] = 1
|
|
||||||
>>> s.values()
|
|
||||||
[1]
|
|
||||||
|
|
||||||
# test .iterkeys()
|
def test_new_session(self):
|
||||||
>>> s.accessed = False
|
self.assertFalse(self.session.modified)
|
||||||
>>> i = s.iterkeys()
|
self.assertFalse(self.session.accessed)
|
||||||
>>> hasattr(i,'__iter__')
|
|
||||||
True
|
|
||||||
>>> s.accessed
|
|
||||||
True
|
|
||||||
>>> list(i)
|
|
||||||
['x']
|
|
||||||
|
|
||||||
# test .itervalues()
|
def test_get_empty(self):
|
||||||
>>> s.accessed = False
|
self.assertEqual(self.session.get('cat'), None)
|
||||||
>>> i = s.itervalues()
|
|
||||||
>>> hasattr(i,'__iter__')
|
|
||||||
True
|
|
||||||
>>> s.accessed
|
|
||||||
True
|
|
||||||
>>> list(i)
|
|
||||||
[1]
|
|
||||||
|
|
||||||
# test .iteritems()
|
def test_store(self):
|
||||||
>>> s.accessed = False
|
self.session['cat'] = "dog"
|
||||||
>>> i = s.iteritems()
|
self.assertTrue(self.session.modified)
|
||||||
>>> hasattr(i,'__iter__')
|
self.assertEqual(self.session.pop('cat'), 'dog')
|
||||||
True
|
|
||||||
>>> s.accessed
|
|
||||||
True
|
|
||||||
>>> list(i)
|
|
||||||
[('x', 1)]
|
|
||||||
|
|
||||||
# test .clear()
|
def test_pop(self):
|
||||||
>>> s.modified = s.accessed = False
|
self.session['some key'] = 'exists'
|
||||||
>>> s.items()
|
# Need to reset these to pretend we haven't accessed it:
|
||||||
[('x', 1)]
|
self.accessed = False
|
||||||
>>> s.clear()
|
self.modified = False
|
||||||
>>> s.items()
|
|
||||||
[]
|
|
||||||
>>> s.accessed, s.modified
|
|
||||||
(True, True)
|
|
||||||
|
|
||||||
#########################
|
self.assertEqual(self.session.pop('some key'), 'exists')
|
||||||
# Custom session expiry #
|
self.assertTrue(self.session.accessed)
|
||||||
#########################
|
self.assertTrue(self.session.modified)
|
||||||
|
self.assertEqual(self.session.get('some key'), None)
|
||||||
|
|
||||||
>>> from django.conf import settings
|
def test_pop_default(self):
|
||||||
>>> from datetime import datetime, timedelta
|
self.assertEqual(self.session.pop('some key', 'does not exist'),
|
||||||
|
'does not exist')
|
||||||
|
self.assertTrue(self.session.accessed)
|
||||||
|
self.assertFalse(self.session.modified)
|
||||||
|
|
||||||
>>> td10 = timedelta(seconds=10)
|
def test_setdefault(self):
|
||||||
|
self.assertEqual(self.session.setdefault('foo', 'bar'), 'bar')
|
||||||
|
self.assertEqual(self.session.setdefault('foo', 'baz'), 'bar')
|
||||||
|
self.assertTrue(self.session.accessed)
|
||||||
|
self.assertTrue(self.session.modified)
|
||||||
|
|
||||||
# A normal session has a max age equal to settings
|
def test_update(self):
|
||||||
>>> s.get_expiry_age() == settings.SESSION_COOKIE_AGE
|
self.session.update({'update key': 1})
|
||||||
True
|
self.assertTrue(self.session.accessed)
|
||||||
|
self.assertTrue(self.session.modified)
|
||||||
|
self.assertEqual(self.session.get('update key', None), 1)
|
||||||
|
|
||||||
# So does a custom session with an idle expiration time of 0 (but it'll expire
|
def test_has_key(self):
|
||||||
# at browser close)
|
self.session['some key'] = 1
|
||||||
>>> s.set_expiry(0)
|
self.session.modified = False
|
||||||
>>> s.get_expiry_age() == settings.SESSION_COOKIE_AGE
|
self.session.accessed = False
|
||||||
True
|
self.assertTrue(self.session.has_key('some key'))
|
||||||
|
self.assertTrue(self.session.accessed)
|
||||||
|
self.assertFalse(self.session.modified)
|
||||||
|
|
||||||
# Custom session idle expiration time
|
def test_values(self):
|
||||||
>>> s.set_expiry(10)
|
self.assertEqual(self.session.values(), [])
|
||||||
>>> delta = s.get_expiry_date() - datetime.now()
|
self.assertTrue(self.session.accessed)
|
||||||
>>> delta.seconds in (9, 10)
|
self.session['some key'] = 1
|
||||||
True
|
self.assertEqual(self.session.values(), [1])
|
||||||
>>> age = s.get_expiry_age()
|
|
||||||
>>> age in (9, 10)
|
|
||||||
True
|
|
||||||
|
|
||||||
# Custom session fixed expiry date (timedelta)
|
def test_iterkeys(self):
|
||||||
>>> s.set_expiry(td10)
|
self.session['x'] = 1
|
||||||
>>> delta = s.get_expiry_date() - datetime.now()
|
self.session.modified = False
|
||||||
>>> delta.seconds in (9, 10)
|
self.session.accessed = False
|
||||||
True
|
i = self.session.iterkeys()
|
||||||
>>> age = s.get_expiry_age()
|
self.assertTrue(hasattr(i, '__iter__'))
|
||||||
>>> age in (9, 10)
|
self.assertTrue(self.session.accessed)
|
||||||
True
|
self.assertFalse(self.session.modified)
|
||||||
|
self.assertEqual(list(i), ['x'])
|
||||||
|
|
||||||
# Custom session fixed expiry date (fixed datetime)
|
def test_iterkeys(self):
|
||||||
>>> s.set_expiry(datetime.now() + td10)
|
self.session['x'] = 1
|
||||||
>>> delta = s.get_expiry_date() - datetime.now()
|
self.session.modified = False
|
||||||
>>> delta.seconds in (9, 10)
|
self.session.accessed = False
|
||||||
True
|
i = self.session.itervalues()
|
||||||
>>> age = s.get_expiry_age()
|
self.assertTrue(hasattr(i, '__iter__'))
|
||||||
>>> age in (9, 10)
|
self.assertTrue(self.session.accessed)
|
||||||
True
|
self.assertFalse(self.session.modified)
|
||||||
|
self.assertEqual(list(i), [1])
|
||||||
|
|
||||||
# Set back to default session age
|
def test_iteritems(self):
|
||||||
>>> s.set_expiry(None)
|
self.session['x'] = 1
|
||||||
>>> s.get_expiry_age() == settings.SESSION_COOKIE_AGE
|
self.session.modified = False
|
||||||
True
|
self.session.accessed = False
|
||||||
|
i = self.session.iteritems()
|
||||||
|
self.assertTrue(hasattr(i, '__iter__'))
|
||||||
|
self.assertTrue(self.session.accessed)
|
||||||
|
self.assertFalse(self.session.modified)
|
||||||
|
self.assertEqual(list(i), [('x',1)])
|
||||||
|
|
||||||
# Allow to set back to default session age even if no alternate has been set
|
def test_clear(self):
|
||||||
>>> s.set_expiry(None)
|
self.session['x'] = 1
|
||||||
|
self.session.modified = False
|
||||||
|
self.session.accessed = False
|
||||||
|
self.assertEqual(self.session.items(), [('x',1)])
|
||||||
|
self.session.clear()
|
||||||
|
self.assertEqual(self.session.items(), [])
|
||||||
|
self.assertTrue(self.session.accessed)
|
||||||
|
self.assertTrue(self.session.modified)
|
||||||
|
|
||||||
|
def test_save(self):
|
||||||
|
self.session.save()
|
||||||
|
self.assertTrue(self.session.exists(self.session.session_key))
|
||||||
|
|
||||||
|
def test_delete(self):
|
||||||
|
self.session.delete(self.session.session_key)
|
||||||
|
self.assertFalse(self.session.exists(self.session.session_key))
|
||||||
|
|
||||||
|
def test_flush(self):
|
||||||
|
self.session['foo'] = 'bar'
|
||||||
|
self.session.save()
|
||||||
|
prev_key = self.session.session_key
|
||||||
|
self.session.flush()
|
||||||
|
self.assertFalse(self.session.exists(prev_key))
|
||||||
|
self.assertNotEqual(self.session.session_key, prev_key)
|
||||||
|
self.assertTrue(self.session.modified)
|
||||||
|
self.assertTrue(self.session.accessed)
|
||||||
|
|
||||||
|
def test_cycle(self):
|
||||||
|
self.session['a'], self.session['b'] = 'c', 'd'
|
||||||
|
self.session.save()
|
||||||
|
prev_key = self.session.session_key
|
||||||
|
prev_data = self.session.items()
|
||||||
|
self.session.cycle_key()
|
||||||
|
self.assertNotEqual(self.session.session_key, prev_key)
|
||||||
|
self.assertEqual(self.session.items(), prev_data)
|
||||||
|
|
||||||
|
def test_invalid_key(self):
|
||||||
|
# Submitting an invalid session key (either by guessing, or if the db has
|
||||||
|
# removed the key) results in a new key being generated.
|
||||||
|
session = self.backend('1')
|
||||||
|
session.save()
|
||||||
|
self.assertNotEqual(session.session_key, '1')
|
||||||
|
self.assertEqual(session.get('cat'), None)
|
||||||
|
session.delete()
|
||||||
|
|
||||||
|
# Custom session expiry
|
||||||
|
def test_default_expiry(self):
|
||||||
|
# A normal session has a max age equal to settings
|
||||||
|
self.assertEqual(self.session.get_expiry_age(), settings.SESSION_COOKIE_AGE)
|
||||||
|
|
||||||
|
# So does a custom session with an idle expiration time of 0 (but it'll
|
||||||
|
# expire at browser close)
|
||||||
|
self.session.set_expiry(0)
|
||||||
|
self.assertEqual(self.session.get_expiry_age(), settings.SESSION_COOKIE_AGE)
|
||||||
|
|
||||||
|
def test_custom_expiry_seconds(self):
|
||||||
|
# Using seconds
|
||||||
|
self.session.set_expiry(10)
|
||||||
|
delta = self.session.get_expiry_date() - datetime.now()
|
||||||
|
self.assertTrue(delta.seconds in (9, 10))
|
||||||
|
|
||||||
|
age = self.session.get_expiry_age()
|
||||||
|
self.assertTrue(age in (9, 10))
|
||||||
|
|
||||||
|
def test_custom_expiry_timedelta(self):
|
||||||
|
# Using timedelta
|
||||||
|
self.session.set_expiry(timedelta(seconds=10))
|
||||||
|
delta = self.session.get_expiry_date() - datetime.now()
|
||||||
|
self.assertTrue(delta.seconds in (9, 10))
|
||||||
|
|
||||||
|
age = self.session.get_expiry_age()
|
||||||
|
self.assertTrue(age in (9, 10))
|
||||||
|
|
||||||
|
def test_custom_expiry_timedelta(self):
|
||||||
|
# Using timedelta
|
||||||
|
self.session.set_expiry(datetime.now() + timedelta(seconds=10))
|
||||||
|
delta = self.session.get_expiry_date() - datetime.now()
|
||||||
|
self.assertTrue(delta.seconds in (9, 10))
|
||||||
|
|
||||||
|
age = self.session.get_expiry_age()
|
||||||
|
self.assertTrue(age in (9, 10))
|
||||||
|
|
||||||
|
def test_custom_expiry_reset(self):
|
||||||
|
self.session.set_expiry(None)
|
||||||
|
self.session.set_expiry(10)
|
||||||
|
self.session.set_expiry(None)
|
||||||
|
self.assertEqual(self.session.get_expiry_age(), settings.SESSION_COOKIE_AGE)
|
||||||
|
|
||||||
|
def test_get_expire_at_browser_close(self):
|
||||||
|
# Tests get_expire_at_browser_close with different settings and different
|
||||||
|
# set_expiry calls
|
||||||
|
try:
|
||||||
|
original_expire_at_browser_close = settings.SESSION_EXPIRE_AT_BROWSER_CLOSE
|
||||||
|
settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = False
|
||||||
|
|
||||||
|
self.session.set_expiry(10)
|
||||||
|
self.assertFalse(self.session.get_expire_at_browser_close())
|
||||||
|
|
||||||
|
self.session.set_expiry(0)
|
||||||
|
self.assertTrue(self.session.get_expire_at_browser_close())
|
||||||
|
|
||||||
|
self.session.set_expiry(None)
|
||||||
|
self.assertFalse(self.session.get_expire_at_browser_close())
|
||||||
|
|
||||||
|
settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = True
|
||||||
|
|
||||||
|
self.session.set_expiry(10)
|
||||||
|
self.assertFalse(self.session.get_expire_at_browser_close())
|
||||||
|
|
||||||
|
self.session.set_expiry(0)
|
||||||
|
self.assertTrue(self.session.get_expire_at_browser_close())
|
||||||
|
|
||||||
|
self.session.set_expiry(None)
|
||||||
|
self.assertTrue(self.session.get_expire_at_browser_close())
|
||||||
|
|
||||||
|
except:
|
||||||
|
raise
|
||||||
|
|
||||||
|
finally:
|
||||||
|
settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = original_expire_at_browser_close
|
||||||
|
|
||||||
|
|
||||||
# We're changing the setting then reverting back to the original setting at the
|
class DatabaseSessionTests(SessionTestsMixin, TestCase):
|
||||||
# end of these tests.
|
|
||||||
>>> original_expire_at_browser_close = settings.SESSION_EXPIRE_AT_BROWSER_CLOSE
|
|
||||||
>>> settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = False
|
|
||||||
|
|
||||||
# Custom session age
|
backend = DatabaseSession
|
||||||
>>> s.set_expiry(10)
|
|
||||||
>>> s.get_expire_at_browser_close()
|
|
||||||
False
|
|
||||||
|
|
||||||
# Custom expire-at-browser-close
|
|
||||||
>>> s.set_expiry(0)
|
|
||||||
>>> s.get_expire_at_browser_close()
|
|
||||||
True
|
|
||||||
|
|
||||||
# Default session age
|
class CacheDBSessionTests(SessionTestsMixin, TestCase):
|
||||||
>>> s.set_expiry(None)
|
|
||||||
>>> s.get_expire_at_browser_close()
|
|
||||||
False
|
|
||||||
|
|
||||||
>>> settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = True
|
backend = CacheDBSession
|
||||||
|
|
||||||
# Custom session age
|
# Don't need DB flushing for these tests, so can use unittest.TestCase as base class
|
||||||
>>> s.set_expiry(10)
|
class FileSessionTests(SessionTestsMixin, unittest.TestCase):
|
||||||
>>> s.get_expire_at_browser_close()
|
|
||||||
False
|
|
||||||
|
|
||||||
# Custom expire-at-browser-close
|
backend = FileSession
|
||||||
>>> s.set_expiry(0)
|
|
||||||
>>> s.get_expire_at_browser_close()
|
|
||||||
True
|
|
||||||
|
|
||||||
# Default session age
|
def setUp(self):
|
||||||
>>> s.set_expiry(None)
|
super(FileSessionTests, self).setUp()
|
||||||
>>> s.get_expire_at_browser_close()
|
# Do file session tests in an isolated directory, and kill it after we're done.
|
||||||
True
|
self.original_session_file_path = settings.SESSION_FILE_PATH
|
||||||
|
self.temp_session_store = settings.SESSION_FILE_PATH = tempfile.mkdtemp()
|
||||||
|
|
||||||
>>> settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = original_expire_at_browser_close
|
def tearDown(self):
|
||||||
"""
|
settings.SESSION_FILE_PATH = self.original_session_file_path
|
||||||
|
shutil.rmtree(self.temp_session_store)
|
||||||
|
super(FileSessionTests, self).tearDown()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
def test_configuration_check(self):
|
||||||
import doctest
|
# Make sure the file backend checks for a good storage dir
|
||||||
doctest.testmod()
|
settings.SESSION_FILE_PATH = "/if/this/directory/exists/you/have/a/weird/computer"
|
||||||
|
self.assertRaises(ImproperlyConfigured, self.backend)
|
||||||
|
|
||||||
|
|
||||||
|
class CacheSessionTests(SessionTestsMixin, unittest.TestCase):
|
||||||
|
|
||||||
|
backend = CacheSession
|
||||||
|
|
Loading…
Reference in New Issue