Repository: ckan/ckan

Issue:
related_update needs owner_id and type
I discovered while writing tests for activity stream that `related_update` doesn't seem to work without providing `owner_id` and `type`. It should only need `id`.



Discussion:

Resolving patch:
diff --git a/ckan/logic/action/update.py b/ckan/logic/action/update.py
--- a/ckan/logic/action/update.py
+++ b/ckan/logic/action/update.py
@@ -132,7 +132,7 @@ def related_update(context, data_dict):
     id = _get_or_bust(data_dict, "id")
 
     session = context['session']
-    schema = context.get('schema') or schema_.default_related_schema()
+    schema = context.get('schema') or schema_.default_update_related_schema()
 
     related = model.Related.get(id)
     context["related"] = related
diff --git a/ckan/logic/schema.py b/ckan/logic/schema.py
--- a/ckan/logic/schema.py
+++ b/ckan/logic/schema.py
@@ -332,6 +332,15 @@ def default_related_schema():
     return schema
 
 
+def default_update_related_schema():
+    schema = default_related_schema()
+    schema['id'] = [not_empty, unicode]
+    schema['title'] = [ignore_missing, unicode]
+    schema['type'] = [ignore_missing, unicode]
+    schema['owner_id'] = [ignore_missing, unicode]
+    return schema
+
+
 def default_extras_schema():
 
     schema = {


Test patch:
diff --git a/ckan/tests/logic/test_action.py b/ckan/tests/logic/test_action.py
--- a/ckan/tests/logic/test_action.py
+++ b/ckan/tests/logic/test_action.py
@@ -1975,3 +1975,69 @@ def _assert_we_can_add_user_to_group(self, user_id, group_id):
         group_ids = [g.id for g in groups]
         assert res['success'] is True, res
         assert group.id in group_ids, (group, user_groups)
+
+
+class TestRelatedAction(WsgiAppCase):
+
+    sysadmin_user = None
+
+    normal_user = None
+
+    @classmethod
+    def setup_class(cls):
+        search.clear()
+        CreateTestData.create()
+        cls.sysadmin_user = model.User.get('testsysadmin')
+
+    @classmethod
+    def teardown_class(cls):
+        model.repo.rebuild_db()
+
+    def _add_basic_package(self, package_name=u'test_package', **kwargs):
+        package = {
+            'name': package_name,
+            'title': u'A Novel By Tolstoy',
+            'resources': [{
+                'description': u'Full text.',
+                'format': u'plain text',
+                'url': u'http://www.annakarenina.com/download/'
+            }]
+        }
+        package.update(kwargs)
+
+        postparams = '%s=1' % json.dumps(package)
+        res = self.app.post('/api/action/package_create', params=postparams,
+                            extra_environ={'Authorization': 'tester'})
+        return json.loads(res.body)['result']
+
+    def test_update_add_related_item(self):
+        package = self._add_basic_package()
+        related_item = {
+            "description": "Testing a Description",
+            "url": "http://example.com/image.png",
+            "title": "Testing",
+            "featured": 0,
+            "image_url": "http://example.com/image.png",
+            "type": "idea",
+            "dataset_id": package['id'],
+        }
+        related_item_json = json.dumps(related_item)
+        res_create = self.app.post('/api/action/related_create',
+                                   params=related_item_json,
+                                   extra_environ={'Authorization': 'tester'})
+        assert res_create.json['success']
+
+        related_update = res_create.json['result']
+        related_update = {'id': related_update['id'], 'title': 'Updated'}
+        related_update_json = json.dumps(related_update)
+        res_update = self.app.post('/api/action/related_update',
+                                   params=related_update_json,
+                                   extra_environ={'Authorization': 'tester'})
+        assert res_update.json['success']
+        res_update_json = res_update.json['result']
+        assert res_update_json['title'] == related_update['title']
+
+        related_item.pop('title')
+        related_item.pop('dataset_id')
+        for field in related_item:
+            assert related_item[field] == res_update_json[field]