def test_creates_artifact(self, mock_report_errors,
                              mock_report_error_summary,
                              mock_olxcleaner_validate):
        """
        Tests olx validation in case of errors.
        Verify that in case of olx validation errors, course import does not fail & errors
        are logged in task artifact.
        """
        errors = [Mock(description='DuplicateURLNameError', level_val=3)]

        mock_olxcleaner_validate.return_value = [
            Mock(),
            Mock(errors=errors, return_error=Mock(return_value=True)),
            Mock()
        ]
        mock_report_errors.return_value = [
            f'ERROR {error.description} found in content' for error in errors
        ]
        mock_report_error_summary.return_value = [f'Errors: {len(errors)}']

        with patch(self.LOGGER) as patched_log:
            self.assertTrue(
                validate_course_olx(self.course.id, self.toy_course_path,
                                    self.status))
            patched_log.error.assert_called_once_with(
                f'Course import {self.course.id}: CourseOlx validation failed')
        task_artifact = UserTaskArtifact.objects.filter(
            status=self.status, name='OLX_VALIDATION_ERROR').first()
        self.assertIsNotNone(task_artifact)
 def test_config_settings_disabled(self, mock_olxcleaner_validate):
     """
     Tests olx validation with config setting is enabled.
     """
     with patch.dict(settings.FEATURES, ENABLE_COURSE_OLX_VALIDATION=True):
         self.assertTrue(validate_course_olx(self.course.id, self.toy_course_path, self.status))
         self.assertTrue(mock_olxcleaner_validate.called)
 def test_with_library_locator(self, mock_olxcleaner_validate):
     """
     Tests that olx is validation is skipped with library locator.
     """
     library_key = LibraryLocator(org='TestOrg', library='TestProbs')
     self.assertTrue(validate_course_olx(library_key, self.toy_course_path, self.status))
     self.assertFalse(mock_olxcleaner_validate.called)
    def test_validate_calls_with(self, mock_olxcleaner_validate):
        """
        Tests that olx library is called with expected keyword arguments.
        """
        allowed_xblocks = ALL_ALLOWED_XBLOCKS
        steps = 2
        ignore = ['edx-xblock']
        mock_olxcleaner_validate.return_value = [Mock(), Mock(errors=[], return_error=Mock(return_value=False)), Mock()]

        with override_settings(COURSE_OLX_VALIDATION_STAGE=steps, COURSE_OLX_VALIDATION_IGNORE_LIST=ignore):
            validate_course_olx(self.course.id, self.toy_course_path, self.status)
            mock_olxcleaner_validate.assert_called_with(
                filename=self.toy_course_path,
                steps=steps,
                ignore=ignore,
                allowed_xblocks=allowed_xblocks
            )
 def test_waffle_flag_settings(self, mock_olxcleaner_validate):
     """
     Tests olx validation in case of waffle flag is off.
     """
     with patch.dict(settings.FEATURES, ENABLE_COURSE_OLX_VALIDATION=False):
         self.assertTrue(
             validate_course_olx(self.course.id, self.toy_course_path,
                                 self.status))
         self.assertFalse(mock_olxcleaner_validate.called)
 def test_waffle_flag_settings(self, mock_olxcleaner_validate):
     """
     Tests olx validation in case of waffle flag is off.
     """
     with override_waffle_flag(self.waffle_flg, active=False):
         self.assertTrue(
             validate_course_olx(self.course.id, self.toy_course_path,
                                 self.status))
         self.assertFalse(mock_olxcleaner_validate.called)
    def test_exception_during_validation(self, mock_olxcleaner_validate):
        """
        Tests olx validation in case of unexpected error.

        In case of any unexpected exception during the olx validation,
         the course import continues and information is logged on the server.
        """
        mock_olxcleaner_validate.side_effect = Exception
        with mock.patch(self.LOGGER) as patched_log:
            self.assertTrue(validate_course_olx(self.course.id, self.toy_course_path, self.status))
            self.assertTrue(mock_olxcleaner_validate.called)
            patched_log.exception.assert_called_once_with(
                f'Course import {self.course.id}: CourseOlx could not be validated')
 def test_no_errors(self, mock_olxcleaner_validate):
     """
     Tests olx validation with no errors.
     Verify that in case for no validation errors, no artifact object is created.
     """
     mock_olxcleaner_validate.return_value = [
         Mock(),
         Mock(errors=[], return_error=Mock(return_value=False)),
         Mock()
     ]
     self.assertTrue(validate_course_olx(self.course.id, self.toy_course_path, self.status))
     task_artifact = UserTaskArtifact.objects.filter(status=self.status, name='OLX_VALIDATION_ERROR').first()
     self.assertIsNone(task_artifact)
     self.assertTrue(mock_olxcleaner_validate.called)