Only make users who haven't previously enrolled in a non-upsellable course eligible for the discount
This commit is contained in:
@@ -11,6 +11,7 @@ not other discounts like coupons or enterprise/program offers configured in ecom
|
||||
from course_modes.models import CourseMode
|
||||
from openedx.core.djangoapps.waffle_utils import WaffleFlag, WaffleFlagNamespace
|
||||
from openedx.features.discounts.models import DiscountRestrictionConfig
|
||||
from student.models import CourseEnrollment
|
||||
|
||||
# .. feature_toggle_name: discounts.enable_discounting
|
||||
# .. feature_toggle_type: flag
|
||||
@@ -55,6 +56,11 @@ def can_receive_discount(user, course): # pylint: disable=unused-argument
|
||||
if DiscountRestrictionConfig.disabled_for_course_stacked_config(course):
|
||||
return False
|
||||
|
||||
# Don't allow users who have enrolled in any courses in non-upsellable
|
||||
# modes
|
||||
if CourseEnrollment.objects.filter(user=user).exclude(mode__in=CourseMode.UPSELL_TO_VERIFIED_MODES).exists():
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
|
||||
@@ -2,19 +2,22 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from datetime import timedelta
|
||||
import ddt
|
||||
from django.utils.timezone import now
|
||||
|
||||
from course_modes.models import CourseMode
|
||||
from course_modes.tests.factories import CourseModeFactory
|
||||
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
|
||||
from openedx.core.djangoapps.waffle_utils.testutils import override_waffle_flag
|
||||
from openedx.features.discounts.models import DiscountRestrictionConfig
|
||||
from student.tests.factories import UserFactory
|
||||
from student.tests.factories import UserFactory, CourseEnrollmentFactory
|
||||
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
|
||||
from xmodule.modulestore.tests.factories import CourseFactory
|
||||
|
||||
from ..applicability import can_receive_discount, DISCOUNT_APPLICABILITY_FLAG
|
||||
|
||||
|
||||
@ddt.ddt
|
||||
class TestApplicability(ModuleStoreTestCase):
|
||||
"""
|
||||
Applicability determines if this combination of user and course can receive a discount. Make
|
||||
@@ -54,3 +57,24 @@ class TestApplicability(ModuleStoreTestCase):
|
||||
DiscountRestrictionConfig.objects.create(disabled=True, course=disabled_course_overview)
|
||||
applicability = can_receive_discount(user=self.user, course=disabled_course)
|
||||
self.assertEqual(applicability, False)
|
||||
|
||||
@ddt.data(*(
|
||||
[[]] +
|
||||
[[mode] for mode in CourseMode.ALL_MODES] +
|
||||
[
|
||||
[mode1, mode2]
|
||||
for mode1 in CourseMode.ALL_MODES
|
||||
for mode2 in CourseMode.ALL_MODES
|
||||
if mode1 != mode2
|
||||
]
|
||||
))
|
||||
@override_waffle_flag(DISCOUNT_APPLICABILITY_FLAG, active=True)
|
||||
def test_can_receive_discount_previous_verified_enrollment(self, existing_enrollments):
|
||||
"""
|
||||
Ensure that only users who have not already purchased courses receive the discount.
|
||||
"""
|
||||
for mode in existing_enrollments:
|
||||
CourseEnrollmentFactory.create(mode=mode, user=self.user)
|
||||
|
||||
applicability = can_receive_discount(user=self.user, course=self.course)
|
||||
assert applicability == all(mode in CourseMode.UPSELL_TO_VERIFIED_MODES for mode in existing_enrollments)
|
||||
|
||||
Reference in New Issue
Block a user