diff --git a/eox_nelp/signals/receivers.py b/eox_nelp/signals/receivers.py index 167c9185..fddaccdd 100644 --- a/eox_nelp/signals/receivers.py +++ b/eox_nelp/signals/receivers.py @@ -390,7 +390,9 @@ def pearson_vue_course_completion_handler(instance, **kwargs): # pylint: disabl Arguments: instance: Instance of BlockCompletion model. """ - if not getattr(settings, "PEARSON_RTI_ACTIVATE_COMPLETION_GATE", False): + if not getattr(settings, "PEARSON_RTI_ACTIVATE_COMPLETION_GATE", False) or not str(instance.context_key) in getattr( + settings, "PEARSON_ENGINE_COURSES_ENABLED", [] + ): return is_complete, graded = get_completed_and_graded(user_id=instance.user_id, course_id=str(instance.context_key)) @@ -426,7 +428,9 @@ def pearson_vue_course_passed_handler(user, course_id, **kwargs): # pylint: dis user : Instance of auth user model. course_id : Course locator. """ - if not getattr(settings, "PEARSON_RTI_ACTIVATE_GRADED_GATE", False): + if not getattr(settings, "PEARSON_RTI_ACTIVATE_GRADED_GATE", False) or not str(course_id) in getattr( + settings, "PEARSON_ENGINE_COURSES_ENABLED", [] + ): return LOGGER.info( diff --git a/eox_nelp/signals/tests/test_receivers.py b/eox_nelp/signals/tests/test_receivers.py index dea3b912..2d9f117e 100644 --- a/eox_nelp/signals/tests/test_receivers.py +++ b/eox_nelp/signals/tests/test_receivers.py @@ -12,6 +12,7 @@ import unittest from ddt import data, ddt +from django.conf import settings from django.contrib.auth import get_user_model from django.test import override_settings from django.utils import timezone @@ -697,7 +698,11 @@ def test_call_async_task(self, task_mock): @ddt class PearsonVueCompletionHandlerTestCase(unittest.TestCase): """Test class for pearson_vue_course_completion_handler function.""" + course_id = "course-v1:test+Cx105+2022_T4" + user_id = 5 + course_exam_configuration = [course_id] + @override_settings(PEARSON_ENGINE_COURSES_ENABLED=course_exam_configuration) @patch("eox_nelp.signals.receivers.real_time_import_task") def test_invalid_feature_flag(self, task_mock): """Test when the PEARSON_RTI_ACTIVATE_COMPLETION_GATE settings is False. @@ -706,20 +711,40 @@ def test_invalid_feature_flag(self, task_mock): - real_time_import_task mock has not been called. """ instance = Mock() - instance.user_id = 17 - course_id = "course-v1:test+Cx105+2022_T4" - instance.context_key = CourseKey.from_string(course_id) + instance.user_id = self.user_id + instance.context_key = CourseKey.from_string(self.course_id) pearson_vue_course_completion_handler(instance) task_mock.delay.assert_not_called() + @data({}, [], ["exam_series_code"], ["key"], ["wrong_course_id"]) + @override_settings(PEARSON_RTI_ACTIVATE_COMPLETION_GATE=True) + @patch("eox_nelp.signals.receivers.real_time_import_task") + def test_invalid_course_configuration(self, wrong_course_config, task_mock): + """Test when the PEARSON_RTI_ACTIVATE_COMPLETION_GATE settings is True, + but invalid course configuration for PEARSON_ENGINE_COURSES_ENABLED. + Expected behavior: + - real_time_import_task mock has not been called. + """ + instance = Mock() + instance.user_id = self.user_id + instance.context_key = CourseKey.from_string(self.course_id) + setattr(settings, "PEARSON_ENGINE_COURSES_ENABLED", wrong_course_config) + + pearson_vue_course_completion_handler(instance) + + task_mock.delay.assert_not_called() + + @override_settings( + PEARSON_RTI_ACTIVATE_COMPLETION_GATE=True, + PEARSON_ENGINE_COURSES_ENABLED=course_exam_configuration, + ) @data( # is_complete and graded values respectively (True, True), (False, True), (False, False), ) - @override_settings(PEARSON_RTI_ACTIVATE_COMPLETION_GATE=True) @patch("eox_nelp.signals.receivers.get_completed_and_graded") @patch("eox_nelp.signals.receivers.real_time_import_task") def test_invalid_course_state(self, invalid_state, task_mock, get_completed_and_graded_mock): @@ -729,16 +754,18 @@ def test_invalid_course_state(self, invalid_state, task_mock, get_completed_and_ - real_time_import_task mock has not been called. """ instance = Mock() - instance.user_id = 17 - course_id = "course-v1:test+Cx105+2022_T4" - instance.context_key = CourseKey.from_string(course_id) + instance.user_id = self.user_id + instance.context_key = CourseKey.from_string(self.course_id) get_completed_and_graded_mock.return_value = invalid_state pearson_vue_course_completion_handler(instance) task_mock.delay.assert_not_called() - @override_settings(PEARSON_RTI_ACTIVATE_COMPLETION_GATE=True) + @override_settings( + PEARSON_RTI_ACTIVATE_COMPLETION_GATE=True, + PEARSON_ENGINE_COURSES_ENABLED=course_exam_configuration, + ) @patch("eox_nelp.signals.receivers.get_completed_and_graded") @patch("eox_nelp.signals.receivers.real_time_import_task") def test_call_async_task(self, task_mock, get_completed_and_graded_mock): @@ -748,19 +775,22 @@ def test_call_async_task(self, task_mock, get_completed_and_graded_mock): - delay method is called with the right values. """ instance = Mock() - instance.user_id = 5 - course_id = "course-v1:test+Cx105+2022_T4" - instance.context_key = CourseKey.from_string(course_id) + instance.user_id = self.user_id + instance.context_key = CourseKey.from_string(self.course_id) get_completed_and_graded_mock.return_value = (True, False) # is_complete and graded values respectively pearson_vue_course_completion_handler(instance) task_mock.delay.assert_called_with( user_id=instance.user_id, - course_id=course_id, + course_id=self.course_id, ) - @override_settings(PEARSON_RTI_ACTIVATE_COMPLETION_GATE=True, USE_PEARSON_ENGINE_SERVICE=True) + @override_settings( + PEARSON_RTI_ACTIVATE_COMPLETION_GATE=True, + USE_PEARSON_ENGINE_SERVICE=True, + PEARSON_ENGINE_COURSES_ENABLED=course_exam_configuration, + ) @patch("eox_nelp.signals.receivers.get_completed_and_graded") @patch("eox_nelp.signals.receivers.real_time_import_task_v2") def test_call_async_task_v2(self, task_mock, get_completed_and_graded_mock): @@ -770,38 +800,59 @@ def test_call_async_task_v2(self, task_mock, get_completed_and_graded_mock): - delay method is called with the right values. """ instance = Mock() - instance.user_id = 5 - course_id = "course-v1:test+Cx105+2022_T4" - instance.context_key = CourseKey.from_string(course_id) + instance.user_id = self.user_id + instance.context_key = CourseKey.from_string(self.course_id) get_completed_and_graded_mock.return_value = (True, False) # is_complete and graded values respectively pearson_vue_course_completion_handler(instance) task_mock.delay.assert_called_with( user_id=instance.user_id, - exam_id=course_id, + exam_id=self.course_id, action_name="rti" ) +@ddt class PearsonVueCoursePassedHandlerTestCase(unittest.TestCase): """Test class for mt_course_passed_handler function.""" + course_id = "course-v1:test+Cz105+2022_T4" + course_exam_configuration = [course_id] - @patch("eox_nelp.signals.receivers.update_mt_training_stage") + @override_settings(PEARSON_ENGINE_COURSES_ENABLED=course_exam_configuration) + @patch("eox_nelp.signals.receivers.real_time_import_task") def test_invalid_feature_flag(self, task_mock): """Test when the PEARSON_RTI_ACTIVATE_GRADED_GATE settings is False. Expected behavior: - real_time_import_task mock has not been called. """ - course_id = "course-v1:test+Cx105+2022_T4" user_instance, _ = User.objects.get_or_create(username="Severus") - pearson_vue_course_passed_handler(user_instance, CourseKey.from_string(course_id)) + pearson_vue_course_passed_handler(user_instance, CourseKey.from_string(self.course_id)) task_mock.delay.assert_not_called() @override_settings(PEARSON_RTI_ACTIVATE_GRADED_GATE=True) + @data({}, [], ["exam_series_code"], ["key"], ["wrong_course_id"]) + @patch("eox_nelp.signals.receivers.real_time_import_task") + def test_invalid_course_configuration(self, wrong_course_config, task_mock): + """Test when the PEARSON_RTI_ACTIVATE_GRADED_GATE settings is True, + but invalid course configuration for PEARSON_ENGINE_COURSES_ENABLED + + Expected behavior: + - real_time_import_task mock has not been called. + """ + user_instance, _ = User.objects.get_or_create(username="Severus") + setattr(settings, "PEARSON_ENGINE_COURSES_ENABLED", wrong_course_config) + pearson_vue_course_passed_handler(user_instance, CourseKey.from_string(self.course_id)) + + task_mock.delay.assert_not_called() + + @override_settings( + PEARSON_RTI_ACTIVATE_GRADED_GATE=True, + PEARSON_ENGINE_COURSES_ENABLED=course_exam_configuration, + ) @patch("eox_nelp.signals.receivers.real_time_import_task") def test_call_async_task(self, task_mock): """Test that the async task is called with the right parameters @@ -809,17 +860,20 @@ def test_call_async_task(self, task_mock): Expected behavior: - delay method is called with the right values. """ - course_id = "course-v1:test+Cx105+2022_T4" user_instance, _ = User.objects.get_or_create(username="Severus") - pearson_vue_course_passed_handler(user_instance, CourseKey.from_string(course_id)) + pearson_vue_course_passed_handler(user_instance, CourseKey.from_string(self.course_id)) task_mock.delay.assert_called_with( - course_id=course_id, + course_id=self.course_id, user_id=user_instance.id, ) - @override_settings(PEARSON_RTI_ACTIVATE_GRADED_GATE=True, USE_PEARSON_ENGINE_SERVICE=True) + @override_settings( + PEARSON_RTI_ACTIVATE_GRADED_GATE=True, + USE_PEARSON_ENGINE_SERVICE=True, + PEARSON_ENGINE_COURSES_ENABLED=course_exam_configuration, + ) @patch("eox_nelp.signals.receivers.real_time_import_task_v2") def test_call_async_task_v2(self, task_mock): """Test that the async task is called with the right parameters @@ -827,13 +881,12 @@ def test_call_async_task_v2(self, task_mock): Expected behavior: - delay method is called with the right values. """ - course_id = "course-v1:test+Cx105+2022_T4" user_instance, _ = User.objects.get_or_create(username="Severus") - pearson_vue_course_passed_handler(user_instance, CourseKey.from_string(course_id)) + pearson_vue_course_passed_handler(user_instance, CourseKey.from_string(self.course_id)) task_mock.delay.assert_called_with( - exam_id=course_id, + exam_id=self.course_id, user_id=user_instance.id, action_name="rti", )