web/test/unittest_session.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 09 May 2012 17:15:44 +0200
changeset 8394 b8b23af60019
parent 7817 cb6174065c39
permissions -rw-r--r--
[server/hook] a tiny bit of doc about before_server_shutdown
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5251
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
# -*- coding: iso-8859-1 -*-
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
"""unit tests for cubicweb.web.application
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
:organization: Logilab
7817
cb6174065c39 [test] fix test broken by 7815:2a164a9cf81c
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6367
diff changeset
     5
:copyright: 2001-2011 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
5251
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
"""
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
from cubicweb.devtools.testlib import CubicWebTC
6032
52f9a43d8e08 [session] session auto reconnect feature has gone with the session-time refactoring in 6012:d56fd78006cd
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5785
diff changeset
    10
from cubicweb.web import InvalidSession
5251
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
class SessionTC(CubicWebTC):
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
6032
52f9a43d8e08 [session] session auto reconnect feature has gone with the session-time refactoring in 6012:d56fd78006cd
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5785
diff changeset
    14
    def test_session_expiration(self):
5251
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
        sm = self.app.session_handler.session_manager
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    16
        # make is if the web session has been opened by the session manager
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
        sm._sessions[self.cnx.sessionid] = self.websession
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
        sessionid = self.websession.sessionid
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5785
diff changeset
    19
        self.assertEqual(len(sm._sessions), 1)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5785
diff changeset
    20
        self.assertEqual(self.websession.sessionid, self.websession.cnx.sessionid)
5251
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
        # fake the repo session is expiring
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
        self.repo.close(sessionid)
5785
c0661b963ced [test] fix test w/ closed connection pb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5251
diff changeset
    23
        try:
c0661b963ced [test] fix test w/ closed connection pb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5251
diff changeset
    24
            # fake an incoming http query with sessionid in session cookie
c0661b963ced [test] fix test w/ closed connection pb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5251
diff changeset
    25
            # don't use self.request() which try to call req.set_session
c0661b963ced [test] fix test w/ closed connection pb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5251
diff changeset
    26
            req = self.requestcls(self.vreg)
6032
52f9a43d8e08 [session] session auto reconnect feature has gone with the session-time refactoring in 6012:d56fd78006cd
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5785
diff changeset
    27
            self.assertRaises(InvalidSession, sm.get_session, req, sessionid)
6367
d4c485ec1ca1 fix merge, some buffers weren't saved...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6366
diff changeset
    28
            self.assertEqual(len(sm._sessions), 0)
5785
c0661b963ced [test] fix test w/ closed connection pb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5251
diff changeset
    29
        finally:
c0661b963ced [test] fix test w/ closed connection pb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5251
diff changeset
    30
            # avoid error in tearDown by telling this connection is closed...
c0661b963ced [test] fix test w/ closed connection pb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5251
diff changeset
    31
            self.cnx._closed = True
5251
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    32
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    33
if __name__ == '__main__':
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    34
    from logilab.common.testlib import unittest_main
b675edd05c19 [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.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
    unittest_main()