def test_dispatch_no_default(self): class Controller(object): def show(self, shirt, pants=None): return (shirt, pants) resource = wsgi.Resource(None) self.assertRaises(AttributeError, resource.dispatch, Controller(), 'index', 'on', pants='off')
def test_get_action_args_del_controller_error(self): actions = {'format': None, 'action': 'update', 'id': 12} env = {'wsgiorg.routing_args': [None, actions]} expected = {'action': 'update', 'id': 12} actual = wsgi.Resource(None).get_action_args(env) self.assertEqual(expected, actual)
def test_dispatch_default(self): class Controller(object): def default(self, shirt, pants=None): return (shirt, pants) resource = wsgi.Resource(None) actual = resource.dispatch(Controller(), 'index', 'on', pants='off') expected = ('on', 'off') self.assertEqual(expected, actual)
def test_resource_client_exceptions_dont_log_error(self): class Controller(object): def __init__(self, excpetion_to_raise): self.excpetion_to_raise = excpetion_to_raise def raise_exception(self, req, body): raise self.excpetion_to_raise() actions = {'action': 'raise_exception', 'body': 'data'} env = {'wsgiorg.routing_args': [None, actions]} request = wsgi.Request.blank('/tests/123', environ=env) request.body = encodeutils.safe_encode('{"foo": "value"}') resource = wsgi.Resource(Controller(self.exception)) e = self.assertRaises(self.exception_catch, resource, request) e = e.exc if hasattr(e, 'exc') else e self.assertNotIn(six.text_type(e), self.LOG.output)
def test_get_action_args(self): env = { 'wsgiorg.routing_args': [ None, { 'controller': None, 'format': None, 'action': 'update', 'id': 12, }, ], } expected = {'action': 'update', 'id': 12} actual = wsgi.Resource(None).get_action_args(env) self.assertEqual(expected, actual)
def test_resource_call_with_version_header(self): class Controller(object): def dance(self, req): return {'foo': 'bar'} actions = {'action': 'dance'} env = {'wsgiorg.routing_args': [None, actions]} request = wsgi.Request.blank('/tests/123', environ=env) request.version_request = vr.APIVersionRequest('1.0') resource = wsgi.Resource(Controller()) resp = resource(request) self.assertEqual('{"foo": "bar"}', encodeutils.safe_decode(resp.body)) self.assertTrue(hasattr(resp, 'headers')) expected = 'clustering 1.0' self.assertEqual(expected, resp.headers['OpenStack-API-Version']) self.assertEqual('OpenStack-API-Version', resp.headers['Vary'])
def test_resource_call_error_handle(self): class Controller(object): def delete(self, req, identity): return (req, identity) actions = {'action': 'delete', 'id': 12, 'body': 'data'} env = {'wsgiorg.routing_args': [None, actions]} request = wsgi.Request.blank('/tests/123', environ=env) request.body = encodeutils.safe_encode('{"foo" : "value"}') resource = wsgi.Resource(Controller()) # The Resource does not throw webob.HTTPExceptions, since they # would be considered responses by wsgi and the request flow would end, # instead they are wrapped so they can reach the fault application # where they are converted to a JSON response e = self.assertRaises(exception.HTTPExceptionDisguise, resource, request) self.assertIsInstance(e.exc, webob.exc.HTTPBadRequest)
def test_resource_call_error_handle_localized(self, mock_translate): class Controller(object): def delete(self, req, identity): return (req, identity) def fake_translate_exception(ex, locale): return translated_ex mock_translate.side_effect = fake_translate_exception actions = {'action': 'delete', 'id': 12, 'body': 'data'} env = {'wsgiorg.routing_args': [None, actions]} request = wsgi.Request.blank('/tests/123', environ=env) request.body = encodeutils.safe_encode('{"foo" : "value"}') message_es = "No Encontrado" translated_ex = webob.exc.HTTPBadRequest(message_es) resource = wsgi.Resource(Controller()) e = self.assertRaises(exception.HTTPExceptionDisguise, resource, request) self.assertEqual(message_es, str(e.exc))
def __init__(self, conf, **local_conf): self.conf = conf mapper = routes.Mapper() # version res = wsgi.Resource(version.VersionController(conf)) with mapper.submapper(controller=res) as sub_mapper: sub_mapper.connect("version", "/", action="version", conditions={'method': 'GET'}) # Profile_types res = wsgi.Resource(profile_types.ProfileTypeController(conf)) with mapper.submapper(controller=res) as sub_mapper: sub_mapper.connect("profile_type_index", "/profile-types", action="index", conditions={'method': 'GET'}) sub_mapper.connect("profile_type_get", "/profile-types/{type_name}", action="get", conditions={'method': 'GET'}) sub_mapper.connect("profile_type_ops", "/profile-types/{type_name}/ops", action="ops", conditions={'method': 'GET'}) # Profiles res = wsgi.Resource(profiles.ProfileController(conf)) with mapper.submapper(controller=res) as sub_mapper: sub_mapper.connect("profile_index", "/profiles", action="index", conditions={'method': 'GET'}) sub_mapper.connect("profile_create", "/profiles", action="create", conditions={'method': 'POST'}, success=201) sub_mapper.connect("profile_get", "/profiles/{profile_id}", action="get", conditions={'method': 'GET'}) sub_mapper.connect("profile_update", "/profiles/{profile_id}", action="update", conditions={'method': 'PATCH'}) sub_mapper.connect("profile_delete", "/profiles/{profile_id}", action="delete", conditions={'method': 'DELETE'}) sub_mapper.connect("profile_validate", "/profiles/validate", action="validate", conditions={'method': 'POST'}) # Policy Types res = wsgi.Resource(policy_types.PolicyTypeController(conf)) with mapper.submapper(controller=res) as sub_mapper: # Policy collection sub_mapper.connect("policy_type_index", "/policy-types", action="index", conditions={'method': 'GET'}) sub_mapper.connect("policy_type_get", "/policy-types/{type_name}", action="get", conditions={'method': 'GET'}) # Policies res = wsgi.Resource(policies.PolicyController(conf)) with mapper.submapper(controller=res) as sub_mapper: sub_mapper.connect("policy_index", "/policies", action="index", conditions={'method': 'GET'}) sub_mapper.connect("policy_create", "/policies", action="create", conditions={'method': 'POST'}, success=201) sub_mapper.connect("policy_get", "/policies/{policy_id}", action="get", conditions={'method': 'GET'}) sub_mapper.connect("policy_update", "/policies/{policy_id}", action="update", conditions={'method': 'PATCH'}) sub_mapper.connect("policy_delete", "/policies/{policy_id}", action="delete", conditions={'method': 'DELETE'}) sub_mapper.connect("policy_validate", "/policies/validate", action="validate", conditions={'method': 'POST'}) # Clusters res = wsgi.Resource(clusters.ClusterController(conf)) with mapper.submapper(controller=res) as sub_mapper: sub_mapper.connect("cluster_index", "/clusters", action="index", conditions={'method': 'GET'}) sub_mapper.connect("cluster_create", "/clusters", action="create", conditions={'method': 'POST'}, success=202) sub_mapper.connect("cluster_get", "/clusters/{cluster_id}", action="get", conditions={'method': 'GET'}) sub_mapper.connect("cluster_update", "/clusters/{cluster_id}", action="update", conditions={'method': 'PATCH'}, success=202) sub_mapper.connect("cluster_action", "/clusters/{cluster_id}/actions", action="action", conditions={'method': 'POST'}, success=202) sub_mapper.connect("cluster_collect", "/clusters/{cluster_id}/attrs/{path}", action="collect", conditions={'method': 'GET'}) sub_mapper.connect("cluster_delete", "/clusters/{cluster_id}", action="delete", conditions={'method': 'DELETE'}, success=202) sub_mapper.connect("cluster_operation", "/clusters/{cluster_id}/ops", action="operation", conditions={'method': 'POST'}, success=202) # Nodes res = wsgi.Resource(nodes.NodeController(conf)) with mapper.submapper(controller=res) as sub_mapper: sub_mapper.connect("node_index", "/nodes", action="index", conditions={'method': 'GET'}) sub_mapper.connect("node_create", "/nodes", action="create", conditions={'method': 'POST'}, success=202) sub_mapper.connect("node_adopt", "/nodes/adopt", action="adopt", conditions={'method': 'POST'}) sub_mapper.connect("node_adopt_preview", "/nodes/adopt-preview", action="adopt_preview", conditions={'method': 'POST'}) sub_mapper.connect("node_get", "/nodes/{node_id}", action="get", conditions={'method': 'GET'}) sub_mapper.connect("node_update", "/nodes/{node_id}", action="update", conditions={'method': 'PATCH'}, success=202) sub_mapper.connect("node_action", "/nodes/{node_id}/actions", action="action", conditions={'method': 'POST'}, success=202) sub_mapper.connect("node_delete", "/nodes/{node_id}", action="delete", conditions={'method': 'DELETE'}, success=202) sub_mapper.connect("node_operation", "/nodes/{node_id}/ops", action="operation", conditions={'method': 'POST'}, success=202) # Cluster Policies res = wsgi.Resource(cluster_policies.ClusterPolicyController(conf)) policies_path = "/clusters/{cluster_id}" with mapper.submapper(controller=res, path_prefix=policies_path) as sub_mapper: sub_mapper.connect("cluster_policy_list", "/policies", action="index", conditions={'method': 'GET'}) sub_mapper.connect("cluster_policy_show", "/policies/{policy_id}", action="get", conditions={'method': 'GET'}) # Actions res = wsgi.Resource(actions.ActionController(conf)) with mapper.submapper(controller=res) as sub_mapper: sub_mapper.connect("action_index", "/actions", action="index", conditions={'method': 'GET'}) sub_mapper.connect("action_create", "/actions", action="create", conditions={'method': 'POST'}, success=201) sub_mapper.connect("action_get", "/actions/{action_id}", action="get", conditions={'method': 'GET'}) # Receivers res = wsgi.Resource(receivers.ReceiverController(conf)) with mapper.submapper(controller=res) as sub_mapper: sub_mapper.connect("receivers_index", "/receivers", action="index", conditions={'method': 'GET'}) sub_mapper.connect("receiver_create", "/receivers", action="create", conditions={'method': 'POST'}, success=201) sub_mapper.connect("receiver_get", "/receivers/{receiver_id}", action="get", conditions={'method': 'GET'}) sub_mapper.connect("receiver_update", "/receivers/{receiver_id}", action="update", conditions={'method': 'PATCH'}) sub_mapper.connect("receiver_delete", "/receivers/{receiver_id}", action="delete", conditions={'method': 'DELETE'}) sub_mapper.connect("receiver_notify", "/receivers/{receiver_id}/notify", action="notify", conditions={'method': 'POST'}) # Webhooks res = wsgi.Resource(webhooks.WebhookController(conf)) with mapper.submapper(controller=res) as sub_mapper: sub_mapper.connect("webhook_trigger", "/webhooks/{webhook_id}/trigger", action="trigger", conditions={'method': 'POST'}, success=202) # Events res = wsgi.Resource(events.EventController(conf)) with mapper.submapper(controller=res) as sub_mapper: sub_mapper.connect("event_index", "/events", action="index", conditions={'method': 'GET'}) sub_mapper.connect("event_get", "/events/{event_id}", action="get", conditions={'method': 'GET'}) # Info res = wsgi.Resource(build_info.BuildInfoController(conf)) with mapper.submapper(controller=res) as sub_mapper: sub_mapper.connect("build_info", "/build-info", action="build_info", conditions={'method': 'GET'}) super(API, self).__init__(mapper) # Services res = wsgi.Resource(services.ServiceController(conf)) with mapper.submapper(controller=res) as sub_mapper: sub_mapper.connect("service_index", "/services", action="index", conditions={'method': 'GET'}) super(API, self).__init__(mapper)
def test_get_action_args_invalid_index(self): env = {'wsgiorg.routing_args': []} expected = {} actual = wsgi.Resource(None).get_action_args(env) self.assertEqual(expected, actual)