From fcbb0a424b208e02c7c70be4d76bec8461673d32 Mon Sep 17 00:00:00 2001 From: Jacky Ng Date: Tue, 3 Sep 2024 08:12:25 -0500 Subject: [PATCH] fix: add new tests, refactor tx search, fix tests --- .../apps/commercetools/clients.py | 7 ++---- .../apps/commercetools/tests/test_clients.py | 22 ++++++++++------ .../apps/commercetools/tests/test_utils.py | 25 ++++++++++++++++++- .../apps/commercetools/utils.py | 6 ++--- 4 files changed, 43 insertions(+), 17 deletions(-) diff --git a/commerce_coordinator/apps/commercetools/clients.py b/commerce_coordinator/apps/commercetools/clients.py index 568dfbf08..0df503e94 100644 --- a/commerce_coordinator/apps/commercetools/clients.py +++ b/commerce_coordinator/apps/commercetools/clients.py @@ -393,7 +393,6 @@ def update_return_payment_state_after_successful_refund(self, order_id: str, try: logger.info(f"[CommercetoolsAPIClient] - Updating payment state for return " f"with id {return_line_item_return_id} to '{ReturnPaymentState.REFUNDED}'.") - return_payment_state_action = OrderSetReturnPaymentStateAction( return_item_id=return_line_item_return_id, payment_state=ReturnPaymentState.REFUNDED @@ -401,11 +400,9 @@ def update_return_payment_state_after_successful_refund(self, order_id: str, if not payment_intent_id: payment_intent_id = '' logger.info(f'Creating return for order - payment_intent_id: {payment_intent_id}') - payment = self.get_payment_by_key(payment_intent_id) logger.info(f"Payment found: {payment}") - order = self.get_order_by_id(order_id=order_id) - transaction_id = find_refund_transaction(order, amount_in_cents) + transaction_id = find_refund_transaction(payment, amount_in_cents) update_transaction_id_action = OrderSetReturnItemCustomTypeAction( return_item_id=return_line_item_return_id, type=CTTypeResourceIdentifier( @@ -429,7 +426,7 @@ def update_return_payment_state_after_successful_refund(self, order_id: str, version=order_version, actions=[return_payment_state_action, update_transaction_id_action] ) - _ = self.base_client.payments.update_by_id( + self.base_client.payments.update_by_id( id=payment.id, version=payment.version, actions=[return_transaction_return_item_action] diff --git a/commerce_coordinator/apps/commercetools/tests/test_clients.py b/commerce_coordinator/apps/commercetools/tests/test_clients.py index d03e5b0d8..b9d8c9db1 100644 --- a/commerce_coordinator/apps/commercetools/tests/test_clients.py +++ b/commerce_coordinator/apps/commercetools/tests/test_clients.py @@ -8,14 +8,17 @@ Customer, CustomerDraft, CustomerPagedQueryResponse, + MoneyType, Order, OrderPagedQueryResponse, ReturnInfo, ReturnPaymentState, ReturnShipmentState, TransactionState, + TransactionType, Type, - TypeDraft + TypeDraft, + TypedMoney ) from django.test import TestCase from mock import patch @@ -33,7 +36,8 @@ gen_order_history, gen_payment, gen_retired_customer, - gen_return_item + gen_return_item, + gen_payment_with_multiple_transactions ) from commerce_coordinator.apps.commercetools.tests.sub_messages.test_tasks import CommercetoolsAPIClientMock from commerce_coordinator.apps.core.constants import ORDER_HISTORY_PER_SYSTEM_REQ_LIMIT @@ -485,7 +489,11 @@ def test_successful_order_return_payment_state_update(self): # Mocked expected order recieved after CT SDK call to update the order mock_response_order = gen_order("mock_order_id") - mock_payment = gen_payment() + mock_payment = gen_payment_with_multiple_transactions(TransactionType.CHARGE, 4900, TransactionType.REFUND, + TypedMoney(cent_amount=4900, + currency_code='USD', + type=MoneyType.CENT_PRECISION, + fraction_digits=2)) mock_response_order.version = "3" mock_response_return_item = gen_return_item("mock_return_item_id", ReturnPaymentState.REFUNDED) mock_response_return_info = ReturnInfo(items=[mock_response_return_item]) @@ -828,7 +836,7 @@ def setUp(self): { '__init__': lambda _: None, 'get_order_by_id': self.mock.get_order_by_id, - 'get_customer_by_id': self.mock.get_customer_by_id, + # 'get_customer_by_id': self.mock.get_customer_by_id, 'get_payment_by_key': self.mock.get_payment_by_key, 'create_return_for_order': self.mock.create_return_for_order, 'create_return_payment_transaction': self.mock.create_return_payment_transaction @@ -836,7 +844,7 @@ def setUp(self): ) def tearDown(self): - self.mock.order_mock.side_effect = None + self.mock.payment_mock.side_effect = None MonkeyPatch.unmonkey(CommercetoolsAPIClient) super().tearDown() @@ -851,10 +859,10 @@ def test_update_return_payment_state_exception(self): ], {}, "123456" ) - def _throw(order_id): + def _throw(_payment_id): raise mock_error_response - self.mock.order_mock.side_effect = _throw + self.mock.payment_mock.side_effect = _throw with self.assertRaises(OpenEdxFilterException): self.client_set.client.update_return_payment_state_after_successful_refund( diff --git a/commerce_coordinator/apps/commercetools/tests/test_utils.py b/commerce_coordinator/apps/commercetools/tests/test_utils.py index 3b120ef84..8b33b890b 100644 --- a/commerce_coordinator/apps/commercetools/tests/test_utils.py +++ b/commerce_coordinator/apps/commercetools/tests/test_utils.py @@ -9,7 +9,7 @@ import pytest import requests_mock from braze.client import BrazeClient -from commercetools.platform.models import TransactionState, TransactionType +from commercetools.platform.models import TransactionState, TransactionType, TypedMoney, MoneyType from django.conf import settings from django.test import override_settings from django.urls import reverse @@ -29,6 +29,7 @@ extract_ct_order_information_for_braze_canvas, extract_ct_product_information_for_braze_canvas, get_braze_client, + find_refund_transaction, has_full_refund_transaction, has_refund_transaction, send_order_confirmation_email, @@ -231,6 +232,28 @@ def test_has_no_refund_transaction(self): self.assertFalse(has_full_refund_transaction(payment)) +class TestFindRefundTransaction(unittest.TestCase): + """ + Tests for Find Refund Transaction Utils function + """ + + def test_has_no_refund_transaction(self): + payment = gen_payment_with_multiple_transactions(TransactionType.CHARGE, 4900) + self.assertEqual(find_refund_transaction(payment, 4900), {}) + + def test_has_matching_refund_transaction(self): + payment = gen_payment_with_multiple_transactions(TransactionType.CHARGE, 4900, TransactionType.REFUND, + TypedMoney(cent_amount=4900, + currency_code='USD', + type=MoneyType.CENT_PRECISION, + fraction_digits=2)) + self.assertEqual(find_refund_transaction(payment, 2500), {}) + + def test_has_no_matching_refund_transaction(self): + payment = gen_payment_with_multiple_transactions(TransactionType.CHARGE, 4900) + self.assertEqual(find_refund_transaction(payment, 4000), {}) + + class TestTranslateStripeRefundStatus(unittest.TestCase): """ Tests for Translating Stripes Refund Status Utils class diff --git a/commerce_coordinator/apps/commercetools/utils.py b/commerce_coordinator/apps/commercetools/utils.py index 4319d33ba..831fe497d 100644 --- a/commerce_coordinator/apps/commercetools/utils.py +++ b/commerce_coordinator/apps/commercetools/utils.py @@ -17,7 +17,6 @@ from commerce_coordinator.apps.commercetools.catalog_info.edx_utils import ( get_edx_lms_user_name, - get_edx_successful_stripe_payment ) from commerce_coordinator.apps.commercetools.catalog_info.utils import typed_money_to_string @@ -169,12 +168,11 @@ def has_full_refund_transaction(payment: Payment): return False -def find_refund_transaction(order: Order, amount: decimal): +def find_refund_transaction(payment: Payment, amount: decimal): """ Utility to find the refund transaction in a payment """ - pmt = get_edx_successful_stripe_payment(order) - for transaction in pmt.transactions: + for transaction in payment.transactions: if transaction.type == TransactionType.REFUND: if decimal.Decimal(typed_money_to_string(transaction.amount, money_as_decimal_string=True)) == amount: return transaction.id