def test_response_headers_encoded(self): # prepare environment for_openstack_comrades = \ u'\u0417\u0430 \u043e\u043f\u0435\u043d\u0441\u0442\u0435\u043a, ' \ u'\u0442\u043e\u0432\u0430\u0440\u0438\u0449\u0438' class FakeController(object): def index(self, shirt, pants=None): return (shirt, pants) class FakeSerializer(object): def index(self, response, result): response.headers['unicode_test'] = for_openstack_comrades # make request resource = wsgi.Resource(FakeController(), None, FakeSerializer()) actions = {'action': 'index'} env = {'wsgiorg.routing_args': [None, actions]} request = wsgi.Request.blank('/tests/123', environ=env) response = resource.__call__(request) # ensure it has been encoded correctly value = (response.headers['unicode_test'].decode('utf-8') if six.PY2 else response.headers['unicode_test']) self.assertEqual(for_openstack_comrades, value)
def test_dispatch_default(self): class Controller(object): def default(self, shirt, pants=None): return (shirt, pants) resource = wsgi.Resource(None, None, None) actual = resource.dispatch(Controller(), 'index', 'on', pants='off') expected = ('on', 'off') self.assertEqual(expected, actual)
def test_dispatch_no_default(self): class Controller(object): def show(self, shirt, pants=None): return (shirt, pants) resource = wsgi.Resource(None, None, None) self.assertRaises(AttributeError, resource.dispatch, Controller(), 'index', 'on', pants='off')
def test_call_raises_exception(self): class FakeController(object): def index(self, shirt, pants=None): return (shirt, pants) resource = wsgi.Resource(FakeController(), None, None) with mock.patch('glare.common.wsgi.Resource.dispatch', side_effect=Exception("test exception")): request = wsgi.Request.blank('/') response = resource.__call__(request) self.assertIsInstance(response, webob.exc.HTTPInternalServerError) self.assertEqual(http.INTERNAL_SERVER_ERROR, response.status_code)
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, None, None).get_action_args(env) self.assertEqual(expected, actual)
def test_resource_call_error_handle_localized(self, mock_translate_exception): class Controller(object): def delete(self, req, identity): raise webob.exc.HTTPBadRequest(explanation='Not Found') actions = {'action': 'delete', 'identity': 12} env = {'wsgiorg.routing_args': [None, actions]} request = wsgi.Request.blank('/tests/123', environ=env) message_es = 'No Encontrado' resource = wsgi.Resource(Controller(), wsgi.JSONRequestDeserializer(), None) translated_exc = webob.exc.HTTPBadRequest(message_es) mock_translate_exception.return_value = translated_exc e = self.assertRaises(webob.exc.HTTPBadRequest, resource, request) self.assertEqual(message_es, str(e))
def test_call(self): class FakeController(object): def index(self, shirt, pants=None): return shirt, pants resource = wsgi.Resource(FakeController(), None, None) def dispatch(obj, *args, **kwargs): if isinstance(obj, wsgi.JSONRequestDeserializer): return [] if isinstance(obj, wsgi.JSONResponseSerializer): raise webob.exc.HTTPForbidden() with mock.patch('glare.common.wsgi.Resource.dispatch', side_effect=dispatch): request = wsgi.Request.blank('/') response = resource.__call__(request) self.assertIsInstance(response, webob.exc.HTTPForbidden) self.assertEqual(http.FORBIDDEN, response.status_code)
def create_resource(): """Artifact resource factory method""" deserializer = RequestDeserializer() serializer = ResponseSerializer() controller = ArtifactsController() return wsgi.Resource(controller, deserializer, serializer)
def __init__(self, mapper): glare_resource = resource.create_resource() reject_method_resource = wsgi.Resource(wsgi.RejectMethodController()) # ---schemas--- mapper.connect('/schemas', controller=glare_resource, action='list_type_schemas', conditions={'method': ['GET']}, body_reject=True) mapper.connect('/schemas', controller=reject_method_resource, action='reject', allowed_methods='GET') mapper.connect('/schemas/{type_name}', controller=glare_resource, action='show_type_schema', conditions={'method': ['GET']}, body_reject=True) mapper.connect('/schemas/{type_name}', controller=reject_method_resource, action='reject', allowed_methods='GET') # ---artifacts--- mapper.connect('/artifacts/{type_name}', controller=glare_resource, action='list', conditions={'method': ['GET']}, body_reject=True) mapper.connect('/artifacts/{type_name}', controller=glare_resource, action='create', conditions={'method': ['POST']}) mapper.connect('/artifacts/{type_name}', controller=reject_method_resource, action='reject', allowed_methods='GET, POST') mapper.connect('/artifacts/{type_name}/{artifact_id}', controller=glare_resource, action='update', conditions={'method': ['PATCH']}) mapper.connect('/artifacts/{type_name}/{artifact_id}', controller=glare_resource, action='show', conditions={'method': ['GET']}, body_reject=True) mapper.connect('/artifacts/{type_name}/{artifact_id}', controller=glare_resource, action='delete', conditions={'method': ['DELETE']}, body_reject=True) mapper.connect('/artifacts/{type_name}/{artifact_id}', controller=reject_method_resource, action='reject', allowed_methods='GET, PATCH, DELETE') # ---blobs--- mapper.connect('/artifacts/{type_name}/{artifact_id}/{blob_path:.*?}', controller=glare_resource, action='download_blob', conditions={'method': ['GET']}, body_reject=True) mapper.connect('/artifacts/{type_name}/{artifact_id}/{blob_path:.*?}', controller=glare_resource, action='upload_blob', conditions={'method': ['PUT']}) mapper.connect('/artifacts/{type_name}/{artifact_id}/{blob_path:.*?}', controller=glare_resource, action='delete_external_blob', conditions={'method': ['DELETE']}) mapper.connect('/artifacts/{type_name}/{artifact_id}/{blob_path:.*?}', controller=reject_method_resource, action='reject', allowed_methods='GET, PUT, DELETE') # ---quotas--- mapper.connect('/quotas', controller=glare_resource, action='set_quotas', conditions={'method': ['PUT']}) mapper.connect('/quotas', controller=glare_resource, action='list_all_quotas', conditions={'method': ['GET']}, body_reject=True) mapper.connect('/quotas', controller=reject_method_resource, action='reject', allowed_methods='PUT, GET') mapper.connect('/project-quotas', controller=glare_resource, action='list_project_quotas', conditions={'method': ['GET']}, body_reject=True) mapper.connect('/project-quotas', controller=reject_method_resource, action='reject', allowed_methods='GET') mapper.connect('/project-quotas/{project_id}', controller=glare_resource, action='list_project_quotas', conditions={'method': ['GET']}, body_reject=True) mapper.connect('/project-quotas/{project_id}', controller=reject_method_resource, action='reject', allowed_methods='GET') super(API, self).__init__(mapper)
def _get_artifacts_resource(self): deserializer = resource.RequestDeserializer() serializer = resource.ResponseSerializer() controller = resource.ArtifactsController() return wsgi.Resource(controller, deserializer, serializer)
def test_get_action_args_del_format_error(self): actions = {'action': 'update', 'id': 12} env = {'wsgiorg.routing_args': [None, actions]} expected = {'action': 'update', 'id': 12} actual = wsgi.Resource(None, None, None).get_action_args(env) self.assertEqual(expected, actual)
def test_get_action_args_invalid_index(self): env = {'wsgiorg.routing_args': []} expected = {} actual = wsgi.Resource(None, None, None).get_action_args(env) self.assertEqual(expected, actual)