Commit cd00ab11 authored by Aurelien Campeas's avatar Aurelien Campeas
Browse files

[connection] eliminate ClientConnection leftovers

* in the documentation
* in the docstrings
* in code notations (e.g. clt_cnx -> cnx)

Related to #3837233.

From now, there should be just one ClientConnection left in repoapi.py.
parent 60b8204fcca3
......@@ -157,7 +157,7 @@ cwconfig.SMTP = MockSMTP
class TestCaseConnectionProxy(object):
"""thin wrapper around `cubicweb.repoapi.ClientConnection` context-manager
"""thin wrapper around `cubicweb.repoapi.Connection` context-manager
used in CubicWebTC (cf. `cubicweb.devtools.testlib.CubicWebTC.login` method)
It just proxies to the default connection context manager but
......@@ -299,9 +299,9 @@ class CubicWebTC(TestCase):
def __init__(self, *args, **kwargs):
self._admin_session = None
self._admin_clt_cnx = None
self._admin_cnx = None
self._current_session = None
self._current_clt_cnx = None
self._current_cnx = None
self.repo = None
self._open_access = set()
super(CubicWebTC, self).__init__(*args, **kwargs)
......@@ -327,21 +327,21 @@ class CubicWebTC(TestCase):
@deprecated('[3.19] explicitly use RepoAccess object in test instead')
def set_cnx(self, cnx):
assert getattr(cnx, '_session', None) is not None
if cnx is self._admin_clt_cnx:
if cnx is self._admin_cnx:
self._pop_custom_cnx()
else:
self._cnxs.add(cnx) # register the cnx to make sure it is removed
self._current_session = cnx.session
self._current_clt_cnx = cnx
self._current_cnx = cnx
@property
@deprecated('[3.19] explicitly use RepoAccess object in test instead')
def cnx(self):
# XXX we want to deprecate this
clt_cnx = self._current_clt_cnx
if clt_cnx is None:
clt_cnx = self._admin_clt_cnx
return clt_cnx
cnx = self._current_cnx
if cnx is None:
cnx = self._admin_cnx
return cnx
def _close_cnx(self):
"""ensure that all cnx used by a test have been closed"""
......@@ -380,22 +380,22 @@ class CubicWebTC(TestCase):
else:
if not kwargs:
kwargs['password'] = str(login)
clt_cnx = repoapi.connect(self.repo, login, **kwargs)
self.set_cnx(clt_cnx)
clt_cnx.__enter__()
return TestCaseConnectionProxy(self, clt_cnx)
cnx = repoapi.connect(self.repo, login, **kwargs)
self.set_cnx(cnx)
cnx.__enter__()
return TestCaseConnectionProxy(self, cnx)
@deprecated('[3.19] explicitly use RepoAccess object in test instead')
def restore_connection(self):
self._pop_custom_cnx()
def _pop_custom_cnx(self):
if self._current_clt_cnx is not None:
if self._current_clt_cnx._open:
self._current_clt_cnx.close()
if self._current_cnx is not None:
if self._current_cnx._open:
self._current_cnx.close()
if not self._current_session.closed:
self.repo.close(self._current_session.sessionid)
self._current_clt_cnx = None
self._current_cnx = None
self._current_session = None
#XXX this doesn't need to a be classmethod anymore
......@@ -411,9 +411,9 @@ class CubicWebTC(TestCase):
login = unicode(db_handler.config.default_admin_config['login'])
self.admin_access = self.new_access(login)
self._admin_session = self.admin_access._session
self._admin_clt_cnx = repoapi.Connection(self._admin_session)
self._cnxs.add(self._admin_clt_cnx)
self._admin_clt_cnx.__enter__()
self._admin_cnx = repoapi.Connection(self._admin_session)
self._cnxs.add(self._admin_cnx)
self._admin_cnx.__enter__()
self.config.repository = lambda x=None: self.repo
......@@ -517,13 +517,13 @@ class CubicWebTC(TestCase):
raise
self.addCleanup(self._close_access)
self.setup_database()
self._admin_clt_cnx.commit()
self._admin_cnx.commit()
MAILBOX[:] = [] # reset mailbox
def tearDown(self):
# XXX hack until logilab.common.testlib is fixed
if self._admin_clt_cnx is not None:
self._admin_clt_cnx = None
if self._admin_cnx is not None:
self._admin_cnx = None
if self._admin_session is not None:
self.repo.close(self._admin_session.sessionid)
self._admin_session = None
......@@ -573,9 +573,7 @@ class CubicWebTC(TestCase):
groups = login
elif isinstance(login, tuple):
groups = login
login = req
assert not isinstance(self, type)
req = self._admin_clt_cnx
req = self._admin_cnx
if login is not None:
login = unicode(login)
if password is None:
......@@ -969,8 +967,8 @@ class CubicWebTC(TestCase):
def assertAuthSuccess(self, req, origsession, nbsessions=1):
sh = self.app.session_handler
session = self.app.get_session(req)
clt_cnx = repoapi.Connection(session)
req.set_cnx(clt_cnx)
cnx = repoapi.Connection(session)
req.set_cnx(cnx)
self.assertEqual(len(self.open_sessions), nbsessions, self.open_sessions)
self.assertEqual(session.login, origsession.login)
self.assertEqual(session.anonymous_session, False)
......
......@@ -118,13 +118,13 @@ API
```
The elements we gave in overview for above are built in three layers,
from ``cubicweb.req.RequestSessionBase``, ``cubicweb.repoapi.ClientConnection`` and
from ``cubicweb.req.RequestSessionBase``, ``cubicweb.repoapi.Connection`` and
``cubicweb.web.ConnectionCubicWebRequestBase``.
.. autoclass:: cubicweb.req.RequestSessionBase
:members:
.. autoclass:: cubicweb.repoapi.ClientConnection
.. autoclass:: cubicweb.repoapi.Connection
:members:
.. autoclass:: cubicweb.web.request.ConnectionCubicWebRequestBase
......
......@@ -53,10 +53,7 @@ def get_repository(uri=None, config=None, vreg=None):
raise ConnectionError('unknown protocol: `%s`' % protocol)
def connect(repo, login, **kwargs):
"""Take credential and return associated ClientConnection.
The ClientConnection is associated to a new Session object that will be
closed when the ClientConnection is closed.
"""Take credential and return associated Connection.
raise AuthenticationError if the credential are invalid."""
sessionid = repo.connect(login, **kwargs)
......@@ -66,10 +63,7 @@ def connect(repo, login, **kwargs):
return Connection(session)
def anonymous_cnx(repo):
"""return a ClientConnection for Anonymous user.
The ClientConnection is associated to a new Session object that will be
closed when the ClientConnection is closed.
"""return a Connection for Anonymous user.
raises an AuthenticationError if anonymous usage is not allowed
"""
......
......@@ -952,7 +952,7 @@ class NativeSQLSource(SQLAdapterMixIn, AbstractSource):
# undo support #############################################################
def undoable_transactions(self, cnx, ueid=None, **actionfilters):
"""See :class:`cubicweb.repoapi.ClientConnection.undoable_transactions`"""
"""See :class:`cubicweb.repoapi.Connection.undoable_transactions`"""
# force filtering to connection's user if not a manager
if not cnx.user.is_in_group('managers'):
ueid = cnx.user.eid
......@@ -1025,11 +1025,11 @@ class NativeSQLSource(SQLAdapterMixIn, AbstractSource):
return [tx.Transaction(*args) for args in cu.fetchall()]
def tx_info(self, cnx, txuuid):
"""See :class:`cubicweb.repoapi.ClientConnection.transaction_info`"""
"""See :class:`cubicweb.repoapi.Connection.transaction_info`"""
return tx.Transaction(txuuid, *self._tx_info(cnx, unicode(txuuid)))
def tx_actions(self, cnx, txuuid, public):
"""See :class:`cubicweb.repoapi.ClientConnection.transaction_actions`"""
"""See :class:`cubicweb.repoapi.Connection.transaction_actions`"""
txuuid = unicode(txuuid)
self._tx_info(cnx, txuuid)
restr = {'tx_uuid': txuuid}
......@@ -1052,7 +1052,7 @@ class NativeSQLSource(SQLAdapterMixIn, AbstractSource):
return sorted(actions, key=lambda x: x.order)
def undo_transaction(self, cnx, txuuid):
"""See :class:`cubicweb.repoapi.ClientConnection.undo_transaction`
"""See :class:`cubicweb.repoapi.Connection.undo_transaction`
important note: while undoing of a transaction, only hooks in the
'integrity', 'activeintegrity' and 'undo' categories are called.
......
......@@ -26,7 +26,7 @@ from cubicweb.repoapi import Connection, connect, anonymous_cnx
class REPOAPITC(CubicWebTC):
def test_clt_cnx_basic_usage(self):
def test_cnx_basic_usage(self):
"""Test that a client connection can be used to access the database"""
with self.admin_access.client_cnx() as cltcnx:
# (1) some RQL request
......@@ -52,7 +52,7 @@ class REPOAPITC(CubicWebTC):
''')
self.assertTrue(rset)
def test_clt_cnx_life_cycle(self):
def test_cnx_life_cycle(self):
"""Check that ClientConnection requires explicit open and close
"""
access = self.admin_access
......@@ -69,18 +69,18 @@ class REPOAPITC(CubicWebTC):
def test_connect(self):
"""check that repoapi.connect works and returns a usable connection"""
clt_cnx = connect(self.repo, login='admin', password='gingkow')
self.assertEqual('admin', clt_cnx.user.login)
with clt_cnx:
rset = clt_cnx.execute('Any X WHERE X is CWUser')
cnx = connect(self.repo, login='admin', password='gingkow')
self.assertEqual('admin', cnx.user.login)
with cnx:
rset = cnx.execute('Any X WHERE X is CWUser')
self.assertTrue(rset)
def test_anonymous_connect(self):
"""check that you can get anonymous connection when the data exist"""
clt_cnx = anonymous_cnx(self.repo)
self.assertEqual('anon', clt_cnx.user.login)
with clt_cnx:
rset = clt_cnx.execute('Any X WHERE X is CWUser')
cnx = anonymous_cnx(self.repo)
self.assertEqual('anon', cnx.user.login)
with cnx:
rset = cnx.execute('Any X WHERE X is CWUser')
self.assertTrue(rset)
......
......@@ -371,7 +371,7 @@ class ApplicationTC(CubicWebTC):
def _test_auth_anon(self, req):
asession = self.app.get_session(req)
# important otherwise _reset_cookie will not use the right session
req.set_cnx(repoapi.ClientConnection(asession))
req.set_cnx(repoapi.Connection(asession))
self.assertEqual(len(self.open_sessions), 1)
self.assertEqual(asession.login, 'anon')
self.assertTrue(asession.anonymous_session)
......@@ -381,7 +381,7 @@ class ApplicationTC(CubicWebTC):
self.assertEqual(1, len(self.open_sessions))
session = self.app.get_session(req)
# important otherwise _reset_cookie will not use the right session
req.set_cnx(repoapi.ClientConnection(session))
req.set_cnx(repoapi.Connection(session))
self.assertEqual(req.message, 'authentication failure')
self.assertEqual(req.session.anonymous_session, True)
self.assertEqual(1, len(self.open_sessions))
......
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