|
7 | 7 | from mitol.common.utils import now_in_utc |
8 | 8 |
|
9 | 9 | from b2b import factories |
10 | | -from b2b.api import create_contract_run |
| 10 | +from b2b.api import create_contract_run, validate_basket_for_b2b_purchase |
11 | 11 | from b2b.constants import B2B_RUN_TAG_FORMAT |
| 12 | +from b2b.factories import ContractPageFactory |
12 | 13 | from courses.factories import CourseFactory |
| 14 | +from ecommerce.api_test import create_basket |
| 15 | +from ecommerce.factories import ProductFactory, UnlimitedUseDiscountFactory |
| 16 | +from ecommerce.models import BasketDiscount, DiscountProduct |
13 | 17 | from main.utils import date_to_datetime |
14 | 18 |
|
15 | 19 | FAKE = faker.Factory.create() |
@@ -73,3 +77,71 @@ def test_create_single_course_run(mocker, has_start, has_end): |
73 | 77 | assert run.enrollment_end is None |
74 | 78 |
|
75 | 79 | assert product.purchasable_object == run |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.parametrize( |
| 83 | + ( |
| 84 | + "run_contract", |
| 85 | + "apply_code", |
| 86 | + ), |
| 87 | + [ |
| 88 | + (False, False), |
| 89 | + (False, True), |
| 90 | + (True, False), |
| 91 | + (True, True), |
| 92 | + ], |
| 93 | +) |
| 94 | +def test_b2b_basket_validation(user, run_contract, apply_code): |
| 95 | + """ |
| 96 | + Test that a basket is validated correctly for B2B contracts. |
| 97 | +
|
| 98 | + Basically, if the user is adding a product that links to a course run that |
| 99 | + is also linked to a contract, we need to have also applied the discount code |
| 100 | + that matches the product, or we shouldn't be allowed to buy it. |
| 101 | +
|
| 102 | + The truth table for this should be: |
| 103 | +
|
| 104 | + | run_contract | apply_code | result | |
| 105 | + |--------------|------------|--------| |
| 106 | + | False | False | True | |
| 107 | + | False | True | True | |
| 108 | + | True | False | False | |
| 109 | + | True | True | True | |
| 110 | + """ |
| 111 | + |
| 112 | + product = ProductFactory.create() |
| 113 | + discount = UnlimitedUseDiscountFactory.create() |
| 114 | + discount_product = DiscountProduct.objects.create( |
| 115 | + discount=discount, product=product |
| 116 | + ) |
| 117 | + discount_product.save() |
| 118 | + discount.products.add(discount_product) |
| 119 | + |
| 120 | + if run_contract: |
| 121 | + contract = ContractPageFactory.create() |
| 122 | + |
| 123 | + product.purchasable_object.b2b_contract = contract |
| 124 | + product.purchasable_object.save() |
| 125 | + product.refresh_from_db() |
| 126 | + |
| 127 | + basket = create_basket(user, [product]) |
| 128 | + |
| 129 | + if apply_code: |
| 130 | + redemption = BasketDiscount( |
| 131 | + redemption_date=now_in_utc(), |
| 132 | + redeemed_by=user, |
| 133 | + redeemed_discount=discount, |
| 134 | + redeemed_basket=basket, |
| 135 | + ) |
| 136 | + |
| 137 | + redemption.save() |
| 138 | + basket.refresh_from_db() |
| 139 | + |
| 140 | + check_result = validate_basket_for_b2b_purchase(basket) |
| 141 | + |
| 142 | + if run_contract and not apply_code: |
| 143 | + # User is trying to buy something that's linked to a contract but hasn't |
| 144 | + # applied the code, so this should be false. |
| 145 | + assert check_result is False |
| 146 | + else: |
| 147 | + assert check_result is True |
0 commit comments