Commit 5f9cb432 authored by Chris McDonough's avatar Chris McDonough

Fixed bug in SessionDataManager's "hasSessionData" method in which

a call to it would create a session data object -- the hasSessionData
call now does not create a session data object inadvertently as a result
of its usage.

Also updated tests.
parent 66747113
...@@ -95,7 +95,10 @@ class SessionDataManager(Item, Implicit, Persistent, RoleManager, Owned, Tabs): ...@@ -95,7 +95,10 @@ class SessionDataManager(Item, Implicit, Persistent, RoleManager, Owned, Tabs):
security.declareProtected(ACCESS_SESSIONDATA_PERM, 'hasSessionData') security.declareProtected(ACCESS_SESSIONDATA_PERM, 'hasSessionData')
def hasSessionData(self): def hasSessionData(self):
""" """ """ """
return not not self.getSessionData(create=0) key = self.getBrowserIdManager().getBrowserId(create=0)
if key is None:
return 0
return self._hasSessionDataObject(key)
security.declareProtected(ARBITRARY_SESSIONDATA_PERM,'getSessionDataByKey') security.declareProtected(ARBITRARY_SESSIONDATA_PERM,'getSessionDataByKey')
def getSessionDataByKey(self, key): def getSessionDataByKey(self, key):
......
...@@ -37,6 +37,7 @@ import sys ...@@ -37,6 +37,7 @@ import sys
tf_name = 'temp_folder' tf_name = 'temp_folder'
idmgr_name = 'browser_id_manager' idmgr_name = 'browser_id_manager'
toc_name = 'temp_transient_container' toc_name = 'temp_transient_container'
sdm_name = 'session_data_manager'
stuff = {} stuff = {}
...@@ -66,7 +67,7 @@ def _populate(app): ...@@ -66,7 +67,7 @@ def _populate(app):
tf = MountedTemporaryFolder(tf_name, title="Temporary Folder") tf = MountedTemporaryFolder(tf_name, title="Temporary Folder")
toc = TransientObjectContainer(toc_name, title='Temporary ' toc = TransientObjectContainer(toc_name, title='Temporary '
'Transient Object Container', timeout_mins=20) 'Transient Object Container', timeout_mins=20)
session_data_manager=SessionDataManager(id='session_data_manager', session_data_manager=SessionDataManager(id=sdm_name,
path='/'+tf_name+'/'+toc_name, title='Session Data Manager', path='/'+tf_name+'/'+toc_name, title='Session Data Manager',
requestName='TESTOFSESSION') requestName='TESTOFSESSION')
...@@ -76,7 +77,7 @@ def _populate(app): ...@@ -76,7 +77,7 @@ def _populate(app):
try: app._delObject(tf_name) try: app._delObject(tf_name)
except AttributeError: pass except AttributeError: pass
try: app._delObject('session_data_manager') try: app._delObject(sdm_name)
except AttributeError: pass except AttributeError: pass
try: app._delObject('index_html') try: app._delObject('index_html')
...@@ -84,7 +85,7 @@ def _populate(app): ...@@ -84,7 +85,7 @@ def _populate(app):
app._setObject(idmgr_name, bidmgr) app._setObject(idmgr_name, bidmgr)
app._setObject('session_data_manager', session_data_manager) app._setObject(sdm_name, session_data_manager)
app._setObject(tf_name, tf) app._setObject(tf_name, tf)
get_transaction().commit() get_transaction().commit()
...@@ -110,48 +111,53 @@ class TestBase(TestCase): ...@@ -110,48 +111,53 @@ class TestBase(TestCase):
class TestSessionManager(TestBase): class TestSessionManager(TestBase):
def testHasId(self): def testHasId(self):
assert self.app.session_data_manager.id == 'session_data_manager' self.failUnless(self.app.session_data_manager.id == \
sdm_name)
def testHasTitle(self): def testHasTitle(self):
assert self.app.session_data_manager.title == 'Session Data Manager' self.failUnless(self.app.session_data_manager.title \
== 'Session Data Manager')
def testGetSessionDataNoCreate(self): def testGetSessionDataNoCreate(self):
sd = self.app.session_data_manager.getSessionData(0) sd = self.app.session_data_manager.getSessionData(0)
assert sd is None, repr(sd) self.failUnless(sd is None)
def testGetSessionDataCreate(self): def testGetSessionDataCreate(self):
sd = self.app.session_data_manager.getSessionData(1) sd = self.app.session_data_manager.getSessionData(1)
assert sd.__class__ is TransientObject, repr(sd) self.failUnless(sd.__class__ is TransientObject)
def testHasSessionData(self): def testHasSessionData(self):
sd = self.app.session_data_manager.getSessionData() sd = self.app.session_data_manager.getSessionData()
assert self.app.session_data_manager.hasSessionData() self.failUnless(self.app.session_data_manager.hasSessionData())
def testNotHasSessionData(self):
self.failUnless(not self.app.session_data_manager.hasSessionData())
def testSessionDataWrappedInSDMandTOC(self): def testSessionDataWrappedInSDMandTOC(self):
sd = self.app.session_data_manager.getSessionData(1) sd = self.app.session_data_manager.getSessionData(1)
assert aq_base(sd.aq_parent) is \ sdm = aq_base(getattr(self.app, sdm_name))
aq_base(getattr(self.app, 'session_data_manager')), sd.aq_parent toc = aq_base(getattr(self.app.temp_folder, toc_name))
assert aq_base(sd.aq_parent.aq_parent) is \
aq_base(getattr(self.app.temp_folder, toc_name)), \ self.failUnless(aq_base(sd.aq_parent) is sdm)
sd.aq_parent.aq_parent self.failUnless(aq_base(sd.aq_parent.aq_parent) is toc)
def testNewSessionDataObjectIsValid(self): def testNewSessionDataObjectIsValid(self):
sdType = type(TransientObject(1)) sdType = type(TransientObject(1))
sd = self.app.session_data_manager.getSessionData() sd = self.app.session_data_manager.getSessionData()
assert type(getattr(sd, 'aq_base', sd)) is sdType self.failUnless(type(aq_base(sd)) is sdType)
assert not hasattr(sd, '_invalid') self.failUnless(not hasattr(sd, '_invalid'))
def testBrowserIdIsSet(self): def testBrowserIdIsSet(self):
sd = self.app.session_data_manager.getSessionData() sd = self.app.session_data_manager.getSessionData()
mgr = getattr(self.app, idmgr_name) mgr = getattr(self.app, idmgr_name)
assert mgr.hasBrowserId() self.failUnless(mgr.hasBrowserId())
def testGetSessionDataByKey(self): def testGetSessionDataByKey(self):
sd = self.app.session_data_manager.getSessionData() sd = self.app.session_data_manager.getSessionData()
mgr = getattr(self.app, idmgr_name) mgr = getattr(self.app, idmgr_name)
token = mgr.getBrowserId() token = mgr.getBrowserId()
bykeysd = self.app.session_data_manager.getSessionDataByKey(token) bykeysd = self.app.session_data_manager.getSessionDataByKey(token)
assert sd == bykeysd, (sd, bykeysd, token) self.failUnless(sd == bykeysd)
def testBadExternalSDCPath(self): def testBadExternalSDCPath(self):
sdm = self.app.session_data_manager sdm = self.app.session_data_manager
...@@ -168,7 +174,7 @@ class TestSessionManager(TestBase): ...@@ -168,7 +174,7 @@ class TestSessionManager(TestBase):
sd = sdm.getSessionData() sd = sdm.getSessionData()
sd['test'] = 'Its alive! Alive!' sd['test'] = 'Its alive! Alive!'
sd.invalidate() sd.invalidate()
assert not sdm.getSessionData().has_key('test') self.failUnless(not sdm.getSessionData().has_key('test'))
def testGhostUnghostSessionManager(self): def testGhostUnghostSessionManager(self):
sdm = self.app.session_data_manager sdm = self.app.session_data_manager
...@@ -177,12 +183,12 @@ class TestSessionManager(TestBase): ...@@ -177,12 +183,12 @@ class TestSessionManager(TestBase):
sd.set('foo', 'bar') sd.set('foo', 'bar')
sdm._p_changed = None sdm._p_changed = None
get_transaction().commit() get_transaction().commit()
assert sdm.getSessionData().get('foo') == 'bar' self.failUnless(sdm.getSessionData().get('foo') == 'bar')
def testSubcommit(self): def testSubcommit(self):
sd = self.app.session_data_manager.getSessionData() sd = self.app.session_data_manager.getSessionData()
sd.set('foo', 'bar') sd.set('foo', 'bar')
assert get_transaction().commit(1) == None self.failUnless(get_transaction().commit(1) == None)
def testForeignObject(self): def testForeignObject(self):
self.assertRaises(InvalidObjectReference, self._foreignAdd) self.assertRaises(InvalidObjectReference, self._foreignAdd)
...@@ -205,7 +211,7 @@ class TestSessionManager(TestBase): ...@@ -205,7 +211,7 @@ class TestSessionManager(TestBase):
self.app.REQUEST['PARENTS'] = [self.app] self.app.REQUEST['PARENTS'] = [self.app]
self.app.REQUEST['URL'] = 'a' self.app.REQUEST['URL'] = 'a'
self.app.REQUEST.traverse('/') self.app.REQUEST.traverse('/')
assert self.app.REQUEST.has_key('TESTOFSESSION') self.failUnless(self.app.REQUEST.has_key('TESTOFSESSION'))
def testUnlazifyAutoPopulated(self): def testUnlazifyAutoPopulated(self):
self.app.REQUEST['PARENTS'] = [self.app] self.app.REQUEST['PARENTS'] = [self.app]
...@@ -213,7 +219,7 @@ class TestSessionManager(TestBase): ...@@ -213,7 +219,7 @@ class TestSessionManager(TestBase):
self.app.REQUEST.traverse('/') self.app.REQUEST.traverse('/')
sess = self.app.REQUEST['TESTOFSESSION'] sess = self.app.REQUEST['TESTOFSESSION']
sdType = type(TransientObject(1)) sdType = type(TransientObject(1))
assert type(aq_base(sess)) is sdType, type(aq_base(sess)) self.failUnless(type(aq_base(sess)) is sdType)
def test_suite(): def test_suite():
test_datamgr = makeSuite(TestSessionManager, 'test') test_datamgr = makeSuite(TestSessionManager, 'test')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment