web/test/unittest_session.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 14 Apr 2010 17:38:24 +0200
changeset 5251 b675edd05c19
child 5785 c0661b963ced
permissions -rw-r--r--
[web session] fix web session id bug on automatic reconnection. The web session id should keep the first connection id, then differ of the repo connection id once some reconnection has been done (since the session cookie isn't updated in such cases). Also, use a lock to avoid potential race condition on reconnection.

# -*- coding: iso-8859-1 -*-
"""unit tests for cubicweb.web.application

:organization: Logilab
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
"""
from cubicweb.devtools.testlib import CubicWebTC

class SessionTC(CubicWebTC):

    def test_auto_reconnection(self):
        sm = self.app.session_handler.session_manager
        # make is if the web session has been opened by the session manager
        sm._sessions[self.cnx.sessionid] = self.websession
        sessionid = self.websession.sessionid
        self.assertEquals(len(sm._sessions), 1)
        self.assertEquals(self.websession.sessionid, self.websession.cnx.sessionid)
        # fake the repo session is expiring
        self.repo.close(sessionid)
        # fake an incoming http query with sessionid in session cookie
        # don't use self.request() which try to call req.set_session
        req = self.requestcls(self.vreg)
        websession = sm.get_session(req, sessionid)
        self.assertEquals(len(sm._sessions), 1)
        self.assertIs(websession, self.websession)
        self.assertEquals(websession.sessionid, sessionid)
        self.assertNotEquals(websession.sessionid, websession.cnx.sessionid)

if __name__ == '__main__':
    from logilab.common.testlib import unittest_main
    unittest_main()