Skip to content

Commit

Permalink
Merge pull request #1971 from aliceh75/1970-ensure-field-names-are-st…
Browse files Browse the repository at this point in the history
…ripped

Ensure that field names are stripped
  • Loading branch information
wardi committed Nov 3, 2014
2 parents b9c98d9 + d20d539 commit 0bbfcc1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 33 deletions.
4 changes: 3 additions & 1 deletion ckanext/datastore/db.py
Expand Up @@ -71,11 +71,13 @@ def _pluck(field, arr):
def _is_valid_field_name(name):
'''
Check that field name is valid:
* can't start or end with whitespace characters
* can't start with underscore
* can't contain double quote (")
* can't be empty
'''
return name.strip() and not name.startswith('_') and not '"' in name
return (name and name == name.strip() and not name.startswith('_')
and not '"' in name)


def _is_valid_table_name(name):
Expand Down
46 changes: 14 additions & 32 deletions ckanext/datastore/tests/test_create.py
Expand Up @@ -305,39 +305,21 @@ def test_create_invalid_field_type(self):

def test_create_invalid_field_name(self):
resource = model.Package.get('annakarenina').resources[0]
data = {
'resource_id': resource.id,
'fields': [{'id': 'book', 'type': 'text'},
{'id': '_author', 'type': 'text'}]
}
postparams = '%s=1' % json.dumps(data)
auth = {'Authorization': str(self.sysadmin_user.apikey)}
res = self.app.post('/api/action/datastore_create', params=postparams,
extra_environ=auth, status=409)
res_dict = json.loads(res.body)
assert res_dict['success'] is False

data = {
'resource_id': resource.id,
'fields': [{'id': 'book', 'type': 'text'},
{'id': '"author', 'type': 'text'}]
}
postparams = '%s=1' % json.dumps(data)
res = self.app.post('/api/action/datastore_create', params=postparams,
extra_environ=auth, status=409)
res_dict = json.loads(res.body)
assert res_dict['success'] is False

data = {
'resource_id': resource.id,
'fields': [{'id': 'book', 'type': 'text'},
{'id': '', 'type': 'text'}]
}
postparams = '%s=1' % json.dumps(data)
res = self.app.post('/api/action/datastore_create', params=postparams,
extra_environ=auth, status=409)
res_dict = json.loads(res.body)
assert res_dict['success'] is False
invalid_names = ['_author', '"author', '', ' author', 'author ',
'\tauthor', 'author\n']

for field_name in invalid_names:
data = {
'resource_id': resource.id,
'fields': [{'id': 'book', 'type': 'text'},
{'id': field_name, 'type': 'text'}]
}
postparams = '%s=1' % json.dumps(data)
res = self.app.post('/api/action/datastore_create', params=postparams,
extra_environ=auth, status=409)
res_dict = json.loads(res.body)
assert res_dict['success'] is False

def test_create_invalid_record_field(self):
resource = model.Package.get('annakarenina').resources[0]
Expand Down

0 comments on commit 0bbfcc1

Please sign in to comment.