From a35176e09b74a279c936e4806a9011c4934cc9a0 Mon Sep 17 00:00:00 2001 From: Muhammad Bilal Tahir <106396899+bilaltahir21@users.noreply.github.com> Date: Wed, 27 Jul 2022 15:52:39 +0500 Subject: [PATCH] fix: revert api client for UNENROLL_DONE signal --- openedx/core/djangoapps/commerce/utils.py | 22 +++++++++++++++++++ .../features/enterprise_support/signals.py | 18 +++++++-------- .../enterprise_support/tests/test_signals.py | 13 ++++++----- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/openedx/core/djangoapps/commerce/utils.py b/openedx/core/djangoapps/commerce/utils.py index c9bda98b0446..5bb908043a0f 100644 --- a/openedx/core/djangoapps/commerce/utils.py +++ b/openedx/core/djangoapps/commerce/utils.py @@ -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 @@ -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. diff --git a/openedx/features/enterprise_support/signals.py b/openedx/features/enterprise_support/signals.py index 9c05e988a0ef..c8122e2102ca 100644 --- a/openedx/features/enterprise_support/signals.py +++ b/openedx/features/enterprise_support/signals.py @@ -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 @@ -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 @@ -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) ) diff --git a/openedx/features/enterprise_support/tests/test_signals.py b/openedx/features/enterprise_support/tests/test_signals.py index d1b6f9390fac..b4c6bc6604db 100644 --- a/openedx/features/enterprise_support/tests/test_signals.py +++ b/openedx/features/enterprise_support/tests/test_signals.py @@ -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 @@ -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 @@ -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