def test_request_timeout_backoff(self, mocked_request, mocked_sleep): """ Verify make_request function is backoff for 5 times on ConnectionError exceeption """ config = {"start_date": "2017-01-01T00:00:00Z"} chargebee_client = _client.ChargebeeClient(config) try: chargebee_client.make_request('/abc', 'GET') except requests.exceptions.ConnectionError: pass # Verify that requests.request is called 5 times self.assertEqual(mocked_request.call_count, 5)
def test_json_decode_200(self, mocked_jsondecode_successful): """ No exception should be raised for successfull API request """ json_decode_str = '{"success": true, "data" : []}' mocked_jsondecode_successful.return_value = get_mock_http_response( 200, json_decode_str) config = {"start_date": "2017-01-01T00:00:00Z"} chargebee_client = _client.ChargebeeClient(config) # No exception should be raised with JSON decoder error chargebee_client.make_request('/abc', 'GET') self.assertEqual(mocked_jsondecode_successful.call_count, 1)
def test_json_decode_failed_4XX(self, mocked_sleep, mocked_jsondecode_failure): """ Exception with proper custom error message should be raised if invalid JSON response returned with 4XX error """ json_decode_error_str = '<>"success": true, "data" : []}' mocked_jsondecode_failure.return_value = get_mock_http_response( 400, json_decode_error_str) config = {"start_date": "2017-01-01T00:00:00Z"} chargebee_client = _client.ChargebeeClient(config) expected_message = "HTTP-error-code: 400, Error: The request URI does not match the APIs in the system." with self.assertRaises(_client.ChargebeeBadRequestError) as e: chargebee_client.make_request("/abc", "GET") # Verifying the formatted message for json decoder exception self.assertEquals(str(e), str(expected_message))
def test_no_request_timeout_in_config(self, mocked_request): """ Verify that if request_timeout is not provided in config then default value is used """ config = { "start_date": "2017-01-01T00:00:00Z" } # No request_timeout in config chargebee_client = _client.ChargebeeClient(config) # Call make_request method which call requests.request with timeout chargebee_client.make_request('/abc', 'GET') # Verify requests.request is called with expected timeout mocked_request.assert_called_with('GET', '/abc', auth=(None, ''), headers={}, json=None, params={ 'limit': 100, 'include_deleted': True }, timeout=300) # Expected timeout
def test_json_decode_failed_4XX(self, mocked_jsondecode_failure): """ Exception with Unknown error message should be raised if invalid JSON response returned with 4XX error """ json_decode_error_str = '<>"success": true, "data" : []}' mocked_jsondecode_failure.return_value = get_mock_http_response( 400, json_decode_error_str) config = {"start_date": "2017-01-01T00:00:00Z"} chargebee_client = _client.ChargebeeClient(config) try: chargebee_client.make_request('/abc', 'GET') except _client.Server4xxError as e: expected_message = { "message": "Did not get response from the server due to an unknown error.", "http_status_code": 400 } # Verifying the formatted message for json decoder exception self.assertEquals(str(e), str(expected_message)) pass
def test_string_request_timeout_in_config(self, mocked_request): """ Verify that if request_timeout is provided in config(string value) then it should be use """ config = { "start_date": "2017-01-01T00:00:00Z", "request_timeout": "100" } # string format timeout in config chargebee_client = _client.ChargebeeClient(config) # Call make_request method which call requests.request with timeout chargebee_client.make_request('/abc', 'GET') # Verify requests.request is called with expected timeout mocked_request.assert_called_with('GET', '/abc', auth=(None, ''), headers={}, json=None, params={ 'limit': 100, 'include_deleted': True }, timeout=100.0) # Expected timeout
def test_json_decode_successfull_4XX(self, mocked_jsondecode_successful): """ Exception with response message should be raised if valid JSON response returned with 4XX error """ json_decode_str = { "message": "Sorry, authentication failed. Invalid api key", "api_error_code": "api_authentication_failed", "error_code": "api_authentication_invalid_key", "error_msg": "Sorry, authentication failed. Invalid api key", "http_status_code": 401 } mocked_jsondecode_successful.return_value = get_mock_http_response( 401, json.dumps(json_decode_str)) config = {"start_date": "2017-01-01T00:00:00Z"} chargebee_client = _client.ChargebeeClient(config) try: chargebee_client.make_request('/abc', 'GET') except _client.Server4xxError as e: expected_message = json_decode_str # Verifying the message should be API response self.assertEquals(str(e), str(expected_message)) pass