Skip to content

Commit

Permalink
fix: deactivate_enrollment api without EDX_API_KEY key
Browse files Browse the repository at this point in the history
  • Loading branch information
mudassir-hafeez committed May 22, 2024
1 parent 653e032 commit 8f7a319
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions openedx/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,10 +726,10 @@ def unenroll_edx_course_run(run_enrollment):
EdxApiEnrollErrorException: Raised if the underlying edX API HTTP request fails
UnknownEdxApiEnrollException: Raised if an unknown error was encountered during the edX API request
"""
edx_client = get_edx_api_client(run_enrollment.user)
edx_client = get_edx_api_service_client()
try:
deactivated_enrollment = edx_client.enrollments.deactivate_enrollment(
run_enrollment.run.courseware_id
run_enrollment.run.courseware_id, username=run_enrollment.user.username
)
except HTTPError as exc:
raise EdxApiEnrollErrorException(run_enrollment.user, run_enrollment.run, exc) # noqa: B904
Expand Down
22 changes: 22 additions & 0 deletions openedx/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
retry_failed_edx_enrollments,
subscribe_to_edx_course_emails,
sync_enrollments_with_edx,
unenroll_edx_course_run,
unsubscribe_from_edx_course_emails,
update_edx_user_email,
update_edx_user_name,
Expand Down Expand Up @@ -642,6 +643,27 @@ def test_retry_users_grace_period(mocker):
patched_repair_user.assert_called_once_with(user_to_repair)


def test_unenroll_edx_course_run(mocker):
"""Tests that unenroll_edx_course_run makes a call to unenroll in edX via the API client"""
mock_client = mocker.MagicMock()
run_enrollment = CourseRunEnrollmentFactory.create(edx_enrolled=True)
courseware_id = run_enrollment.run.courseware_id
username = run_enrollment.user.username
enroll_return_value = mocker.Mock(
json={"course_id": courseware_id, "user": username}
)
mock_client.enrollments.deactivate_enrollment = mocker.Mock(
return_value=enroll_return_value
)
mocker.patch("openedx.api.get_edx_api_service_client", return_value=mock_client)
deactivated_enrollment = unenroll_edx_course_run(run_enrollment)

mock_client.enrollments.deactivate_enrollment.assert_called_once_with(
courseware_id, username=username
)
assert deactivated_enrollment == enroll_return_value


def test_update_user_edx_name(mocker, user):
"""Test that update_edx_user makes a call to update update_user_name in edX via API client"""
user.name = "Test Name"
Expand Down

0 comments on commit 8f7a319

Please sign in to comment.