[ws] Update ark assignment web-service to be given an ark instead of an eid
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 20 Jul 2017 12:16:28 +0200
changeset 3326 a04a79d6be78
parent 3323 abf232b816b8
child 3327 0fef4d250010
[ws] Update ark assignment web-service to be given an ark instead of an eid since Organisation has been granted an ark identifier, its exported RDF contains it as dc:identifier, so that's what should be accepted by the web-service, not the internal eid. Related to extranet #29484932
cubicweb_saem_ref/views/ws.py
doc/web-services.rst
test/test_views.py
--- a/cubicweb_saem_ref/views/ws.py	Wed Jul 19 10:03:32 2017 +0200
+++ b/cubicweb_saem_ref/views/ws.py	Thu Jul 20 12:16:28 2017 +0200
@@ -35,13 +35,14 @@
 
     # XXX could check Accept=application/json
     def call(self):
-        org_eid = int(self._cw.form['organization'])
+        org_ark = self._cw.form['organization']
+        org_ark = org_ark.replace('ark://', '')
 
         def error(msg):
-            self.wdata([{'error': msg.format(org_eid)}])
+            self.wdata([{'error': msg.format(org_ark)}])
 
         try:
-            org = self._cw.find('Organization', eid=org_eid).one()
+            org = self._cw.find('Organization', ark=org_ark).one()
         except (TypeResolverException, NoResultError):
             error('No organization matching identifier "{0}".')
         else:
--- a/doc/web-services.rst	Wed Jul 19 10:03:32 2017 +0200
+++ b/doc/web-services.rst	Thu Jul 20 12:16:28 2017 +0200
@@ -57,10 +57,11 @@
 
 Le point d'accès ``/ark`` permet d'obtenir un identifiant ARK à partir d'une
 *autorité administrative* (collectivité) spécifié via le paramètre de requête
-``organization=<identifiant>``. L'identifiant d'une autorité administrative
+``organization=<identifiant ARK>``. L'identifiant d'une autorité administrative
 peut être obtenu à partir des données RDF (elles-mêmes disponibles via une
 requête OAI-PMH sur une unité administrative) en récupérant la valeur du champ
-``dc:identifier`` (normalement un nombre entier).
+``dc:identifier`` (normalement une chaîne de caractères commençant par
+'ark://').
 
 Pour utiliser ce service il faut être authentifié.
 
@@ -68,7 +69,7 @@
 
 ::
 
-    POST /ark&organization=123
+    POST /ark&organization=ark%3A%2F%2F12345%2Fo67
     Accept: application/json
 
 Exemples de réponse (JSON) ::
@@ -79,9 +80,9 @@
 
     [{'error': 'This service requires authentication.'}]
 
-    [{'error': 'Organization "123" cannot assign ARK identifiers.'}]
+    [{'error': 'Organization "12345/o67" cannot assign ARK identifiers.'}]
 
-    [{'error': 'No organization matching identifier "123".'}]
+    [{'error': 'No organization matching identifier "12345/o67".'}]
 
     [{'error': 'Missing required "organization" query parameter.'}])
 
--- a/test/test_views.py	Wed Jul 19 10:03:32 2017 +0200
+++ b/test/test_views.py	Thu Jul 20 12:16:28 2017 +0200
@@ -413,8 +413,8 @@
 
     def test_authenticated_organization_method_not_post(self):
         with self.admin_access.cnx() as cnx:
-            org_eid = testutils.authority_with_naa(cnx).eid
-        params = {'organization': text_type(org_eid)}
+            org_ark = testutils.authority_with_naa(cnx).ark
+        params = {'organization': org_ark}
         with self.admin_access.web_request(
                 headers={'Accept': 'application/json'}, **params) as req:
             result = self.app_handle_request(req, 'ark')
@@ -431,42 +431,49 @@
             result = self.app_handle_request(req, 'ark')
             self.assertEqual(json.loads(result),
                              [{'error': expected_msg.format(0)}])
-        # Specified eid does not match an Organization entity.
+        # Specified ark does not match an Organization entity.
         with self.admin_access.cnx() as cnx:
-            user_eid = cnx.find('CWUser')[0][0]
-        bad_params = {'organization': text_type(user_eid)}
+            agent_ark = testutils.agent(cnx, u'bob').ark
+        bad_params = {'organization': agent_ark}
         with self.admin_access.web_request(
                 headers={'Accept': 'application/json'},
                 method='POST', **bad_params) as req:
             result = self.app_handle_request(req, 'ark')
             self.assertEqual(json.loads(result),
-                             [{'error': expected_msg.format(user_eid)}])
+                             [{'error': expected_msg.format(agent_ark)}])
 
     def test_authenticated_organization_has_no_naa_method_post(self):
         with self.admin_access.cnx() as cnx:
-            org_eid = cnx.create_entity(
-                'Organization', name=u'dummy', ark=u'123').eid
+            org_ark = cnx.create_entity(
+                'Organization', name=u'dummy', ark=u'123').ark
             cnx.commit()
-        bad_params = {'organization': text_type(org_eid)}
+        bad_params = {'organization': org_ark}
         with self.admin_access.web_request(
                 headers={'Accept': 'application/json'},
                 method='POST', **bad_params) as req:
             result = self.app_handle_request(req, 'ark')
             expected_msg = 'Organization "{0}" cannot assign ARK identifiers.'
             self.assertEqual(json.loads(result),
-                             [{'error': expected_msg.format(org_eid)}])
+                             [{'error': expected_msg.format(org_ark)}])
 
     def test_ok(self):
         with self.admin_access.cnx() as cnx:
-            org_eid = testutils.authority_with_naa(cnx).eid
+            org_ark = testutils.authority_with_naa(cnx).ark
             cnx.commit()
-        params = {'organization': text_type(org_eid)}
+        params = {'organization': org_ark}
         with self.admin_access.web_request(
                 headers={'Accept': 'application/json'},
                 method='POST', **params) as req:
             result = self.app_handle_request(req, 'ark')
             self.assertEqual(json.loads(result),
                              [{'ark': '0/a000000001'}])
+        params = {'organization': 'ark://' + org_ark}
+        with self.admin_access.web_request(
+                headers={'Accept': 'application/json'},
+                method='POST', **params) as req:
+            result = self.app_handle_request(req, 'ark')
+            self.assertEqual(json.loads(result),
+                             [{'ark': '0/a000000002'}])
 
 
 class TimelineViewsTC(CubicWebTC):