From 8057ce1b2df863f9fe7478f9a72d8b0bc0598837 Mon Sep 17 00:00:00 2001 From: Syed Sajjad Hussain Shah Date: Thu, 28 Nov 2024 13:28:45 +0500 Subject: [PATCH] feat: add tests --- commerce_coordinator/apps/lms/tasks.py | 8 +++- .../apps/lms/tests/constants.py | 5 ++- .../apps/lms/tests/test_tasks.py | 44 ++++++++++++++++++- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/commerce_coordinator/apps/lms/tasks.py b/commerce_coordinator/apps/lms/tasks.py index 999950c3c..6b4f9200e 100644 --- a/commerce_coordinator/apps/lms/tasks.py +++ b/commerce_coordinator/apps/lms/tasks.py @@ -20,7 +20,7 @@ class CourseEnrollTaskAfterReturn(Task): def on_failure(self, exc, task_id, args, kwargs, einfo): - error_message = json.loads(exc.response.text).get('message', '') + error_message = json.loads(exc.response.text).get('message', '') if exc.response else 'Unknown error' edx_lms_user_id = kwargs.get('edx_lms_user_id') user_email = kwargs.get('user_email') order_number = kwargs.get('order_number') @@ -157,4 +157,8 @@ def fulfill_order_placed_send_enroll_in_course_task( 'celery_task_id': self.request.id } - return LMSAPIClient().enroll_user_in_course(enrollment_data, line_item_state_payload, fulfillment_logging_obj) + print('hehe before calling lms = ', enrollment_data, line_item_state_payload, fulfillment_logging_obj) + + output = LMSAPIClient().enroll_user_in_course(enrollment_data, line_item_state_payload, fulfillment_logging_obj) + print('hehe = ', output) + return output diff --git a/commerce_coordinator/apps/lms/tests/constants.py b/commerce_coordinator/apps/lms/tests/constants.py index a9f05f194..7241cb1c2 100644 --- a/commerce_coordinator/apps/lms/tests/constants.py +++ b/commerce_coordinator/apps/lms/tests/constants.py @@ -29,7 +29,10 @@ 'line_item_id': '822d77c4-00a6-4fb9-909b-094ef0b8c4b9', 'item_quantity': 1, 'line_item_state_id': '8f2e888e-9777-4557-9a7f-c649153770c2', - 'message_id': '1063f19c-08f3-41a4-a952-a8577374373c' + 'message_id': '1063f19c-08f3-41a4-a952-a8577374373c', + 'user_first_name': 'test', + 'user_email': 'test@example.com', + 'course_title': 'Demonstration Course', } EXAMPLE_FULFILLMENT_REQUEST_PAYLOAD = { diff --git a/commerce_coordinator/apps/lms/tests/test_tasks.py b/commerce_coordinator/apps/lms/tests/test_tasks.py index 595d68a28..7f470d190 100644 --- a/commerce_coordinator/apps/lms/tests/test_tasks.py +++ b/commerce_coordinator/apps/lms/tests/test_tasks.py @@ -3,7 +3,7 @@ """ import logging -from unittest.mock import patch, sentinel +from unittest.mock import patch, sentinel, Mock, PropertyMock from django.test import TestCase from requests import RequestException @@ -49,7 +49,10 @@ def unpack_for_uut(values): values['line_item_id'], values['item_quantity'], values['line_item_state_id'], - values['message_id'] + values['message_id'], + values['user_first_name'], + values['user_email'], + values['course_title'] ) def setUp(self): @@ -130,3 +133,40 @@ def test_retry_logic(self, mock_ct_get_order, mock_ct_get_state, mock_client): mock_ct_get_state.assert_called_with(TwoUKeys.FAILURE_FULFILMENT_STATE) mock_ct_get_order.assert_called_with(EXAMPLE_FULFILLMENT_SIGNAL_PAYLOAD.get('order_id')) + + # @patch('commerce_coordinator.apps.lms.tasks.send_fulfillment_error_email') + # @patch.object(fulfill_order_placed_send_enroll_in_course_task, 'max_retries', 5) + # def test_fulfillment_error_email_is_sent_on_failure(self, mock_send_email, mock_client): + # """ + # Test that `on_failure` sends the appropriate failure email. + # """ + # mock_response = Mock() + # mock_response.text = '{"message": "course mode is expired or otherwise unavailable for course run"}' + # exception = RequestException("400 Bad Request") + # exception.response = mock_response + # + # exc = exception + # task_id = "test_task_id" + # args = [] + # kwargs = EXAMPLE_FULFILLMENT_SIGNAL_PAYLOAD + # einfo = Mock() + # + # fulfill_order_placed_send_enroll_in_course_task.push_request(retries=5) + # fulfill_order_placed_send_enroll_in_course_task.on_failure( + # exc=exc, + # task_id=task_id, + # args=args, + # kwargs=kwargs, + # einfo=einfo + # ) + # + # mock_send_email.assert_called_once_with( + # EXAMPLE_FULFILLMENT_SIGNAL_PAYLOAD['edx_lms_user_id'], + # EXAMPLE_FULFILLMENT_SIGNAL_PAYLOAD['user_email'], + # { + # 'order_number': EXAMPLE_FULFILLMENT_SIGNAL_PAYLOAD['order_number'], + # 'product_type': 'course', + # 'product_name': EXAMPLE_FULFILLMENT_SIGNAL_PAYLOAD['course_title'], + # 'first_name': EXAMPLE_FULFILLMENT_SIGNAL_PAYLOAD['user_first_name'], + # } + # )