def test_customer_usage_limit(rf): default_price = 10 request, product = _init_test_for_product_without_basket(rf, default_price) shop = request.shop customers = [] for x in range(3): customers.append(factories.create_random_company(shop=shop)) discount_percentage = 0.20 coupon = CouponCode.objects.create(code="sUpErAle", active=True, usage_limit_customer=2) coupon.shops = [request.shop] discount = Discount.objects.create( active=True, product=product, coupon_code=coupon, discount_percentage=discount_percentage) discount.shops.add(request.shop) # Order product twice for each customer for customer in customers: for y in range(2): _create_order(request, customer, coupon, product, request.shop.create_price(8)) assert coupon.usages.count() == 6 # Each customer 2 orders # Any of the customers created shouldn't be allowed to # order more with this coupon code. for customer in customers: assert not CouponCode.is_usable(shop, coupon, customer) # New customer should still be able to order some new_customer = factories.create_random_person() assert CouponCode.is_usable(shop, coupon, new_customer) _create_order(request, new_customer, coupon, product, request.shop.create_price(8)) assert coupon.usages.count() == 7 # Set usage limit and the new customer shouldn't be able to use the code coupon.usage_limit = 7 coupon.save() assert not CouponCode.is_usable(request.shop, coupon, new_customer) _create_order(request, new_customer, coupon, product, request.shop.create_price(default_price)) assert coupon.usages.count() == 7 # One of the customer got refund refunded_customer = customers[0] order = refunded_customer.customer_orders.first() coupon_code_modifier = CouponCodeModule() coupon_code_modifier.clear_codes(order) assert coupon.usages.count() == 6 # New customer still doesn't able to create coupon new_customer = factories.create_random_person() assert CouponCode.is_usable(shop, coupon, new_customer) _create_order(request, new_customer, coupon, product, request.shop.create_price(8)) assert coupon.usages.count() == 7
def test_matching_coupon_code(rf): default_price = 10 request, product = _init_test_for_product_without_basket(rf, default_price) discount_amount = 4 coupon_code = CouponCode.objects.create(code="HORSESHOW2018", active=True) coupon_code.shops = [request.shop] discount = Discount.objects.create(active=True, product=product, coupon_code=coupon_code, discount_amount_value=discount_amount) discount.shops.add(request.shop) # No basket means no coupon code in basket which means no discount assert product.get_price_info(request).price == request.shop.create_price( default_price) request, product, basket = _init_test_for_product_with_basket( rf, default_price) assert request.basket == basket # Ok now we have basket, but the coupon is not yet applied assert product.get_price_info(request).price == request.shop.create_price( default_price) # Make sure disabling discount makes coupon un-usable coupon_code_modifier = CouponCodeModule() assert coupon_code_modifier.can_use_code(request.basket, coupon_code.code) discount.active = False discount.save() assert not coupon_code_modifier.can_use_code(request.basket, coupon_code.code) discount.active = True discount.save() assert coupon_code_modifier.can_use_code(request.basket, coupon_code.code) basket.add_code(coupon_code) assert coupon_code.code in basket.codes assert coupon_code.code in request.basket.codes assert product.get_price_info(request).price == request.shop.create_price( default_price - discount_amount) # Apply coupon code after order is created basket.clear_codes() creator = OrderCreator() order = creator.create_order(basket) assert order.taxful_total_price == request.shop.create_price(default_price) # Make sure non active discount can't be used discount.active = False discount.save() order_modifier = CouponCodeModule() assert not order_modifier.use_code(order, coupon_code.code) discount.active = True discount.save() assert isinstance(order_modifier.use_code(order, coupon_code.code), CouponUsage)
def test_customer_usage_limit(rf): default_price = 10 request, product = _init_test_for_product_without_basket(rf, default_price) shop = request.shop customers = [] for x in range(3): customers.append(factories.create_random_company(shop=shop)) discount_percentage = 0.20 coupon = CouponCode.objects.create(code="sUpErAle", active=True, usage_limit_customer=2) coupon.shops = [request.shop] discount = Discount.objects.create( active=True, product=product, coupon_code=coupon, discount_percentage=discount_percentage) discount.shops.add(request.shop) # Order product twice for each customer for customer in customers: for y in range(2): _create_order(request, customer, coupon, product, request.shop.create_price(8)) assert coupon.usages.count() == 6 # Each customer 2 orders # Any of the customers created shouldn't be allowed to # order more with this coupon code. for customer in customers: assert not CouponCode.is_usable(shop, coupon, customer) # New customer should still be able to order some new_customer = factories.create_random_person() assert CouponCode.is_usable(shop, coupon, new_customer) _create_order(request, new_customer, coupon, product, request.shop.create_price(8)) assert coupon.usages.count() == 7 # Set usage limit and the new customer shouldn't be able to use the code coupon.usage_limit = 7 coupon.save() assert not CouponCode.is_usable(request.shop, coupon, new_customer) _create_order(request, new_customer, coupon, product, request.shop.create_price(default_price)) assert coupon.usages.count() == 7 # One of the customer got refund refunded_customer = customers[0] order = refunded_customer.customer_orders.first() coupon_code_modifier = CouponCodeModule() coupon_code_modifier.clear_codes(order) assert coupon.usages.count() == 6 # New customer still doesn't able to create coupon new_customer = factories.create_random_person() assert CouponCode.is_usable(shop, coupon, new_customer) _create_order(request, new_customer, coupon, product, request.shop.create_price(8)) assert coupon.usages.count() == 7
def test_matching_coupon_code(rf): default_price = 10 request, product = _init_test_for_product_without_basket(rf, default_price) discount_amount = 4 coupon_code = CouponCode.objects.create(code="HORSESHOW2018", active=True) coupon_code.shops = [request.shop] discount = Discount.objects.create( active=True, product=product, coupon_code=coupon_code, discount_amount_value=discount_amount) discount.shops.add(request.shop) # No basket means no coupon code in basket which means no discount assert product.get_price_info(request).price == request.shop.create_price(default_price) request, product, basket = _init_test_for_product_with_basket(rf, default_price) assert request.basket == basket # Ok now we have basket, but the coupon is not yet applied assert product.get_price_info(request).price == request.shop.create_price(default_price) # Make sure disabling discount makes coupon un-usable coupon_code_modifier = CouponCodeModule() assert coupon_code_modifier.can_use_code(request.basket, coupon_code.code) discount.active = False discount.save() assert not coupon_code_modifier.can_use_code(request.basket, coupon_code.code) discount.active = True discount.save() assert coupon_code_modifier.can_use_code(request.basket, coupon_code.code) basket.add_code(coupon_code) assert coupon_code.code in basket.codes assert coupon_code.code in request.basket.codes get_price_info(context=request, product=product.id) # Test if get_price_info works with product.id sent assert product.get_price_info(request).price == request.shop.create_price(default_price - discount_amount) # Apply coupon code after order is created basket.clear_codes() creator = OrderCreator() order = creator.create_order(basket) assert order.taxful_total_price == request.shop.create_price(default_price) # Make sure non active discount can't be used discount.active = False discount.save() order_modifier = CouponCodeModule() assert not order_modifier.use_code(order, coupon_code.code) discount.active = True discount.save() assert isinstance(order_modifier.use_code(order, coupon_code.code), CouponUsage)