Commit d58ed158 authored by Boxiang Sun's avatar Boxiang Sun Committed by Romain Courteaud

erp5_hal_json_style: return 404 error if no view was found

Do not return wrong content type
parent 5134dd20
......@@ -1449,7 +1449,11 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
view_context = traversed_document
else:
view_context = traversed_document.restrictedTraverse(current_action['other_context'])
view_instance = getattr(view_context, current_action['view_id'])
view_instance = getattr(view_context, current_action['view_id'], None)
if view_instance is None:
response.setStatus(404)
return ""
if (view_instance is not None):
embedded_dict = {
'_links': {
......@@ -2385,7 +2389,6 @@ if mode == 'url_generator':
context.Base_prepareCorsResponse(RESPONSE=response)
response.setHeader('Content-Type', mime_type)
hateoas = calculateHateoas(relative_url=relative_url,
REQUEST=REQUEST, response=response, view=view, mode=mode,
query=query, select_list=select_list, limit=limit, form=form,
......@@ -2396,4 +2399,5 @@ hateoas = calculateHateoas(relative_url=relative_url,
if hateoas == "":
return hateoas
else:
response.setHeader('Content-Type', mime_type)
return json.dumps(hateoas, indent=2)
......@@ -708,6 +708,19 @@ class TestERP5Document_getHateoas_mode_traverse(ERP5HALJSONStyleSkinsMixin):
document.getRelativeUrl()))
self.assertEqual(result_dict['_embedded']['_view']['_actions']['put']['method'], 'POST')
@simulate('Base_getRequestUrl', '*args, **kwargs',
'return "http://example.org/bar"')
@simulate('Base_getRequestHeader', '*args, **kwargs',
'return "application/hal+json"')
@changeSkin('Hal')
def test_getHateoasDocument_non_existing_action(self):
document = self._makeDocument()
fake_request = do_fake_request("GET")
result = self.portal.web_site_module.hateoas.ERP5Document_getHateoas(REQUEST=fake_request, mode="traverse", relative_url=document.getRelativeUrl(), view="not_existing_action")
self.assertEquals(fake_request.RESPONSE.status, 404)
self.assertEquals(fake_request.RESPONSE.getHeader('Content-Type'), None)
self.assertEqual(result, "")
@simulate('Base_getRequestUrl', '*args, **kwargs',
'return "http://example.org/bar"')
@simulate('Base_getRequestHeader', '*args, **kwargs',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment