Commit af0c79aa authored by Romain Courteaud's avatar Romain Courteaud

erp5_json_rpc_api: fail fail

parent 763b1475
...@@ -34,15 +34,30 @@ class JsonRpcAPITestCase(ERP5TypeTestCase): ...@@ -34,15 +34,30 @@ class JsonRpcAPITestCase(ERP5TypeTestCase):
_type_id = 'JSON RPC API Test Service' _type_id = 'JSON RPC API Test Service'
_action_list_text = '' _action_list_text = ''
def addJSONForm(self, script_id, body): def addJSONForm(self, script_id, input_json_schema=None,
after_method_id=None):
self.portal.portal_callables.newContent( self.portal.portal_callables.newContent(
portal_type='JSON Form', portal_type='JSON Form',
id=script_id, id=script_id,
text_content=body text_content=input_json_schema,
after_method_id=after_method_id
) )
self.tic() self.tic()
self._json_form_id_to_cleanup.append(script_id) self._json_form_id_to_cleanup.append(script_id)
def addPythonScript(self, script_id, params, body):
skin_folder = self.portal.portal_skins['custom']
skin_folder.manage_addProduct['ERP5'].addPythonScriptThroughZMI(
id=script_id)
self.script = skin_folder.get(script_id)
self.script.setParameterSignature(params)
self.script.setBody(body)
self.tic()
self._python_script_id_to_cleanup.append(script_id)
self.portal.changeSkin(None)
def afterSetUp(self): def afterSetUp(self):
self.portal.portal_types.newContent( self.portal.portal_types.newContent(
portal_type='JSON RPC API Type', portal_type='JSON RPC API Type',
...@@ -84,6 +99,9 @@ class JsonRpcAPITestCase(ERP5TypeTestCase): ...@@ -84,6 +99,9 @@ class JsonRpcAPITestCase(ERP5TypeTestCase):
self.tic() self.tic()
if self._json_form_id_to_cleanup: if self._json_form_id_to_cleanup:
self.portal.portal_callables.manage_delObjects(self._json_form_id_to_cleanup) self.portal.portal_callables.manage_delObjects(self._json_form_id_to_cleanup)
skin_folder = self.portal.portal_skins['custom']
if self._python_script_id_to_cleanup:
skin_folder.manage_delObjects(self._python_script_id_to_cleanup)
self.tic() self.tic()
""" """
...@@ -341,6 +359,81 @@ error.handling.callable | JsonRpcService_testExample''' ...@@ -341,6 +359,81 @@ error.handling.callable | JsonRpcService_testExample'''
'type': 'unknown-error' 'type': 'unknown-error'
}) })
def test_requestErrorHandling_unknownAfterMethod(self):
self.addJSONForm(
'JsonRpcService_testExample',
'{}',
after_method_id='THISSCRIPTDOESNOTEXIST',
)
response = self.publish(
self.connector.getPath() + '/error.handling.callable',
user='ERP5TypeTestCase',
request_method='POST',
stdin=io.BytesIO(
'{}'.encode()),
env={'CONTENT_TYPE': 'application/json'})
self.assertEqual(response.getStatus(), 500)
self.assertEqual(response.getHeader('content-type'), 'application/json')
self.assertEqual(
json.loads(response.getBody()), {
"type": "unknown-error",
"title": "AttributeError: 'RequestContainer' object has no attribute 'THISSCRIPTDOESNOTEXIST'"
})
def test_requestErrorHandling_failingAfterMethod(self):
self.addPythonScript(
'JsonRpcService_fail',
'data_dict, json_form',
'1//0',
)
self.addJSONForm(
'JsonRpcService_testExample',
'{}',
after_method_id='JsonRpcService_fail',
)
response = self.publish(
self.connector.getPath() + '/error.handling.callable',
user='ERP5TypeTestCase',
request_method='POST',
stdin=io.BytesIO(
'{}'.encode()),
env={'CONTENT_TYPE': 'application/json'})
self.assertEqual(response.getStatus(), 500)
self.assertEqual(response.getHeader('content-type'), 'application/json')
self.assertEqual(
json.loads(response.getBody()), {
"type": "unknown-error",
"title": "ZeroDivisionError: integer division or modulo by zero"
})
def test_requestErrorHandling_abortTransactionOnError(self):
self.addPythonScript(
'JsonRpcService_fail',
'data_dict, json_form',
'context.getPortalObject().setTitle("ooops")\n'
'1/0',
)
self.addJSONForm(
'JsonRpcService_testExample',
'{}',
after_method_id='JsonRpcService_fail',
)
response = self.publish(
self.connector.getPath() + '/error.handling.callable',
user='ERP5TypeTestCase',
request_method='POST',
stdin=io.BytesIO(
'{}'.encode()),
env={'CONTENT_TYPE': 'application/json'})
self.assertEqual(response.getStatus(), 500)
self.assertEqual(response.getHeader('content-type'), 'application/json')
self.assertEqual(
json.loads(response.getBody()), {
"type": "unknown-error",
"title": "ZeroDivisionError: integer division or modulo by zero"
})
self.assertNotEqual(self.portal.getTitle(), "ooops")
class TestJsonRpcAPIJsonFormHandling(JsonRpcAPITestCase): class TestJsonRpcAPIJsonFormHandling(JsonRpcAPITestCase):
_action_list_text = '''json.form.handling.callable | JsonRpcService_callFromTest''' _action_list_text = '''json.form.handling.callable | JsonRpcService_callFromTest'''
......
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