def test_get_billing_address_exception(self, mock_logger): """ Verify that funtion is rturning None and log proper error in case of exception. """ basket = self.create_basket_with_product() response_data = transaction_detail_response_success_data transaction_detail_xml = get_authorizenet_transaction_reponse_xml( self.transaction_id, basket, response_data) transaction_detail_response = objectify.fromstring( transaction_detail_xml) transaction_bill = transaction_detail_response.transaction.billTo order_number = str( transaction_detail_response.transaction.order.invoiceNumber) Country.objects.filter( iso_3166_1_a2__iexact=transaction_bill.country).delete() actual_billing_address = self.view.get_billing_address( transaction_bill, order_number, basket) self.assertEqual(actual_billing_address, None) expected_exception_msg = ( 'An error occurred while parsing the billing address for basket [%d]. ' 'No billing address will be stored for the resulting order [%s].') mock_logger.exception.assert_called_once_with(expected_exception_msg, basket.id, order_number)
def test_notification_for_invalid_basket(self, mock_controller, mock_basket_id, mock_logger): """ Test received notification for an invalid basket id. """ basket = self.create_basket_with_product() response_data = transaction_detail_response_success_data transaction_detail_xml = get_authorizenet_transaction_reponse_xml( self.transaction_id, basket, response_data) transaction_detail_response = objectify.fromstring( transaction_detail_xml) mock_controller.return_value.getresponse.return_value = transaction_detail_response basket_id = "invalid_basket_id" mock_basket_id.return_value = basket_id response = self.get_notification_response( responseCode=3, transaction_id=self.transaction_id) self.assertEqual(response.status_code, 200) mock_logger.error.assert_called_once_with( 'Received AuthorizeNet payment notification for non-existent basket [%s].', basket_id) self.assertFalse( Order.objects.filter( number=basket.order_number, total_incl_tax=basket.total_incl_tax).exists())
def test_get_billing_address(self): """ Verify that Billing address object is returning proprly containing required information. """ basket = self.create_basket_with_product() response_data = transaction_detail_response_success_data transaction_detail_xml = get_authorizenet_transaction_reponse_xml( self.transaction_id, basket, response_data) transaction_detail_response = objectify.fromstring( transaction_detail_xml) transaction_bill = transaction_detail_response.transaction.billTo order_number = str( transaction_detail_response.transaction.order.invoiceNumber) actual_billing_address = self.view.get_billing_address( transaction_bill, order_number, basket) self.assertEqual(actual_billing_address.first_name, transaction_bill.firstName) self.assertEqual(actual_billing_address.last_name, transaction_bill.lastName) self.assertEqual(actual_billing_address.line1, '') self.assertEqual(actual_billing_address.state, '') self.assertEqual(actual_billing_address.country.printable_name, self.country_name)
def test_notification_error_transaction(self, mock_controller, mock_email): """ Test received notification of an error transaction. """ basket = self.create_basket_with_product() response_data = transaction_detail_response_success_data transaction_detail_xml = get_authorizenet_transaction_reponse_xml( self.transaction_id, basket, response_data) transaction_detail_response = objectify.fromstring( transaction_detail_xml) mock_controller.return_value.getresponse.return_value = transaction_detail_response response = self.get_notification_response( responseCode=3, transaction_id=self.transaction_id) self.assertEqual(response.status_code, 200) mock_email.assert_called_once_with( basket.owner, 'TRANSACTION_REJECTED', { 'course_title': basket.all_lines()[0].product.title, 'transaction_status': 'Error', }, basket.site) self.assertFalse( Order.objects.filter( number=basket.order_number, total_incl_tax=basket.total_incl_tax).exists())
def test_get_transaction_detail_error(self, mock_controller): """ Verify the processor raises MissingTransactionDetailError on receiving error response from AuthorizeNet (transaction detail) API. """ response_data = transaction_detail_response_error_data transaction_detail_xml = get_authorizenet_transaction_reponse_xml( self.transaction_id, self.basket, response_data) transaction_detail_response = objectify.fromstring(transaction_detail_xml) mock_controller.return_value.getresponse.return_value = transaction_detail_response self.assertRaises( MissingTransactionDetailError, self.processor.get_transaction_detail, self.transaction_id)
def test_call_handle_order_placement(self): """ Verify that funtion is placing order properly. """ basket = self.create_basket_with_product() response_data = transaction_detail_response_success_data transaction_detail_xml = get_authorizenet_transaction_reponse_xml( self.transaction_id, basket, response_data) transaction_detail_response = objectify.fromstring( transaction_detail_xml) self.view.call_handle_order_placement(basket, self.client, transaction_detail_response) Order.objects.get(number=basket.order_number, total_incl_tax=basket.total_incl_tax)
def test_get_transaction_detail_success(self, mock_controller): """ Verify the processor returns the transaction_detail properly on receiving success response from AuthorizeNet (transaction detail) API """ response_data = transaction_detail_response_success_data transaction_detail_xml = get_authorizenet_transaction_reponse_xml( self.transaction_id, self.basket, response_data) transaction_detail_response = objectify.fromstring(transaction_detail_xml) mock_controller.return_value.getresponse.return_value = transaction_detail_response expected_transaction_detail_object = objectify.fromstring(transaction_detail_xml) expected_transaction_detail_xml = etree.tostring(expected_transaction_detail_object) actual_transaction_detail_object = self.processor.get_transaction_detail(self.transaction_id) actual_transaction_detail_xml = etree.tostring(actual_transaction_detail_object) self.assertEqual(actual_transaction_detail_xml, expected_transaction_detail_xml)
def test_notification_success_transaction(self, mock_controller): """ Test received notification success scenerio. """ basket = self.create_basket_with_product() response_data = transaction_detail_response_success_data transaction_detail_xml = get_authorizenet_transaction_reponse_xml( self.transaction_id, basket, response_data) transaction_detail_response = objectify.fromstring( transaction_detail_xml) mock_controller.return_value.getresponse.return_value = transaction_detail_response response = self.get_notification_response( responseCode=1, transaction_id=self.transaction_id) card_info = transaction_detail_response.transaction.payment.creditCard self.assertEqual(response.status_code, 200) self.assert_order_created(basket, card_info.cardType, card_info.cardNumber)
def test_handle_processor_response(self): """ Verify that the processor creates the appropriate PaymentEvent and Source objects. """ response_data = transaction_detail_response_success_data transaction_detail_xml = get_authorizenet_transaction_reponse_xml( self.transaction_id, self.basket, response_data) transaction_response = objectify.fromstring(transaction_detail_xml) expected_transaction_dict = LxmlObjectJsonEncoder().encode(transaction_response) expected_transaction = transaction_response.transaction expected_card_info = expected_transaction.payment.creditCard actual_handled_response = self.processor.handle_processor_response(transaction_response, basket=self.basket) self.assertEqual(actual_handled_response.currency, self.basket.currency) self.assertEqual(actual_handled_response.total, float(expected_transaction.settleAmount)) self.assertEqual(actual_handled_response.transaction_id, expected_transaction.transId) self.assertEqual(actual_handled_response.card_type, expected_card_info.cardType) self.assertEqual(actual_handled_response.card_number, expected_card_info.cardNumber) self.assert_processor_response_recorded( self.processor_name, expected_transaction.transId, expected_transaction_dict, basket=self.basket)
def test_call_handle_order_placement_exception(self, mock_order_calculator, mock_logger_function): """ Verify that function do not place an order in case of exception """ basket = self.create_basket_with_product() response_data = transaction_detail_response_success_data transaction_detail_xml = get_authorizenet_transaction_reponse_xml( self.transaction_id, basket, response_data) transaction_detail_response = objectify.fromstring( transaction_detail_xml) mock_order_calculator.side_effect = Exception self.view.call_handle_order_placement(basket, self.client, transaction_detail_response) mock_logger_function.assert_called_once_with(basket.order_number, basket.id) self.assertFalse( Order.objects.filter( number=basket.order_number, total_incl_tax=basket.total_incl_tax).exists())