Skip to content

Commit

Permalink
Fixes use of "redemption_type" when generating discount codes in bulk…
Browse files Browse the repository at this point in the history
…, fixes test error (#2077)
  • Loading branch information
jkachel authored Feb 1, 2024
1 parent c878b1a commit 84eba58
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
8 changes: 5 additions & 3 deletions courses/views/v2/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from rest_framework import status

from courses.factories import DepartmentFactory
from courses.models import Program
from courses.models import Course, Program
from courses.serializers.v2.courses import CourseWithCourseRunsSerializer
from courses.serializers.v2.departments import DepartmentWithCountSerializer
from courses.serializers.v2.programs import ProgramSerializer
Expand Down Expand Up @@ -165,7 +165,7 @@ def test_delete_program(


@pytest.mark.parametrize("course_catalog_course_count", [100], indirect=True)
@pytest.mark.parametrize("course_catalog_program_count", [15], indirect=True)
@pytest.mark.parametrize("course_catalog_program_count", [12], indirect=True)
@pytest.mark.parametrize("include_finaid", [True, False])
def test_get_courses(
user_drf_client,
Expand All @@ -175,10 +175,12 @@ def test_get_courses(
include_finaid,
):
"""Test the view that handles requests for all Courses"""
courses, _, _ = course_catalog_data
course_catalog_data
courses_from_fixture = []
num_queries = 0

courses = Course.objects.order_by("title").prefetch_related("departments").all()

if include_finaid:
mock_context["include_approved_financial_aid"] = True

Expand Down
11 changes: 11 additions & 0 deletions ecommerce/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from ecommerce.constants import (
ALL_DISCOUNT_TYPES,
ALL_PAYMENT_TYPES,
ALL_REDEMPTION_TYPES,
DISCOUNT_TYPE_PERCENT_OFF,
PAYMENT_TYPE_FINANCIAL_ASSISTANCE,
REDEMPTION_TYPE_ONE_TIME,
Expand Down Expand Up @@ -671,9 +672,13 @@ def generate_discount_code(**kwargs):
UUID - if you want one (the convention is a -), you need to ensure it's
there in the prefix (and that counts against the limit)
If you specify redemption_type, specifying one_time or one_time_per_user will not be
honored.
Keyword Args:
* discount_type - one of the valid discount types
* payment_type - one of the valid payment types
* redemption_type - one of the valid redemption types (overrules use of the flags)
* amount - the value of the discount
* one_time - boolean; discount can only be redeemed once
* one_time_per_user - boolean; discount can only be redeemed once per user
Expand Down Expand Up @@ -730,6 +735,12 @@ def generate_discount_code(**kwargs):
if "once_per_user" in kwargs and kwargs["once_per_user"]:
redemption_type = REDEMPTION_TYPE_ONE_TIME_PER_USER

if (
"redemption_type" in kwargs
and kwargs["redemption_type"] in ALL_REDEMPTION_TYPES
):
redemption_type = kwargs["redemption_type"]

if "expires" in kwargs and kwargs["expires"] is not None:
expiration_date = parse_supplied_date(kwargs["expires"])
else:
Expand Down
29 changes: 19 additions & 10 deletions ecommerce/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,22 +1037,30 @@ def test_start_checkout_with_zero_value(settings, user, user_client, products):
)


def test_bulk_discount_create(admin_drf_client):
@pytest.mark.parametrize("use_redemption_type_flags", [True, False])
def test_bulk_discount_create(admin_drf_client, use_redemption_type_flags):
"""
Try to make some bulk discounts.
"""

test_payload = {
"discount_type": DISCOUNT_TYPE_PERCENT_OFF,
"payment_type": PAYMENT_TYPE_CUSTOMER_SUPPORT,
"count": 5,
"amount": 50,
"prefix": "Generated-Code-",
"expires": "2030-01-01T00:00:00",
"one_time": True,
}

if use_redemption_type_flags:
test_payload["one_time"] = True
else:
test_payload["redemption_type"] = REDEMPTION_TYPE_ONE_TIME

resp = admin_drf_client.post(
reverse("discounts_api-create_batch"),
{
"discount_type": DISCOUNT_TYPE_PERCENT_OFF,
"payment_type": PAYMENT_TYPE_CUSTOMER_SUPPORT,
"count": 5,
"amount": 50,
"prefix": "Generated-Code-",
"expires": "2030-01-01T00:00:00",
"one_time": True,
},
test_payload,
)

assert resp.status_code == 201
Expand All @@ -1064,5 +1072,6 @@ def test_bulk_discount_create(admin_drf_client):
assert len(discounts) == 5

assert discounts[0].discount_type == DISCOUNT_TYPE_PERCENT_OFF
assert discounts[0].redemption_type == REDEMPTION_TYPE_ONE_TIME
assert discounts[0].amount == 50
assert discounts[0].is_bulk

0 comments on commit 84eba58

Please sign in to comment.