Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
syedsajjadkazmii committed Nov 28, 2024
1 parent b9d49c6 commit 5baa361
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 10 deletions.
1 change: 1 addition & 0 deletions commerce_coordinator/apps/commercetools/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ def gen_example_customer() -> CTCustomer:

def gen_customer(email: str, un: str):
return CTCustomer(
first_name='John',
email=email,
custom=CTCustomFields(
type=CTTypeReference(
Expand Down
22 changes: 16 additions & 6 deletions commerce_coordinator/apps/lms/tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
LMS Celery tasks
"""

import json

from celery import Task, shared_task
Expand All @@ -18,15 +19,24 @@
User = get_user_model()


class CourseEnrollTaskAfterReturn(Task):
class CourseEnrollTaskAfterReturn(Task): # pylint: disable=abstract-method
"""
Base class for fulfill_order_placed_send_enroll_in_course_task
"""

def on_failure(self, exc, task_id, args, kwargs, einfo):
error_message = json.loads(exc.response.text).get('message', '')
edx_lms_user_id = kwargs.get('edx_lms_user_id')
user_email = kwargs.get('user_email')
order_number = kwargs.get('order_number')
user_first_name = kwargs.get('user_first_name')
course_title = kwargs.get('course_title')

error_message = (
json.loads(exc.response.text).get('message', '')
if hasattr(exc, 'response') and exc.response is not None
else exc
)

logger.error(
f"Task {self.name} failed after max retries with error message: {error_message} "
f"for user with User Id: {edx_lms_user_id}, Email: {user_email}, "
Expand Down Expand Up @@ -76,9 +86,9 @@ def fulfill_order_placed_send_enroll_in_course_task(
item_quantity,
line_item_state_id,
message_id,
user_first_name,
user_email,
course_title
user_first_name, # pylint: disable=unused-argument
user_email, # pylint: disable=unused-argument
course_title # pylint: disable=unused-argument
):
"""
Celery task for order placed fulfillment and enrollment via LMS Enrollment API.
Expand All @@ -87,7 +97,7 @@ def fulfill_order_placed_send_enroll_in_course_task(
f'LMS fulfill_order_placed_send_enroll_in_course_task fired with {locals()},'
)

user = User.objects.get(lms_user_id=edx_lms_user_id)
user = User.objects.get(lms_user_id=1)

enrollment_data = {
'user': user.username,
Expand Down
5 changes: 4 additions & 1 deletion commerce_coordinator/apps/lms/tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': '[email protected]',
'course_title': 'Demonstration Course',
}

EXAMPLE_FULFILLMENT_REQUEST_PAYLOAD = {
Expand Down
5 changes: 4 additions & 1 deletion commerce_coordinator/apps/lms/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class FulfillOrderPlacedSendEnrollInCourseTest(CoordinatorSignalReceiverTestCase
'line_item_id': 11,
'item_quantity': 1,
'line_item_state_id': 12,
'message_id': 13
'message_id': 13,
'user_first_name': 14,
'user_email': 15,
'course_title': 16,
}

def test_correct_arguments_passed(self, mock_task):
Expand Down
46 changes: 44 additions & 2 deletions commerce_coordinator/apps/lms/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import logging
from unittest.mock import patch, sentinel
from unittest.mock import patch, sentinel, Mock

from django.test import TestCase
from requests import RequestException
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -130,3 +133,42 @@ 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
): # pylint: disable=unused-argument
"""
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'],
}
)
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def get_version(*file_paths):
# A list of warning types to suppress arbitrary warning messages.
suppress_warnings = [
'image.nonlocal_uri',
'role:setting',
]

# Add any paths that contain templates here, relative to this directory.
Expand Down

0 comments on commit 5baa361

Please sign in to comment.