From efb1f81bc7514a1fa2c4f70a0c3b532e37a0c9f5 Mon Sep 17 00:00:00 2001 From: mubbsharanwar Date: Thu, 19 Dec 2024 02:27:46 +0500 Subject: [PATCH] fix: update payment object mock --- .../commercetools/catalog_info/edx_utils.py | 3 +- .../apps/commercetools/pipeline.py | 3 +- .../tests/sub_messages/test_tasks.py | 30 ++++++++++--------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/commerce_coordinator/apps/commercetools/catalog_info/edx_utils.py b/commerce_coordinator/apps/commercetools/catalog_info/edx_utils.py index 5ce6948b..86aad446 100644 --- a/commerce_coordinator/apps/commercetools/catalog_info/edx_utils.py +++ b/commerce_coordinator/apps/commercetools/catalog_info/edx_utils.py @@ -51,7 +51,8 @@ def get_edx_successful_payment_info(order: CTOrder): for pr in order.payment_info.payments: pmt = pr.obj if pmt.payment_status.interface_code == PAYMENT_STATUS_INTERFACE_CODE_SUCCEEDED and pmt.interface_id: - return pmt, pmt.payment_method_info.payment_interface + ct_payment_provider_id = pmt.payment_method_info.payment_interface + return pmt, ct_payment_provider_id return None, None diff --git a/commerce_coordinator/apps/commercetools/pipeline.py b/commerce_coordinator/apps/commercetools/pipeline.py index 05c6b5a9..c3bb6fcb 100644 --- a/commerce_coordinator/apps/commercetools/pipeline.py +++ b/commerce_coordinator/apps/commercetools/pipeline.py @@ -113,11 +113,12 @@ def run_filter(self, active_order_management_system, order_number, **kwargs): # log.info(f"[Performance Check] get_order_by_number call took {duration} seconds") payment, psp = get_edx_successful_payment_info(ct_order) + ct_payment_provider_id = payment.interface_id if payment else None ret_val = { "order_data": ct_order, "psp": psp, - "payment_intent_id": payment.interface_id if payment else None + "payment_intent_id": ct_payment_provider_id } return ret_val diff --git a/commerce_coordinator/apps/commercetools/tests/sub_messages/test_tasks.py b/commerce_coordinator/apps/commercetools/tests/sub_messages/test_tasks.py index dc2a10ab..d8e3163c 100644 --- a/commerce_coordinator/apps/commercetools/tests/sub_messages/test_tasks.py +++ b/commerce_coordinator/apps/commercetools/tests/sub_messages/test_tasks.py @@ -1,11 +1,13 @@ """Commercetools Task Tests""" import logging -from unittest import TestCase, skip +from unittest import TestCase from unittest.mock import MagicMock, call, patch +from commercetools.platform.models import MoneyType as CTMoneyType from commercetools.platform.models import Order as CTOrder from commercetools.platform.models import ReturnInfo as CTReturnInfo from commercetools.platform.models import ReturnPaymentState as CTReturnPaymentState +from commercetools.platform.models import TypedMoney as CTTypedMoney from edx_django_utils.cache import TieredCache from commerce_coordinator.apps.commercetools.clients import CommercetoolsAPIClient @@ -252,6 +254,18 @@ def setUp(self): self.mock.update_return_payment_state_after_successful_refund } ) + # TODO: Properly mock the Payment object. + payment = self.mock.get_payment_by_key.return_value + amount = CTTypedMoney( + currency_code='USD', + cent_amount=1000, + type=CTMoneyType.CENT_PRECISION, + fraction_digits=2, + ) + for transaction in payment.transactions: + transaction.amount = amount + + self.payment_mock = payment def tearDown(self): MonkeyPatch.unmonkey(CommercetoolsAPIClient) @@ -270,35 +284,23 @@ def unpack_for_uut(values): def get_uut(): return fulfill_order_returned_uut - # TODO: Fix this test. It is failing because of the way the mock is set up. @patch('commerce_coordinator.apps.commercetools.sub_messages.tasks.is_edx_lms_order') @patch('commerce_coordinator.apps.stripe.pipeline.StripeAPIClient') - @patch.object(CommercetoolsAPIClientMock, 'payment_mock', new_callable=MagicMock) - @skip(reason="It is failing because of the way the mock is set up.") - def test_correct_arguments_passed_already_refunded_doest_break( - self, - _stripe_api_mock, - _lms_signal, - custom_payment_mock - ): + def test_correct_arguments_passed_already_refunded_doest_break(self, _stripe_api_mock, _lms_signal): """ Check calling uut with mock_parameters yields call to client with expected_data. """ mock_values = self.mock - custom_payment_mock.return_value = CTPaymentByKey() - ret_val = self.get_uut()(*self.unpack_for_uut(self.mock.example_payload)) self.assertTrue(ret_val) mock_values.order_mock.assert_has_calls([call(mock_values.order_id), call(order_id=mock_values.order_id)]) mock_values.customer_mock.assert_called_once_with(mock_values.customer_id) - # TODO: Fix this test. It is failing because of the way the mock is set up. @patch('commerce_coordinator.apps.commercetools.sub_messages.tasks.is_edx_lms_order') @patch('commerce_coordinator.apps.stripe.pipeline.StripeAPIClient') @patch('commerce_coordinator.apps.commercetools.clients.CommercetoolsAPIClient.create_return_for_order') - @skip(reason="It is failing because of the way the mock is set up.") def test_correct_arguments_passed_valid_stripe_refund( self, _return_order_mock: MagicMock,