From: Tatyana Leontovich Date: Thu, 24 Jan 2013 15:33:57 +0000 (+0200) Subject: Fix InvalidContentType can't be raised because of error in constructor X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=cb7dae08aee84d49537477d25a5798f515ff2321;p=openstack-build%2Fneutron-build.git Fix InvalidContentType can't be raised because of error in constructor InvalidContentType exception class is defined two times: in quantum.openstack.common.exception and quantum.common.exceptions with two different signatures. A lot of code like wsgi.Serializer.serialize() imports one of them but attempts to use it with the signature of another one, which ends with TypeError exception. This change fixes the bug by removing the second definition of InvalidContentType class and leaving only one which works correctly with all code. Also adds unit tests for exceptions to prevent this and similar bugs. Change-Id: I5d932d75ad184a0a6c6419190f2940bd47b7504e Fixes: bug #1104090 --- diff --git a/quantum/common/exceptions.py b/quantum/common/exceptions.py index 719618e62..2892c4518 100644 --- a/quantum/common/exceptions.py +++ b/quantum/common/exceptions.py @@ -21,6 +21,7 @@ Quantum base exception handling. from quantum.openstack.common.exception import Error from quantum.openstack.common.exception import OpenstackException +from quantum.openstack.common.exception import InvalidContentType class QuantumException(OpenstackException): @@ -181,10 +182,6 @@ class InvalidInput(BadRequest): message = _("Invalid input for operation: %(error_message)s.") -class InvalidContentType(Invalid): - message = _("Invalid content type %(content_type)s.") - - class InvalidAllocationPool(BadRequest): message = _("The allocation pool %(pool)s is not valid.") diff --git a/quantum/tests/unit/test_wsgi.py b/quantum/tests/unit/test_wsgi.py index bacd86e62..049b54f28 100644 --- a/quantum/tests/unit/test_wsgi.py +++ b/quantum/tests/unit/test_wsgi.py @@ -21,6 +21,7 @@ import socket import unittest2 as unittest from quantum import wsgi +from quantum.common import exceptions as exception class TestWSGIServer(unittest.TestCase): @@ -77,3 +78,181 @@ class TestWSGIServer(unittest.TestCase): None, mock_listen.return_value) ]) + + +class SerializerTest(unittest.TestCase): + def test_serialize_unknown_content_type(self): + """ + Test serialize verifies that exception InvalidContentType is raised + """ + input_dict = dict(servers={'test': 'pass'}) + content_type = 'application/unknown' + serializer = wsgi.Serializer() + + self.assertRaises( + exception.InvalidContentType, serializer.serialize, + input_dict, content_type) + + def test_get_deserialize_handler_unknown_content_type(self): + """ + Test get deserialize verifies + that exception InvalidContentType is raised + """ + content_type = 'application/unknown' + serializer = wsgi.Serializer() + + self.assertRaises( + exception.InvalidContentType, + serializer.get_deserialize_handler, content_type) + + +class RequestDeserializerTest(unittest.TestCase): + def test_get_body_deserializer_unknown_content_type(self): + """ + Test get body deserializer verifies + that exception InvalidContentType is raised + """ + content_type = 'application/unknown' + deserializer = wsgi.RequestDeserializer() + self.assertRaises( + exception.InvalidContentType, + deserializer.get_body_deserializer, content_type) + + +class ResponseSerializerTest(unittest.TestCase): + def setUp(self): + class JSONSerializer(object): + def serialize(self, data, action='default'): + return 'pew_json' + + class XMLSerializer(object): + def serialize(self, data, action='default'): + return 'pew_xml' + + class HeadersSerializer(object): + def serialize(self, response, data, action): + response.status_int = 404 + + self.body_serializers = { + 'application/json': JSONSerializer(), + 'application/xml': XMLSerializer()} + + self.serializer = wsgi.ResponseSerializer( + self.body_serializers, HeadersSerializer()) + + def test_serialize_unknown_content_type(self): + """ + Test serialize verifies + that exception InvalidContentType is raised + """ + self.assertRaises( + exception.InvalidContentType, + self.serializer.serialize, + {}, 'application/unknown') + + def test_get_body_serializer(self): + """ + Test get body serializer verifies + that exception InvalidContentType is raised + """ + self.assertRaises( + exception.InvalidContentType, + self.serializer.get_body_serializer, 'application/unknown') + + +class XMLDeserializerTest(unittest.TestCase): + def test_default_raise_Maiformed_Exception(self): + """ + Test verifies that exception MalformedRequestBody is raised + """ + data_string = "" + deserializer = wsgi.XMLDeserializer() + + self.assertRaises( + exception.MalformedRequestBody, deserializer.default, data_string) + + +class JSONDeserializerTest(unittest.TestCase): + def test_default_raise_Maiformed_Exception(self): + """ + Test verifies JsonDeserializer.default + raises exception MalformedRequestBody correctly + """ + data_string = "" + deserializer = wsgi.JSONDeserializer() + + self.assertRaises( + exception.MalformedRequestBody, deserializer.default, data_string) + + +class ResourceTest(unittest.TestCase): + def test_dispatch_unknown_controller_action(self): + class Controller(object): + def index(self, request, pants=None): + return pants + + def my_fault_body_function(): + return 'off' + + resource = wsgi.Resource(Controller(), my_fault_body_function) + self.assertRaises( + AttributeError, resource.dispatch, + resource.controller, 'create', {}) + + def test_malformed_request_body_throws_bad_request(self): + def my_fault_body_function(): + return 'off' + + resource = wsgi.Resource(None, my_fault_body_function) + request = wsgi.Request.blank( + "/", body="{mal:formed", method='POST', + headers={'Content-Type': "application/json"}) + + response = resource(request) + self.assertEqual(response.status_int, 400) + + def test_wrong_content_type_throws_unsupported_media_type_error(self): + def my_fault_body_function(): + return 'off' + resource = wsgi.Resource(None, my_fault_body_function) + request = wsgi.Request.blank( + "/", body="{some:json}", method='POST', + headers={'Content-Type': "xxx"}) + + response = resource(request) + self.assertEqual(response.status_int, 400) + + def test_wrong_content_type_server_error(self): + def my_fault_body_function(): + return 'off' + resource = wsgi.Resource(None, my_fault_body_function) + request = wsgi.Request.blank( + "/", method='POST', headers={'Content-Type': "unknow"}) + + response = resource(request) + self.assertEqual(response.status_int, 500) + + def test_call_resource_class_bad_request(self): + class Controller(object): + def index(self, request, index=None): + return index + + def my_fault_body_function(): + return 'off' + + class FakeRequest(): + def __init__(self): + self.url = 'http://where.no' + self.environ = 'environ' + self.body = 'body' + + def method(self): + pass + + def best_match_content_type(self): + return 'best_match_content_type' + + resource = wsgi.Resource(Controller(), my_fault_body_function) + request = FakeRequest() + result = resource(request) + self.assertEqual(400, result.status_int)