fix ``call_service`` API and add test service API
authorFlorent Cayré <florent.cayre@logilab.fr>
Wed, 11 Apr 2012 14:43:51 +0200
changeset 8366 6599f69bb846
parent 8365 51c390500c63
child 8367 fc59d2380c48
fix ``call_service`` API and add test service API ``call_service`` was taking a session argument. But Service is an AppoObject and already has the same session accessible as ``self._cw``.
server/repository.py
test/data/views.py
test/unittest_dbapi.py
--- a/server/repository.py	Wed Apr 11 14:16:27 2012 +0200
+++ b/server/repository.py	Wed Apr 11 14:43:51 2012 +0200
@@ -902,7 +902,7 @@
             session = self._get_session(sessionid, setcnxset=True)
             service = session.vreg['services'].select(regid, session, **kwargs)
             try:
-                return service.call(session, **kwargs)
+                return service.call(**kwargs)
             finally:
                 session.rollback() # free cnxset
         if async:
--- a/test/data/views.py	Wed Apr 11 14:16:27 2012 +0200
+++ b/test/data/views.py	Wed Apr 11 14:43:51 2012 +0200
@@ -17,3 +17,17 @@
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
 from cubicweb.web.views import xmlrss
 xmlrss.RSSIconBox.visible = True
+
+
+from cubicweb.predicates import match_user_groups
+from cubicweb.server import Service
+
+
+class TestService(Service):
+    __regid__ = 'test_service'
+    __select__ = Service.__select__ & match_user_groups('managers')
+    passed_here = []
+
+    def call(self, msg):
+        self.passed_here.append(msg)
+        return 'babar'
--- a/test/unittest_dbapi.py	Wed Apr 11 14:16:27 2012 +0200
+++ b/test/unittest_dbapi.py	Wed Apr 11 14:43:51 2012 +0200
@@ -23,10 +23,11 @@
 
 from logilab.common import tempattr
 
-from cubicweb import ConnectionError, cwconfig
+from cubicweb import ConnectionError, cwconfig, NoSelectableObject
 from cubicweb.dbapi import ProgrammingError
 from cubicweb.devtools.testlib import CubicWebTC
 
+
 class DBAPITC(CubicWebTC):
 
     def test_public_repo_api(self):
@@ -82,6 +83,19 @@
             req.ajax_replace_url('domid') # don't crash
             req.user.cw_adapt_to('IBreadCrumbs') # don't crash
 
+    def test_call_service(self):
+        req = self.request()
+        ServiceClass = self.vreg['services']['test_service'][0]
+        ret_value = req.cnx.call_service('test_service', msg='coucou')
+        self.assertEqual('coucou', ServiceClass.passed_here.pop())
+        self.assertEqual('babar', ret_value)
+        with self.login('anon') as ctm:
+            with self.assertRaises(NoSelectableObject):
+                self.request().cnx.call_service('test_service', msg='toto')
+            self.rollback()
+            self.assertEqual([], ServiceClass.passed_here)
+
+
 if __name__ == '__main__':
     from logilab.common.testlib import unittest_main
     unittest_main()