# HG changeset patch # User Pierre-Yves David # Date 1371825612 -7200 # Node ID e86fdab3d2961d8478ad66fdfe57afc7937e11c3 # Parent 8c0016d7a0919943c3da362bceb2f6ae11c69618 [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. diff -r 8c0016d7a091 -r e86fdab3d296 repoapi.py --- 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 diff -r 8c0016d7a091 -r e86fdab3d296 test/unittest_repoapi.py --- 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) + +