From f4496c0d93ad6cd67248fa360031e4778ef31f56 Mon Sep 17 00:00:00 2001 From: amercader Date: Tue, 18 Dec 2018 12:06:10 +0100 Subject: [PATCH] [#4594] Support alternative license format, add tests --- ckan/model/license.py | 4 +++- ckan/tests/model/licenses.v1 | 16 +++++++++++++- ckan/tests/model/licenses.v2 | 15 ++++++++++++- ckan/tests/model/test_license.py | 37 ++++++++++++++++++++++++-------- 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/ckan/model/license.py b/ckan/model/license.py index 46fd7038357..8965de0e3f6 100644 --- a/ckan/model/license.py +++ b/ckan/model/license.py @@ -6,7 +6,7 @@ from ckan.common import config from paste.deploy.converters import asbool -from six import text_type +from six import text_type, string_types from ckan.common import _, json import ckan.lib.maintain as maintain @@ -127,6 +127,8 @@ def load_licenses(self, license_url): msg = "Couldn't read response from licenses service %r: %s" % (response_body, inst) raise Exception(inst) for license in license_data: + if isinstance(license, string_types): + license = license_data[license] if license.get('title'): license['title'] = _(license['title']) self._create_license_list(license_data, license_url) diff --git a/ckan/tests/model/licenses.v1 b/ckan/tests/model/licenses.v1 index cf0ee424080..c50ab678a4e 100644 --- a/ckan/tests/model/licenses.v1 +++ b/ckan/tests/model/licenses.v1 @@ -13,5 +13,19 @@ "domain_software": "False", "id": "cc-by" -} +}, + { + "domain_content": true, + "domain_data": false, + "domain_software": false, + "family": "", + "id": "other-open", + "is_generic": true, + "maintainer": "", + "od_conformance": "approved", + "osd_conformance": "not reviewed", + "status": "active", + "title": "Other (Open)", + "url": "" + } ] diff --git a/ckan/tests/model/licenses.v2 b/ckan/tests/model/licenses.v2 index cc78f75d93c..91b338d0cdd 100644 --- a/ckan/tests/model/licenses.v2 +++ b/ckan/tests/model/licenses.v2 @@ -12,6 +12,19 @@ "status": "active", "title": "Creative Commons Attribution 4.0", "url": "https://creativecommons.org/licenses/by/4.0/" - +}, +"other-open": { + "domain_content": true, + "domain_data": false, + "domain_software": false, + "family": "", + "id": "other-open", + "is_generic": true, + "maintainer": "", + "od_conformance": "approved", + "osd_conformance": "not reviewed", + "status": "active", + "title": "Other (Open)", + "url": "" } } diff --git a/ckan/tests/model/test_license.py b/ckan/tests/model/test_license.py index 733cbdb5f4b..85dcc514d2b 100644 --- a/ckan/tests/model/test_license.py +++ b/ckan/tests/model/test_license.py @@ -6,7 +6,11 @@ from ckan.common import config from ckan.model.license import LicenseRegister -from ckan.tests import helpers +from ckan.tests import helpers, factories + +assert_in = helpers.assert_in + +this_dir = os.path.dirname(os.path.realpath(__file__)) class TestLicenseRegister(object): @@ -24,11 +28,8 @@ def test_default_register_has_basic_properties_of_a_license(self): assert_equal(license.isopen(), True) assert_equal(license.title, 'Creative Commons Attribution') + @helpers.change_config('licenses_group_url', 'file:///%s/licenses.v1' % this_dir) def test_import_v1_style_register(self): - this_dir = os.path.dirname(os.path.realpath(__file__)) - # v1 is used by CKAN so far - register_filepath = '%s/licenses.v1' % this_dir - config['licenses_group_url'] = 'file:///%s' % register_filepath reg = LicenseRegister() license = reg['cc-by'] @@ -37,11 +38,9 @@ def test_import_v1_style_register(self): assert_equal(license.isopen(), True) assert_equal(license.title, 'Creative Commons Attribution') + # v2 is used by http://licenses.opendefinition.org in recent times + @helpers.change_config('licenses_group_url', 'file:///%s/licenses.v2' % this_dir) def test_import_v2_style_register(self): - this_dir = os.path.dirname(os.path.realpath(__file__)) - # v2 is used by http://licenses.opendefinition.org in recent times - register_filepath = '%s/licenses.v2' % this_dir - config['licenses_group_url'] = 'file:///%s' % register_filepath reg = LicenseRegister() license = reg['CC-BY-4.0'] @@ -50,6 +49,26 @@ def test_import_v2_style_register(self): assert_equal(license.isopen(), True) assert_equal(license.title, 'Creative Commons Attribution 4.0') + @helpers.change_config('licenses_group_url', 'file:///%s/licenses.v1' % this_dir) + @helpers.change_config('ckan.locale_default', 'ca') + def test_import_v1_style_register_i18n(self): + + sysadmin = factories.Sysadmin() + app = helpers._get_test_app() + + resp = app.get('/dataset/new', extra_environ={'REMOTE_USER': str(sysadmin['name'])}) + assert_in('Altres (Oberta)', resp.body) + + @helpers.change_config('licenses_group_url', 'file:///%s/licenses.v2' % this_dir) + @helpers.change_config('ckan.locale_default', 'ca') + def test_import_v2_style_register_i18n(self): + + sysadmin = factories.Sysadmin() + app = helpers._get_test_app() + + resp = app.get('/dataset/new', extra_environ={'REMOTE_USER': str(sysadmin['name'])}) + assert_in('Altres (Oberta)', resp.body) + class TestLicense: def test_access_via_attribute(self):