[PATCH saem v2] [ws] Update ark assignment web-service to be given an ark instead of an eid

Sylvain Thenault sylvain.thenault at logilab.fr
Thu Jul 20 15:35:15 CEST 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1500545788 -7200
#      Thu Jul 20 12:16:28 2017 +0200
# Node ID a04a79d6be78527e539ce5b445a287dba86b8460
# Parent  abf232b816b8d40ec23a23480f9ef2ce84c91518
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r a04a79d6be78
[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

diff --git a/cubicweb_saem_ref/views/ws.py b/cubicweb_saem_ref/views/ws.py
--- a/cubicweb_saem_ref/views/ws.py
+++ b/cubicweb_saem_ref/views/ws.py
@@ -33,17 +33,18 @@ class AssignArkWebService(json.JsonMixIn
         & match_http_method('POST')
     )
 
     # 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:
             if not org.ark_naa:
                 error('Organization "{0}" cannot assign ARK identifiers.')
diff --git a/doc/web-services.rst b/doc/web-services.rst
--- a/doc/web-services.rst
+++ b/doc/web-services.rst
@@ -55,35 +55,36 @@ Description complète du jeu de données
 Allocation d'identifiants ARK
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 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é.
 
 Exemple de requête :
 
 ::
 
-    POST /ark&organization=123
+    POST /ark&organization=ark%3A%2F%2F12345%2Fo67
     Accept: application/json
 
 Exemples de réponse (JSON) ::
 
     [{'ark': '12345/ext-000000001'}]
 
     [{'error': 'This service is only accessible using POST.'}]
 
     [{'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.'}])
 
 
 Import d'une notice d'autorité au format XML EAC
diff --git a/test/test_views.py b/test/test_views.py
--- a/test/test_views.py
+++ b/test/test_views.py
@@ -411,12 +411,12 @@ class AssignArkWebServiceTC(CubicWebTC):
             self.assertEqual(json.loads(result),
                              [{'error': 'Missing required "organization" query parameter.'}])
 
     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')
             self.assertEqual(json.loads(result),
                              [{'error': 'This service is only accessible using POST.'}])
@@ -429,46 +429,53 @@ class AssignArkWebServiceTC(CubicWebTC):
                 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(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):
 
     def test_timeline_json(self):


More information about the saem-devel mailing list