Testing session in Plone 3.3

Yesterday, while I was working on LifeInLjubljana.si, I had to test if some data was correctly written to Plone’s session. I couldn’t use the standard ptc.FunctionalTestCase test case as it does not have a session and I would get errors like this:

(Pdb) self.context.session_data_manager
*** AttributeError: session_data_manager
(Pdb) self.context.REQUEST.SESSION
*** AttributeError: SESSION

Following a tip on the Plone-Users mailing list and some trail-and-error I have come up with a solution. Below is my test case with which I can build functional tests for sessions:

class SessionFunctionalTestCase(ptc.FunctionalTestCase):
    """ We use this base class for all the session-related tests in this package. """
    
    def afterSetUp(self):
        # Set up sessioning objects
        ztc.utils.setupCoreSessions(self.app)

    class Session(dict):
        def set(self, key, value):
            self[key] = value

    def _setup(self):
        ptc.FunctionalTestCase._setup(self)
        self.app.REQUEST['SESSION'] = self.Session()

Neyts Zupan

Neyts is the Digital Overlord of Niteo, poking his nose into any and all technical things.

See other posts »