refactor: Extract core functionality of enrollment api in a python api to avoid REST calls from edx-enterprise (#28202)
* feat: Refactor out non REST portions of enrollment api from enrollment POST method For use with edx-enterprise to avoid making REST calls for bulk enrollment and other use cases ENT-4746 * feat: Remove unused test Testing is covered by test_views * refactor: isort isort fixes * docs: ADR for why this change ADR ENT-4746 * test: Fix test failure by restoring course_id to correct object * test: Test fix * refactor: pylint fixes * refactor: raise from to avoid pylint error * refactor: Start to work toward a util in enterprise_support instead of refactoring this endpoint * feat: Add util function in enterprise_support to eventually handle enrollment, only used by bulk enrollment for now * feat: One more revised idea, this time low risk in edx platform and also helps address enterprise specific flow. testing pending * feat: syntax and unused constant * feat: Restore view and add new util function to use in edx-enterprise instead * feat: breakpoint * unused import * feat: don't fail on existing enrollment * docs: ADR update * docs: docstring minor update * test: unit test add_user_to_course_cohort * refactor: imports * feat: remove unused error classes * refactor: lint * test: Test cases * test: Two more tests for negative cases * feat: missing init.py file * test: Fix tests to use correct user mock * unused import * refactor: Review feedback, test fixes, needs rebase now * feat: rebase changes * feat: keep audit_log with similar logic as in the view * refactor: Review feedback, test constant usage
This commit is contained in:
@@ -7,6 +7,7 @@ class CourseEnrollmentError(Exception):
|
||||
Describes any error that may occur when reading or updating enrollment information for a user or a course.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, msg, data=None):
|
||||
super().__init__(msg)
|
||||
# Corresponding information to help resolve the error.
|
||||
|
||||
30
openedx/core/djangoapps/enrollments/tests/test_utils.py
Normal file
30
openedx/core/djangoapps/enrollments/tests/test_utils.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""
|
||||
Tests for enrollment utils
|
||||
"""
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from openedx.core.djangoapps.enrollments.utils import add_user_to_course_cohort
|
||||
|
||||
|
||||
class EnrollmentUtilsTest(unittest.TestCase):
|
||||
"""
|
||||
Enrollment utils test cases
|
||||
"""
|
||||
@patch("openedx.core.djangoapps.enrollments.utils.add_user_to_cohort")
|
||||
@patch("openedx.core.djangoapps.enrollments.utils.get_cohort_by_name")
|
||||
def test_adds_user_to_cohort(self, mock_get_cohort_by_name, mock_add_user_to_cohort):
|
||||
user = {}
|
||||
mock_get_cohort_by_name.return_value = "a_cohort"
|
||||
|
||||
add_user_to_course_cohort("a_cohort", "a_course_id", user)
|
||||
assert mock_add_user_to_cohort.call_count == 1
|
||||
|
||||
@patch("openedx.core.djangoapps.enrollments.utils.add_user_to_cohort")
|
||||
@patch("openedx.core.djangoapps.enrollments.utils.get_cohort_by_name")
|
||||
def test_does_not_add_user_to_cohort(self, mock_get_cohort_by_name, mock_add_user_to_cohort):
|
||||
user = {}
|
||||
mock_get_cohort_by_name.return_value = "a_cohort"
|
||||
|
||||
add_user_to_course_cohort(None, "a_course_id", user)
|
||||
assert mock_add_user_to_cohort.call_count == 0
|
||||
20
openedx/core/djangoapps/enrollments/utils.py
Normal file
20
openedx/core/djangoapps/enrollments/utils.py
Normal file
@@ -0,0 +1,20 @@
|
||||
"""
|
||||
Utils for use in enrollment code
|
||||
"""
|
||||
import logging
|
||||
from openedx.core.djangoapps.course_groups.cohorts import add_user_to_cohort, get_cohort_by_name
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def add_user_to_course_cohort(cohort_name, course_id, user):
|
||||
"""
|
||||
If cohort_name is provided, adds user to the cohort
|
||||
"""
|
||||
if cohort_name is not None:
|
||||
cohort = get_cohort_by_name(course_id, cohort_name)
|
||||
try:
|
||||
add_user_to_cohort(cohort, user)
|
||||
except ValueError:
|
||||
# user already in cohort, probably because they were un-enrolled and re-enrolled
|
||||
logger.exception('Cohort re-addition')
|
||||
Reference in New Issue
Block a user