Skip to content

Commit

Permalink
Require organization_id to be provided for updates to collections
Browse files Browse the repository at this point in the history
Ref #67
  • Loading branch information
mark-saeon committed May 3, 2019
1 parent 227143a commit da6c591
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ckanext/metadata/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def metadata_collection_create_schema():

def metadata_collection_update_schema():
schema = metadata_collection_create_schema()
schema['organization_id'] = [empty] # cannot change the organization to which a collection belongs
schema['organization_id'] = [v.not_empty, unicode, v.metadata_collection_org_unchanged]
_make_update_schema(schema)
return schema

Expand Down
17 changes: 17 additions & 0 deletions ckanext/metadata/logic/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,23 @@ def owner_org_owns_metadata_collection(key, data, errors, context):
raise tk.Invalid(_("owner_org must be the same organization that owns the metadata collection"))


def metadata_collection_org_unchanged(key, data, errors, context):
"""
Ensures that a metadata collection cannot be moved to a different organization.
"""
model = context['model']
session = context['session']

id_ = data.get(key[:-1] + ('id',))
new_organization_id = data.get(key[:-1] + ('organization_id',))
new_organization_id = model.Group.get(new_organization_id).id
old_organization_id = session.query(model.GroupExtra.value) \
.filter_by(group_id=id_, key='organization_id').scalar()

if new_organization_id != old_organization_id:
raise tk.Invalid(_("Organization cannot be changed"))


def metadata_record_id_name_generator(key, data, errors, context):
"""
Generates id and name for a new metadata record where these values have not been supplied.
Expand Down
5 changes: 4 additions & 1 deletion ckanext/metadata/tests/test_metadata_collection_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ def test_update_valid(self):
'name': 'updated-test-metadata-collection',
'title': 'Updated Test Metadata Collection',
'description': 'Updated test metadata collection',
'organization_id': metadata_collection['organization_id'],
}
result, obj = self.test_action('metadata_collection_update', **input_dict)
assert obj.type == 'metadata_collection'
assert obj.is_organization == False
del input_dict['organization_id']
assert_object_matches_dict(obj, input_dict)
assert_group_has_extra(obj.id, 'organization_id', metadata_collection['organization_id'])

Expand All @@ -83,6 +85,7 @@ def test_update_valid_partial(self):
input_dict = {
'id': metadata_collection['id'],
'title': 'Updated Test Metadata Collection',
'organization_id': metadata_collection['organization_id'],
}
result, obj = self.test_action('metadata_collection_update', **input_dict)
assert obj.type == 'metadata_collection'
Expand Down Expand Up @@ -120,7 +123,7 @@ def test_update_invalid_cannot_change_organization(self):
'organization_id': organization['id'],
}
result, obj = self.test_action('metadata_collection_update', should_error=True, **input_dict)
assert_error(result, 'organization_id', 'The input field organization_id was not expected.')
assert_error(result, 'organization_id', 'Organization cannot be changed')

def test_delete_valid(self):
metadata_collection = ckanext_factories.MetadataCollection()
Expand Down

0 comments on commit da6c591

Please sign in to comment.