Skip to content

Commit

Permalink
feat: Add First Purchase Discount override (#35143)
Browse files Browse the repository at this point in the history
REV-4098
  • Loading branch information
julianajlk authored Jul 22, 2024
1 parent 004cd29 commit a196977
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
27 changes: 21 additions & 6 deletions lms/djangoapps/course_home_api/outline/tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import json # lint-amnesty, pylint: disable=wrong-import-order
from completion.models import BlockCompletion
from django.conf import settings # lint-amnesty, pylint: disable=wrong-import-order
from django.test import override_settings
from django.urls import reverse # lint-amnesty, pylint: disable=wrong-import-order
from edx_toggles.toggles.testutils import override_waffle_flag # lint-amnesty, pylint: disable=wrong-import-order

Expand All @@ -33,7 +34,10 @@
DISPLAY_COURSE_SOCK_FLAG,
ENABLE_COURSE_GOALS
)
from openedx.features.discounts.applicability import DISCOUNT_APPLICABILITY_FLAG
from openedx.features.discounts.applicability import (
DISCOUNT_APPLICABILITY_FLAG,
FIRST_PURCHASE_DISCOUNT_OVERRIDE_FLAG
)
from xmodule.course_block import COURSE_VISIBILITY_PUBLIC, COURSE_VISIBILITY_PUBLIC_OUTLINE # lint-amnesty, pylint: disable=wrong-import-order
from xmodule.modulestore.tests.factories import CourseFactory, BlockFactory # lint-amnesty, pylint: disable=wrong-import-order

Expand Down Expand Up @@ -179,17 +183,28 @@ def test_welcome_message(self, welcome_message_is_dismissed):
welcome_message_html = self.client.get(self.url).data['welcome_message_html']
assert welcome_message_html == (None if welcome_message_is_dismissed else '<p>Welcome</p>')

def test_offer(self):
@ddt.data(
(False, 'EDXWELCOME'),
(True, 'NOTEDXWELCOME'),
)
@ddt.unpack
def test_offer(self, is_fpd_override_waffle_flag_on, fpd_code):
"""
Test that the offer data contains the correct code for the first purchase discount,
which can be overriden via a waffle flag from the default EDXWELCOME.
"""
CourseEnrollment.enroll(self.user, self.course.id)

response = self.client.get(self.url)
assert response.data['offer'] is None

with override_waffle_flag(DISCOUNT_APPLICABILITY_FLAG, active=True):
response = self.client.get(self.url)
with override_settings(FIRST_PURCHASE_DISCOUNT_OVERRIDE_CODE='NOTEDXWELCOME'):
with override_waffle_flag(DISCOUNT_APPLICABILITY_FLAG, active=True):
with override_waffle_flag(FIRST_PURCHASE_DISCOUNT_OVERRIDE_FLAG, active=is_fpd_override_waffle_flag_on):
response = self.client.get(self.url)

# Just a quick spot check that the dictionary looks like what we expect
assert response.data['offer']['code'] == 'EDXWELCOME'
# Just a quick spot check that the dictionary looks like what we expect
assert response.data['offer']['code'] == fpd_code

def test_access_expiration(self):
enrollment = CourseEnrollment.enroll(self.user, self.course.id, CourseMode.VERIFIED)
Expand Down
3 changes: 3 additions & 0 deletions lms/envs/devstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ def should_show_debug_toolbar(request): # lint-amnesty, pylint: disable=missing
########################## Authn MFE Context API #######################
ENABLE_DYNAMIC_REGISTRATION_FIELDS = True

########################## Discount/Coupons #######################
FIRST_PURCHASE_DISCOUNT_OVERRIDE_CODE = ''

############## ECOMMERCE API CONFIGURATION SETTINGS ###############
ECOMMERCE_PUBLIC_URL_ROOT = 'http://localhost:18130'
ECOMMERCE_API_URL = 'http://edx.devstack.ecommerce:18130/api/v2'
Expand Down
3 changes: 3 additions & 0 deletions lms/envs/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@
# Enable a parental consent age limit for testing
PARENTAL_CONSENT_AGE_LIMIT = 13

# Enable First Purchase Discount offer override
FIRST_PURCHASE_DISCOUNT_OVERRIDE_CODE = ''

# Local Directories
TEST_ROOT = path("test_root")
# Want static files in the same dir for running on jenkins.
Expand Down
14 changes: 12 additions & 2 deletions openedx/features/discounts/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import ddt
from django.contrib.auth.models import AnonymousUser
from django.test import TestCase
from django.test import TestCase, override_settings
from django.utils.translation import override as override_lang
from edx_toggles.toggles.testutils import override_waffle_flag

Expand All @@ -14,7 +14,11 @@
from common.djangoapps.student.models import CourseEnrollment
from common.djangoapps.student.tests.factories import UserFactory
from openedx.core.djangoapps.content.course_overviews.tests.factories import CourseOverviewFactory
from openedx.features.discounts.applicability import DISCOUNT_APPLICABILITY_FLAG, get_discount_expiration_date
from openedx.features.discounts.applicability import (
DISCOUNT_APPLICABILITY_FLAG,
FIRST_PURCHASE_DISCOUNT_OVERRIDE_FLAG,
get_discount_expiration_date
)

from .. import utils

Expand Down Expand Up @@ -84,6 +88,12 @@ def test_spanish_code(self):
with override_lang('es-419'):
assert utils.generate_offer_data(self.user, self.overview)['code'] == 'BIENVENIDOAEDX'

def test_override(self):
with override_settings(FIRST_PURCHASE_DISCOUNT_OVERRIDE_CODE='NOTEDXWELCOME'):
with override_waffle_flag(DISCOUNT_APPLICABILITY_FLAG, active=True):
with override_waffle_flag(FIRST_PURCHASE_DISCOUNT_OVERRIDE_FLAG, active=True):
assert utils.generate_offer_data(self.user, self.overview)['code'] == 'NOTEDXWELCOME'

def test_anonymous(self):
assert utils.generate_offer_data(AnonymousUser(), self.overview) is None

Expand Down
13 changes: 12 additions & 1 deletion openedx/features/discounts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime

import pytz
from django.conf import settings
from django.utils.translation import get_language
from django.utils.translation import gettext as _

Expand All @@ -13,6 +14,7 @@
from lms.djangoapps.courseware.utils import verified_upgrade_deadline_link
from openedx.core.djangolib.markup import HTML
from openedx.features.discounts.applicability import (
FIRST_PURCHASE_DISCOUNT_OVERRIDE_FLAG,
REV1008_EXPERIMENT_ID,
can_receive_discount,
discount_percentage,
Expand Down Expand Up @@ -98,8 +100,17 @@ def generate_offer_data(user, course):

original, discounted, percentage = _get_discount_prices(user, course, assume_discount=True)

# Override the First Purchase Discount to another code only if flag is enabled
first_purchase_discount_code = 'BIENVENIDOAEDX' if get_language() == 'es-419' else 'EDXWELCOME'
if FIRST_PURCHASE_DISCOUNT_OVERRIDE_FLAG.is_enabled():
first_purchase_discount_code = getattr(
settings,
'FIRST_PURCHASE_DISCOUNT_OVERRIDE_CODE',
first_purchase_discount_code
)

return {
'code': 'BIENVENIDOAEDX' if get_language() == 'es-419' else 'EDXWELCOME',
'code': first_purchase_discount_code,
'expiration_date': expiration_date,
'original_price': original,
'discounted_price': discounted,
Expand Down

0 comments on commit a196977

Please sign in to comment.