From 243d45dfcc3b2755282508bb2a45f50c3f5e7161 Mon Sep 17 00:00:00 2001 From: Mudassir Hafeez Date: Thu, 16 May 2024 23:07:24 +0500 Subject: [PATCH] fix: unenrollment/deactivate_enrollment without EdX-Api-Key --- courseware/api.py | 5 +++-- courseware/api_test.py | 13 +++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/courseware/api.py b/courseware/api.py index 90eb8d3f31..f001844f66 100644 --- a/courseware/api.py +++ b/courseware/api.py @@ -739,10 +739,11 @@ 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, TRY200 diff --git a/courseware/api_test.py b/courseware/api_test.py index a80bbed876..f389dddd68 100644 --- a/courseware/api_test.py +++ b/courseware/api_test.py @@ -716,14 +716,19 @@ def test_unenroll_edx_course_run(mocker): mock_client = mocker.MagicMock() run_enrollment = CourseRunEnrollmentFactory.create(edx_enrolled=True) courseware_id = run_enrollment.run.courseware_id - enroll_return_value = mocker.Mock(json={"course_id": 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("courseware.api.get_edx_api_client", return_value=mock_client) + mocker.patch("courseware.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) + mock_client.enrollments.deactivate_enrollment.assert_called_once_with( + courseware_id, username=username + ) assert deactivated_enrollment == enroll_return_value @@ -744,7 +749,7 @@ def test_unenroll_edx_course_run_failure( mock_client.enrollments.deactivate_enrollment = mocker.Mock( side_effect=client_exception_raised ) - mocker.patch("courseware.api.get_edx_api_client", return_value=mock_client) + mocker.patch("courseware.api.get_edx_api_service_client", return_value=mock_client) with pytest.raises(expected_exception): unenroll_edx_course_run(run_enrollment)