From d9659a496c610079d5b13ab472b0732553ab0e7d Mon Sep 17 00:00:00 2001 From: Anna Date: Wed, 17 Jul 2024 14:53:38 -0400 Subject: [PATCH 1/7] set is_active on enrollment --- openedx/api.py | 1 + 1 file changed, 1 insertion(+) diff --git a/openedx/api.py b/openedx/api.py index 579a59258b..39ddf4d7a6 100644 --- a/openedx/api.py +++ b/openedx/api.py @@ -668,6 +668,7 @@ def enroll_in_edx_course_runs( mode=mode, username=username, force_enrollment=force_enrollment, + enrollment_attributes={"is_active": True}, ) results.append(enrollment) except HTTPError as exc: # noqa: PERF203 From 73d752df9ba7e01a6c5b234b271bf8d76cb194f1 Mon Sep 17 00:00:00 2001 From: Anna Date: Mon, 22 Jul 2024 13:27:00 -0400 Subject: [PATCH 2/7] enrollment_attributes --- openedx/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openedx/api.py b/openedx/api.py index 39ddf4d7a6..d1503be2b1 100644 --- a/openedx/api.py +++ b/openedx/api.py @@ -668,7 +668,7 @@ def enroll_in_edx_course_runs( mode=mode, username=username, force_enrollment=force_enrollment, - enrollment_attributes={"is_active": True}, + enrollment_attributes={"name": "is_active", "value": True}, ) results.append(enrollment) except HTTPError as exc: # noqa: PERF203 From 18dd89c530efc46b71c0a7c50223e6e7d4030034 Mon Sep 17 00:00:00 2001 From: Anna Date: Fri, 26 Jul 2024 22:03:01 -0400 Subject: [PATCH 3/7] if is_active is false call api again --- openedx/api.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/openedx/api.py b/openedx/api.py index d1503be2b1..7bfb5fd10c 100644 --- a/openedx/api.py +++ b/openedx/api.py @@ -668,8 +668,14 @@ def enroll_in_edx_course_runs( mode=mode, username=username, force_enrollment=force_enrollment, - enrollment_attributes={"name": "is_active", "value": True}, ) + 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 From 844ed49349a7c1c29106255a91b8a41d9b50bdd5 Mon Sep 17 00:00:00 2001 From: Anna Date: Mon, 29 Jul 2024 11:12:24 -0400 Subject: [PATCH 4/7] test --- openedx/api.py | 3 ++- openedx/api_test.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/openedx/api.py b/openedx/api.py index 7bfb5fd10c..b545419ddb 100644 --- a/openedx/api.py +++ b/openedx/api.py @@ -669,7 +669,8 @@ def enroll_in_edx_course_runs( username=username, force_enrollment=force_enrollment, ) - if not enrollment.is_active: + print(enrollment) + if not enrollment["is_active"]: enrollment = edx_client.enrollments.create_student_enrollment( course_run.courseware_id, mode=mode, diff --git a/openedx/api_test.py b/openedx/api_test.py index 1e9ee7661d..3fc3519b7b 100644 --- a/openedx/api_test.py +++ b/openedx/api_test.py @@ -447,7 +447,7 @@ 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 = [{"is_active": True}, {"is_active": False}, {"is_active": True}] mock_client.enrollments.create_student_enrollment = mocker.Mock( side_effect=enroll_return_values ) @@ -467,7 +467,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 == [{"is_active": True}, {"is_active": True}] def test_enroll_api_fail(mocker, user): From f8529b03e7c916a4c42d118639d97ff42ac45d50 Mon Sep 17 00:00:00 2001 From: Anna Date: Mon, 29 Jul 2024 16:03:33 -0400 Subject: [PATCH 5/7] print --- openedx/api.py | 1 - 1 file changed, 1 deletion(-) diff --git a/openedx/api.py b/openedx/api.py index b545419ddb..b57d4080e3 100644 --- a/openedx/api.py +++ b/openedx/api.py @@ -669,7 +669,6 @@ def enroll_in_edx_course_runs( username=username, force_enrollment=force_enrollment, ) - print(enrollment) if not enrollment["is_active"]: enrollment = edx_client.enrollments.create_student_enrollment( course_run.courseware_id, From 0fff7b7a004db8ba4b2c796f4a49b9b370c1cdc5 Mon Sep 17 00:00:00 2001 From: Anna Date: Mon, 29 Jul 2024 17:12:45 -0400 Subject: [PATCH 6/7] mocking --- openedx/api.py | 2 +- openedx/api_test.py | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/openedx/api.py b/openedx/api.py index b57d4080e3..7bfb5fd10c 100644 --- a/openedx/api.py +++ b/openedx/api.py @@ -669,7 +669,7 @@ def enroll_in_edx_course_runs( username=username, force_enrollment=force_enrollment, ) - if not enrollment["is_active"]: + if not enrollment.is_active: enrollment = edx_client.enrollments.create_student_enrollment( course_run.courseware_id, mode=mode, diff --git a/openedx/api_test.py b/openedx/api_test.py index 3fc3519b7b..26e5d4707c 100644 --- a/openedx/api_test.py +++ b/openedx/api_test.py @@ -447,10 +447,8 @@ 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 = [{"is_active": True}, {"is_active": False}, {"is_active": True}] - mock_client.enrollments.create_student_enrollment = mocker.Mock( - side_effect=enroll_return_values - ) + 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) mocker.patch("openedx.api.get_edx_api_client", return_value=mock_client) mocker.patch("openedx.api.get_edx_api_service_client", return_value=mock_client) course_runs = CourseRunFactory.build_batch(2) @@ -467,7 +465,7 @@ def test_enroll_in_edx_course_runs(settings, mocker, user): username=user.username, force_enrollment=True, ) - assert enroll_results == [{"is_active": True}, {"is_active": True}] + assert enroll_results == [enroll_return_values[0], enroll_return_values[2]] def test_enroll_api_fail(mocker, user): From 493ca45556b7bb2ecc9c0842a9fa33966948780e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 29 Jul 2024 21:13:09 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- openedx/api_test.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/openedx/api_test.py b/openedx/api_test.py index 26e5d4707c..6d5ccd13fd 100644 --- a/openedx/api_test.py +++ b/openedx/api_test.py @@ -447,8 +447,14 @@ 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 = [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) + 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 + ) mocker.patch("openedx.api.get_edx_api_client", return_value=mock_client) mocker.patch("openedx.api.get_edx_api_service_client", return_value=mock_client) course_runs = CourseRunFactory.build_batch(2)