app/django/contrib/sessions/tests.py
changeset 323 ff1a9aa48cfd
parent 54 03e267d67478
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
     3 >>> from django.conf import settings
     3 >>> from django.conf import settings
     4 >>> from django.contrib.sessions.backends.db import SessionStore as DatabaseSession
     4 >>> from django.contrib.sessions.backends.db import SessionStore as DatabaseSession
     5 >>> from django.contrib.sessions.backends.cache import SessionStore as CacheSession
     5 >>> from django.contrib.sessions.backends.cache import SessionStore as CacheSession
     6 >>> from django.contrib.sessions.backends.file import SessionStore as FileSession
     6 >>> from django.contrib.sessions.backends.file import SessionStore as FileSession
     7 >>> from django.contrib.sessions.backends.base import SessionBase
     7 >>> from django.contrib.sessions.backends.base import SessionBase
       
     8 >>> from django.contrib.sessions.models import Session
     8 
     9 
     9 >>> db_session = DatabaseSession()
    10 >>> db_session = DatabaseSession()
    10 >>> db_session.modified
    11 >>> db_session.modified
    11 False
    12 False
       
    13 >>> db_session.get('cat')
    12 >>> db_session['cat'] = "dog"
    14 >>> db_session['cat'] = "dog"
    13 >>> db_session.modified
    15 >>> db_session.modified
    14 True
    16 True
    15 >>> db_session.pop('cat')
    17 >>> db_session.pop('cat')
    16 'dog'
    18 'dog'
    21 True
    23 True
    22 >>> db_session.delete(db_session.session_key)
    24 >>> db_session.delete(db_session.session_key)
    23 >>> db_session.exists(db_session.session_key)
    25 >>> db_session.exists(db_session.session_key)
    24 False
    26 False
    25 
    27 
       
    28 >>> db_session['foo'] = 'bar'
       
    29 >>> db_session.save()
       
    30 >>> db_session.exists(db_session.session_key)
       
    31 True
       
    32 >>> prev_key = db_session.session_key
       
    33 >>> db_session.flush()
       
    34 >>> db_session.exists(prev_key)
       
    35 False
       
    36 >>> db_session.session_key == prev_key
       
    37 False
       
    38 >>> db_session.modified, db_session.accessed
       
    39 (True, True)
       
    40 >>> db_session['a'], db_session['b'] = 'c', 'd'
       
    41 >>> db_session.save()
       
    42 >>> prev_key = db_session.session_key
       
    43 >>> prev_data = db_session.items()
       
    44 >>> db_session.cycle_key()
       
    45 >>> db_session.session_key == prev_key
       
    46 False
       
    47 >>> db_session.items() == prev_data
       
    48 True
       
    49 
       
    50 # Submitting an invalid session key (either by guessing, or if the db has
       
    51 # removed the key) results in a new key being generated.
       
    52 >>> Session.objects.filter(pk=db_session.session_key).delete()
       
    53 >>> db_session = DatabaseSession(db_session.session_key)
       
    54 >>> db_session.save()
       
    55 >>> DatabaseSession('1').get('cat')
       
    56 
       
    57 # Do file session tests in an isolated directory, and kill it after we're done.
       
    58 >>> original_session_file_path = settings.SESSION_FILE_PATH
       
    59 >>> import tempfile
       
    60 >>> temp_session_store = settings.SESSION_FILE_PATH = tempfile.mkdtemp()
       
    61 
    26 >>> file_session = FileSession()
    62 >>> file_session = FileSession()
    27 >>> file_session.modified
    63 >>> file_session.modified
    28 False
    64 False
    29 >>> file_session['cat'] = "dog"
    65 >>> file_session['cat'] = "dog"
    30 >>> file_session.modified
    66 >>> file_session.modified
    37 >>> file_session.exists(file_session.session_key)
    73 >>> file_session.exists(file_session.session_key)
    38 True
    74 True
    39 >>> file_session.delete(file_session.session_key)
    75 >>> file_session.delete(file_session.session_key)
    40 >>> file_session.exists(file_session.session_key)
    76 >>> file_session.exists(file_session.session_key)
    41 False
    77 False
       
    78 >>> FileSession('1').get('cat')
       
    79 
       
    80 >>> file_session['foo'] = 'bar'
       
    81 >>> file_session.save()
       
    82 >>> file_session.exists(file_session.session_key)
       
    83 True
       
    84 >>> prev_key = file_session.session_key
       
    85 >>> file_session.flush()
       
    86 >>> file_session.exists(prev_key)
       
    87 False
       
    88 >>> file_session.session_key == prev_key
       
    89 False
       
    90 >>> file_session.modified, file_session.accessed
       
    91 (True, True)
       
    92 >>> file_session['a'], file_session['b'] = 'c', 'd'
       
    93 >>> file_session.save()
       
    94 >>> prev_key = file_session.session_key
       
    95 >>> prev_data = file_session.items()
       
    96 >>> file_session.cycle_key()
       
    97 >>> file_session.session_key == prev_key
       
    98 False
       
    99 >>> file_session.items() == prev_data
       
   100 True
       
   101 
       
   102 >>> Session.objects.filter(pk=file_session.session_key).delete()
       
   103 >>> file_session = FileSession(file_session.session_key)
       
   104 >>> file_session.save()
    42 
   105 
    43 # Make sure the file backend checks for a good storage dir
   106 # Make sure the file backend checks for a good storage dir
    44 >>> settings.SESSION_FILE_PATH = "/if/this/directory/exists/you/have/a/weird/computer"
   107 >>> settings.SESSION_FILE_PATH = "/if/this/directory/exists/you/have/a/weird/computer"
    45 >>> FileSession()
   108 >>> FileSession()
    46 Traceback (innermost last):
   109 Traceback (innermost last):
    47     ...
   110     ...
    48 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.
   111 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.
    49 
   112 
       
   113 # Clean up after the file tests
       
   114 >>> settings.SESSION_FILE_PATH = original_session_file_path
       
   115 >>> import shutil
       
   116 >>> shutil.rmtree(temp_session_store)
       
   117 
       
   118 #
       
   119 # Cache-based tests
       
   120 # NB: be careful to delete any sessions created; stale sessions fill up the
       
   121 # /tmp and eventually overwhelm it after lots of runs (think buildbots)
       
   122 #
       
   123 
    50 >>> cache_session = CacheSession()
   124 >>> cache_session = CacheSession()
    51 >>> cache_session.modified
   125 >>> cache_session.modified
    52 False
   126 False
    53 >>> cache_session['cat'] = "dog"
   127 >>> cache_session['cat'] = "dog"
    54 >>> cache_session.modified
   128 >>> cache_session.modified
    59 'does not exist'
   133 'does not exist'
    60 >>> cache_session.save()
   134 >>> cache_session.save()
    61 >>> cache_session.delete(cache_session.session_key)
   135 >>> cache_session.delete(cache_session.session_key)
    62 >>> cache_session.exists(cache_session.session_key)
   136 >>> cache_session.exists(cache_session.session_key)
    63 False
   137 False
       
   138 >>> cache_session['foo'] = 'bar'
       
   139 >>> cache_session.save()
       
   140 >>> cache_session.exists(cache_session.session_key)
       
   141 True
       
   142 >>> prev_key = cache_session.session_key
       
   143 >>> cache_session.flush()
       
   144 >>> cache_session.exists(prev_key)
       
   145 False
       
   146 >>> cache_session.session_key == prev_key
       
   147 False
       
   148 >>> cache_session.modified, cache_session.accessed
       
   149 (True, True)
       
   150 >>> cache_session['a'], cache_session['b'] = 'c', 'd'
       
   151 >>> cache_session.save()
       
   152 >>> prev_key = cache_session.session_key
       
   153 >>> prev_data = cache_session.items()
       
   154 >>> cache_session.cycle_key()
       
   155 >>> cache_session.session_key == prev_key
       
   156 False
       
   157 >>> cache_session.items() == prev_data
       
   158 True
       
   159 
       
   160 >>> Session.objects.filter(pk=cache_session.session_key).delete()
       
   161 >>> cache_session = CacheSession(cache_session.session_key)
       
   162 >>> cache_session.save()
       
   163 >>> cache_session.delete(cache_session.session_key)
    64 
   164 
    65 >>> s = SessionBase()
   165 >>> s = SessionBase()
    66 >>> s._session['some key'] = 'exists' # Pre-populate the session with some data
   166 >>> s._session['some key'] = 'exists' # Pre-populate the session with some data
    67 >>> s.accessed = False   # Reset to pretend this wasn't accessed previously
   167 >>> s.accessed = False   # Reset to pretend this wasn't accessed previously
    68 
   168 
    86 >>> s.accessed, s.modified
   186 >>> s.accessed, s.modified
    87 (True, True)
   187 (True, True)
    88 
   188 
    89 >>> s.pop('some key', 'does not exist')
   189 >>> s.pop('some key', 'does not exist')
    90 'does not exist'
   190 'does not exist'
       
   191 
       
   192 
       
   193 >>> s.get('update key', None)
       
   194 
       
   195 # test .update()
       
   196 >>> s.modified = s.accessed = False   # Reset to pretend this wasn't accessed previously
       
   197 >>> s.update({'update key':1})
       
   198 >>> s.accessed, s.modified
       
   199 (True, True)
       
   200 >>> s.get('update key', None)
       
   201 1
       
   202 
       
   203 # test .has_key()
       
   204 >>> s.modified = s.accessed = False   # Reset to pretend this wasn't accessed previously
       
   205 >>> s.has_key('update key')
       
   206 True
       
   207 >>> s.accessed, s.modified
       
   208 (True, False)
       
   209 
       
   210 # test .values()
       
   211 >>> s = SessionBase()
       
   212 >>> s.values()
       
   213 []
       
   214 >>> s.accessed
       
   215 True
       
   216 >>> s['x'] = 1
       
   217 >>> s.values()
       
   218 [1]
       
   219 
       
   220 # test .iterkeys()
       
   221 >>> s.accessed = False
       
   222 >>> i = s.iterkeys()
       
   223 >>> hasattr(i,'__iter__')
       
   224 True
       
   225 >>> s.accessed
       
   226 True
       
   227 >>> list(i)
       
   228 ['x']
       
   229 
       
   230 # test .itervalues()
       
   231 >>> s.accessed = False
       
   232 >>> i = s.itervalues()
       
   233 >>> hasattr(i,'__iter__')
       
   234 True
       
   235 >>> s.accessed
       
   236 True
       
   237 >>> list(i)
       
   238 [1]
       
   239 
       
   240 # test .iteritems()
       
   241 >>> s.accessed = False
       
   242 >>> i = s.iteritems()
       
   243 >>> hasattr(i,'__iter__')
       
   244 True
       
   245 >>> s.accessed
       
   246 True
       
   247 >>> list(i)
       
   248 [('x', 1)]
       
   249 
       
   250 # test .clear()
       
   251 >>> s.modified = s.accessed = False
       
   252 >>> s.items()
       
   253 [('x', 1)]
       
   254 >>> s.clear()
       
   255 >>> s.items()
       
   256 []
       
   257 >>> s.accessed, s.modified
       
   258 (True, True)
       
   259 
       
   260 #########################
       
   261 # Custom session expiry #
       
   262 #########################
       
   263 
       
   264 >>> from django.conf import settings
       
   265 >>> from datetime import datetime, timedelta
       
   266 
       
   267 >>> td10 = timedelta(seconds=10)
       
   268 
       
   269 # A normal session has a max age equal to settings
       
   270 >>> s.get_expiry_age() == settings.SESSION_COOKIE_AGE
       
   271 True
       
   272 
       
   273 # So does a custom session with an idle expiration time of 0 (but it'll expire
       
   274 # at browser close)
       
   275 >>> s.set_expiry(0)
       
   276 >>> s.get_expiry_age() == settings.SESSION_COOKIE_AGE
       
   277 True
       
   278 
       
   279 # Custom session idle expiration time
       
   280 >>> s.set_expiry(10)
       
   281 >>> delta = s.get_expiry_date() - datetime.now()
       
   282 >>> delta.seconds in (9, 10)
       
   283 True
       
   284 >>> age = s.get_expiry_age()
       
   285 >>> age in (9, 10)
       
   286 True
       
   287 
       
   288 # Custom session fixed expiry date (timedelta)
       
   289 >>> s.set_expiry(td10)
       
   290 >>> delta = s.get_expiry_date() - datetime.now()
       
   291 >>> delta.seconds in (9, 10)
       
   292 True
       
   293 >>> age = s.get_expiry_age()
       
   294 >>> age in (9, 10)
       
   295 True
       
   296 
       
   297 # Custom session fixed expiry date (fixed datetime)
       
   298 >>> s.set_expiry(datetime.now() + td10)
       
   299 >>> delta = s.get_expiry_date() - datetime.now()
       
   300 >>> delta.seconds in (9, 10)
       
   301 True
       
   302 >>> age = s.get_expiry_age()
       
   303 >>> age in (9, 10)
       
   304 True
       
   305 
       
   306 # Set back to default session age
       
   307 >>> s.set_expiry(None)
       
   308 >>> s.get_expiry_age() == settings.SESSION_COOKIE_AGE
       
   309 True
       
   310 
       
   311 # Allow to set back to default session age even if no alternate has been set
       
   312 >>> s.set_expiry(None)
       
   313 
       
   314 
       
   315 # We're changing the setting then reverting back to the original setting at the
       
   316 # end of these tests.
       
   317 >>> original_expire_at_browser_close = settings.SESSION_EXPIRE_AT_BROWSER_CLOSE
       
   318 >>> settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = False
       
   319 
       
   320 # Custom session age
       
   321 >>> s.set_expiry(10)
       
   322 >>> s.get_expire_at_browser_close()
       
   323 False
       
   324 
       
   325 # Custom expire-at-browser-close
       
   326 >>> s.set_expiry(0)
       
   327 >>> s.get_expire_at_browser_close()
       
   328 True
       
   329 
       
   330 # Default session age
       
   331 >>> s.set_expiry(None)
       
   332 >>> s.get_expire_at_browser_close()
       
   333 False
       
   334 
       
   335 >>> settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = True
       
   336 
       
   337 # Custom session age
       
   338 >>> s.set_expiry(10)
       
   339 >>> s.get_expire_at_browser_close()
       
   340 False
       
   341 
       
   342 # Custom expire-at-browser-close
       
   343 >>> s.set_expiry(0)
       
   344 >>> s.get_expire_at_browser_close()
       
   345 True
       
   346 
       
   347 # Default session age
       
   348 >>> s.set_expiry(None)
       
   349 >>> s.get_expire_at_browser_close()
       
   350 True
       
   351 
       
   352 >>> settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = original_expire_at_browser_close
    91 """
   353 """
    92 
   354 
    93 if __name__ == '__main__':
   355 if __name__ == '__main__':
    94     import doctest
   356     import doctest
    95     doctest.testmod()
   357     doctest.testmod()