Skip to content

Commit

Permalink
fix: fix tests and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
nyujacky committed Aug 27, 2024
1 parent 9d8fd6c commit 1b5fde2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 52 deletions.
10 changes: 3 additions & 7 deletions commerce_coordinator/apps/commercetools/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,12 @@ def run_filter(
try:
ct_api_client = CommercetoolsAPIClient()
order = ct_api_client.get_order_by_id(order_id=order_id)
intent_id = get_edx_payment_intent_id(order)

if not is_commercetools_line_item_already_refunded(order, order_line_item_id):
returned_order = ct_api_client.create_return_for_order(
order_id=order.id,
order_version=order.version,
order_line_item_id=order_line_item_id,
payment_intent_id=intent_id
order_line_item_id=order_line_item_id
)

returned_line_item_return_id = returned_order.return_info[0].items[0].id
Expand Down Expand Up @@ -256,8 +254,6 @@ class UpdateCommercetoolsOrderReturnPaymentStatus(PipelineStep):

def run_filter(
self,
payment_intent_id,
amount_in_cents,
**kwargs
):
"""
Expand All @@ -279,8 +275,8 @@ def run_filter(
order_id=order.id,
order_version=order.version,
return_line_item_return_id=return_line_item_return_id,
payment_intent_id=payment_intent_id,
amount_in_cents= amount_in_cents
payment_intent_id=kwargs['payment_intent_id'],
amount_in_cents=kwargs['amount_in_cents']
)

return {
Expand Down
90 changes: 48 additions & 42 deletions commerce_coordinator/apps/commercetools/tests/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,54 @@ def test_create_return_for_order_exception(self):

log_mock.assert_called_once_with(expected_message)

def test_successful_order_return_payment_state_update(self):
base_url = self.client_set.get_base_url_from_client()

# Mocked order to be passed in to update method
mock_order = gen_order("mock_order_id")
mock_order.version = "2"
mock_return_item = gen_return_item("mock_return_item_id", ReturnPaymentState.INITIAL)
mock_return_info = ReturnInfo(items=[mock_return_item])
mock_order.return_info.append(mock_return_info)

# 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_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])
mock_response_order.return_info.append(mock_response_return_info)

with requests_mock.Mocker(real_http=True, case_sensitive=False) as mocker:
mocker.post(
f"{base_url}orders/mock_order_id",
json=mock_response_order.serialize(),
status_code=200
)
mocker.post(
f"{base_url}payments/{mock_payment.id}",
json=mock_payment.serialize(),
status_code=200
)
mocker.get(
f"{base_url}payments/key={mock_payment.id}",
json=mock_payment.serialize(),
status_code=200
)
mocker.get(
f"{base_url}orders/mock_order_id",
json=mock_response_order.serialize(),
status_code=200
)
result = self.client_set.client.update_return_payment_state_after_successful_refund(
mock_order.id,
mock_order.version,
mock_response_return_item.line_item_id,
mock_payment.id,
10000
)
self.assertEqual(result.return_info[1].items[0].payment_state, ReturnPaymentState.REFUNDED)

def test_create_refund_transaction(self):
base_url = self.client_set.get_base_url_from_client()

Expand Down Expand Up @@ -784,8 +832,6 @@ def setUp(self):
'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
# 'update_return_payment_state_after_successful_refund':
# self.mock.update_return_payment_state_after_successful_refund
}
)

Expand All @@ -794,46 +840,6 @@ def tearDown(self):
MonkeyPatch.unmonkey(CommercetoolsAPIClient)
super().tearDown()

def test_successful_order_return_payment_state_update(self):
base_url = self.client_set.get_base_url_from_client()

# Mocked order to be passed in to update method
mock_order = gen_order("mock_order_id")
mock_order.version = "2"
mock_return_item = gen_return_item("mock_return_item_id", ReturnPaymentState.INITIAL)
mock_return_info = ReturnInfo(items=[mock_return_item])
mock_order.return_info.append(mock_return_info)

# Mocked expected order recieved after CT SDK call to update the order
mock_response_order = gen_order("mock_order_id")
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])
mock_response_order.return_info.append(mock_response_return_info)

mock_response_payment = gen_payment()

with requests_mock.Mocker(real_http=True, case_sensitive=False) as mocker:
mocker.post(
f"{base_url}orders/{mock_response_order.id}",
json=mock_response_order.serialize(),
status_code=200
)
mocker.get(
f"{base_url}payments/{mock_response_payment.id}",
json=mock_response_payment.serialize(),
status_code=200
)
result = self.client_set.client.update_return_payment_state_after_successful_refund(
mock_order.id,
mock_order.version,
mock_response_return_item.line_item_id,
"pm_1",
10000
)

self.assertEqual(result.return_info[1].items[0].payment_state, ReturnPaymentState.REFUNDED)

def test_update_return_payment_state_exception(self):
mock_error_response: CommercetoolsError = CommercetoolsError(
"Could not update ReturnPaymentState", [
Expand Down
9 changes: 6 additions & 3 deletions commerce_coordinator/apps/commercetools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from braze.client import BrazeClient
from commercetools import CommercetoolsError
from commercetools.platform.models import Customer, LineItem, Order, Payment, TransactionState, TransactionType
from commercetools.platform.models import Order as CTOrder
from commercetools.platform.models import Payment, TransactionState, TransactionType
from django.conf import settings
from django.urls import reverse

Expand Down Expand Up @@ -170,14 +168,19 @@ def has_full_refund_transaction(payment: Payment):
return True
return False

def find_refund_transaction(order: CTOrder, amount: decimal):

def find_refund_transaction(order: Order, amount: decimal):
"""
Utility to find the refund transaction in a payment
"""
pmt = get_edx_successful_stripe_payment(order)
for transaction in pmt.transactions:
if transaction.type == TransactionType.REFUND:
if decimal.Decimal(typed_money_to_string(transaction.amount, money_as_decimal_string=True)) == amount:
return transaction.id
return {}


def translate_stripe_refund_status_to_transaction_status(stripe_refund_status: str):
"""
Utility to translate stripe's refund object's status attribute to a valid CT transaction state
Expand Down

0 comments on commit 1b5fde2

Please sign in to comment.