Skip to content

Commit

Permalink
feat: Added data_share_consent field to order fullfillment notes
Browse files Browse the repository at this point in the history
  • Loading branch information
IrfanUddinAhmad committed Apr 7, 2023
1 parent 3ad694a commit a62074f
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 2 deletions.
9 changes: 9 additions & 0 deletions ecommerce/enterprise/tests/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,15 @@ def mock_consent_get(self, username, course_id, ec_uuid):
ec_uuid
)

def mock_consent_post(self, username, course_id, ec_uuid):
self.mock_consent_response(
username,
course_id,
ec_uuid,
method=responses.POST,
granted=True
)

def mock_consent_missing(self, username, course_id, ec_uuid):
self.mock_consent_response(
username,
Expand Down
18 changes: 18 additions & 0 deletions ecommerce/enterprise/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from ecommerce.enterprise.tests.mixins import EnterpriseServiceMockMixin
from ecommerce.enterprise.utils import (
CUSTOMER_CATALOGS_DEFAULT_RESPONSE,
create_enterprise_customer_user_consent,
enterprise_customer_user_needs_consent,
find_active_enterprise_customer_user,
get_enterprise_catalog,
Expand Down Expand Up @@ -144,6 +145,23 @@ def test_ecu_needs_consent(self):
self.mock_consent_not_required(**opts)
self.assertEqual(enterprise_customer_user_needs_consent(**kw), False)

@responses.activate
def test_ecu_create_consent(self):
opts = {
'ec_uuid': 'fake-uuid',
'course_id': 'course-v1:real+course+id',
'username': 'johnsmith',
}
kw = {
'enterprise_customer_uuid': 'fake-uuid',
'course_id': 'course-v1:real+course+id',
'username': 'johnsmith',
'site': self.site
}
self.mock_access_token_response()
self.mock_consent_post(**opts)
self.assertEqual(create_enterprise_customer_user_consent(**kw), True)

def test_get_enterprise_customer_uuid(self):
"""
Verify that enterprise customer UUID is returned for a voucher with an associated enterprise customer.
Expand Down
28 changes: 28 additions & 0 deletions ecommerce/enterprise/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,34 @@ def enterprise_customer_user_needs_consent(site, enterprise_customer_uuid, cours
return response.json()['consent_required']


def create_enterprise_customer_user_consent(site, enterprise_customer_uuid, course_id, username):
"""
Create a new consent for a particular username/EC UUID/course ID combination if one doesn't already exist.
Args:
site (Site): The site which is handling the consent-sensitive request
enterprise_customer_uuid (str): The UUID of the relevant EnterpriseCustomer
course_id (str): The ID of the relevant course for enrollment
username (str): The username of the user attempting to enroll into the course
Returns:
bool: consent recorded for the user specified by the username argument
for the EnterpriseCustomer specified by the enterprise_customer_uuid
argument and the course specified by the course_id argument.
"""
data = {
"username": username,
"enterprise_customer_uuid": enterprise_customer_uuid,
"course_id": course_id
}
api_client = site.siteconfiguration.oauth_api_client
consent_url = urljoin(f"{site.siteconfiguration.consent_api_url}/", "data_sharing_consent")

response = api_client.post(consent_url, json=data)
response.raise_for_status()
return response.json()['consent_provided']


def get_enterprise_customer_uuid_from_voucher(voucher):
"""
Given a Voucher, find the associated Enterprise Customer UUID, if it exists.
Expand Down
2 changes: 2 additions & 0 deletions ecommerce/extensions/executive_education_2u/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def place_free_order(
address,
user_details,
terms_accepted_at,
data_share_consent,
request=None,
): # pylint: disable=arguments-differ
"""
Expand Down Expand Up @@ -49,6 +50,7 @@ def place_free_order(
'address': address,
'user_details': user_details,
'terms_accepted_at': terms_accepted_at,
'data_share_consent': data_share_consent,
})

# Place an order. If order placement succeeds, the order is committed
Expand Down
1 change: 1 addition & 0 deletions ecommerce/extensions/executive_education_2u/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ class CheckoutActionSerializer(serializers.Serializer): # pylint: disable=abstr
address = AddressSerializer(required=False)
user_details = UserDetailsSerializer()
terms_accepted_at = serializers.CharField()
data_share_consent = serializers.BooleanField(required=False)
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def setUp(self):
'mobile_phone': '1234567890'
}
self.mock_terms_accepted_at = '2022-08-05T15:28:46.493Z',
self.mock_data_share_consent = True

# Ensure that the basket attribute type exists for these tests
self.basket_attribute_type, _ = BasketAttributeType.objects.get_or_create(
Expand All @@ -57,12 +58,14 @@ def test_order_note_created(self):
'address': self.mock_address,
'user_details': self.mock_user_details,
'terms_accepted_at': self.mock_terms_accepted_at,
'data_share_consent': self.mock_data_share_consent,
})
order = ExecutiveEducation2UOrderPlacementMixin().place_free_order(
basket,
self.mock_address,
self.mock_user_details,
self.mock_terms_accepted_at,
self.mock_data_share_consent,
)

self.assertEqual(basket.status, Basket.SUBMITTED)
Expand All @@ -77,4 +80,5 @@ def test_non_free_basket_order(self):
self.mock_address,
self.mock_user_details,
self.mock_terms_accepted_at,
self.mock_data_share_consent,
)
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ def _create_finish_checkout_payload(self, sku):
'mobile_phone': '1234567890'
},
'terms_accepted_at': '2022-08-05T15:28:46.493Z',
'data_share_consent': True,
}

def _create_basket(self, product, has_offer=False):
Expand Down
1 change: 1 addition & 0 deletions ecommerce/extensions/executive_education_2u/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ def finish_checkout(self, request):
address=request.data.get('address', {}),
user_details={**request.data['user_details'], 'email': request.user.email},
terms_accepted_at=request.data['terms_accepted_at'],
data_share_consent=request.data.get('data_share_consent', None),
request=request
)

Expand Down
32 changes: 30 additions & 2 deletions ecommerce/extensions/fulfillment/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from ecommerce.enterprise.conditions import BasketAttributeType
from ecommerce.enterprise.mixins import EnterpriseDiscountMixin
from ecommerce.enterprise.utils import (
create_enterprise_customer_user_consent,
get_enterprise_customer_uuid_from_voucher,
get_or_create_enterprise_customer_user
)
Expand Down Expand Up @@ -949,8 +950,9 @@ def _create_enterprise_allocation_payload(
# This will be the offer that was applied. We will let an error be thrown if this doesn't exist.
discount = order.discounts.first()
enterprise_customer_uuid = str(discount.offer.condition.enterprise_customer_uuid)
data_share_consent = fulfillment_details.get('data_share_consent', None)

return {
payload = {
'payment_reference': order.number,
'enterprise_customer_uuid': enterprise_customer_uuid,
'currency': currency,
Expand All @@ -966,8 +968,29 @@ def _create_enterprise_allocation_payload(
],
**fulfillment_details.get('address', {}),
**fulfillment_details.get('user_details', {}),
'terms_accepted_at': fulfillment_details.get('terms_accepted_at', '')
'terms_accepted_at': fulfillment_details.get('terms_accepted_at', ''),
}
if data_share_consent:
payload['data_share_consent'] = data_share_consent

return payload

def _create_enterprise_customer_user_consent(
self,
order,
line,
fulfillment_details
):
data_share_consent = fulfillment_details.get('data_share_consent', None)
if data_share_consent and line.product.is_seat_product:
discount = order.discounts.first()
enterprise_customer_uuid = str(discount.offer.condition.enterprise_customer_uuid)
create_enterprise_customer_user_consent(
site=order.site,
enterprise_customer_uuid=enterprise_customer_uuid,
course_id=line.product.attr.course_key,
username=order.user.username
)

def _get_fulfillment_details(self, order):
fulfillment_details_note = order.notes.filter(note_type='Fulfillment Details').first()
Expand Down Expand Up @@ -1020,6 +1043,11 @@ def fulfill_product(self, order, lines, email_opt_in=False):
)

try:
self._create_enterprise_customer_user_consent(
order=order,
line=line,
fulfillment_details=fulfillment_details
)
self.get_smarter_client.create_enterprise_allocation(**allocation_payload)
except Exception as ex: # pylint: disable=broad-except
reason = ''
Expand Down
1 change: 1 addition & 0 deletions ecommerce/extensions/fulfillment/tests/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,7 @@ def setUp(self):
'mobile_phone': '+12015551234',
},
'terms_accepted_at': '2022-07-25T10:29:56Z',
'data_share_consent': True
})

self.mock_settings = {
Expand Down

0 comments on commit a62074f

Please sign in to comment.