diff --git a/courses/views/v2/views_test.py b/courses/views/v2/views_test.py index 9e1397dc5b..e7dc644daa 100644 --- a/courses/views/v2/views_test.py +++ b/courses/views/v2/views_test.py @@ -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 @@ -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, @@ -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 diff --git a/ecommerce/api.py b/ecommerce/api.py index f71e20425f..fbfa37b153 100644 --- a/ecommerce/api.py +++ b/ecommerce/api.py @@ -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, @@ -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 @@ -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: diff --git a/ecommerce/views_test.py b/ecommerce/views_test.py index d0695beb1d..1be1b308d3 100644 --- a/ecommerce/views_test.py +++ b/ecommerce/views_test.py @@ -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 @@ -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