Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert api client for UNENROLL_DONE signal #30792

Merged
merged 1 commit into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions openedx/core/djangoapps/commerce/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import requests
from django.conf import settings
from edx_rest_api_client.auth import SuppliedJwtAuth
from edx_rest_api_client.client import EdxRestApiClient
from eventtracking import tracker

from openedx.core.djangoapps.oauth_dispatch.jwt import create_jwt_for_user
Expand Down Expand Up @@ -38,6 +39,27 @@ def get_ecommerce_api_base_url():
return configuration_helpers.get_value('ECOMMERCE_API_URL', settings.ECOMMERCE_API_URL)


def ecommerce_api_client(user, session=None):
"""
Returns an E-Commerce API client setup with authentication for the specified user.

DEPRECATED: To be replaced with get_ecommerce_api_client.
"""
claims = {'tracking_context': create_tracking_context(user)}
scopes = [
'user_id',
'email',
'profile'
]
jwt = create_jwt_for_user(user, additional_claims=claims, scopes=scopes)

return EdxRestApiClient(
configuration_helpers.get_value('ECOMMERCE_API_URL', settings.ECOMMERCE_API_URL),
jwt=jwt,
session=session
)


def get_ecommerce_api_client(user):
"""
Returns an E-Commerce API client setup with authentication for the specified user.
Expand Down
18 changes: 8 additions & 10 deletions openedx/features/enterprise_support/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


import logging
from urllib.parse import urljoin

from django.conf import settings
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
Expand All @@ -15,10 +14,10 @@
transmit_single_learner_data,
transmit_single_subsection_learner_data
)
from requests.exceptions import HTTPError
from slumber.exceptions import HttpClientError

from common.djangoapps.student.signals import UNENROLL_DONE
from openedx.core.djangoapps.commerce.utils import get_ecommerce_api_base_url, get_ecommerce_api_client
from openedx.core.djangoapps.commerce.utils import ecommerce_api_client
from openedx.core.djangoapps.signals.signals import COURSE_ASSESSMENT_GRADE_CHANGED, COURSE_GRADE_NOW_PASSED
from openedx.features.enterprise_support.tasks import clear_enterprise_customer_data_consent_share_cache
from openedx.features.enterprise_support.utils import clear_data_consent_share_cache, is_enterprise_learner
Expand Down Expand Up @@ -106,17 +105,16 @@ def refund_order_voucher(sender, course_enrollment, skip_refund=False, **kwargs)
return

service_user = User.objects.get(username=settings.ECOMMERCE_SERVICE_WORKER_USERNAME)
client = get_ecommerce_api_client(service_user)
api_url = urljoin(
f"{get_ecommerce_api_base_url()}/", "coupons/create_refunded_voucher/"
)

# TODO: Replace ecommerce_api_client with get_ecommerce_api_client after completing ENT-6112
# https://2u-internal.atlassian.net/browse/ENT-6112
client = ecommerce_api_client(service_user)
order_number = course_enrollment.get_order_attribute_value('order_number')
if order_number:
error_message = "Encountered {} from ecommerce while creating refund voucher. Order={}, enrollment={}, user={}"
try:
response = client.post(api_url, data={"order": order_number})
response.raise_for_status()
except HTTPError as ex:
client.enterprise.coupons.create_refunded_voucher.post({"order": order_number})
except HttpClientError as ex:
log.info(
error_message.format(type(ex).__name__, order_number, course_enrollment, course_enrollment.user)
)
Expand Down
13 changes: 7 additions & 6 deletions openedx/features/enterprise_support/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from django.utils.timezone import now
from edx_django_utils.cache import TieredCache
from opaque_keys.edx.keys import CourseKey
# from slumber.exceptions import HttpClientError, HttpServerError
from requests.exceptions import HTTPError
from slumber.exceptions import HttpClientError, HttpServerError
# from requests.exceptions import HTTPError
from testfixtures import LogCapture

from common.djangoapps.course_modes.tests.factories import CourseModeFactory
Expand Down Expand Up @@ -157,13 +157,14 @@ def test_refund_order_voucher(
"""
mock_is_order_voucher_refundable.return_value = order_voucher_refundable
enrollment = self._create_enrollment_to_refund(no_of_days_placed, enterprise_enrollment_exists)
with patch('openedx.features.enterprise_support.signals.get_ecommerce_api_client') as mock_ecommerce_api_client:
with patch('openedx.features.enterprise_support.signals.ecommerce_api_client') as mock_ecommerce_api_client:
enrollment.update_enrollment(is_active=False, skip_refund=skip_refund)
assert mock_ecommerce_api_client.called == api_called

@patch('common.djangoapps.student.models.CourseEnrollment.is_order_voucher_refundable')
@ddt.data(
(HTTPError, 'INFO'),
(HttpClientError, 'INFO'),
(HttpServerError, 'ERROR'),
(Exception, 'ERROR'),
)
@ddt.unpack
Expand All @@ -173,9 +174,9 @@ def test_refund_order_voucher_with_client_errors(self, mock_error, log_level, mo
"""
mock_is_order_voucher_refundable.return_value = True
enrollment = self._create_enrollment_to_refund()
with patch('openedx.features.enterprise_support.signals.get_ecommerce_api_client') as mock_ecommerce_api_client:
with patch('openedx.features.enterprise_support.signals.ecommerce_api_client') as mock_ecommerce_api_client:
client_instance = mock_ecommerce_api_client.return_value
client_instance.post.side_effect = mock_error()
client_instance.enterprise.coupons.create_refunded_voucher.post.side_effect = mock_error()
with LogCapture(LOGGER_NAME) as logger:
enrollment.update_enrollment(is_active=False)
assert mock_ecommerce_api_client.called is True
Expand Down