diff --git a/openedx/api.py b/openedx/api.py index 579a59258b..7bfb5fd10c 100644 --- a/openedx/api.py +++ b/openedx/api.py @@ -669,6 +669,13 @@ def enroll_in_edx_course_runs( username=username, force_enrollment=force_enrollment, ) + if not enrollment.is_active: + enrollment = edx_client.enrollments.create_student_enrollment( + course_run.courseware_id, + mode=mode, + username=username, + force_enrollment=force_enrollment, + ) results.append(enrollment) except HTTPError as exc: # noqa: PERF203 raise EdxApiEnrollErrorException(user, course_run, exc) from exc diff --git a/openedx/api_test.py b/openedx/api_test.py index 1e9ee7661d..6d5ccd13fd 100644 --- a/openedx/api_test.py +++ b/openedx/api_test.py @@ -447,7 +447,11 @@ def test_enroll_in_edx_course_runs(settings, mocker, user): """Tests that enroll_in_edx_course_runs uses the EdxApi client to enroll in course runs""" settings.OPENEDX_SERVICE_WORKER_API_TOKEN = "mock_api_token" # noqa: S105 mock_client = mocker.MagicMock() - enroll_return_values = ["result1", "result2"] + enroll_return_values = [ + mocker.Mock(is_active=True), + mocker.Mock(is_active=False), + mocker.Mock(is_active=True), + ] mock_client.enrollments.create_student_enrollment = mocker.Mock( side_effect=enroll_return_values ) @@ -467,7 +471,7 @@ def test_enroll_in_edx_course_runs(settings, mocker, user): username=user.username, force_enrollment=True, ) - assert enroll_results == enroll_return_values + assert enroll_results == [enroll_return_values[0], enroll_return_values[2]] def test_enroll_api_fail(mocker, user):