Skip to content

Commit

Permalink
Fix 500 errors caused by templates (#395)
Browse files Browse the repository at this point in the history
- includes small helper function that validates descriptor - tested
- logic updated and test included to check invalid licenses 

refs #355
  • Loading branch information
zelima authored May 10, 2017
1 parent 7426499 commit dd984f7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/logic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def get_package(publisher, package):
return None

descriptor = metadata.get('descriptor')
descriptor = validate_for_template(descriptor)
readme = metadata.get('readme')
descriptor['owner'] = publisher
readme_variables_replaced = dp_in_readme(readme, descriptor)
Expand Down Expand Up @@ -218,3 +219,25 @@ def generate_signed_url():
props=filedata[relative_path])
res_payload['filedata'][relative_path] = response.build_file_information()
return res_payload


#### helpers

def validate_for_template(descriptor):
'''
Validates field types in the descriptor for template, e.g. licenses property should be a list.
'''
licenses = descriptor.get('licenses')

if licenses is None or type(licenses) is list:
return descriptor

if type(licenses) is dict:
license = descriptor.pop('licenses')
license = license.get('type')
descriptor['license'] = license
return descriptor

descriptor.pop('licenses')

return descriptor
6 changes: 6 additions & 0 deletions app/templates/_snippets.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ <h3>KEY INFO</h3>
<br />
<a href="https://opendefinition.org" title="This data is open data as per the Open Definition"><img src="https://assets.okfn.org/images/buttons/od_80x15_blue.png" /></a>
</div>
{% elif dataset.license %}
<div class="license">
<i class="fa fa-gavel"></i> {{ dataset.license }}
<br />
<a href="https://opendefinition.org" title="This data is open data as per the Open Definition"><img src="https://assets.okfn.org/images/buttons/od_80x15_blue.png" /></a>
</div>
{% endif %}
</div>

Expand Down
31 changes: 31 additions & 0 deletions tests/logic/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,34 @@ def tearDown(self):
db.session.remove()
db.drop_all()
db.engine.dispose()

class HelpersTest(unittest.TestCase):


def setUp(self):
self.descriptor = json.loads(open('fixtures/datapackage.json').read())


def test_validate_for_jinja_returns_descriptor_if_no_licenses(self):
descriptor = validate_for_template(self.descriptor)
self.assertEqual(self.descriptor, descriptor)

def test_validate_for_jinja_returns_descriptor_if_licenses_is_list(self):
self.descriptor['licenses'] = []
descriptor = validate_for_template(self.descriptor)
self.assertEqual(self.descriptor, descriptor)

def test_validate_for_jinja_modifies_descriptor_if_licenses_is_dict(self):
self.descriptor['licenses'] = {'url': 'test/url', 'type': 'Test'}
descriptor = validate_for_template(self.descriptor)
self.assertEqual(descriptor['license'], 'Test')

def test_validate_for_not_errors_if_licenses_is_dict_and_has_no_type_key(self):
self.descriptor['licenses'] = {'url': 'test/url'}
descriptor = validate_for_template(self.descriptor)
self.assertEqual(descriptor['license'], None)

def test_validate_for_jinja_removes_licenses_if_invalid_type(self):
self.descriptor['licenses'] = 1
descriptor = validate_for_template(self.descriptor)
self.assertEqual(descriptor.get('licenses'), None)
15 changes: 15 additions & 0 deletions tests/site/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ def test_data_package_page(self):
rv = self.client.get('/non-existing/demo-package')
self.assertEqual(404, rv.status_code)


def test_data_package_page_loads_if_descriptor_has_bad_licenses(self):
descriptor = json.loads(open('fixtures/datapackage.json').read())
descriptor['licenses'] = {'url': 'test/url', 'type': 'Test'}
with self.app.app_context():
publisher = Publisher(name=self.publisher)
metadata = Package(name=self.package)
metadata.tags.append(PackageTag(descriptor=descriptor))
publisher.packages.append(metadata)
db.session.add(publisher)
db.session.commit()
rv = self.client.get('/%s/%s' %(self.publisher,self.package))
self.assertEqual(200, rv.status_code)


def test_data_package_page_load_without_views(self):
descriptor = {"data": [], "resources": []}
with self.app.app_context():
Expand Down

0 comments on commit dd984f7

Please sign in to comment.