Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 5eece23296c481fc2f8b896b7d41e71bebe6114e 1 parent c5e83a9
Luke Plant authored August 05, 2010
2  django/contrib/sessions/backends/file.py
@@ -58,7 +58,7 @@ def load(self):
58 58
             finally:
59 59
                 session_file.close()
60 60
         except IOError:
61  
-            pass
  61
+            self.create()
62 62
         return session_data
63 63
 
64 64
     def create(self):
661  django/contrib/sessions/tests.py
... ...
@@ -1,388 +1,273 @@
1  
-r"""
2  
-
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.cached_db import SessionStore as CacheDBSession
7  
->>> from django.contrib.sessions.backends.file import SessionStore as FileSession
8  
->>> from django.contrib.sessions.backends.base import SessionBase
9  
->>> from django.contrib.sessions.models import Session
10  
-
11  
->>> db_session = DatabaseSession()
12  
->>> db_session.modified
13  
-False
14  
->>> db_session.get('cat')
15  
->>> db_session['cat'] = "dog"
16  
->>> db_session.modified
17  
-True
18  
->>> db_session.pop('cat')
19  
-'dog'
20  
->>> db_session.pop('some key', 'does not exist')
21  
-'does not exist'
22  
->>> db_session.save()
23  
->>> db_session.exists(db_session.session_key)
24  
-True
25  
->>> db_session.delete(db_session.session_key)
26  
->>> db_session.exists(db_session.session_key)
27  
-False
28  
-
29  
->>> db_session['foo'] = 'bar'
30  
->>> db_session.save()
31  
->>> db_session.exists(db_session.session_key)
32  
-True
33  
->>> prev_key = db_session.session_key
34  
->>> db_session.flush()
35  
->>> db_session.exists(prev_key)
36  
-False
37  
->>> db_session.session_key == prev_key
38  
-False
39  
->>> db_session.modified, db_session.accessed
40  
-(True, True)
41  
->>> db_session['a'], db_session['b'] = 'c', 'd'
42  
->>> db_session.save()
43  
->>> prev_key = db_session.session_key
44  
->>> prev_data = db_session.items()
45  
->>> db_session.cycle_key()
46  
->>> db_session.session_key == prev_key
47  
-False
48  
->>> db_session.items() == prev_data
49  
-True
50  
-
51  
-# Submitting an invalid session key (either by guessing, or if the db has
52  
-# removed the key) results in a new key being generated.
53  
->>> Session.objects.filter(pk=db_session.session_key).delete()
54  
->>> db_session = DatabaseSession(db_session.session_key)
55  
->>> db_session.save()
56  
->>> DatabaseSession('1').get('cat')
57  
-
58  
-#
59  
-# Cached DB session tests
60  
-#
61  
-
62  
->>> cdb_session = CacheDBSession()
63  
->>> cdb_session.modified
64  
-False
65  
->>> cdb_session['cat'] = "dog"
66  
->>> cdb_session.modified
67  
-True
68  
->>> cdb_session.pop('cat')
69  
-'dog'
70  
->>> cdb_session.pop('some key', 'does not exist')
71  
-'does not exist'
72  
->>> cdb_session.save()
73  
->>> cdb_session.exists(cdb_session.session_key)
74  
-True
75  
->>> cdb_session.delete(cdb_session.session_key)
76  
->>> cdb_session.exists(cdb_session.session_key)
77  
-False
78  
-
79  
-#
80  
-# File session tests.
81  
-#
82  
-
83  
-# Do file session tests in an isolated directory, and kill it after we're done.
84  
->>> original_session_file_path = settings.SESSION_FILE_PATH
85  
->>> import tempfile
86  
->>> temp_session_store = settings.SESSION_FILE_PATH = tempfile.mkdtemp()
87  
-
88  
->>> file_session = FileSession()
89  
->>> file_session.modified
90  
-False
91  
->>> file_session['cat'] = "dog"
92  
->>> file_session.modified
93  
-True
94  
->>> file_session.pop('cat')
95  
-'dog'
96  
->>> file_session.pop('some key', 'does not exist')
97  
-'does not exist'
98  
->>> file_session.save()
99  
->>> file_session.exists(file_session.session_key)
100  
-True
101  
->>> file_session.delete(file_session.session_key)
102  
->>> file_session.exists(file_session.session_key)
103  
-False
104  
->>> FileSession('1').get('cat')
105  
-
106  
->>> file_session['foo'] = 'bar'
107  
->>> file_session.save()
108  
->>> file_session.exists(file_session.session_key)
109  
-True
110  
->>> prev_key = file_session.session_key
111  
->>> file_session.flush()
112  
->>> file_session.exists(prev_key)
113  
-False
114  
->>> file_session.session_key == prev_key
115  
-False
116  
->>> file_session.modified, file_session.accessed
117  
-(True, True)
118  
->>> file_session['a'], file_session['b'] = 'c', 'd'
119  
->>> file_session.save()
120  
->>> prev_key = file_session.session_key
121  
->>> prev_data = file_session.items()
122  
->>> file_session.cycle_key()
123  
->>> file_session.session_key == prev_key
124  
-False
125  
->>> file_session.items() == prev_data
126  
-True
127  
-
128  
->>> Session.objects.filter(pk=file_session.session_key).delete()
129  
->>> file_session = FileSession(file_session.session_key)
130  
->>> file_session.save()
131  
-
132  
-# Make sure the file backend checks for a good storage dir
133  
->>> settings.SESSION_FILE_PATH = "/if/this/directory/exists/you/have/a/weird/computer"
134  
->>> FileSession()
135  
-Traceback (innermost last):
136  
-    ...
137  
-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.
138  
-
139  
-# Clean up after the file tests
140  
->>> settings.SESSION_FILE_PATH = original_session_file_path
141  
->>> import shutil
142  
->>> shutil.rmtree(temp_session_store)
143  
-
144  
-#
145  
-# Cache-based tests
146  
-# NB: be careful to delete any sessions created; stale sessions fill up the
147  
-# /tmp and eventually overwhelm it after lots of runs (think buildbots)
148  
-#
149  
-
150  
->>> cache_session = CacheSession()
151  
->>> cache_session.modified
152  
-False
153  
->>> cache_session['cat'] = "dog"
154  
->>> cache_session.modified
155  
-True
156  
->>> cache_session.pop('cat')
157  
-'dog'
158  
->>> cache_session.pop('some key', 'does not exist')
159  
-'does not exist'
160  
->>> cache_session.save()
161  
->>> cache_session.delete(cache_session.session_key)
162  
->>> cache_session.exists(cache_session.session_key)
163  
-False
164  
->>> cache_session['foo'] = 'bar'
165  
->>> cache_session.save()
166  
->>> cache_session.exists(cache_session.session_key)
167  
-True
168  
->>> prev_key = cache_session.session_key
169  
->>> cache_session.flush()
170  
->>> cache_session.exists(prev_key)
171  
-False
172  
->>> cache_session.session_key == prev_key
173  
-False
174  
->>> cache_session.modified, cache_session.accessed
175  
-(True, True)
176  
->>> cache_session['a'], cache_session['b'] = 'c', 'd'
177  
->>> cache_session.save()
178  
->>> prev_key = cache_session.session_key
179  
->>> prev_data = cache_session.items()
180  
->>> cache_session.cycle_key()
181  
->>> cache_session.session_key == prev_key
182  
-False
183  
->>> cache_session.items() == prev_data
184  
-True
185  
->>> cache_session = CacheSession()
186  
->>> cache_session.save()
187  
->>> key = cache_session.session_key
188  
->>> cache_session.exists(key)
189  
-True
190  
-
191  
->>> Session.objects.filter(pk=cache_session.session_key).delete()
192  
->>> cache_session = CacheSession(cache_session.session_key)
193  
->>> cache_session.save()
194  
->>> cache_session.delete(cache_session.session_key)
195  
-
196  
->>> s = SessionBase()
197  
->>> s._session['some key'] = 'exists' # Pre-populate the session with some data
198  
->>> s.accessed = False   # Reset to pretend this wasn't accessed previously
199  
-
200  
->>> s.accessed, s.modified
201  
-(False, False)
202  
-
203  
->>> s.pop('non existant key', 'does not exist')
204  
-'does not exist'
205  
->>> s.accessed, s.modified
206  
-(True, False)
207  
-
208  
->>> s.setdefault('foo', 'bar')
209  
-'bar'
210  
->>> s.setdefault('foo', 'baz')
211  
-'bar'
212  
-
213  
->>> s.accessed = False  # Reset the accessed flag
214  
-
215  
->>> s.pop('some key')
216  
-'exists'
217  
->>> s.accessed, s.modified
218  
-(True, True)
219  
-
220  
->>> s.pop('some key', 'does not exist')
221  
-'does not exist'
222  
-
223  
-
224  
->>> s.get('update key', None)
225  
-
226  
-# test .update()
227  
->>> s.modified = s.accessed = False   # Reset to pretend this wasn't accessed previously
228  
->>> s.update({'update key':1})
229  
->>> s.accessed, s.modified
230  
-(True, True)
231  
->>> s.get('update key', None)
232  
-1
233  
-
234  
-# test .has_key()
235  
->>> s.modified = s.accessed = False   # Reset to pretend this wasn't accessed previously
236  
->>> s.has_key('update key')
237  
-True
238  
->>> s.accessed, s.modified
239  
-(True, False)
240  
-
241  
-# test .values()
242  
->>> s = SessionBase()
243  
->>> s.values()
244  
-[]
245  
->>> s.accessed
246  
-True
247  
->>> s['x'] = 1
248  
->>> s.values()
249  
-[1]
250  
-
251  
-# test .iterkeys()
252  
->>> s.accessed = False
253  
->>> i = s.iterkeys()
254  
->>> hasattr(i,'__iter__')
255  
-True
256  
->>> s.accessed
257  
-True
258  
->>> list(i)
259  
-['x']
260  
-
261  
-# test .itervalues()
262  
->>> s.accessed = False
263  
->>> i = s.itervalues()
264  
->>> hasattr(i,'__iter__')
265  
-True
266  
->>> s.accessed
267  
-True
268  
->>> list(i)
269  
-[1]
270  
-
271  
-# test .iteritems()
272  
->>> s.accessed = False
273  
->>> i = s.iteritems()
274  
->>> hasattr(i,'__iter__')
275  
-True
276  
->>> s.accessed
277  
-True
278  
->>> list(i)
279  
-[('x', 1)]
280  
-
281  
-# test .clear()
282  
->>> s.modified = s.accessed = False
283  
->>> s.items()
284  
-[('x', 1)]
285  
->>> s.clear()
286  
->>> s.items()
287  
-[]
288  
->>> s.accessed, s.modified
289  
-(True, True)
290  
-
291  
-#########################
292  
-# Custom session expiry #
293  
-#########################
294  
-
295  
->>> from django.conf import settings
296  
->>> from datetime import datetime, timedelta
297  
-
298  
->>> td10 = timedelta(seconds=10)
299  
-
300  
-# A normal session has a max age equal to settings
301  
->>> s.get_expiry_age() == settings.SESSION_COOKIE_AGE
302  
-True
303  
-
304  
-# So does a custom session with an idle expiration time of 0 (but it'll expire
305  
-# at browser close)
306  
->>> s.set_expiry(0)
307  
->>> s.get_expiry_age() == settings.SESSION_COOKIE_AGE
308  
-True
309  
-
310  
-# Custom session idle expiration time
311  
->>> s.set_expiry(10)
312  
->>> delta = s.get_expiry_date() - datetime.now()
313  
->>> delta.seconds in (9, 10)
314  
-True
315  
->>> age = s.get_expiry_age()
316  
->>> age in (9, 10)
317  
-True
318  
-
319  
-# Custom session fixed expiry date (timedelta)
320  
->>> s.set_expiry(td10)
321  
->>> delta = s.get_expiry_date() - datetime.now()
322  
->>> delta.seconds in (9, 10)
323  
-True
324  
->>> age = s.get_expiry_age()
325  
->>> age in (9, 10)
326  
-True
327  
-
328  
-# Custom session fixed expiry date (fixed datetime)
329  
->>> s.set_expiry(datetime.now() + td10)
330  
->>> delta = s.get_expiry_date() - datetime.now()
331  
->>> delta.seconds in (9, 10)
332  
-True
333  
->>> age = s.get_expiry_age()
334  
->>> age in (9, 10)
335  
-True
336  
-
337  
-# Set back to default session age
338  
->>> s.set_expiry(None)
339  
->>> s.get_expiry_age() == settings.SESSION_COOKIE_AGE
340  
-True
341  
-
342  
-# Allow to set back to default session age even if no alternate has been set
343  
->>> s.set_expiry(None)
344  
-
345  
-
346  
-# We're changing the setting then reverting back to the original setting at the
347  
-# end of these tests.
348  
->>> original_expire_at_browser_close = settings.SESSION_EXPIRE_AT_BROWSER_CLOSE
349  
->>> settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = False
350  
-
351  
-# Custom session age
352  
->>> s.set_expiry(10)
353  
->>> s.get_expire_at_browser_close()
354  
-False
355  
-
356  
-# Custom expire-at-browser-close
357  
->>> s.set_expiry(0)
358  
->>> s.get_expire_at_browser_close()
359  
-True
360  
-
361  
-# Default session age
362  
->>> s.set_expiry(None)
363  
->>> s.get_expire_at_browser_close()
364  
-False
365  
-
366  
->>> settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = True
367  
-
368  
-# Custom session age
369  
->>> s.set_expiry(10)
370  
->>> s.get_expire_at_browser_close()
371  
-False
372  
-
373  
-# Custom expire-at-browser-close
374  
->>> s.set_expiry(0)
375  
->>> s.get_expire_at_browser_close()
376  
-True
377  
-
378  
-# Default session age
379  
->>> s.set_expiry(None)
380  
->>> s.get_expire_at_browser_close()
381  
-True
382  
-
383  
->>> settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = original_expire_at_browser_close
384  
-"""
385  
-
386  
-if __name__ == '__main__':
387  
-    import doctest
388  
-    doctest.testmod()
  1
+from datetime import datetime, timedelta
  2
+from django.conf import settings
  3
+from django.contrib.sessions.backends.db import SessionStore as DatabaseSession
  4
+from django.contrib.sessions.backends.cache import SessionStore as CacheSession
  5
+from django.contrib.sessions.backends.cached_db import SessionStore as CacheDBSession
  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
  9
+from django.core.exceptions import ImproperlyConfigured
  10
+from django.test import TestCase
  11
+import shutil
  12
+import tempfile
  13
+import unittest
  14
+
  15
+
  16
+class SessionTestsMixin(object):
  17
+    # This does not inherit from TestCase to avoid any tests being run with this
  18
+    # class, which wouldn't work, and to allow different TestCase subclasses to
  19
+    # be used.
  20
+
  21
+    backend = None # subclasses must specify
  22
+
  23
+    def setUp(self):
  24
+        self.session = self.backend()
  25
+
  26
+    def tearDown(self):
  27
+        # NB: be careful to delete any sessions created; stale sessions fill up
  28
+        # the /tmp (with some backends) and eventually overwhelm it after lots
  29
+        # of runs (think buildbots)
  30
+        self.session.delete()
  31
+
  32
+    def test_new_session(self):
  33
+        self.assertFalse(self.session.modified)
  34
+        self.assertFalse(self.session.accessed)
  35
+
  36
+    def test_get_empty(self):
  37
+        self.assertEqual(self.session.get('cat'), None)
  38
+
  39
+    def test_store(self):
  40
+        self.session['cat'] = "dog"
  41
+        self.assertTrue(self.session.modified)
  42
+        self.assertEqual(self.session.pop('cat'), 'dog')
  43
+
  44
+    def test_pop(self):
  45
+        self.session['some key'] = 'exists'
  46
+        # Need to reset these to pretend we haven't accessed it:
  47
+        self.accessed = False
  48
+        self.modified = False
  49
+
  50
+        self.assertEqual(self.session.pop('some key'), 'exists')
  51
+        self.assertTrue(self.session.accessed)
  52
+        self.assertTrue(self.session.modified)
  53
+        self.assertEqual(self.session.get('some key'), None)
  54
+
  55
+    def test_pop_default(self):
  56
+        self.assertEqual(self.session.pop('some key', 'does not exist'),
  57
+                         'does not exist')
  58
+        self.assertTrue(self.session.accessed)
  59
+        self.assertFalse(self.session.modified)
  60
+
  61
+    def test_setdefault(self):
  62
+        self.assertEqual(self.session.setdefault('foo', 'bar'), 'bar')
  63
+        self.assertEqual(self.session.setdefault('foo', 'baz'), 'bar')
  64
+        self.assertTrue(self.session.accessed)
  65
+        self.assertTrue(self.session.modified)
  66
+
  67
+    def test_update(self):
  68
+        self.session.update({'update key': 1})
  69
+        self.assertTrue(self.session.accessed)
  70
+        self.assertTrue(self.session.modified)
  71
+        self.assertEqual(self.session.get('update key', None), 1)
  72
+
  73
+    def test_has_key(self):
  74
+        self.session['some key'] = 1
  75
+        self.session.modified = False
  76
+        self.session.accessed = False
  77
+        self.assertTrue(self.session.has_key('some key'))
  78
+        self.assertTrue(self.session.accessed)
  79
+        self.assertFalse(self.session.modified)
  80
+
  81
+    def test_values(self):
  82
+        self.assertEqual(self.session.values(), [])
  83
+        self.assertTrue(self.session.accessed)
  84
+        self.session['some key'] = 1
  85
+        self.assertEqual(self.session.values(), [1])
  86
+
  87
+    def test_iterkeys(self):
  88
+        self.session['x'] = 1
  89
+        self.session.modified = False
  90
+        self.session.accessed = False
  91
+        i = self.session.iterkeys()
  92
+        self.assertTrue(hasattr(i, '__iter__'))
  93
+        self.assertTrue(self.session.accessed)
  94
+        self.assertFalse(self.session.modified)
  95
+        self.assertEqual(list(i), ['x'])
  96
+
  97
+    def test_iterkeys(self):
  98
+        self.session['x'] = 1
  99
+        self.session.modified = False
  100
+        self.session.accessed = False
  101
+        i = self.session.itervalues()
  102
+        self.assertTrue(hasattr(i, '__iter__'))
  103
+        self.assertTrue(self.session.accessed)
  104
+        self.assertFalse(self.session.modified)
  105
+        self.assertEqual(list(i), [1])
  106
+
  107
+    def test_iteritems(self):
  108
+        self.session['x'] = 1
  109
+        self.session.modified = False
  110
+        self.session.accessed = False
  111
+        i = self.session.iteritems()
  112
+        self.assertTrue(hasattr(i, '__iter__'))
  113
+        self.assertTrue(self.session.accessed)
  114
+        self.assertFalse(self.session.modified)
  115
+        self.assertEqual(list(i), [('x',1)])
  116
+
  117
+    def test_clear(self):
  118
+        self.session['x'] = 1
  119
+        self.session.modified = False
  120
+        self.session.accessed = False
  121
+        self.assertEqual(self.session.items(), [('x',1)])
  122
+        self.session.clear()
  123
+        self.assertEqual(self.session.items(), [])
  124
+        self.assertTrue(self.session.accessed)
  125
+        self.assertTrue(self.session.modified)
  126
+
  127
+    def test_save(self):
  128
+        self.session.save()
  129
+        self.assertTrue(self.session.exists(self.session.session_key))
  130
+
  131
+    def test_delete(self):
  132
+        self.session.delete(self.session.session_key)
  133
+        self.assertFalse(self.session.exists(self.session.session_key))
  134
+
  135
+    def test_flush(self):
  136
+        self.session['foo'] = 'bar'
  137
+        self.session.save()
  138
+        prev_key = self.session.session_key
  139
+        self.session.flush()
  140
+        self.assertFalse(self.session.exists(prev_key))
  141
+        self.assertNotEqual(self.session.session_key, prev_key)
  142
+        self.assertTrue(self.session.modified)
  143
+        self.assertTrue(self.session.accessed)
  144
+
  145
+    def test_cycle(self):
  146
+        self.session['a'], self.session['b'] = 'c', 'd'
  147
+        self.session.save()
  148
+        prev_key = self.session.session_key
  149
+        prev_data = self.session.items()
  150
+        self.session.cycle_key()
  151
+        self.assertNotEqual(self.session.session_key, prev_key)
  152
+        self.assertEqual(self.session.items(), prev_data)
  153
+
  154
+    def test_invalid_key(self):
  155
+        # Submitting an invalid session key (either by guessing, or if the db has
  156
+        # removed the key) results in a new key being generated.
  157
+        session = self.backend('1')
  158
+        session.save()
  159
+        self.assertNotEqual(session.session_key, '1')
  160
+        self.assertEqual(session.get('cat'), None)
  161
+        session.delete()
  162
+
  163
+    # Custom session expiry
  164
+    def test_default_expiry(self):
  165
+        # A normal session has a max age equal to settings
  166
+        self.assertEqual(self.session.get_expiry_age(), settings.SESSION_COOKIE_AGE)
  167
+
  168
+        # So does a custom session with an idle expiration time of 0 (but it'll
  169
+        # expire at browser close)
  170
+        self.session.set_expiry(0)
  171
+        self.assertEqual(self.session.get_expiry_age(), settings.SESSION_COOKIE_AGE)
  172
+
  173
+    def test_custom_expiry_seconds(self):
  174
+        # Using seconds
  175
+        self.session.set_expiry(10)
  176
+        delta = self.session.get_expiry_date() - datetime.now()
  177
+        self.assertTrue(delta.seconds in (9, 10))
  178
+
  179
+        age = self.session.get_expiry_age()
  180
+        self.assertTrue(age in (9, 10))
  181
+
  182
+    def test_custom_expiry_timedelta(self):
  183
+        # Using timedelta
  184
+        self.session.set_expiry(timedelta(seconds=10))
  185
+        delta = self.session.get_expiry_date() - datetime.now()
  186
+        self.assertTrue(delta.seconds in (9, 10))
  187
+
  188
+        age = self.session.get_expiry_age()
  189
+        self.assertTrue(age in (9, 10))
  190
+
  191
+    def test_custom_expiry_timedelta(self):
  192
+        # Using timedelta
  193
+        self.session.set_expiry(datetime.now() + timedelta(seconds=10))
  194
+        delta = self.session.get_expiry_date() - datetime.now()
  195
+        self.assertTrue(delta.seconds in (9, 10))
  196
+
  197
+        age = self.session.get_expiry_age()
  198
+        self.assertTrue(age in (9, 10))
  199
+
  200
+    def test_custom_expiry_reset(self):
  201
+        self.session.set_expiry(None)
  202
+        self.session.set_expiry(10)
  203
+        self.session.set_expiry(None)
  204
+        self.assertEqual(self.session.get_expiry_age(), settings.SESSION_COOKIE_AGE)
  205
+
  206
+    def test_get_expire_at_browser_close(self):
  207
+        # Tests get_expire_at_browser_close with different settings and different
  208
+        # set_expiry calls
  209
+        try:
  210
+            original_expire_at_browser_close = settings.SESSION_EXPIRE_AT_BROWSER_CLOSE
  211
+            settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = False
  212
+
  213
+            self.session.set_expiry(10)
  214
+            self.assertFalse(self.session.get_expire_at_browser_close())
  215
+
  216
+            self.session.set_expiry(0)
  217
+            self.assertTrue(self.session.get_expire_at_browser_close())
  218
+
  219
+            self.session.set_expiry(None)
  220
+            self.assertFalse(self.session.get_expire_at_browser_close())
  221
+
  222
+            settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = True
  223
+
  224
+            self.session.set_expiry(10)
  225
+            self.assertFalse(self.session.get_expire_at_browser_close())
  226
+
  227
+            self.session.set_expiry(0)
  228
+            self.assertTrue(self.session.get_expire_at_browser_close())
  229
+
  230
+            self.session.set_expiry(None)
  231
+            self.assertTrue(self.session.get_expire_at_browser_close())
  232
+
  233
+        except:
  234
+            raise
  235
+
  236
+        finally:
  237
+            settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = original_expire_at_browser_close
  238
+
  239
+
  240
+class DatabaseSessionTests(SessionTestsMixin, TestCase):
  241
+
  242
+    backend = DatabaseSession
  243
+
  244
+
  245
+class CacheDBSessionTests(SessionTestsMixin, TestCase):
  246
+
  247
+    backend = CacheDBSession
  248
+
  249
+# Don't need DB flushing for these tests, so can use unittest.TestCase as base class
  250
+class FileSessionTests(SessionTestsMixin, unittest.TestCase):
  251
+
  252
+    backend = FileSession
  253
+
  254
+    def setUp(self):
  255
+        super(FileSessionTests, self).setUp()
  256
+        # Do file session tests in an isolated directory, and kill it after we're done.
  257
+        self.original_session_file_path = settings.SESSION_FILE_PATH
  258
+        self.temp_session_store = settings.SESSION_FILE_PATH = tempfile.mkdtemp()
  259
+
  260
+    def tearDown(self):
  261
+        settings.SESSION_FILE_PATH = self.original_session_file_path
  262
+        shutil.rmtree(self.temp_session_store)
  263
+        super(FileSessionTests, self).tearDown()
  264
+
  265
+    def test_configuration_check(self):
  266
+        # Make sure the file backend checks for a good storage dir
  267
+        settings.SESSION_FILE_PATH = "/if/this/directory/exists/you/have/a/weird/computer"
  268
+        self.assertRaises(ImproperlyConfigured, self.backend)
  269
+
  270
+
  271
+class CacheSessionTests(SessionTestsMixin, unittest.TestCase):
  272
+
  273
+    backend = CacheSession

0 notes on commit 5eece23

Please sign in to comment.
Something went wrong with that request. Please try again.