Skip to content
Browse files

remove unnecessary fund call

don't' call _new_session unless you have to and then use .get method as appropriate

Add two new tests to stay at 100% coverage
  • Loading branch information...
1 parent af6430c commit a1a5078ed61d98e6627c2c6266b54555a94aa273 @reedobrien reedobrien committed Dec 5, 2012
Showing with 37 additions and 11 deletions.
  1. +4 −4 lumin/session.py
  2. +33 −7 lumin/tests/test_session.py
View
8 lumin/session.py
@@ -110,7 +110,7 @@ def __init__(self, request):
created = accessed = now
new = True
value = None
- state = self._new_session()
+ state = {} # self._new_session()
cookieval = request.cookies.get(self._cookie_name)
if cookieval is not None:
@@ -138,7 +138,7 @@ def __init__(self, request):
self.created = created
self.accessed = accessed
self.new = new
- dict.__init__(self, state)
+ dict.__init__(self, state if state else self._new_session())
def _new_session(self):
if self._cookie_max_age:
@@ -157,7 +157,7 @@ def changed(self):
def invalidate(self, spec=None):
if not spec:
- spec = {'_id': self['_id']}
+ spec = {'_id': self.get('_id', None)}
self.db[self.collection].remove(spec)
self.clear()
# XXX probably needs to unset cookie
@@ -230,7 +230,7 @@ def _set_cookie(self, response):
self.save()
cookieval = signed_serialize(
- (self.accessed, self.created, self['_id']), self._secret
+ (self.accessed, self.created, self.get('_id', None)), self._secret
)
response.set_cookie(
self._cookie_name,
View
40 lumin/tests/test_session.py
@@ -15,8 +15,10 @@ def test_ctor_no_cookie(self):
request.registry.settings = {}
request.db = DummyDB()
session = self._makeOne(request)
- self.assertEqual(type(dict(session)['_id']), ObjectId)
+ self.assertEqual(type(dict(session).get('_id', None)), ObjectId)
self.assertEqual(len(dict(session)), 3)
+ self.assertTrue(all([x in dict(session) for x in \
+ ("_id", "atime", "ctime")]))
def test_instance_conforms(self):
from zope.interface.verify import verifyObject
@@ -41,6 +43,19 @@ def test_ctor_with_cookie_still_valid(self):
session = self._makeOne(request)
self.assertEqual(dict(session), {'_id': 1})
+ def test_ctor_with_cookie_no_state(self):
+ import time
+ from bson.objectid import ObjectId
+ request = testing.DummyRequest()
+ request.registry.settings = {}
+ request.db = DummyDB()
+ cookieval = self._serialize(time.time(), None)
+ request.cookies['lumin_session'] = cookieval
+ session = self._makeOne(request)
+ self.assertTrue(all([x in dict(session) for x in \
+ ("_id", "atime", "ctime")]))
+ self.assertEqual(type(dict(session)['_id']), ObjectId)
+
def test_ctor_with_cookie_expired(self):
from bson.objectid import ObjectId
request = testing.DummyRequest()
@@ -52,15 +67,24 @@ def test_ctor_with_cookie_expired(self):
self.assertEqual(type(dict(session)['_id']), ObjectId)
def test_ctor_with_bad_cookie(self):
- from bson.objectid import ObjectId
request = testing.DummyRequest()
request.registry.settings = {}
request.db = DummyDB()
cookieval = 'abc'
request.cookies['lumin_session'] = cookieval
session = self._makeOne(request)
- self.assertEqual(type(dict(session)['_id']), ObjectId)
- self.assertEqual(len(dict(session)), 1)
+ self.assertEqual(type(dict(session)), dict)
+ self.assertEqual(len(dict(session)), 3)
+
+ def test__cookie_with_max_age(self):
+ from bson.objectid import ObjectId
+ request = testing.DummyRequest()
+ request.registry.settings = {}
+ request.db = DummyDB()
+ session = self._makeOne(request,
+ cookie_max_age=86)
+ self.assertEqual(type(dict(session).get("_id")), ObjectId)
+ self.assertEqual(session._cookie_max_age, 86)
def test_not_changed(self):
request = testing.DummyRequest()
@@ -131,18 +155,20 @@ def test__set_cookie_options(self):
cookie_domain='localhost',
cookie_secure=True,
cookie_httponly=True,
+ cookie_max_age=86400
)
session['abc'] = 'x'
response = Response()
self.assertEqual(session._set_cookie(response), True)
cookieval = response.headerlist[-1][1]
- val, domain, path, secure, httponly = [x.strip() for x in
+ val, domain, maxage, path, expires, secure, httponly = [x.strip() for x in
cookieval.split(';')]
self.assertTrue(val.startswith('abc='))
self.assertEqual(domain, 'Domain=localhost')
self.assertEqual(path, 'Path=/foo')
self.assertEqual(secure, 'secure')
self.assertEqual(httponly, 'HttpOnly')
+ self.assertEqual(maxage, 'Max-Age=86400')
def test_flash_default(self):
request = testing.DummyRequest()
@@ -405,13 +431,13 @@ def __init__(self, find_result):
self.find_result = find_result
def find_one(self, spec):
- return spec
+ return spec if spec.get("_id", None) else None
def remove(self, spec):
return spec['_id']
def save(self, spec):
- return spec['_id']
+ return spec.get('_id', None)
class Database(dict):

0 comments on commit a1a5078

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