Skip to content

Commit

Permalink
ENT-8079 - Make dsc optional based on enterprise config
Browse files Browse the repository at this point in the history
  • Loading branch information
zwidekalanga committed Dec 27, 2023
1 parent 25bed3a commit 81d90df
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
35 changes: 32 additions & 3 deletions enterprise_subsidy/apps/fulfillment/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions enterprise_subsidy/apps/fulfillment/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -208,7 +209,10 @@ def test_validate_pass(self):
'geag_email': '[email protected]',
'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,
Expand All @@ -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`
"""
Expand All @@ -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,
Expand Down

0 comments on commit 81d90df

Please sign in to comment.