Skip to content

Commit

Permalink
[#4594] Support alternative license format, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Dec 18, 2018
1 parent 1566d83 commit f4496c0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
4 changes: 3 additions & 1 deletion ckan/model/license.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion ckan/tests/model/licenses.v1
Expand Up @@ -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": ""
}
]
15 changes: 14 additions & 1 deletion ckan/tests/model/licenses.v2
Expand Up @@ -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": ""
}
}
37 changes: 28 additions & 9 deletions ckan/tests/model/test_license.py
Expand Up @@ -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):
Expand All @@ -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']
Expand All @@ -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']
Expand All @@ -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):
Expand Down

0 comments on commit f4496c0

Please sign in to comment.