Skip to content

Commit

Permalink
[#953] Don't allow private datasets with no organization
Browse files Browse the repository at this point in the history
Add a validator to prevent private datasets with no organization from
being created by package_create or package_update.

Conflicts:

	ckan/logic/schema.py
	ckan/logic/validators.py
  • Loading branch information
Sean Hammond authored and amercader committed Jul 1, 2013
1 parent 1a05c74 commit f7ee934
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
7 changes: 5 additions & 2 deletions ckan/logic/schema.py
Expand Up @@ -48,7 +48,9 @@
user_name_exists,
role_exists,
url_validator,
list_of_strings)
datasets_with_no_organization_cannot_be_private,
list_of_strings,
)
from ckan.logic.converters import (convert_user_name_or_id_to_id,
convert_package_name_or_id_to_id,
convert_group_name_or_id_to_id,)
Expand Down Expand Up @@ -137,7 +139,8 @@ def default_create_package_schema():
'type': [ignore_missing, unicode],
'owner_org': [owner_org_validator, unicode],
'log_message': [ignore_missing, unicode, no_http],
'private': [ignore_missing, boolean_validator],
'private': [ignore_missing, boolean_validator,
datasets_with_no_organization_cannot_be_private],
'__extras': [ignore],
'__junk': [empty],
'resources': default_resource_schema(),
Expand Down
6 changes: 6 additions & 0 deletions ckan/logic/validators.py
Expand Up @@ -639,3 +639,9 @@ def list_of_strings(key, data, errors, context):
for x in value:
if not isinstance(x, basestring):
raise Invalid('%s: %s' % (_('Not a string'), x))

def datasets_with_no_organization_cannot_be_private(key, data, errors,
context):
if data[key] is True and data.get(('owner_org',)) is None:
errors[key].append(
_("Datasets with no organization can't be private."))
32 changes: 32 additions & 0 deletions ckan/tests/functional/api/model/test_package.py
Expand Up @@ -11,6 +11,8 @@
from ckan.tests.functional.api.base import Api1TestCase as Version1TestCase
from ckan.tests.functional.api.base import Api2TestCase as Version2TestCase

import ckan.tests as tests

# Todo: Remove this ckan.model stuff.
import ckan.model as model

Expand Down Expand Up @@ -776,6 +778,36 @@ def test_package_revisions(self):
revisions = res.json
assert len(revisions) == 3, len(revisions)

def test_create_private_package_with_no_organization(self):
'''Test that private packages with no organization cannot be created.
'''
testsysadmin = model.User.by_name('testsysadmin')
result = tests.call_action_api(self.app, 'package_create', name='test',
private=True, apikey=testsysadmin.apikey, status=409)
assert result == {'__type': 'Validation Error',
'private': ["Datasets with no organization can't be private."]}

def test_create_public_package_with_no_organization(self):
'''Test that public packages with no organization can be created.'''
testsysadmin = model.User.by_name('testsysadmin')
tests.call_action_api(self.app, 'package_create', name='test',
private=False, apikey=testsysadmin.apikey)

def test_make_package_with_no_organization_private(self):
'''Test that private packages with no organization cannot be created
by package_update.
'''
testsysadmin = model.User.by_name('testsysadmin')
package = tests.call_action_api(self.app, 'package_create',
name='test_2', private=False, apikey=testsysadmin.apikey)
package['private'] = True
result = tests.call_action_api(self.app, 'package_update',
apikey=testsysadmin.apikey, status=409, **package)
assert result == {'__type': 'Validation Error',
'private': ["Datasets with no organization can't be private."]}


class TestPackagesVersion1(Version1TestCase, PackagesTestCase):
def test_06_create_pkg_using_download_url(self):
Expand Down

0 comments on commit f7ee934

Please sign in to comment.