Skip to content

Commit

Permalink
refactor: remove EdX-Api-Key usage (#2217)
Browse files Browse the repository at this point in the history
  • Loading branch information
mudassir-hafeez authored May 24, 2024
1 parent d194c09 commit 5b0c3d4
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 34 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ jobs:
MITX_ONLINE_ADMIN_EMAIL: example@localhost
OPENEDX_API_CLIENT_ID: fake_client_id
OPENEDX_API_CLIENT_SECRET: fake_client_secret # pragma: allowlist secret
OPENEDX_API_KEY: test-openedx-api-key # pragma: allowlist secret

- name: Tests
run: |
Expand All @@ -118,7 +117,6 @@ jobs:
OPENEDX_API_BASE_URL: http://localhost:18000
OPENEDX_API_CLIENT_ID: fake_client_id
OPENEDX_API_CLIENT_SECRET: fake_client_secret # pragma: allowlist secret
OPENEDX_API_KEY: test-openedx-api-key # pragma: allowlist secret
SECRET_KEY: local_unsafe_key # pragma: allowlist secret

- name: Upload coverage to CodeCov
Expand Down
9 changes: 1 addition & 8 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,6 @@
"hashed_secret": "b235838f76594bf21886c6eec9c06a207e9ec5ce",
"is_verified": false,
"line_number": 20
},
{
"type": "Secret Keyword",
"filename": "pytest.ini",
"hashed_secret": "e035cdf1f57666859ec5949ee389d479d03ea1e3",
"is_verified": false,
"line_number": 21
}
],
"users/api.py": [
Expand Down Expand Up @@ -247,5 +240,5 @@
}
]
},
"generated_at": "2024-05-15T14:13:55Z"
"generated_at": "2024-05-22T12:17:57Z"
}
4 changes: 0 additions & 4 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -485,10 +485,6 @@
"description": "The OAuth2 client secret to connect to Open edX with",
"required": true
},
"OPENEDX_API_KEY": {
"description": "edX API key (EDX_API_KEY setting in Open edX)",
"required": true
},
"OPENEDX_BASE_REDIRECT_URL": {
"description": "The base redirect URL for an OAuth Application for the Open edX API",
"required": false
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ x-environment:
CELERY_TASK_ALWAYS_EAGER: 'False'
REDIS_URL: redis://redis:6379/4
DOCKER_HOST: ${DOCKER_HOST:-missing}
OPENEDX_API_KEY: ${OPENEDX_API_KEY:-PUT_YOUR_API_KEY_HERE}

x-extra-hosts:
&default-extra-hosts
Expand Down
6 changes: 0 additions & 6 deletions main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,12 +1019,6 @@
description="The OAuth2 client secret to connect to Open edX with",
required=True,
)
OPENEDX_API_KEY = get_string(
name="OPENEDX_API_KEY",
default=None,
description="edX API key (EDX_API_KEY setting in Open edX)",
required=True,
)

MITX_ONLINE_REGISTRATION_ACCESS_TOKEN = get_string(
name="MITX_ONLINE_REGISTRATION_ACCESS_TOKEN",
Expand Down
11 changes: 4 additions & 7 deletions openedx/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def get_edx_api_client(user, ttl_in_seconds=OPENEDX_AUTH_DEFAULT_TTL_IN_SECONDS)
except OpenEdxApiAuth.DoesNotExist:
raise NoEdxApiAuthError(f"{user!s} does not have an associated OpenEdxApiAuth") # noqa: B904, EM102
return EdxApi(
{"access_token": auth.access_token, "api_key": settings.OPENEDX_API_KEY},
{"access_token": auth.access_token},
settings.OPENEDX_API_BASE_URL,
timeout=settings.EDX_API_CLIENT_TIMEOUT,
)
Expand All @@ -497,10 +497,7 @@ def get_edx_api_service_client():
raise ImproperlyConfigured("OPENEDX_SERVICE_WORKER_API_TOKEN is not set") # noqa: EM101

edx_client = EdxApi(
{
"access_token": settings.OPENEDX_SERVICE_WORKER_API_TOKEN,
"api_key": settings.OPENEDX_API_KEY,
},
{"access_token": settings.OPENEDX_SERVICE_WORKER_API_TOKEN},
settings.OPENEDX_API_BASE_URL,
timeout=settings.EDX_API_CLIENT_TIMEOUT,
)
Expand Down Expand Up @@ -729,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
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ django-webpack-loader = "^1.4.1"
djangorestframework = "^3.12.4"
djoser = "^2.1.0"
drf-extensions = "^0.7.1"
edx-api-client = "^1.8.0"
edx-api-client = "^1.9.0"
hubspot-api-client = "^6.1.0"
hypothesis = "4.23.9"
ipython = "^8.0.0"
Expand Down
1 change: 0 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ env =
OPENEDX_API_BASE_URL=http://localhost:18000
OPENEDX_API_CLIENT_ID=fake_client_id
OPENEDX_API_CLIENT_SECRET=fake_client_secret
OPENEDX_API_KEY=test-openedx-api-key
SENTRY_DSN=
RECAPTCHA_SITE_KEY=
RECAPTCHA_SECRET_KEY=
Expand Down

0 comments on commit 5b0c3d4

Please sign in to comment.