From 5e141a765dd7d0d7fbe2a6b5fc11b3ad53d18291 Mon Sep 17 00:00:00 2001 From: moeez96 Date: Wed, 5 Jul 2023 12:25:34 +0500 Subject: [PATCH] refactor: Add logging to mobile IAP checkout/ API --- ecommerce/extensions/iap/api/v1/constants.py | 1 + .../extensions/iap/api/v1/ios_validator.py | 7 +++---- .../extensions/iap/api/v1/tests/test_views.py | 18 +++++++++++++++--- ecommerce/extensions/iap/api/v1/views.py | 4 ++-- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/ecommerce/extensions/iap/api/v1/constants.py b/ecommerce/extensions/iap/api/v1/constants.py index ce42c936b16..4ba77782b25 100644 --- a/ecommerce/extensions/iap/api/v1/constants.py +++ b/ecommerce/extensions/iap/api/v1/constants.py @@ -19,6 +19,7 @@ LOGGER_BASKET_CREATED = "Basket created for user [%s] with SKUS [%s]" LOGGER_BASKET_CREATION_FAILED = "Basket creation failed for user [%s]. Error: [%s]" LOGGER_BASKET_NOT_FOUND = "Basket [%s] not found for user [%s]." +LOGGER_CHECKOUT_ERROR = "Checkout failed with the error [%s] and status code [%s]." LOGGER_EXECUTE_ALREADY_PURCHASED = "Execute payment failed for user [%s] and basket [%s]. " \ "Products already purchased." LOGGER_EXECUTE_GATEWAY_ERROR = "Execute payment validation failed for user [%s] and basket [%s]. Error: [%s]" diff --git a/ecommerce/extensions/iap/api/v1/ios_validator.py b/ecommerce/extensions/iap/api/v1/ios_validator.py index 9cd0c882a15..40ccae1473e 100644 --- a/ecommerce/extensions/iap/api/v1/ios_validator.py +++ b/ecommerce/extensions/iap/api/v1/ios_validator.py @@ -12,10 +12,9 @@ def validate(self, receipt, configuration): Apple for the mentioned productId. """ bundle_id = configuration.get('ios_bundle_id') - # If True, automatically query sandbox endpoint if validation fails on production endpoint - # TODO: Add auto_retry_wrong_env_request to environment variables - auto_retry_wrong_env_request = True - validator = AppStoreValidator(bundle_id, auto_retry_wrong_env_request=auto_retry_wrong_env_request) + # auto_retry_wrong_env_request = True automatically queries sandbox endpoint if + # validation fails on production endpoint + validator = AppStoreValidator(bundle_id, auto_retry_wrong_env_request=True) try: validation_result = validator.validate( diff --git a/ecommerce/extensions/iap/api/v1/tests/test_views.py b/ecommerce/extensions/iap/api/v1/tests/test_views.py index e2fac4b553d..b1ddebea2ed 100644 --- a/ecommerce/extensions/iap/api/v1/tests/test_views.py +++ b/ecommerce/extensions/iap/api/v1/tests/test_views.py @@ -42,6 +42,7 @@ LOGGER_BASKET_CREATED, LOGGER_BASKET_CREATION_FAILED, LOGGER_BASKET_NOT_FOUND, + LOGGER_CHECKOUT_ERROR, LOGGER_EXECUTE_ALREADY_PURCHASED, LOGGER_EXECUTE_GATEWAY_ERROR, LOGGER_EXECUTE_ORDER_CREATION_FAILED, @@ -641,6 +642,7 @@ def setUp(self): 'basket_id': self.basket.id, 'payment_processor': 'android-iap' } + self.logger_name = 'ecommerce.extensions.iap.api.v1.views' def test_authentication_required(self): """ Verify the endpoint requires authentication. """ @@ -651,10 +653,20 @@ def test_authentication_required(self): def test_no_basket(self): """ Verify the endpoint returns HTTP 400 if the user has no associated baskets. """ self.user.baskets.all().delete() + expected_status = 400 + expected_error = "Basket [%s] not found." % str(self.post_data['basket_id']) expected_content = b'{"error": "Basket [%s] not found."}' % str(self.post_data['basket_id']).encode() - response = self.client.post(self.path, data=self.post_data) - self.assertEqual(response.status_code, 400) - self.assertEqual(response.content, expected_content) + with LogCapture(self.logger_name) as logger: + response = self.client.post(self.path, data=self.post_data) + logger.check( + ( + self.logger_name, + 'ERROR', + LOGGER_CHECKOUT_ERROR % (expected_error, expected_status) + ), + ) + self.assertEqual(response.status_code, expected_status) + self.assertEqual(response.content, expected_content) @override_settings( PAYMENT_PROCESSORS=['ecommerce.extensions.iap.processors.android_iap.AndroidIAP'] diff --git a/ecommerce/extensions/iap/api/v1/views.py b/ecommerce/extensions/iap/api/v1/views.py index 7930c533de9..c80d4605235 100644 --- a/ecommerce/extensions/iap/api/v1/views.py +++ b/ecommerce/extensions/iap/api/v1/views.py @@ -51,6 +51,7 @@ LOGGER_BASKET_CREATED, LOGGER_BASKET_CREATION_FAILED, LOGGER_BASKET_NOT_FOUND, + LOGGER_CHECKOUT_ERROR, LOGGER_EXECUTE_ALREADY_PURCHASED, LOGGER_EXECUTE_GATEWAY_ERROR, LOGGER_EXECUTE_ORDER_CREATION_FAILED, @@ -158,10 +159,9 @@ class MobileCheckoutView(APIView): permission_classes = (IsAuthenticated,) def post(self, request): - # TODO: Add check for products_in_basket_already_purchased - # TODO: Add logging for api/user_info after reading from request obj response = CheckoutView.as_view()(request._request) # pylint: disable=W0212 if response.status_code != 200: + logger.exception(LOGGER_CHECKOUT_ERROR, response.content.decode(), response.status_code) return JsonResponse({'error': response.content.decode()}, status=response.status_code) return response