Skip to content

Commit

Permalink
Merge pull request #34376 from openedx/pwnage101/ENT-8525
Browse files Browse the repository at this point in the history
feat: support the core enrollment API's force_enrollment flag from enterprise_support
  • Loading branch information
pwnage101 authored Mar 18, 2024
2 parents 1bcf25f + 7e3e3a3 commit 762730a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,45 @@ def test_calls_enrollment_and_cohort_apis(
is_active=True,
enrollment_attributes=None,
enterprise_uuid=ENTERPRISE_UUID,
force_enrollment=False,
)
mock_get_enrollment_api.assert_called_once_with(USERNAME, str(COURSE_ID))

@mock.patch('openedx.features.enterprise_support.enrollments.utils.enrollment_api.add_enrollment')
@mock.patch('openedx.features.enterprise_support.enrollments.utils.enrollment_api.get_enrollment')
@mock.patch('openedx.features.enterprise_support.enrollments.utils.User.objects.get')
@mock.patch('openedx.features.enterprise_support.enrollments.utils.transaction')
def test_passes_force_enrollment_flag(
self,
mock_tx,
mock_user_model,
mock_get_enrollment_api,
mock_add_enrollment_api,
):
"""
Everything about this test is the same as the standard happy case, except we're just making sure the
force_enrollment flag gets passed to add_enrollment().
"""
expected_response = {'mode': COURSE_MODE, 'is_active': True}

mock_add_enrollment_api.return_value = expected_response
mock_tx.return_value.atomic.side_effect = None

mock_user_model.return_value = self.a_user
mock_get_enrollment_api.return_value = None

response = lms_update_or_create_enrollment(
USERNAME, COURSE_ID, COURSE_MODE, is_active=True, enterprise_uuid=ENTERPRISE_UUID, force_enrollment=True
)
assert response == expected_response
mock_add_enrollment_api.assert_called_once_with(
USERNAME,
str(COURSE_ID),
mode=COURSE_MODE,
is_active=True,
enrollment_attributes=None,
enterprise_uuid=ENTERPRISE_UUID,
force_enrollment=True, # Literally the only purpose of this test.
)
mock_get_enrollment_api.assert_called_once_with(USERNAME, str(COURSE_ID))

Expand Down
5 changes: 5 additions & 0 deletions openedx/features/enterprise_support/enrollments/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def lms_update_or_create_enrollment(
desired_mode,
is_active,
enterprise_uuid=None,
force_enrollment=False,
):
"""
Update or create the user's course enrollment based on the existing enrollment mode.
Expand All @@ -50,6 +51,9 @@ def lms_update_or_create_enrollment(
- is_active (bool): A Boolean value that indicates whether the
enrollment is to be set to inactive (if False). Usually we want a True if enrolling anew.
- enterprise_uuid (str): Optional. id to identify the enterprise to enroll under
- force_enrollment (bool):
Enroll user even if course enrollment_end date is expired (default False). This only has an effect when the
enrollment is being created, not when it is only updated.
Returns: A serializable dictionary of the new or updated course enrollment. If it hits
CourseEnrollmentError or CourseEnrollmentNotUpdatableError, it raises those exceptions.
Expand Down Expand Up @@ -105,6 +109,7 @@ def lms_update_or_create_enrollment(
is_active=is_active,
enrollment_attributes=None,
enterprise_uuid=enterprise_uuid,
force_enrollment=force_enrollment,
)
if not response:
log.exception(
Expand Down

0 comments on commit 762730a

Please sign in to comment.