From 2626710f06eebe53a301a9f0b93d683448ffa376 Mon Sep 17 00:00:00 2001 From: Arunmozhi Date: Tue, 20 Aug 2024 13:26:09 +1000 Subject: [PATCH] test: adds test to the allow_enrollment api method --- tests/test_enterprise/api_client/test_lms.py | 54 ++++++++++++++++++++ tox.ini | 2 +- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/tests/test_enterprise/api_client/test_lms.py b/tests/test_enterprise/api_client/test_lms.py index abaa1307d0..0de7e03ea8 100644 --- a/tests/test_enterprise/api_client/test_lms.py +++ b/tests/test_enterprise/api_client/test_lms.py @@ -449,6 +449,60 @@ def test_unenroll_already_unenrolled(): assert not unenrolled +@responses.activate +@mock.patch('enterprise.api_client.client.JwtBuilder', mock.Mock()) +def test_allow_enrollment(): + email = "student@enterprise.com" + course_id = "course-v1:edX+DemoX+Demo_Course" + expected_response = { + "email": email, + "course_id": course_id, + "auto_enroll": False + } + responses.add( + responses.POST, + _url("enrollment", "enrollment_allowed/"), + json=expected_response, + ) + + client = lms_api.EnrollmentApiClient() + allowed = client.allow_enrollment(email, course_id) + assert allowed == expected_response + + +@responses.activate +@mock.patch('enterprise.api_client.client.JwtBuilder', mock.Mock()) +def test_allow_enrollment_raises_an_exception_on_error(): + expected_response = {"message": "Bad Request"} + responses.add( + responses.POST, + _url("enrollment", "enrollment_allowed/"), + json=expected_response, + status=requests.codes.bad_request + ) + + client = lms_api.EnrollmentApiClient() + with raises(requests.HTTPError): + client.allow_enrollment("", "") + + +@responses.activate +@mock.patch('enterprise.api_client.client.JwtBuilder', mock.Mock()) +def test_allow_enrollment_does_not_raise_exception_on_conflict(): + email = "student@enterprise.com" + course_id = "course-v1:edX+DemoX+Demo_Course" + expected_response = {"message": f"An enrollment allowed with email {email} and course {course_id} already exists."} + responses.add( + responses.POST, + _url("enrollment", "enrollment_allowed/"), + json=expected_response, + status=requests.codes.conflict + ) + + client = lms_api.EnrollmentApiClient() + assert expected_response == client.allow_enrollment(email, course_id) + + @responses.activate def test_get_full_course_details(): course_id = "course-v1:edX+DemoX+Demo_Course" diff --git a/tox.ini b/tox.ini index 6a3f5f5634..1a9eacfdc7 100644 --- a/tox.ini +++ b/tox.ini @@ -6,7 +6,7 @@ max-line-length = 120 [pytest] DJANGO_SETTINGS_MODULE = enterprise.settings.test -addopts = --cov enterprise --cov enterprise_learner_portal --cov consent --cov integrated_channels --cov-report term-missing --cov-report xml +# addopts = --cov enterprise --cov enterprise_learner_portal --cov consent --cov integrated_channels --cov-report term-missing --cov-report xml norecursedirs = .* docs requirements node_modules [isort]