Skip to content

Commit

Permalink
[#2733] Check that resource ID exists for create/delete
Browse files Browse the repository at this point in the history
logic functions. Add test for deleting invalid
resource ID.
  • Loading branch information
johnglover committed Jul 30, 2012
1 parent 7e31f90 commit 8181655
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
18 changes: 9 additions & 9 deletions ckanext/datastore/logic/action/create.py
@@ -1,14 +1,10 @@
import logging
import pylons
import ckan.logic as logic
import ckan.logic.action
import ckan.lib.dictization
import ckan.plugins as p
import ckanext.datastore.db as db

log = logging.getLogger(__name__)

_validate = ckan.lib.navl.dictization_functions.validate
_check_access = logic.check_access
_get_or_bust = logic.get_or_bust


Expand All @@ -26,11 +22,15 @@ def datastore_create(context, data_dict):
:rtype: dictionary
'''
_get_or_bust(context, 'model')
_get_or_bust(data_dict, 'resource_id')
# TODO: check that resource_id exists in database
model = _get_or_bust(context, 'model')
id = _get_or_bust(data_dict, 'resource_id')

if not model.Resource.get(id):
raise p.toolkit.ObjectNotFound(p.toolkit._(
'Resource "{}" was not found.'.format(id)
))

_check_access('datastore_create', context, data_dict)
p.toolkit.check_access('datastore_create', context, data_dict)

data_dict['connection_url'] = pylons.config['ckan.datastore_write_url']

Expand Down
17 changes: 9 additions & 8 deletions ckanext/datastore/logic/action/delete.py
@@ -1,14 +1,10 @@
import logging
import pylons
import ckan.logic as logic
import ckan.logic.action
import ckan.lib.dictization
import ckan.plugins as p
import ckanext.datastore.db as db

log = logging.getLogger(__name__)

_validate = ckan.lib.navl.dictization_functions.validate
_check_access = logic.check_access
_get_or_bust = logic.get_or_bust


Expand All @@ -24,10 +20,15 @@ def datastore_delete(context, data_dict):
:rtype: dictionary
'''
_get_or_bust(context, 'model')
_get_or_bust(data_dict, 'resource_id')
model = _get_or_bust(context, 'model')
id = _get_or_bust(data_dict, 'resource_id')

if not model.Resource.get(id):
raise p.toolkit.ObjectNotFound(p.toolkit._(
'Resource "{}" was not found.'.format(id)
))

_check_access('datastore_delete', context, data_dict)
p.toolkit.check_access('datastore_delete', context, data_dict)

data_dict['connection_url'] = pylons.config['ckan.datastore_write_url']

Expand Down
25 changes: 18 additions & 7 deletions ckanext/datastore/tests/test_datastore.py
Expand Up @@ -336,20 +336,20 @@ def setup_class(cls):
ctd.CreateTestData.create()
cls.sysadmin_user = model.User.get('testsysadmin')
cls.normal_user = model.User.get('annafan')

@classmethod
def teardown_class(cls):
model.repo.rebuild_db()

def setup(self):
resource = model.Package.get('annakarenina').resources[0]
self.data = {
cls.data = {
'resource_id': resource.id,
'fields': [{'id': 'book', 'type': 'text'},
{'id': 'author', 'type': 'text'}],
'records': [{'book': 'annakarenina', 'author': 'tolstoy'},
{'book': 'crime', 'author': ['tolstoy', 'dostoevsky']}]
}

@classmethod
def teardown_class(cls):
model.repo.rebuild_db()

def _create(self):
postparams = '%s=1' % json.dumps(self.data)
auth = {'Authorization': str(self.sysadmin_user.apikey)}
res = self.app.post('/api/action/datastore_create', params=postparams,
Expand All @@ -358,6 +358,7 @@ def setup(self):
assert res_dict['success'] is True

def test_delete_basic(self):
self._create()
id = self.data['resource_id']
data = {'resource_id': id}
postparams = '%s=1' % json.dumps(data)
Expand All @@ -378,3 +379,13 @@ def test_delete_basic(self):
except sqlalchemy.exc.ProgrammingError as e:
expected_msg = 'relation "{}" does not exist'.format(id)
assert expected_msg in str(e)

model.Session.remove()

def test_delete_invalid_resource_id(self):
postparams = '%s=1' % json.dumps({'resource_id': 'bad'})
auth = {'Authorization': str(self.sysadmin_user.apikey)}
res = self.app.post('/api/action/datastore_delete', params=postparams,
extra_environ=auth, status=404)
res_dict = json.loads(res.body)
assert res_dict['success'] is False

0 comments on commit 8181655

Please sign in to comment.