Repository: ckan/ckan

Issue:
resource_delete does not work
`resource_delete` was broken with this commit https://github.com/okfn/ckan/commit/c972c1c02778d484cb22158a0980bf52564a8327.  pkg_dict['resources'] is a `list`, not a `dict`, so line 76 will never return `True`.

Accidentally ran into this when testing filestore modifications. **Update**: Seems to affect only master.

 ![Programming](http://i.imgur.com/Fy75IvC.gif)



Discussion:

Resolving patch:
diff --git a/ckan/logic/action/delete.py b/ckan/logic/action/delete.py
--- a/ckan/logic/action/delete.py
+++ b/ckan/logic/action/delete.py
@@ -73,8 +73,9 @@ def resource_delete(context, data_dict):
 
     pkg_dict = _get_action('package_show')(context, {'id': package_id})
 
-    if 'resources' in pkg_dict and id in pkg_dict['resources']:
-        pkg_dict['resources'].remove(id)
+    if pkg_dict.get('resources'):
+        pkg_dict['resources'] = [r for r in pkg_dict['resources'] if not
+                r['id'] == id]
     try:
         pkg_dict = _get_action('package_update')(context, pkg_dict)
     except ValidationError, e:


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
@@ -1705,3 +1705,56 @@ def test_2_view_group(self):
         res_json = json.loads(res.body)
         assert res_json['success'] is False
 
+
+class TestResourceAction(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_01_delete_resource(self):
+        res_dict = self._add_basic_package()
+        pkg_id = res_dict['id']
+
+        resource_count = len(res_dict['resources'])
+        id = res_dict['resources'][0]['id']
+        url = '/api/action/resource_delete'
+
+        # Use the sysadmin user because this package doesn't belong to an org
+        res = self.app.post(url, params=json.dumps({'id': id}),
+                extra_environ={'Authorization': str(self.sysadmin_user.apikey)})
+        res_dict = json.loads(res.body)
+        assert res_dict['success'] is True
+
+        url = '/api/action/package_show'
+        res = self.app.get(url, {'id': pkg_id})
+        res_dict = json.loads(res.body)
+        assert res_dict['success'] is True
+        assert len(res_dict['result']['resources']) == resource_count - 1