From 81d90df3176a5d82ab77b0a6912f4982ac167b29 Mon Sep 17 00:00:00 2001 From: Cyril Nxumalo Date: Thu, 21 Dec 2023 13:39:12 +0200 Subject: [PATCH] ENT-8079 - Make dsc optional based on enterprise config --- enterprise_subsidy/apps/fulfillment/api.py | 35 +++++++++++++++++-- .../apps/fulfillment/tests/test_api.py | 15 ++++++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/enterprise_subsidy/apps/fulfillment/api.py b/enterprise_subsidy/apps/fulfillment/api.py index 0b30c2de..4e27a648 100644 --- a/enterprise_subsidy/apps/fulfillment/api.py +++ b/enterprise_subsidy/apps/fulfillment/api.py @@ -56,6 +56,9 @@ class GEAGFulfillmentHandler(): 'geag_data_share_consent', ] + def __init__(self): + self._cached_enterprise_data = None + def get_smarter_client(self): return GetSmarterEnterpriseApiClient( client_id=settings.GET_SMARTER_OAUTH2_KEY, @@ -81,11 +84,33 @@ def _get_geag_variant_id(self, transaction): ent_uuid = self._get_enterprise_customer_uuid(transaction) return ContentMetadataApi().get_geag_variant_id(ent_uuid, transaction.content_key) - def _get_auth_org_id(self, transaction): + def _get_enterprise_customer_data(self, transaction): + """ + Fetches and caches enterprise customer data based on a transaction. + + This method checks if the enterprise customer data is already cached. + If it is, the cached data is returned to avoid redundant API calls. + If not, it fetches the data from the enterprise client and caches it + for future use. + + Args: + transaction: The transaction object used to identify the enterprise customer. + + Returns: + The enterprise customer data. + """ + # Check if data is already cached + if self._cached_enterprise_data is not None: + return self._cached_enterprise_data + # If data is not cached, fetch and cache it enterprise_customer_uuid = str(self._get_enterprise_customer_uuid(transaction)) ent_client = self.get_enterprise_client(transaction) - ent_data = ent_client.get_enterprise_customer_data(enterprise_customer_uuid) - return ent_data.get('auth_org_id') + self._cached_enterprise_data = ent_client.get_enterprise_customer_data(enterprise_customer_uuid) + + return self._cached_enterprise_data + + def _get_auth_org_id(self, transaction): + return self._get_enterprise_customer_data(transaction).get('auth_org_id') def _create_allocation_payload(self, transaction, currency='USD'): # TODO: come back an un-hack this once GEAG validation is @@ -121,7 +146,11 @@ def _validate(self, transaction): Raises an exception when the transaction is missing required information """ + enterprise_customer_data = self._get_enterprise_customer_data(transaction) + enable_data_sharing_consent = enterprise_customer_data.get('enable_data_sharing_consent', False) for field in self.REQUIRED_METADATA_FIELDS: + if field == 'geag_data_share_consent' and not enable_data_sharing_consent: + continue if not transaction.metadata.get(field): raise InvalidFulfillmentMetadataException(f'missing {field} transaction metadata') return True diff --git a/enterprise_subsidy/apps/fulfillment/tests/test_api.py b/enterprise_subsidy/apps/fulfillment/tests/test_api.py index c52ee36f..4e520897 100644 --- a/enterprise_subsidy/apps/fulfillment/tests/test_api.py +++ b/enterprise_subsidy/apps/fulfillment/tests/test_api.py @@ -198,7 +198,8 @@ def test_create_allocation_payload(self, mock_get_enterprise_customer_data, mock else: assert geag_payload.get(geag_field) == tx_metadata.get(payload_field) - def test_validate_pass(self): + @mock.patch("enterprise_subsidy.apps.api_client.enterprise.EnterpriseApiClient.get_enterprise_customer_data") + def test_validate_pass(self, mock_get_enterprise_customer_data): """ Ensure `_validate` method passes """ @@ -208,7 +209,10 @@ def test_validate_pass(self): 'geag_email': 'donny@example.com', 'geag_date_of_birth': '1900-01-01', 'geag_terms_accepted_at': '2021-05-21T17:32:28Z', - 'geag_data_share_consent': True, + } + mock_get_enterprise_customer_data.return_value = { + 'auth_org_id': 'asde23eas', + 'enable_data_sharing_consent': False, } transaction = TransactionFactory.create( state=TransactionStateChoices.PENDING, @@ -221,7 +225,8 @@ def test_validate_pass(self): # pylint: disable=protected-access assert self.geag_fulfillment_handler._validate(transaction) - def test_validate_fail(self): + @mock.patch("enterprise_subsidy.apps.api_client.enterprise.EnterpriseApiClient.get_enterprise_customer_data") + def test_validate_fail(self, mock_get_enterprise_customer_data): """ Ensure `_validate` method raises with a missing `geag_terms_accepted_at` """ @@ -233,6 +238,10 @@ def test_validate_fail(self): 'geag_terms_accepted_at': '2021-05-21T17:32:28Z', 'geag_data_share_consent': True, } + mock_get_enterprise_customer_data.return_value = { + 'auth_org_id': 'asde23eas', + 'enable_data_sharing_consent': True + } transaction = TransactionFactory.create( state=TransactionStateChoices.PENDING, quantity=-19998,