Fixes an issue where the organization home page would throw a 505 when no projects...
[Melange.git] / app / django / contrib / sessions / tests.py
blobcf6922449fa7cbf25340287c18eaf0cc72e15a21
1 r"""
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
12 False
13 >>> db_session.get('cat')
14 >>> db_session['cat'] = "dog"
15 >>> db_session.modified
16 True
17 >>> db_session.pop('cat')
18 'dog'
19 >>> db_session.pop('some key', 'does not exist')
20 'does not exist'
21 >>> db_session.save()
22 >>> db_session.exists(db_session.session_key)
23 True
24 >>> db_session.delete(db_session.session_key)
25 >>> db_session.exists(db_session.session_key)
26 False
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
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')
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()
62 >>> file_session = FileSession()
63 >>> file_session.modified
64 False
65 >>> file_session['cat'] = "dog"
66 >>> file_session.modified
67 True
68 >>> file_session.pop('cat')
69 'dog'
70 >>> file_session.pop('some key', 'does not exist')
71 'does not exist'
72 >>> file_session.save()
73 >>> file_session.exists(file_session.session_key)
74 True
75 >>> file_session.delete(file_session.session_key)
76 >>> file_session.exists(file_session.session_key)
77 False
78 >>> FileSession('1').get('cat')
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
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"
108 >>> FileSession()
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
115 >>> import shutil
116 >>> shutil.rmtree(temp_session_store)
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)
124 >>> cache_session = CacheSession()
125 >>> cache_session.modified
126 False
127 >>> cache_session['cat'] = "dog"
128 >>> cache_session.modified
129 True
130 >>> cache_session.pop('cat')
131 'dog'
132 >>> cache_session.pop('some key', 'does not exist')
133 'does not exist'
134 >>> cache_session.save()
135 >>> cache_session.delete(cache_session.session_key)
136 >>> cache_session.exists(cache_session.session_key)
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
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
170 (False, False)
172 >>> s.pop('non existant key', 'does not exist')
173 'does not exist'
174 >>> s.accessed, s.modified
175 (True, False)
177 >>> s.setdefault('foo', 'bar')
178 'bar'
179 >>> s.setdefault('foo', 'baz')
180 'bar'
182 >>> s.accessed = False # Reset the accessed flag
184 >>> s.pop('some key')
185 'exists'
186 >>> s.accessed, s.modified
187 (True, True)
189 >>> s.pop('some key', 'does not exist')
190 'does not exist'
193 >>> s.get('update key', None)
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)
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)
210 # test .values()
211 >>> s = SessionBase()
212 >>> s.values()
214 >>> s.accessed
215 True
216 >>> s['x'] = 1
217 >>> s.values()
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']
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)
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)]
250 # test .clear()
251 >>> s.modified = s.accessed = False
252 >>> s.items()
253 [('x', 1)]
254 >>> s.clear()
255 >>> s.items()
257 >>> s.accessed, s.modified
258 (True, True)
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
271 True
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
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
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
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
306 # Set back to default session age
307 >>> s.set_expiry(None)
308 >>> s.get_expiry_age() == settings.SESSION_COOKIE_AGE
309 True
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
320 # Custom session age
321 >>> s.set_expiry(10)
322 >>> s.get_expire_at_browser_close()
323 False
325 # Custom expire-at-browser-close
326 >>> s.set_expiry(0)
327 >>> s.get_expire_at_browser_close()
328 True
330 # Default session age
331 >>> s.set_expiry(None)
332 >>> s.get_expire_at_browser_close()
333 False
335 >>> settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = True
337 # Custom session age
338 >>> s.set_expiry(10)
339 >>> s.get_expire_at_browser_close()
340 False
342 # Custom expire-at-browser-close
343 >>> s.set_expiry(0)
344 >>> s.get_expire_at_browser_close()
345 True
347 # Default session age
348 >>> s.set_expiry(None)
349 >>> s.get_expire_at_browser_close()
350 True
352 >>> settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = original_expire_at_browser_close
355 if __name__ == '__main__':
356 import doctest
357 doctest.testmod()