3 >>> from django.conf import settings
4 >>> from django.contrib.sessions.backends.db import SessionStore as DatabaseSession
5 >>> from django.contrib.sessions.backends.cache import SessionStore as CacheSession
6 >>> from django.contrib.sessions.backends.file import SessionStore as FileSession
7 >>> from django.contrib.sessions.backends.base import SessionBase
8 >>> from django.contrib.sessions.models import Session
10 >>> db_session = DatabaseSession()
11 >>> db_session.modified
13 >>> db_session.get('cat')
14 >>> db_session['cat'] = "dog"
15 >>> db_session.modified
17 >>> db_session.pop('cat')
19 >>> db_session.pop('some key', 'does not exist')
22 >>> db_session.exists(db_session.session_key)
24 >>> db_session.delete(db_session.session_key)
25 >>> db_session.exists(db_session.session_key)
28 >>> db_session['foo'] = 'bar'
30 >>> db_session.exists(db_session.session_key)
32 >>> prev_key = db_session.session_key
33 >>> db_session.flush()
34 >>> db_session.exists(prev_key)
36 >>> db_session.session_key == prev_key
38 >>> db_session.modified, db_session.accessed
40 >>> db_session['a'], db_session['b'] = 'c', 'd'
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
47 >>> db_session.items() == prev_data
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)
55 >>> DatabaseSession('1').get('cat')
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
60 >>> temp_session_store = settings.SESSION_FILE_PATH = tempfile.mkdtemp()
62 >>> file_session = FileSession()
63 >>> file_session.modified
65 >>> file_session['cat'] = "dog"
66 >>> file_session.modified
68 >>> file_session.pop('cat')
70 >>> file_session.pop('some key', 'does not exist')
72 >>> file_session.save()
73 >>> file_session.exists(file_session.session_key)
75 >>> file_session.delete(file_session.session_key)
76 >>> file_session.exists(file_session.session_key)
78 >>> FileSession('1').get('cat')
80 >>> file_session['foo'] = 'bar'
81 >>> file_session.save()
82 >>> file_session.exists(file_session.session_key)
84 >>> prev_key = file_session.session_key
85 >>> file_session.flush()
86 >>> file_session.exists(prev_key)
88 >>> file_session.session_key == prev_key
90 >>> file_session.modified, file_session.accessed
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
99 >>> file_session.items() == prev_data
102 >>> Session.objects.filter(pk=file_session.session_key).delete()
103 >>> file_session = FileSession(file_session.session_key)
104 >>> file_session.save()
106 # Make sure the file backend checks for a good storage dir
107 >>> settings.SESSION_FILE_PATH = "/if/this/directory/exists/you/have/a/weird/computer"
109 Traceback (innermost last):
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.
113 # Clean up after the file tests
114 >>> settings.SESSION_FILE_PATH = original_session_file_path
116 >>> shutil.rmtree(temp_session_store)
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)
124 >>> cache_session = CacheSession()
125 >>> cache_session.modified
127 >>> cache_session['cat'] = "dog"
128 >>> cache_session.modified
130 >>> cache_session.pop('cat')
132 >>> cache_session.pop('some key', 'does not exist')
134 >>> cache_session.save()
135 >>> cache_session.delete(cache_session.session_key)
136 >>> cache_session.exists(cache_session.session_key)
138 >>> cache_session['foo'] = 'bar'
139 >>> cache_session.save()
140 >>> cache_session.exists(cache_session.session_key)
142 >>> prev_key = cache_session.session_key
143 >>> cache_session.flush()
144 >>> cache_session.exists(prev_key)
146 >>> cache_session.session_key == prev_key
148 >>> cache_session.modified, cache_session.accessed
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
157 >>> cache_session.items() == prev_data
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)
165 >>> s = SessionBase()
166 >>> s._session['some key'] = 'exists' # Pre-populate the session with some data
167 >>> s.accessed = False # Reset to pretend this wasn't accessed previously
169 >>> s.accessed, s.modified
172 >>> s.pop('non existant key', 'does not exist')
174 >>> s.accessed, s.modified
177 >>> s.setdefault('foo', 'bar')
179 >>> s.setdefault('foo', 'baz')
182 >>> s.accessed = False # Reset the accessed flag
184 >>> s.pop('some key')
186 >>> s.accessed, s.modified
189 >>> s.pop('some key', 'does not exist')
193 >>> s.get('update key', None)
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
200 >>> s.get('update key', None)
204 >>> s.modified = s.accessed = False # Reset to pretend this wasn't accessed previously
205 >>> s.has_key('update key')
207 >>> s.accessed, s.modified
211 >>> s = SessionBase()
221 >>> s.accessed = False
223 >>> hasattr(i,'__iter__')
231 >>> s.accessed = False
232 >>> i = s.itervalues()
233 >>> hasattr(i,'__iter__')
241 >>> s.accessed = False
242 >>> i = s.iteritems()
243 >>> hasattr(i,'__iter__')
251 >>> s.modified = s.accessed = False
257 >>> s.accessed, s.modified
260 #########################
261 # Custom session expiry #
262 #########################
264 >>> from django.conf import settings
265 >>> from datetime import datetime, timedelta
267 >>> td10 = timedelta(seconds=10)
269 # A normal session has a max age equal to settings
270 >>> s.get_expiry_age() == settings.SESSION_COOKIE_AGE
273 # So does a custom session with an idle expiration time of 0 (but it'll expire
276 >>> s.get_expiry_age() == settings.SESSION_COOKIE_AGE
279 # Custom session idle expiration time
281 >>> delta = s.get_expiry_date() - datetime.now()
282 >>> delta.seconds in (9, 10)
284 >>> age = s.get_expiry_age()
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)
293 >>> age = s.get_expiry_age()
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)
302 >>> age = s.get_expiry_age()
306 # Set back to default session age
307 >>> s.set_expiry(None)
308 >>> s.get_expiry_age() == settings.SESSION_COOKIE_AGE
311 # Allow to set back to default session age even if no alternate has been set
312 >>> s.set_expiry(None)
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
322 >>> s.get_expire_at_browser_close()
325 # Custom expire-at-browser-close
327 >>> s.get_expire_at_browser_close()
330 # Default session age
331 >>> s.set_expiry(None)
332 >>> s.get_expire_at_browser_close()
335 >>> settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = True
339 >>> s.get_expire_at_browser_close()
342 # Custom expire-at-browser-close
344 >>> s.get_expire_at_browser_close()
347 # Default session age
348 >>> s.set_expiry(None)
349 >>> s.get_expire_at_browser_close()
352 >>> settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = original_expire_at_browser_close
355 if __name__
== '__main__':