def setUp(self): AwsExceptionsFactory._EX_REGISTRY.clear() AwsExceptionsFactory.add_exception("TestEx", TestEx) self._http_cls = Mock() self._http_module = Mock(return_value=self._http_cls) self._http_client = AwsHttpClient(self._http_module)
def test_add_exception(self): '''Test case for add_exception method success scenario.''' AwsExceptionsFactory.add_exception("AccessDenied", TestEx) result = AwsExceptionsFactory._EX_REGISTRY.get("AccessDenied") self.assertIsNotNone(result) self.assertIs(TestEx, result)
def test_get_exception_concrete(self): '''Test case for get exception method when a concrete exception is found.''' AwsExceptionsFactory.add_exception("AccessDenied", TestEx) for code in ["AccessDenied", "Access.Denied"]: self._error_response["ErrorResponse"]["Error"]["Code"] = code ex = AwsExceptionsFactory.get_exception(self._error_response) self.assertIsInstance(ex, TestEx) self.assertEqual(400, ex.http_status) self.assertEqual("Sender", ex.error_type) self.assertEqual("AccessDenied", ex.error_code) self.assertEqual("Test message", ex.error_msg) self.assertEqual("123", ex.request_id)
def do_request(self, url, headers, action, method="GET"): '''Method used to execute an aws http request. In case an exception occurs an aws strong type exception is thrown. Otherwise the json response is returned. :param url: The AWS SQS url we want to invoke through http. :type url: string :param headers: A dictionary containing all signed headers we want to send to aws. :type headers: dict :param action: The sqs action we are currently invoking against SQS. It is used for extracting only useful part of the response. :type action: string :param method: The HTTP method used for invoking the url. :type method: string :returns: json :except: :py:class:`aws.core.aws_exceptions.AwsGenericException` ''' request = self._http_module() resp, content = request.request(url, method, headers=headers) if resp.status >= 400: err_resp = json.loads(content.decode()) raise AwsExceptionsFactory.get_exception(err_resp) if not content: return {} content = json.loads(content.decode()) return content["%sResponse" % action]["%sResult" % action]
def test_get_exception_generic(self): '''Test case for get exception method when no concrete exception is found but the error response is valid.''' ex = AwsExceptionsFactory.get_exception(self._error_response, 400) self.assertIsInstance(ex, AwsGenericException) self.assertEqual(400, ex.http_status) self.assertEqual("Sender", ex.error_type) self.assertEqual("AccessDenied", ex.error_code) self.assertEqual("Test message", ex.error_msg) self.assertEqual("123", ex.request_id)
def test_register_exceptions_from_module(self): '''Test case for checking that exceptions from a given module are registered correctly.''' expected_exceptions = inspect.getmembers(aws_exceptions, lambda obj: inspect.isclass(obj) and issubclass(obj, aws_exceptions.AwsGenericException) and obj != aws_exceptions.AwsGenericException) self.assertGreater(len(expected_exceptions), 0, "aws_exceptions module must contain all common aws exceptions.") aws_config.register_exceptions(aws_exceptions) self.assertEqual(len(expected_exceptions), len(AwsExceptionsFactory._EX_REGISTRY.keys())) err_resp = {"ErrorResponse": {"Error": {"Type": "Sender", "Message": "Test", "Code": "AccessDenied"}, "RequestId": "123"}} ex = AwsExceptionsFactory.get_exception(err_resp) self.assertEqual(403, ex.http_status) self.assertEqual("Test", ex.error_msg) self.assertEqual("Sender", ex.error_type) self.assertEqual("123", ex.request_id)
def test_add_exception_exist(self): '''Test case for add_exception method when the code is already registered in the factory.''' AwsExceptionsFactory.add_exception("AccessDenied", TestEx) self.assertRaises(ValueError, AwsExceptionsFactory.add_exception, *["AccessDenied", TestEx])