From 882778125dca41ff99c1c2dddb8b1895ecac3a91 Mon Sep 17 00:00:00 2001 From: Jacky Ng Date: Fri, 9 Aug 2024 07:58:26 -0500 Subject: [PATCH] fix: fix tests --- .../apps/commercetools/clients.py | 9 ++++-- .../apps/commercetools/tests/test_clients.py | 29 ++++++++++--------- .../apps/commercetools/utils.py | 8 ++--- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/commerce_coordinator/apps/commercetools/clients.py b/commerce_coordinator/apps/commercetools/clients.py index 159e3b04..44012579 100644 --- a/commerce_coordinator/apps/commercetools/clients.py +++ b/commerce_coordinator/apps/commercetools/clients.py @@ -40,7 +40,10 @@ from commerce_coordinator.apps.commercetools.catalog_info.constants import DEFAULT_ORDER_EXPANSION, EdXFieldNames from commerce_coordinator.apps.commercetools.catalog_info.foundational_types import TwoUCustomTypes -from commerce_coordinator.apps.commercetools.utils import translate_stripe_refund_status_to_transaction_status, handle_commercetools_error +from commerce_coordinator.apps.commercetools.utils import ( + handle_commercetools_error, + translate_stripe_refund_status_to_transaction_status +) from commerce_coordinator.apps.core.constants import ORDER_HISTORY_PER_SYSTEM_REQ_LIMIT logger = logging.getLogger(__name__) @@ -441,7 +444,9 @@ def create_return_payment_transaction( return returned_payment except CommercetoolsError as err: - handle_commercetools_error(err, f"Unable to create refund payment transaction for payment {payment_id}") + context = f"Unable to create refund payment transaction for "\ + f"payment {payment_id} and stripe refund {stripe_refund.id}" + handle_commercetools_error(err, context) raise err def update_line_item_transition_state_on_fulfillment(self, order_id: str, order_version: int, diff --git a/commerce_coordinator/apps/commercetools/tests/test_clients.py b/commerce_coordinator/apps/commercetools/tests/test_clients.py index c7343b4c..b5c730c9 100644 --- a/commerce_coordinator/apps/commercetools/tests/test_clients.py +++ b/commerce_coordinator/apps/commercetools/tests/test_clients.py @@ -437,8 +437,8 @@ def test_create_return_for_order_exception(self): "errors": [ { "code": "ConcurrentModification", - "detailedErrorMessage": "Object [mock_order_id] has a " - "different version than expected. Expected: 2 - Actual: 1." + "message": "Object [mock_order_id] has a " + "different version than expected. Expected: 2 - Actual: 1.", }, ], "response": {}, @@ -464,8 +464,8 @@ def test_create_return_for_order_exception(self): expected_message = ( f"[CommercetoolsError] Unable to create return for " - f"order mock_order_id with error correlation id {exception.correlation_id} " - f"and error/s: {exception.errors}" + f"order mock_order_id " + f"- Correlation ID: {exception.correlation_id}, Details: {exception.errors}" ) log_mock.assert_called_once_with(expected_message) @@ -509,8 +509,8 @@ def test_update_return_payment_state_exception(self): "errors": [ { "code": "ConcurrentModification", - "detailedErrorMessage": "Object [mock_order_id] has a " - "different version than expected. Expected: 3 - Actual: 2." + "message": "Object [mock_order_id] has a " + "different version than expected. Expected: 3 - Actual: 2." }, ], "response": {}, @@ -580,8 +580,8 @@ def test_create_refund_transaction_exception(self): "errors": [ { "code": "ConcurrentModification", - "detailedErrorMessage": "Object [mock_order_id] has a " - "different version than expected. Expected: 2 - Actual: 1." + "message": "Object [mock_order_id] has a " + "different version than expected. Expected: 2 - Actual: 1.", }, ], "response": {}, @@ -607,8 +607,8 @@ def test_create_refund_transaction_exception(self): expected_message = ( f"[CommercetoolsError] Unable to create refund payment transaction for " - f"payment mock_payment_id and stripe refund {mock_stripe_refund.id} with " - f"error correlation id {exception.correlation_id} and error/s: {exception.errors}" + f"payment mock_payment_id and stripe refund {mock_stripe_refund.id} " + f"- Correlation ID: {exception.correlation_id}, Details: {exception.errors}" ) log_mock.assert_called_once_with(expected_message) @@ -659,8 +659,8 @@ def test_update_line_item_state_exception(self, mock_state_by_id): "errors": [ { "code": "ConcurrentModification", - "detailedErrorMessage": "Object [mock_order_id] has a " - "different version than expected. Expected: 2 - Actual: 1." + "message": "Object [mock_order_id] has a " + "different version than expected. Expected: 2 - Actual: 1." }, ], "response": {}, @@ -686,8 +686,9 @@ def test_update_line_item_state_exception(self, mock_state_by_id): expected_message = ( f"[CommercetoolsError] Unable to update LineItemState " - f"of order mock_order_id with error correlation id {mock_error_response['correlation_id']} " - f"and error/s: {mock_error_response['errors']}" + f"of order mock_order_id " + f"- Correlation ID: {mock_error_response['correlation_id']}, " + f"Details: {mock_error_response['errors']}" ) log_mock.assert_called_once_with(expected_message) diff --git a/commerce_coordinator/apps/commercetools/utils.py b/commerce_coordinator/apps/commercetools/utils.py index e7f30535..695324d5 100644 --- a/commerce_coordinator/apps/commercetools/utils.py +++ b/commerce_coordinator/apps/commercetools/utils.py @@ -10,7 +10,6 @@ from braze.client import BrazeClient from commercetools import CommercetoolsError from commercetools.platform.models import Customer, LineItem, Order, Payment, TransactionState, TransactionType - from django.conf import settings from django.urls import reverse @@ -33,11 +32,12 @@ def get_braze_client(): app_id='', ) -def handle_commercetools_error(err: CommercetoolsError, context: str) -> None: - error_details = [f"Code: {e.code}, Message: {e.message}" for e in err.errors] - error_message = f"[CommercetoolsError] {context} - Correlation ID: {err.correlation_id}, Details: {error_details}" + +def handle_commercetools_error(err: CommercetoolsError, context: str): + error_message = f"[CommercetoolsError] {context} - Correlation ID: {err.correlation_id}, Details: {err.errors}" logger.error(error_message) + def send_order_confirmation_email( lms_user_id, lms_user_email, canvas_entry_properties ):