From 3fa1b4073aeaf1ba38212743ef81810e9fc0547a Mon Sep 17 00:00:00 2001 From: Mudassir Hafeez Date: Wed, 22 May 2024 11:57:08 +0500 Subject: [PATCH] refactor: remove X-EdX-Api-Key usage (#104) --- .github/workflows/ci.yml | 3 ++- README.md | 1 - edx_api/client.py | 3 +-- edx_api/client_test.py | 4 +--- edx_api/enrollments/__init__.py | 17 ++++++++++------- edx_api/enrollments/init_test.py | 8 ++++++-- edx_api/integration_test.py | 13 ++++++------- 7 files changed, 26 insertions(+), 23 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 81de076..fc59296 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,8 @@ jobs: uses: actions/setup-python@v2 with: python-version: "3.5" + env: + PIP_TRUSTED_HOST: "pypi.python.org pypi.org files.pythonhosted.org" - id: cache uses: actions/cache@v1 @@ -32,4 +34,3 @@ jobs: uses: codecov/codecov-action@v1 with: file: ./coverage.xml - fail_ci_if_error: true diff --git a/README.md b/README.md index 696f0f8..4049c55 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,6 @@ Your edx demo course (course id `course-v1:edX+DemoX+Demo_Course`) must be enabled for CCX. The Course Enrollment integration tests have the following requirements: -- The `API_KEY` value must match the `settings.EDX_API_TOKEN` value in LMS - The user assigned to that `ACCESS_TOKEN` must be an admin in the edX demo course. Adding a user as an admin can be done in Studio (url: `/course_team/course-v1:edX+DemoX+Demo_Course` diff --git a/edx_api/client.py b/edx_api/client.py index 062ab1b..616e17a 100644 --- a/edx_api/client.py +++ b/edx_api/client.py @@ -43,8 +43,7 @@ def get_requester(self, token_type="Bearer"): { "Authorization": "{} {}".format( token_type, self.credentials["access_token"] - ), - "X-EdX-Api-Key": self.credentials.get("api_key"), + ) } ) diff --git a/edx_api/client_test.py b/edx_api/client_test.py index b8ed582..a1ce265 100644 --- a/edx_api/client_test.py +++ b/edx_api/client_test.py @@ -15,7 +15,5 @@ def test_request_id_credential(): def test_instantiation_happypath(): """instantiatable with correct args""" token = 'asdf' - api_key = 'api_key' - client = EdxApi({'access_token': token, 'api_key': api_key}) + client = EdxApi({'access_token': token}) assert client.get_requester().headers['Authorization'] == 'Bearer {token}'.format(token=token) - assert client.get_requester().headers['X-EdX-Api-Key'] == api_key diff --git a/edx_api/enrollments/__init__.py b/edx_api/enrollments/__init__.py index 58d008c..0e3f348 100644 --- a/edx_api/enrollments/__init__.py +++ b/edx_api/enrollments/__init__.py @@ -201,24 +201,27 @@ def create_verified_student_enrollment(self, course_id, username=None): username=username ) - def deactivate_enrollment(self, course_id): + def deactivate_enrollment(self, course_id, username=None): """ Deactivates an enrollment in the given course for the user - whose access token was provided to the API client (in other words, the - user will be unenrolled) Args: course_id (str): An edX course id. + username (str): Username. Returns: Enrollment: object representing the deactivated student enrollment """ + + params = { + "course_details": {"course_id": course_id}, + "is_active": False, + } + if username: + params['user'] = username resp = self.requester.post( urljoin(self.base_url, self.enrollment_url), - json={ - "course_details": {"course_id": course_id}, - "is_active": False, - } + json=params ) resp.raise_for_status() return Enrollment(resp.json()) diff --git a/edx_api/enrollments/init_test.py b/edx_api/enrollments/init_test.py index 738b893..89f133c 100644 --- a/edx_api/enrollments/init_test.py +++ b/edx_api/enrollments/init_test.py @@ -172,14 +172,18 @@ def test_deactivate_enrollment(self, request_mock): """ request_mock.post(self.enrollment_url, json=self.enrollments_json[0]) course_id = 'course_id' - returned_enrollment = self.enrollment_client.deactivate_enrollment(course_id=course_id) + user = 'user' + returned_enrollment = self.enrollment_client.deactivate_enrollment( + course_id=course_id, username=user + ) self.assertDictEqual( request_mock.last_request.json(), { 'course_details': { 'course_id': course_id }, - 'is_active': False + 'is_active': False, + 'user': user } ) assert returned_enrollment.json == self.enrollments_json[0] diff --git a/edx_api/integration_test.py b/edx_api/integration_test.py index 7f7d120..c920e9a 100644 --- a/edx_api/integration_test.py +++ b/edx_api/integration_test.py @@ -45,7 +45,6 @@ ACCESS_TOKEN = os.getenv('ACCESS_TOKEN') -API_KEY = os.getenv('API_KEY') BASE_URL = os.getenv('BASE_URL', 'http://localhost:18000/') ENROLLMENT_CREATION_COURSE_ID = os.getenv('ENROLLMENT_CREATION_COURSE_ID') @@ -111,7 +110,7 @@ def test_course_structure(): Pull down the course structure and validate it has the correct entries. This test assumes that the used user can access the course. """ - api = EdxApi({'access_token': ACCESS_TOKEN, 'api_key': API_KEY}, base_url=BASE_URL) + api = EdxApi({'access_token': ACCESS_TOKEN}, base_url=BASE_URL) structure = api.course_structure.course_blocks('course-v1:edX+DemoX+Demo_Course', 'staff') titles = [ @@ -135,7 +134,7 @@ def test_enrollments(): Enrolls the user in a course and then pulls down the enrollments for the user. This assumes that the course in the edX instance is available for enrollment. """ - api = EdxApi({'access_token': ACCESS_TOKEN, 'api_key': API_KEY}, base_url=BASE_URL) + api = EdxApi({'access_token': ACCESS_TOKEN}, base_url=BASE_URL) # the enrollment will be done manually until # a client to enroll the student is implemented @@ -160,7 +159,7 @@ def test_create_verified_enrollment(): """ Integration test to enroll the user in a course with `verified` mode """ - api = EdxApi({'access_token': ACCESS_TOKEN, 'api_key': API_KEY}, base_url=BASE_URL) + api = EdxApi({'access_token': ACCESS_TOKEN}, base_url=BASE_URL) enrollment = api.enrollments.create_student_enrollment( course_id=ENROLLMENT_CREATION_COURSE_ID, mode=ENROLLMENT_MODE_VERIFIED, @@ -175,7 +174,7 @@ def test_create_audit_enrollment(): """ Integration test to enroll the user in a course with `audit` mode """ - api = EdxApi({'access_token': ACCESS_TOKEN, 'api_key': API_KEY}, base_url=BASE_URL) + api = EdxApi({'access_token': ACCESS_TOKEN}, base_url=BASE_URL) enrollment = api.enrollments.create_student_enrollment( course_id=ENROLLMENT_CREATION_COURSE_ID, mode=ENROLLMENT_MODE_AUDIT, @@ -190,7 +189,7 @@ def test_deactivate_enrollment(): """ Integration test to enroll then deactivate a user in a course """ - api = EdxApi({'access_token': ACCESS_TOKEN, 'api_key': API_KEY}, base_url=BASE_URL) + api = EdxApi({'access_token': ACCESS_TOKEN}, base_url=BASE_URL) api.enrollments.create_student_enrollment( course_id=ENROLLMENT_CREATION_COURSE_ID, mode=ENROLLMENT_MODE_AUDIT @@ -207,7 +206,7 @@ def test_create_enrollment_timeout(): """ Asserts request timeout error on enrollment api """ - api = EdxApi({'access_token': ACCESS_TOKEN, 'api_key': API_KEY}, base_url=BASE_URL) + api = EdxApi({'access_token': ACCESS_TOKEN}, base_url=BASE_URL) enrollments = api.enrollments with patch.object(enrollments.requester, 'post', autospec=True) as post: post.side_effect = mocked_timeout