def test_process_api_response_missing_required_field(): api_response = { 'response': 1, 'responseMessage': 'Transaction successful.', } try: conversions.process_api_response(api_response) except KeyError as error: assert str(error) == "'notice'" else: assert False
def test__process_api_response__missing_required_field(): """Confirms handling when a required field is missing.""" api_response = { 'response': 1, 'responseMessage': 'Transaction successful.', } try: conversions.process_api_response(api_response) except KeyError as error: assert str(error) == "'notice'" else: assert False
def test__process_api_response__valid(): """Tests handling of a valid API response.""" api_response = { 'response': 1, 'responseMessage': 'Transaction successful.', 'notice': 'API v2 being depreciated.', 'transaction': { 'amount': '50.01', 'cardNumber': '1111********9999', } } raw_request = { 'field_1': 'Field value 1', 'field_2': 'Field value 2', } raw_response = 'This is a raw response.' response = conversions.process_api_response(api_response, raw_request, raw_response) assert len(response) == 8 assert response['transaction_success'] is True assert response['response_message'] == 'Transaction successful.' assert response['notice'] == 'API v2 being depreciated.' assert 'field_1=Field value 1' in response['raw_request'] assert 'field_2=Field value 2' in response['raw_request'] assert response['raw_response'] == 'This is a raw response.' assert response['amount'] == Decimal('50.01') assert response['cc_number'] == '1111********9999' assert response['token_f4l4'] == '11119999'
def test_process_api_response_create_f4l4_with_no_cc(): api_response = { 'response': 1, 'responseMessage': '', 'notice': '', 'transaction': {}, } response = conversions.process_api_response(api_response) assert response['token_f4l4'] is None
def test_process_api_response_create_f4l4(): api_response = { 'response': 1, 'responseMessage': '', 'notice': '', 'transaction': { 'cardNumber': '1111********9999', } } response = conversions.process_api_response(api_response) assert response['token_f4l4'] == '11119999'
def test_process_api_response_invalid_type(): api_response = { 'response': 1, 'responseMessage': '', 'notice': '', 'transaction': { 'invalid': 'field', } } response = conversions.process_api_response(api_response) assert 'invalid' in response assert response['invalid'] == 'field'
def test_process_api_response_string_field(): api_response = { 'response': 1, 'responseMessage': 'Transaction successful.', 'notice': '', 'transaction': { 'cardNumber': '1111********9999', } } response = conversions.process_api_response(api_response) assert response['cc_number'] == '1111********9999' assert isinstance(response['cc_number'], str)
def test_process_api_response_time_field(): api_response = { 'response': 1, 'responseMessage': 'Transaction successful.', 'notice': '', 'transaction': { 'time': '08:30:15', } } response = conversions.process_api_response(api_response) assert response['transaction_time'] == time(8, 30, 15) assert isinstance(response['transaction_time'], time)
def test_process_api_response_date_field(): api_response = { 'response': 1, 'responseMessage': 'Transaction successful.', 'notice': '', 'transaction': { 'date': '2018-01-01', } } response = conversions.process_api_response(api_response) assert response['transaction_date'] == date(2018, 1, 1) assert isinstance(response['transaction_date'], date)
def test_process_api_response_boolean_field(): api_response = { 'response': 1, 'responseMessage': 'Transaction successful.', 'notice': '', 'transaction': { 'availability': '1', } } response = conversions.process_api_response(api_response) assert response['availability'] is True assert isinstance(response['availability'], bool)
def test_process_api_response_integer_field(): api_response = { 'response': 1, 'responseMessage': 'Transaction successful.', 'notice': '', 'transaction': { 'transactionId': '101', } } response = conversions.process_api_response(api_response) assert response['transaction_id'] == 101 assert isinstance(response['transaction_id'], int)
def test_process_api_response_decimal_field(): api_response = { 'response': 1, 'responseMessage': 'Transaction successful.', 'notice': '', 'transaction': { 'amount': '50.01', } } response = conversions.process_api_response(api_response) assert response['amount'] == Decimal('50.01') assert isinstance(response['amount'], Decimal)
def test_process_api_response_string_field_none(): """Tests that string field can handle a None response.""" api_response = { 'response': 1, 'responseMessage': 'Transaction successful.', 'notice': '', 'transaction': { 'cardNumber': None, } } response = conversions.process_api_response(api_response) assert response['cc_number'] is None
def test_process_api_response_missing_field(): api_response = { 'response': 1, 'responseMessage': 'Transaction successful.', 'notice': '', 'transaction': { 'fake_field': 'fake.', } } response = conversions.process_api_response(api_response) assert response['fake_field'] == 'fake.' assert isinstance(response['fake_field'], str)
def test_process_api_response_missing_transaction(): api_response = { 'response': 1, 'responseMessage': 'Transaction successful.', 'notice': '', } response = conversions.process_api_response(api_response) assert len(response) == 5 assert 'notice' in response assert 'raw_request' in response assert 'raw_response' in response assert 'response_message' in response assert 'transaction_success' in response
def test__process_api_response__missing_transaction(): """Confirms handling of an API response when transaction field missing.""" api_response = { 'response': 1, 'responseMessage': 'Transaction successful.', 'notice': '', } response = conversions.process_api_response(api_response) assert len(response) == 5 assert 'notice' in response assert 'raw_request' in response assert 'raw_response' in response assert 'response_message' in response assert 'transaction_success' in response
def post(self, post_data=None): """Makes POST to Helcim API and updates response attribute. Parameters: post_data (dict): The parameters to pass with the POST request. Raises: ProcessingError: An error occurred connecting or communicating with Helcim API. """ # Make the POST request try: response = requests.post( self.api['url'], data=post_data, ) except requests.ConnectionError: raise helcim_exceptions.ProcessingError( 'Unable to connect to Helcim API ({})'.format(self.api['url'])) # Catch any response errors in status code if response.status_code != 200: raise helcim_exceptions.ProcessingError( 'Helcim API request failed with status code {}'.format( response.status_code)) # Create the dictionary ('message' is the XML structure object) dict_response = xmltodict.parse(response.text)['message'] # Catch any issues with the API response if dict_response['response'] == '0': self.process_error_response(dict_response['responseMessage']) # Return the response self.response = conversions.process_api_response( dict_response, post_data, response.text)