Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 439 lines (329 sloc) 12.379 kb
f9b5ab1 @diogobaeder Persisting sessions in Redis
authored
1 import pickle
c07d803 @diogobaeder Session expiration in database
authored
2 import time
63cd7e3 @diogobaeder Making the managers more pythonic
authored
3 from unittest import skip, skipIf, TestCase
40698c8 @diogobaeder Initial session tests for cookies
authored
4
e6c6349 @diogobaeder Inserting dict-like behaviour with setitem and getitem
authored
5 from nose.tools import istest, raises
f9b5ab1 @diogobaeder Persisting sessions in Redis
authored
6 import redis
40698c8 @diogobaeder Initial session tests for cookies
authored
7
973c1de @diogobaeder Supporting Memcached driver in sessions
authored
8 from pycket.driver import MemcachedDriver, RedisDriver
35a9a97 @diogobaeder Changing settings structure
authored
9 from pycket.session import ConfigurationError, SessionManager, SessionMixin
40698c8 @diogobaeder Initial session tests for cookies
authored
10
11
1e1b9e4 @diogobaeder Adding coverage for RedisDriver and DriverFactory
authored
12 skip_slow_tests = False
63cd7e3 @diogobaeder Making the managers more pythonic
authored
13
14
40698c8 @diogobaeder Initial session tests for cookies
authored
15 class SessionMixinTest(TestCase):
16 @istest
17 def starts_handler_with_session_manager(self):
18 class StubHandler(SessionMixin):
35a9a97 @diogobaeder Changing settings structure
authored
19 settings = {
20 'pycket': {
21 'engine': 'redis',
22 }
23 }
40698c8 @diogobaeder Initial session tests for cookies
authored
24
25 self.assertIsInstance(StubHandler().session, SessionManager)
26
35a9a97 @diogobaeder Changing settings structure
authored
27 @istest
28 @raises(ConfigurationError)
29 def cannot_start_driver_without_pycket_settings(self):
30 class StubHandler(SessionMixin):
31 settings = {}
32
33 StubHandler().session.get('something')
34
35 @istest
36 @raises(ConfigurationError)
37 def cannot_start_driver_without_pycket_engine(self):
38 class StubHandler(SessionMixin):
39 settings = {
40 'pycket': {
41 'not-an-engine': 'something-useless',
42 }
43 }
44
45 StubHandler().session.get('something')
46
973c1de @diogobaeder Supporting Memcached driver in sessions
authored
47 @istest
48 def creates_session_for_redis(self):
49 class StubHandler(SessionMixin):
50 settings = {
51 'pycket': {
52 'engine': 'redis',
53 }
54 }
55
56 self.assertIsInstance(StubHandler().session.driver, RedisDriver)
57
58 @istest
59 def creates_session_for_memcached(self):
60 class StubHandler(SessionMixin):
61 settings = {
62 'pycket': {
63 'engine': 'memcached',
64 }
65 }
66
67 self.assertIsInstance(StubHandler().session.driver, MemcachedDriver)
68
40698c8 @diogobaeder Initial session tests for cookies
authored
69
f369ad9 @diogobaeder Adding notification support
authored
70 class RedisTestCase(TestCase):
edbe109 @diogobaeder Memcache driver
authored
71 client = None
f9b5ab1 @diogobaeder Persisting sessions in Redis
authored
72
73 def setUp(self):
edbe109 @diogobaeder Memcache driver
authored
74 if self.client is None:
75 self.client = redis.Redis(db=RedisDriver.DEFAULT_STORAGE_IDENTIFIERS['db_sessions'])
76 self.client.flushall()
f9b5ab1 @diogobaeder Persisting sessions in Redis
authored
77
f369ad9 @diogobaeder Adding notification support
authored
78
79 class SessionManagerTest(RedisTestCase):
40698c8 @diogobaeder Initial session tests for cookies
authored
80 @istest
81 def sets_session_id_on_cookies(self):
82 test_case = self
83
84 class StubHandler(SessionMixin):
35a9a97 @diogobaeder Changing settings structure
authored
85 settings = {
86 'pycket': {
87 'engine': 'redis',
88 }
89 }
40698c8 @diogobaeder Initial session tests for cookies
authored
90 def get_secure_cookie(self, name):
91 test_case.assertEqual(name, 'PYCKET_ID')
92 self.cookie_set = True
93 return None
94
0abdd3b @diogobaeder Fixing session expiration
authored
95 def set_secure_cookie(self, name, value, expires_days, expires):
40698c8 @diogobaeder Initial session tests for cookies
authored
96 test_case.assertEqual(name, 'PYCKET_ID')
97 test_case.assertIsInstance(value, basestring)
98 test_case.assertGreater(len(value), 0)
99 self.cookie_retrieved = True
100
101 handler = StubHandler()
102 session_manager = SessionManager(handler)
103 session_manager.set('some-object', 'Some object')
104
105 self.assertTrue(handler.cookie_retrieved)
106 self.assertTrue(handler.cookie_set)
107
108 @istest
109 def does_not_set_session_id_if_already_exists(self):
110 test_case = self
111
112 class StubHandler(SessionMixin):
35a9a97 @diogobaeder Changing settings structure
authored
113 settings = {
114 'pycket': {
115 'engine': 'redis',
116 }
117 }
40698c8 @diogobaeder Initial session tests for cookies
authored
118 def get_secure_cookie(self, name):
119 self.cookie_retrieved = True
120 return 'some-id'
121
122 handler = StubHandler()
0abdd3b @diogobaeder Fixing session expiration
authored
123 manager = SessionManager(handler)
124 manager.set('some-object', 'Some object')
40698c8 @diogobaeder Initial session tests for cookies
authored
125
126 self.assertTrue(handler.cookie_retrieved)
5dcdb9e @diogobaeder Preparing for integration test with Redis
authored
127
f9b5ab1 @diogobaeder Persisting sessions in Redis
authored
128 @istest
129 def saves_session_object_on_redis_with_same_session_id_as_cookie(self):
130 handler = StubHandler()
131 manager = SessionManager(handler)
132
133 manager.set('some-object', {'foo': 'bar'})
134
edbe109 @diogobaeder Memcache driver
authored
135 raw_session = self.client.get(handler.session_id)
f9b5ab1 @diogobaeder Persisting sessions in Redis
authored
136 session = pickle.loads(raw_session)
137
138 self.assertEqual(session['some-object']['foo'], 'bar')
139
140 @istest
141 def retrieves_session_with_same_data_as_saved(self):
142 handler = StubHandler()
143 manager = SessionManager(handler)
144
145 manager.set('some-object', {'foo': 'bar'})
146
147 self.assertEqual(manager.get('some-object')['foo'], 'bar')
148
149 @istest
150 def keeps_previous_items_when_setting_new_ones(self):
151 handler = StubHandler()
152 manager = SessionManager(handler)
153
154 manager.set('some-object', {'foo': 'bar'})
155 manager.set('some-object2', {'foo2': 'bar2'})
156
157 self.assertEqual(manager.get('some-object')['foo'], 'bar')
158 self.assertEqual(manager.get('some-object2')['foo2'], 'bar2')
159
160 @istest
161 def retrieves_none_if_session_object_not_previously_set(self):
162 handler = StubHandler()
163 manager = SessionManager(handler)
164
165 self.assertIsNone(manager.get('unexistant-object'))
166
4201f47 @diogobaeder Session object removal
authored
167 @istest
168 def deletes_objects_from_session(self):
169 handler = StubHandler()
170 manager = SessionManager(handler)
171
172 manager.set('some-object', {'foo': 'bar'})
173 manager.set('some-object2', {'foo2': 'bar2'})
174 manager.delete('some-object')
175
edbe109 @diogobaeder Memcache driver
authored
176 raw_session = self.client.get(handler.session_id)
4201f47 @diogobaeder Session object removal
authored
177 session = pickle.loads(raw_session)
178
179 self.assertEqual(session.keys(), ['some-object2'])
180
c07d803 @diogobaeder Session expiration in database
authored
181 @istest
63cd7e3 @diogobaeder Making the managers more pythonic
authored
182 @skipIf(skip_slow_tests, 'This test is too slow')
c07d803 @diogobaeder Session expiration in database
authored
183 def still_retrieves_object_if_not_passed_from_expiration(self):
184 handler = StubHandler()
185 manager = SessionManager(handler)
186
187 manager.set('foo', 'bar')
188
189 time.sleep(1)
190
191 self.assertEqual(manager.get('foo'), 'bar')
192
193 @istest
63cd7e3 @diogobaeder Making the managers more pythonic
authored
194 @skipIf(skip_slow_tests, 'This test is too slow')
c07d803 @diogobaeder Session expiration in database
authored
195 def cannot_retrieve_object_if_passed_from_expiration(self):
196 handler = StubHandler()
197 manager = SessionManager(handler)
1e1b9e4 @diogobaeder Adding coverage for RedisDriver and DriverFactory
authored
198 manager.driver.EXPIRE_SECONDS = 1
c07d803 @diogobaeder Session expiration in database
authored
199
200 manager.set('foo', 'bar')
201
1e1b9e4 @diogobaeder Adding coverage for RedisDriver and DriverFactory
authored
202 time.sleep(manager.driver.EXPIRE_SECONDS + 1)
c07d803 @diogobaeder Session expiration in database
authored
203
204 self.assertIsNone(manager.get('foo'))
205
35a2ea9 @diogobaeder Repassing Redis settings to the client
authored
206 @istest
e6c6349 @diogobaeder Inserting dict-like behaviour with setitem and getitem
authored
207 def retrieves_object_with_dict_key(self):
208 handler = StubHandler()
209 manager = SessionManager(handler)
210
211 manager.set('foo', 'bar')
212
213 self.assertEqual(manager['foo'], 'bar')
214
215 @istest
216 @raises(KeyError)
217 def raises_key_error_if_object_doesnt_exist(self):
218 handler = StubHandler()
219 manager = SessionManager(handler)
220
221 manager['foo']
222
223 @istest
224 def sets_object_with_dict_key(self):
225 handler = StubHandler()
226 manager = SessionManager(handler)
227
228 manager['foo'] = 'bar'
229
230 self.assertEqual(manager['foo'], 'bar')
231
232 @istest
edbe109 @diogobaeder Memcache driver
authored
233 def gets_default_value_if_provided_and_not_in_client(self):
e6c6349 @diogobaeder Inserting dict-like behaviour with setitem and getitem
authored
234 handler = StubHandler()
235 manager = SessionManager(handler)
236
237 value = manager.get('foo', 'Default')
238
239 self.assertEqual(value, 'Default')
240
0abdd3b @diogobaeder Fixing session expiration
authored
241 @istest
242 def sets_session_id_to_last_a_browser_session_as_default(self):
243 test_case = self
244
245 class StubHandler(SessionMixin):
35a9a97 @diogobaeder Changing settings structure
authored
246 settings = {
247 'pycket': {
248 'engine': 'redis',
249 }
250 }
0abdd3b @diogobaeder Fixing session expiration
authored
251 def get_secure_cookie(self, name):
252 return None
253
254 def set_secure_cookie(self, name, value, expires_days, expires):
255 test_case.assertIsNone(expires_days)
256 test_case.assertIsNone(expires)
257
258 handler = StubHandler()
259 manager = SessionManager(handler)
260 manager.set('some-object', 'Some object')
261
6349349 @diogobaeder Repassing cookies settings
authored
262 @istest
263 def repasses_cookies_options(self):
264 test_case = self
265
266 class StubHandler(SessionMixin):
267 settings = {
35a9a97 @diogobaeder Changing settings structure
authored
268 'pycket': {
269 'engine': 'redis',
270 'cookies': {
271 'foo': 'bar',
272 }
273 },
6349349 @diogobaeder Repassing cookies settings
authored
274 }
275 def get_secure_cookie(self, name):
276 return None
277
278 def set_secure_cookie(self, *args, **kwargs):
279 test_case.assertEqual(kwargs['foo'], 'bar')
280
281 handler = StubHandler()
282 manager = SessionManager(handler)
283 manager.set('some-object', 'Some object')
284
285 @istest
286 def uses_custom_expires_if_provided(self):
287 test_case = self
288
289 class StubHandler(SessionMixin):
290 settings = {
35a9a97 @diogobaeder Changing settings structure
authored
291 'pycket': {
292 'engine': 'redis',
293 'cookies': {
294 'expires': 'St. Neversday',
295 }
296 },
6349349 @diogobaeder Repassing cookies settings
authored
297 }
298 def get_secure_cookie(self, name):
299 return None
300
301 def set_secure_cookie(self, *args, **kwargs):
302 test_case.assertEqual(kwargs['expires'], 'St. Neversday')
303
304 handler = StubHandler()
305 manager = SessionManager(handler)
306 manager.set('some-object', 'Some object')
307
308 @istest
309 def uses_custom_expires_days_if_provided(self):
310 test_case = self
311
312 class StubHandler(SessionMixin):
313 settings = {
35a9a97 @diogobaeder Changing settings structure
authored
314 'pycket': {
315 'engine': 'redis',
316 'cookies': {
317 'expires_days': 'St. Neversday',
318 }
319 },
6349349 @diogobaeder Repassing cookies settings
authored
320 }
321 def get_secure_cookie(self, name):
322 return None
323
324 def set_secure_cookie(self, *args, **kwargs):
325 test_case.assertEqual(kwargs['expires_days'], 'St. Neversday')
326
327 handler = StubHandler()
328 manager = SessionManager(handler)
329 manager.set('some-object', 'Some object')
330
50ed4b4 @diogobaeder Supporting custom dataset numbers
authored
331 @istest
332 def uses_custom_sessions_database_if_provided(self):
333 handler = StubHandler()
334 handler.settings = {
35a9a97 @diogobaeder Changing settings structure
authored
335 'pycket': {
336 'engine': 'redis',
337 'storage': {
338 'db_sessions': 10,
339 'db_notifications': 11,
340 }
341 },
50ed4b4 @diogobaeder Supporting custom dataset numbers
authored
342 }
343 manager = SessionManager(handler)
344 manager.set('foo', 'bar')
edbe109 @diogobaeder Memcache driver
authored
345 self.assertEqual(manager.driver.client.connection_pool._available_connections[0].db, 10)
50ed4b4 @diogobaeder Supporting custom dataset numbers
authored
346
63cd7e3 @diogobaeder Making the managers more pythonic
authored
347 @istest
348 def deletes_multiple_session_objects_at_once(self):
349 handler = StubHandler()
350 manager = SessionManager(handler)
351
352 manager.set('some-object', {'foo': 'bar'})
353 manager.set('some-object2', {'foo2': 'bar2'})
354 manager.delete('some-object', 'some-object2')
355
edbe109 @diogobaeder Memcache driver
authored
356 raw_session = self.client.get(handler.session_id)
63cd7e3 @diogobaeder Making the managers more pythonic
authored
357 session = pickle.loads(raw_session)
358
359 self.assertEqual(session.keys(), [])
360
361 @istest
362 def deletes_item_using_command(self):
363 handler = StubHandler()
364 manager = SessionManager(handler)
365
366 manager.set('some-object', {'foo': 'bar'})
367
368 del manager['some-object']
369
370 self.assertIsNone(manager.get('some-object'))
371
372 @istest
373 def verifies_if_a_session_exist(self):
374 handler = StubHandler()
375 manager = SessionManager(handler)
376
377 self.assertFalse('foo' in manager)
378
379 manager['foo'] = 'bar'
380
381 self.assertTrue('foo' in manager)
382
4fe5919 @diogobaeder Session keys
authored
383 @istest
384 def gets_all_available_keys_from_session(self):
385 handler = StubHandler()
386 manager = SessionManager(handler)
387
388 manager.set('foo', 'FOO')
389 manager.set('bar', 'BAR')
390
391 self.assertListEqual(manager.keys(), ['foo', 'bar'])
392
6f5e17a @diogobaeder Making managers iterable
authored
393 @istest
394 def iterates_with_method_over_keys(self):
395 handler = StubHandler()
396 manager = SessionManager(handler)
397
398 manager.set('foo', 'FOO')
399 manager.set('bar', 'BAR')
400
401 iterations = 0
402
403 for key in manager.iterkeys():
404 self.assertTrue(key in manager)
405 iterations += 1
406
407 self.assertEqual(iterations, 2)
408
409 @istest
410 def iterates_without_method_over_keys(self):
411 handler = StubHandler()
412 manager = SessionManager(handler)
413
414 manager.set('foo', 'FOO')
415 manager.set('bar', 'BAR')
416
417 iterations = 0
418
419 for key in manager:
420 self.assertTrue(key in manager)
421 iterations += 1
422
423 self.assertEqual(iterations, 2)
424
5dcdb9e @diogobaeder Preparing for integration test with Redis
authored
425
7fee627 @diogobaeder Changing stub inheritance to avoid confusion
authored
426 class StubHandler(object):
5dcdb9e @diogobaeder Preparing for integration test with Redis
authored
427 session_id = 'session-id'
452308a @diogobaeder Moved most of the database-specific code to the RedisDriver. Now it's…
authored
428
429 def __init__(self, settings=None):
35a9a97 @diogobaeder Changing settings structure
authored
430 default_settings = {
431 'pycket': {
432 'engine': 'redis',
433 }
434 }
435 self.settings = settings if settings is not None else default_settings
5dcdb9e @diogobaeder Preparing for integration test with Redis
authored
436
437 def get_secure_cookie(self, name):
438 return self.session_id
Something went wrong with that request. Please try again.