def test_null_action_name(self): action = IntrospectionAction(FakeServerOne()) with self.assertRaises(ActionError) as error_context: action( EnrichedActionRequest(action='introspect', body={'action_name': None})) self.assertEqual(1, len(error_context.exception.errors)) self.assertEqual(ERROR_CODE_INVALID, error_context.exception.errors[0].code) self.assertEqual('action_name', error_context.exception.errors[0].field)
def test_whole_server_complex(self): action = IntrospectionAction(FakeServerTwo()) response = action(EnrichedActionRequest(action='introspect', body={})) self.assertEqual([], response.errors) self.assertEqual( { 'documentation': 'Instead, we should get this documentation', 'action_names': ['introspect', 'one', 'status', 'two'], 'actions': { 'introspect': { 'documentation': IntrospectionAction.description, 'request_schema': IntrospectionAction.request_schema.introspect(), 'response_schema': IntrospectionAction.response_schema.introspect(), }, 'status': { 'documentation': BaseStatusAction.description, 'request_schema': BaseStatusAction.request_schema.introspect(), 'response_schema': BaseStatusAction.response_schema.introspect(), }, 'one': { 'documentation': 'The real documentation', 'request_schema': None, 'response_schema': None, }, 'two': { 'documentation': 'Test action documentation', 'request_schema': FakeActionTwo.request_schema.introspect(), 'response_schema': FakeActionTwo.response_schema.introspect(), }, }, }, response.body, )
def test_single_action_simple(self): action = IntrospectionAction(FakeServerOne()) response = action( EnrichedActionRequest(action='introspect', body={'action_name': 'one'})) self.assertEqual([], response.errors) self.assertEqual( { 'action_names': ['one'], 'actions': { 'one': { 'documentation': 'The real documentation', 'request_schema': None, 'response_schema': None, }, }, }, response.body, )
def test_whole_server_simple(self): action = IntrospectionAction(FakeServerOne()) response = action(EnrichedActionRequest(action='introspect', body={})) self.assertEqual([], response.errors) self.assertEqual( { 'documentation': 'This is the documentation we should get', 'action_names': ['introspect', 'one', 'status'], 'actions': { 'introspect': { 'documentation': IntrospectionAction.description, 'request_schema': IntrospectionAction.request_schema.introspect(), 'response_schema': IntrospectionAction.response_schema.introspect(), }, 'status': { 'documentation': BaseStatusAction.description, 'request_schema': BaseStatusAction.request_schema.introspect(), 'response_schema': BaseStatusAction.response_schema.introspect(), }, 'one': { 'documentation': 'The real documentation', 'request_schema': None, 'response_schema': None, }, }, }, response.body, )
def test_single_action_status_default(self): action = IntrospectionAction(FakeServerTwo()) response = action( EnrichedActionRequest(action='introspect', body={'action_name': 'status'})) self.assertEqual([], response.errors) self.assertEqual( { 'action_names': ['status'], 'actions': { 'status': { 'documentation': BaseStatusAction.description, 'request_schema': BaseStatusAction.request_schema.introspect(), 'response_schema': BaseStatusAction.response_schema.introspect(), }, }, }, response.body, )
def test_single_action_complex(self): action = IntrospectionAction(FakeServerTwo()) response = action( EnrichedActionRequest(action='introspect', body={'action_name': 'two'})) self.assertEqual([], response.errors) self.assertEqual( { 'action_names': ['two'], 'actions': { 'two': { 'documentation': 'Test action documentation', 'request_schema': FakeActionTwo.request_schema.introspect(), 'response_schema': FakeActionTwo.response_schema.introspect(), }, }, }, response.body, )
def execute_job(self, job_request): """ Processes and runs the action requests contained in the job and returns a `JobResponse`. :param job_request: The job request :type job_request: dict :return: A `JobResponse` object :rtype: JobResponse """ # Run the Job's Actions job_response = JobResponse() job_switches = RequestSwitchSet(job_request['context']['switches']) for i, raw_action_request in enumerate(job_request['actions']): action_request = EnrichedActionRequest( action=raw_action_request['action'], body=raw_action_request.get('body', None), switches=job_switches, context=job_request['context'], control=job_request['control'], client=job_request['client'], async_event_loop=job_request['async_event_loop'], ) action_in_class_map = action_request.action in self.action_class_map if action_in_class_map or action_request.action in ('status', 'introspect'): # Get action to run if action_in_class_map: action = self.action_class_map[action_request.action](self.settings) elif action_request.action == 'introspect': from pysoa.server.action.introspection import IntrospectionAction action = IntrospectionAction(server=self) else: if not self._default_status_action_class: from pysoa.server.action.status import make_default_status_action_class self._default_status_action_class = make_default_status_action_class(self.__class__) action = self._default_status_action_class(self.settings) # Wrap it in middleware wrapper = self.make_middleware_stack( [m.action for m in self.middleware], action, ) # Execute the middleware stack try: action_response = wrapper(action_request) except ActionError as e: # Error: an error was thrown while running the Action (or Action middleware) action_response = ActionResponse( action=action_request.action, errors=e.errors, ) else: # Error: Action not found. action_response = ActionResponse( action=action_request.action, errors=[Error( code=ERROR_CODE_UNKNOWN, message='The action "{}" was not found on this server.'.format(action_request.action), field='action', )], ) job_response.actions.append(action_response) if ( action_response.errors and not job_request['control'].get('continue_on_error', False) ): # Quit running Actions if an error occurred and continue_on_error is False break return job_response
def test_whole_server_switched(self): action = IntrospectionAction(FakeServerThree()) response = action(EnrichedActionRequest(action='introspect', body={})) self.assertEqual([], response.errors) self.assertEqual( { 'documentation': None, 'action_names': [ 'introspect', 'my_switched_action[DEFAULT]', 'my_switched_action[switch:5]', 'status', 'your_switched_action[DEFAULT]', 'your_switched_action[switch:4]', ], 'actions': { 'introspect': { 'documentation': IntrospectionAction.description, 'request_schema': IntrospectionAction.request_schema.introspect(), 'response_schema': IntrospectionAction.response_schema.introspect(), }, 'my_switched_action[DEFAULT]': { 'documentation': 'The real documentation', 'request_schema': None, 'response_schema': None, }, 'my_switched_action[switch:5]': { 'documentation': 'Test action documentation', 'request_schema': FakeActionTwo.request_schema.introspect(), 'response_schema': FakeActionTwo.response_schema.introspect(), }, 'status': { 'documentation': BaseStatusAction.description, 'request_schema': BaseStatusAction.request_schema.introspect(), 'response_schema': BaseStatusAction.response_schema.introspect(), }, 'your_switched_action[switch:4]': { 'documentation': 'The real documentation', 'request_schema': None, 'response_schema': None, }, 'your_switched_action[DEFAULT]': { 'documentation': 'Test action documentation', 'request_schema': FakeActionTwo.request_schema.introspect(), 'response_schema': FakeActionTwo.response_schema.introspect(), }, }, }, response.body, )
def test_single_action_switched(self): action = IntrospectionAction(FakeServerThree()) response = action( EnrichedActionRequest( action='introspect', body={'action_name': 'my_switched_action[DEFAULT]'}, )) self.assertEqual([], response.errors) self.assertEqual( { 'action_names': ['my_switched_action[DEFAULT]'], 'actions': { 'my_switched_action[DEFAULT]': { 'documentation': 'The real documentation', 'request_schema': None, 'response_schema': None, }, }, }, response.body, ) response = action( EnrichedActionRequest( action='introspect', body={'action_name': 'my_switched_action[switch:3]'}, )) self.assertEqual([], response.errors) self.assertEqual( { 'action_names': ['my_switched_action[switch:3]'], 'actions': { 'my_switched_action[switch:3]': { 'documentation': 'The real documentation', 'request_schema': None, 'response_schema': None, }, }, }, response.body, ) response = action( EnrichedActionRequest( action='introspect', body={'action_name': 'my_switched_action[switch:5]'}, )) self.assertEqual([], response.errors) self.assertEqual( { 'action_names': ['my_switched_action[switch:5]'], 'actions': { 'my_switched_action[switch:5]': { 'documentation': 'Test action documentation', 'request_schema': FakeActionTwo.request_schema.introspect(), 'response_schema': FakeActionTwo.response_schema.introspect(), }, }, }, response.body, ) response = action( EnrichedActionRequest( action='introspect', body={'action_name': 'your_switched_action'}, )) self.assertEqual([], response.errors) self.assertEqual( { 'action_names': [ 'your_switched_action[DEFAULT]', 'your_switched_action[switch:4]' ], 'actions': { 'your_switched_action[switch:4]': { 'documentation': 'The real documentation', 'request_schema': None, 'response_schema': None, }, 'your_switched_action[DEFAULT]': { 'documentation': 'Test action documentation', 'request_schema': FakeActionTwo.request_schema.introspect(), 'response_schema': FakeActionTwo.response_schema.introspect(), }, }, }, response.body, )