From 8bf7e003a329586203d5628369f7c2371cd5b111 Mon Sep 17 00:00:00 2001 From: amercader Date: Mon, 25 Aug 2014 13:16:06 +0100 Subject: [PATCH] [#1894] Prevent free extras to have the same key as a schema field Added a new validator to the 'key' field of the extras schema that checks if the value is present on the 'schema_fields' list that was added to the context on the previous commit. --- ckan/logic/schema.py | 3 ++- ckan/logic/validators.py | 6 ++++++ ckan/new_tests/logic/test_conversion.py | 26 ++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/ckan/logic/schema.py b/ckan/logic/schema.py index d32eb8de647..841c8762319 100644 --- a/ckan/logic/schema.py +++ b/ckan/logic/schema.py @@ -54,6 +54,7 @@ if_empty_guess_format, clean_format, no_loops_in_hierarchy, + extra_key_not_in_root_schema, ) from ckan.logic.converters import (convert_user_name_or_id_to_id, convert_package_name_or_id_to_id, @@ -373,7 +374,7 @@ def default_extras_schema(): schema = { 'id': [ignore], - 'key': [not_empty, unicode], + 'key': [not_empty, extra_key_not_in_root_schema, unicode], 'value': [not_missing], 'state': [ignore], 'deleted': [ignore_missing], diff --git a/ckan/logic/validators.py b/ckan/logic/validators.py index 9c649f33a3f..cc07f7e7ab5 100644 --- a/ckan/logic/validators.py +++ b/ckan/logic/validators.py @@ -784,3 +784,9 @@ def no_loops_in_hierarchy(key, data, errors, context): raise Invalid(_('This parent would create a loop in the ' 'hierarchy')) + +def extra_key_not_in_root_schema(key, data, errors, context): + + for schema_key in context.get('schema_keys', []): + if schema_key == data[key]: + raise Invalid(_('There is a schema field with the same name')) diff --git a/ckan/new_tests/logic/test_conversion.py b/ckan/new_tests/logic/test_conversion.py index d549f069bfe..8c892d5ea37 100644 --- a/ckan/new_tests/logic/test_conversion.py +++ b/ckan/new_tests/logic/test_conversion.py @@ -100,7 +100,7 @@ def test_convert_to_extras_field_can_be_combined_with_more_extras(self): eq_(sorted([e['value'] for e in data['extras']]), ['Bye', 'Bye2', 'Hi']) - def test_convert_to_extras_field_can_be_combined_with_more_extras_deleted(self): + def test_convert_to_extras_field_can_be_combined_with_extras_deleted(self): data_dict = { 'name': 'test-dataset', @@ -127,3 +127,27 @@ def test_convert_to_extras_field_can_be_combined_with_more_extras_deleted(self): ['custom_text', 'proper_extra', 'proper_extra2']) eq_(sorted([e['value'] for e in data['extras']]), ['Bye', 'Bye2', 'Hi']) + + def test_convert_to_extras_free_extra_can_not_have_the_same_key(self): + + data_dict = { + 'name': 'test-dataset', + 'custom_text': 'Hi', + 'extras': [ + {'key': 'custom_text', 'value': 'Bye'}, + ] + } + + context = { + 'model': model, + 'session': model.Session, + } + + package_plugin = lib_plugins.lookup_package_plugin('dataset') + schema = package_plugin.create_package_schema() + + data, errors = validate(data_dict, schema, context) + + assert 'extras' in errors + eq_(errors['extras'], + [{'key': [u'There is a schema field with the same name']}])