[repoapi] add a connect function
This function takes a repo and authentication information to open a new Session
and return a ClientConnection associated to it.
--- a/repoapi.py Mon Jun 24 17:26:13 2013 +0200
+++ b/repoapi.py Fri Jun 21 16:40:12 2013 +0200
@@ -74,6 +74,19 @@
else:
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.
+
+ raise AuthenticationError if the credential are invalid."""
+ sessionid = repo.connect(login, **kwargs)
+ session = repo._get_session(sessionid)
+ # XXX the autoclose_session should probably be handle on the session directly
+ # this is something to consider once we have proper server side Connection.
+ return ClientConnection(session, autoclose_session=True)
+
def _srv_cnx_func(name):
"""Decorate ClientConnection method blindly forward to Connection
THIS TRANSITIONAL PURPOSE
--- a/test/unittest_repoapi.py Mon Jun 24 17:26:13 2013 +0200
+++ b/test/unittest_repoapi.py Fri Jun 21 16:40:12 2013 +0200
@@ -21,7 +21,7 @@
from cubicweb.devtools.testlib import CubicWebTC
from cubicweb import ProgrammingError
-from cubicweb.repoapi import ClientConnection
+from cubicweb.repoapi import ClientConnection, connect
class REPOAPITC(CubicWebTC):
@@ -66,5 +66,14 @@
with self.assertRaises(ProgrammingError):
cltcnx.execute('Any X WHERE X is CWUser')
+ def test_connect(self):
+ """check that repoapi.connect works and return 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')
+ self.assertTrue(rset)
+
+