Skip to content

Commit

Permalink
[#1216] Correct the logic in resource_delete
Browse files Browse the repository at this point in the history
resource_delete, as it stands, is broken. This fixes the logic and adds tests
for resource_delete. Fixes #1216.
  • Loading branch information
nigelbabu committed Sep 3, 2013
1 parent b26147e commit 5588681
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ckan/logic/action/delete.py
Expand Up @@ -73,8 +73,10 @@ 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)
for res in pkg_dict.get('resources', []):
if res['id'] == id:
pkg_dict['resources'].remove(res)
break
try:
pkg_dict = _get_action('package_update')(context, pkg_dict)
except ValidationError, e:
Expand Down
53 changes: 53 additions & 0 deletions ckan/tests/logic/test_action.py
Expand Up @@ -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

0 comments on commit 5588681

Please sign in to comment.