Skip to content

Commit

Permalink
fix: add new tests, refactor tx search, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyujacky committed Sep 3, 2024
1 parent aa28b64 commit fcbb0a4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
7 changes: 2 additions & 5 deletions commerce_coordinator/apps/commercetools/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,19 +393,16 @@ 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
)
if not payment_intent_id:
payment_intent_id = ''

Check failure on line 401 in commerce_coordinator/apps/commercetools/clients.py

View workflow job for this annotation

GitHub Actions / tests (ubuntu-20.04, 3.8, django42)

Missing coverage

Missing coverage on line 401
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(
Expand All @@ -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]
Expand Down
22 changes: 15 additions & 7 deletions commerce_coordinator/apps/commercetools/tests/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -828,15 +836,15 @@ 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
}
)

def tearDown(self):
self.mock.order_mock.side_effect = None
self.mock.payment_mock.side_effect = None
MonkeyPatch.unmonkey(CommercetoolsAPIClient)
super().tearDown()

Expand All @@ -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(
Expand Down
25 changes: 24 additions & 1 deletion commerce_coordinator/apps/commercetools/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions commerce_coordinator/apps/commercetools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Check failure on line 178 in commerce_coordinator/apps/commercetools/utils.py

View workflow job for this annotation

GitHub Actions / tests (ubuntu-20.04, 3.8, django42)

Missing coverage

Missing coverage on line 178
Expand Down

0 comments on commit fcbb0a4

Please sign in to comment.