Skip to content

Commit

Permalink
feat: ecommerce Basket Page -> micro-frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
UvgenGen committed May 12, 2022
1 parent 45ec01a commit af8daa7
Show file tree
Hide file tree
Showing 23 changed files with 41 additions and 1,811 deletions.
18 changes: 3 additions & 15 deletions ecommerce/core/management/commands/create_or_update_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,12 @@ def add_arguments(self, parser):
type=str,
required=True,
help='URL for Discovery service API calls.')
parser.add_argument('--enable-microfrontend-for-basket-page',
action='store',
dest='enable_microfrontend_for_basket_page',
type=bool,
required=False,
help='Use the microfrontend implementation of the '
'basket page instead of the server-side template')
parser.add_argument('--payment-microfrontend-url',
action='store',
dest='payment_microfrontend_url',
type=str,
required=False,
help='URL for the Payment Microfrontend '
'(used if Enable Microfrontend for Basket Page is set)')
required=True,
help='URL for the Payment Microfrontend.')

def handle(self, *args, **options): # pylint: disable=too-many-statements
site_id = options.get('site_id')
Expand All @@ -179,10 +171,7 @@ def handle(self, *args, **options): # pylint: disable=too-many-statements
base_cookie_domain = options.get('base_cookie_domain', '')
discovery_api_url = options.get('discovery_api_url')

enable_microfrontend_for_basket_page = bool(options.get('enable_microfrontend_for_basket_page', False))
payment_microfrontend_url = options.get(
'payment_microfrontend_url'
) if enable_microfrontend_for_basket_page else None
payment_microfrontend_url = options.get('payment_microfrontend_url')

try:
site = Site.objects.get(id=site_id)
Expand Down Expand Up @@ -240,7 +229,6 @@ def handle(self, *args, **options): # pylint: disable=too-many-statements
'oauth_settings': oauth_settings,
'base_cookie_domain': base_cookie_domain,
'discovery_api_url': discovery_api_url,
'enable_microfrontend_for_basket_page': enable_microfrontend_for_basket_page,
'payment_microfrontend_url': payment_microfrontend_url,
}
if payment_support_email:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def setUp(self):
self.payment_support_url = 'http://fake.server/support'
self.base_cookie_domain = '.fake.server'
self.discovery_api_url = 'https://fake.discovery.server/api/v1/'
self.payment_microfrontend_url = 'http://payment_mfe_fake.server'

def _check_site_configuration(self, site, partner):
site_configuration = site.siteconfiguration
Expand Down Expand Up @@ -92,6 +93,7 @@ def _call_command(self,
backend_service_client_id,
backend_service_client_secret,
from_email,
payment_microfrontend_url,
lms_public_url_root=None,
site_id=None,
site_name=None,
Expand All @@ -118,7 +120,8 @@ def _call_command(self,
'--sso-client-secret={}'.format(sso_client_secret),
'--backend-service-client-id={}'.format(backend_service_client_id),
'--backend-service-client-secret={}'.format(backend_service_client_secret),
'--from-email={from_email}'.format(from_email=from_email)
'--from-email={from_email}'.format(from_email=from_email),
'--payment-microfrontend-url={}'.format(payment_microfrontend_url),
]

# Optional arguments
Expand Down Expand Up @@ -186,6 +189,7 @@ def test_create_site(self):
backend_service_client_secret=self.backend_service_client_secret,
segment_key=self.segment_key,
from_email=self.from_email,
payment_microfrontend_url=self.payment_microfrontend_url,
discovery_api_url=self.discovery_api_url,
)

Expand Down Expand Up @@ -218,6 +222,7 @@ def test_update_site(self):
backend_service_client_secret=self.backend_service_client_secret,
segment_key=self.segment_key,
from_email=self.from_email,
payment_microfrontend_url=self.payment_microfrontend_url,
enable_enrollment_codes=True,
payment_support_email=self.payment_support_email,
payment_support_url=self.payment_support_url,
Expand All @@ -239,7 +244,8 @@ def test_update_site(self):
self.assertEqual(site_configuration.payment_support_email, self.payment_support_email)
self.assertEqual(site_configuration.payment_support_url, self.payment_support_url)
self.assertTrue(site_configuration.send_refund_notifications)
self.assertEqual(site.siteconfiguration.base_cookie_domain, self.base_cookie_domain)
self.assertEqual(site_configuration.base_cookie_domain, self.base_cookie_domain)
self.assertEqual(site_configuration.payment_microfrontend_url, self.payment_microfrontend_url)

def test_update_site_with_updated_oauth(self):
"""
Expand All @@ -265,6 +271,7 @@ def test_update_site_with_updated_oauth(self):
backend_service_client_secret=self.backend_service_client_secret,
segment_key=self.segment_key,
from_email=self.from_email,
payment_microfrontend_url=self.payment_microfrontend_url,
enable_enrollment_codes=True,
payment_support_email=self.payment_support_email,
payment_support_url=self.payment_support_url,
Expand All @@ -286,7 +293,8 @@ def test_update_site_with_updated_oauth(self):
self.assertEqual(site_configuration.payment_support_email, self.payment_support_email)
self.assertEqual(site_configuration.payment_support_url, self.payment_support_url)
self.assertTrue(site_configuration.send_refund_notifications)
self.assertEqual(site.siteconfiguration.base_cookie_domain, self.base_cookie_domain)
self.assertEqual(site_configuration.base_cookie_domain, self.base_cookie_domain)
self.assertEqual(site_configuration.payment_microfrontend_url, self.payment_microfrontend_url)

@data(
['--site-id=1'],
Expand Down
12 changes: 2 additions & 10 deletions ecommerce/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,9 @@ class SiteConfiguration(models.Model):
max_length=255,
blank=True
)
enable_microfrontend_for_basket_page = models.BooleanField(
verbose_name=_('Enable Microfrontend for Basket Page'),
help_text=_('Use the microfrontend implementation of the basket page instead of the server-side template'),
blank=True,
default=False
)
payment_microfrontend_url = models.URLField(
verbose_name=_('Payment Microfrontend URL'),
help_text=_('URL for the Payment Microfrontend (used if Enable Microfrontend for Basket Page is set)'),
help_text=_('URL for the Payment Microfrontend'),
null=True,
blank=True
)
Expand Down Expand Up @@ -376,9 +370,7 @@ def enterprise_grant_data_sharing_url(self):

@property
def payment_domain_name(self):
if self.enable_microfrontend_for_basket_page:
return urlsplit(self.payment_microfrontend_url).netloc
return self.site.domain
return urlsplit(self.payment_microfrontend_url).netloc

@property
def access_token(self):
Expand Down
2 changes: 1 addition & 1 deletion ecommerce/coupons/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ def test_inactive_user_email_domain_restricted_coupon_redemption(self):
self.assert_redirected_to_email_confirmation(response)

def get_coupon_redeem_success_expected_redirect_url(self):
return self.get_full_url(path=reverse('basket:summary')) + '?coupon_redeem_redirect=1'
return self.site_configuration.payment_microfrontend_url + '?coupon_redeem_redirect=1'


@ddt.ddt
Expand Down
4 changes: 2 additions & 2 deletions ecommerce/coupons/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
parse_consent_params
)
from ecommerce.extensions.api import exceptions
from ecommerce.extensions.basket.utils import get_payment_microfrontend_or_basket_url, prepare_basket
from ecommerce.extensions.basket.utils import get_payment_microfrontend, prepare_basket
from ecommerce.extensions.checkout.mixins import EdxOrderPlacementMixin
from ecommerce.extensions.checkout.utils import get_receipt_page_url
from ecommerce.extensions.offer.utils import get_redirect_to_email_confirmation_if_required
Expand Down Expand Up @@ -297,7 +297,7 @@ def get(self, request): # pylint: disable=too-many-statements
# The coupon_redeem_redirect query param is used to communicate to the Payment MFE that it may redirect
# and should not display the payment form before making that determination.
# TODO: It would be cleaner if the user could be redirected to their final destination up front.
redirect_url = get_payment_microfrontend_or_basket_url(self.request) + "?coupon_redeem_redirect=1"
redirect_url = get_payment_microfrontend(self.request) + "?coupon_redeem_redirect=1"
return HttpResponseRedirect(redirect_url)


Expand Down
20 changes: 0 additions & 20 deletions ecommerce/extensions/basket/apps.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@


from django.conf.urls import url
from django.contrib.auth.decorators import login_required
from oscar.apps.basket import apps
from oscar.core.loading import get_class


class BasketConfig(apps.BasketConfig):
name = 'ecommerce.extensions.basket'

# pylint: disable=attribute-defined-outside-init
def ready(self):
super().ready()
self.basket_add_items_view = get_class('basket.views', 'BasketAddItemsView')
self.summary_view = get_class('basket.views', 'BasketSummaryView')

def get_urls(self):
urls = [
url(r'^$', login_required(self.summary_view.as_view()), name='summary'),
url(r'^add/(?P<pk>\d+)/$', self.add_view.as_view(), name='add'),
url(r'^vouchers/add/$', self.add_voucher_view.as_view(), name='vouchers-add'),
url(r'^vouchers/(?P<pk>\d+)/remove/$', self.remove_voucher_view.as_view(), name='vouchers-remove'),
url(r'^saved/$', login_required(self.saved_view.as_view()), name='saved'),
url(r'^add/$', self.basket_add_items_view.as_view(), name='basket-add'),
]
return self.post_process_urls(urls)
22 changes: 0 additions & 22 deletions ecommerce/extensions/basket/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
apply_voucher_on_basket_and_check_discount,
attribute_cookie_data,
get_basket_switch_data,
get_payment_microfrontend_url_if_configured,
is_duplicate_seat_attempt,
prepare_basket
)
Expand All @@ -35,7 +34,6 @@
from ecommerce.extensions.order.exceptions import AlreadyPlacedOrderException
from ecommerce.extensions.order.utils import UserAlreadyPlacedOrder
from ecommerce.extensions.partner.models import StockRecord
from ecommerce.extensions.payment.constants import DISABLE_MICROFRONTEND_FOR_BASKET_PAGE_FLAG_NAME
from ecommerce.extensions.test.factories import create_order, prepare_voucher
from ecommerce.referrals.models import Referral
from ecommerce.tests.testcases import TestCase, TransactionTestCase
Expand Down Expand Up @@ -688,26 +686,6 @@ def test_apply_voucher_on_basket_and_check_discount_with_multiple_vouchers(self)
self.assertEqual(applied, False)
self.assertEqual(msg, 'Basket does not qualify for coupon code {code}.'.format(code=invalid_voucher.code))

@ddt.data(
(True, '/payment', False, '/payment'), # Microfrontend not disabled
(True, '/payment', True, None), # Microfrontend disabled
)
@ddt.unpack
def test_disable_microfrontend_for_basket_page_flag(
self,
microfrontend_enabled,
payment_microfrontend_url,
disable_microfrontend_flag_active,
expected_result
):
"""
Verify that the `disable_microfrontend_for_basket_page_flag` correctly disables the microfrontend url retrieval
"""
with override_flag(DISABLE_MICROFRONTEND_FOR_BASKET_PAGE_FLAG_NAME, active=disable_microfrontend_flag_active):
self.site_configuration.enable_microfrontend_for_basket_page = microfrontend_enabled
self.site_configuration.payment_microfrontend_url = payment_microfrontend_url
self.assertEqual(get_payment_microfrontend_url_if_configured(self.request), expected_result)

def test_prepare_basket_with_duplicate_seat(self):
""" Verify a basket fixes the case where flush doesn't work and we attempt adding duplicate seat. """
with mock.patch('ecommerce.extensions.basket.utils.Basket.flush'):
Expand Down
Loading

0 comments on commit af8daa7

Please sign in to comment.