diff --git a/openedx/features/course_experience/tests/views/test_course_home.py b/openedx/features/course_experience/tests/views/test_course_home.py index 9612385742..135c944b34 100644 --- a/openedx/features/course_experience/tests/views/test_course_home.py +++ b/openedx/features/course_experience/tests/views/test_course_home.py @@ -411,17 +411,37 @@ class TestCourseHomePageAccess(CourseHomePageTestCase): self.assertRedirects(response, expected_url) @override_waffle_flag(FIRST_PURCHASE_OFFER_BANNER_DISPLAY, active=True) - def test_first_purchase_offer_banner(self): + @mock.patch('openedx.features.course_experience.utils.discount_percentage') + @mock.patch('openedx.features.course_experience.utils.can_recieve_discount') + @ddt.data( + [True, 15], + [True, 13], + [True, 0], + [False, 15]) + @ddt.unpack + def test_first_purchase_offer_banner_display(self, + applicability, + percentage, + can_recieve_discount_mock, + discount_percentage_mock): """ Ensure first purchase offer banner displays correctly """ + can_recieve_discount_mock.return_value = applicability + discount_percentage_mock.return_value = percentage user = self.create_user_for_course(self.course, CourseUserType.ENROLLED) self.client.login(username=user.username, password=self.TEST_PASSWORD) url = course_home_url(self.course) response = self.client.get(url) - bannerText = u'''
''' - self.assertContains(response, bannerText, html=True) + bannerText = u''''''.format(percentage) + if applicability: + self.assertContains(response, bannerText, html=True) + else: + self.assertNotContains(response, bannerText, html=True) @mock.patch.dict(settings.FEATURES, {'DISABLE_START_DATES': False}) def test_course_does_not_expire_for_verified_user(self): diff --git a/openedx/features/course_experience/utils.py b/openedx/features/course_experience/utils.py index 01e636ba98..e4b0a70a94 100644 --- a/openedx/features/course_experience/utils.py +++ b/openedx/features/course_experience/utils.py @@ -14,6 +14,7 @@ from lms.djangoapps.course_blocks.utils import get_student_module_as_dict from openedx.core.djangolib.markup import HTML from openedx.core.lib.cache_utils import request_cached from openedx.features.course_experience import FIRST_PURCHASE_OFFER_BANNER_DISPLAY +from openedx.features.discounts.applicability import can_recieve_discount, discount_percentage from xmodule.modulestore.django import modulestore @@ -192,14 +193,18 @@ def get_resume_block(block): def get_first_purchase_offer_banner_fragment(user, course): - if FIRST_PURCHASE_OFFER_BANNER_DISPLAY.is_enabled() and user and course: + if (FIRST_PURCHASE_OFFER_BANNER_DISPLAY.is_enabled() and + user and + course and + can_recieve_discount(user=user, course_key_string=unicode(course.id))): # Translator: xgettext:no-python-format - offer_message = _(u'{banner_open}15% off your first upgrade.{span_close}' + offer_message = _(u'{banner_open}{percentage}% off your first upgrade.{span_close}' u' Discount automatically applied.{div_close}') return Fragment(HTML(offer_message).format( banner_open=HTML( '') ))