Example #1
0
def test_get_discount_for_checkout_shipping_voucher_not_applicable(
    is_shipping_required,
    shipping_method,
    discount_value,
    discount_type,
    countries,
    min_amount_spent,
    subtotal,
    error_msg,
):
    subtotal_price = TaxedMoney(net=subtotal, gross=subtotal)
    checkout = Mock(
        get_subtotal=Mock(return_value=subtotal_price),
        is_shipping_required=Mock(return_value=is_shipping_required),
        shipping_method=shipping_method,
    )
    voucher = Voucher(
        code="unique",
        type=VoucherType.SHIPPING,
        discount_value_type=discount_type,
        discount_value=discount_value,
        min_amount_spent=(Money(min_amount_spent, "USD")
                          if min_amount_spent is not None else None),
        countries=countries,
    )
    with pytest.raises(NotApplicable) as e:
        get_voucher_discount_for_checkout(voucher, checkout)
    assert str(e.value) == error_msg
Example #2
0
def test_products_voucher_checkout_discount_not(
    monkeypatch,
    prices,
    discount_value,
    discount_type,
    expected_value,
    apply_once_per_order,
    checkout_with_item,
):
    monkeypatch.setattr(
        "saleor.checkout.utils.get_prices_of_discounted_products",
        lambda lines, discounted_products:
        (TaxedMoney(net=Money(price, "USD"), gross=Money(price, "USD"))
         for price in prices),
    )
    voucher = Voucher(
        code="unique",
        type=VoucherType.PRODUCT,
        discount_value_type=discount_type,
        discount_value=discount_value,
        apply_once_per_order=apply_once_per_order,
    )
    voucher.save()
    checkout = checkout_with_item
    discount = get_voucher_discount_for_checkout(voucher, checkout)
    assert discount == Money(expected_value, "USD")
Example #3
0
def test_specific_products_voucher_checkout_discount(
    monkeypatch,
    prices,
    discount_value,
    discount_type,
    expected_value,
    apply_once_per_order,
    checkout_with_item,
):
    discounts = []
    monkeypatch.setattr(
        "saleor.checkout.utils.get_prices_of_discounted_specific_product",
        lambda lines, discounts, discounted_products:
        (Money(price, "USD") for price in prices),
    )
    voucher = Voucher(
        code="unique",
        type=VoucherType.SPECIFIC_PRODUCT,
        discount_value_type=discount_type,
        discount_value=discount_value,
        apply_once_per_order=apply_once_per_order,
    )
    voucher.save()
    checkout = checkout_with_item
    discount = get_voucher_discount_for_checkout(voucher, checkout, discounts)
    assert discount == Money(expected_value, "USD")
Example #4
0
def test_valid_voucher_limit(settings, limit, value):
    voucher = Voucher(code='unique',
                      type=Voucher.SHIPPING_TYPE,
                      discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
                      discount_value=Price(10, currency='USD'),
                      limit=limit)
    voucher.validate_limit(value)
Example #5
0
def test_get_discount_for_checkout_shipping_voucher(
    shipping_cost,
    shipping_country_code,
    discount_value,
    discount_type,
    countries,
    expected_value,
):
    subtotal = TaxedMoney(net=Money(100, "USD"), gross=Money(100, "USD"))
    shipping_total = TaxedMoney(net=Money(shipping_cost, "USD"),
                                gross=Money(shipping_cost, "USD"))
    checkout = Mock(
        get_subtotal=Mock(return_value=subtotal),
        is_shipping_required=Mock(return_value=True),
        shipping_method=Mock(get_total=Mock(return_value=shipping_total)),
        shipping_address=Mock(country=Country(shipping_country_code)),
    )
    voucher = Voucher(
        code="unique",
        type=VoucherType.SHIPPING,
        discount_value_type=discount_type,
        discount_value=discount_value,
        countries=countries,
    )
    discount = get_voucher_discount_for_checkout(voucher, checkout)
    assert discount == Money(expected_value, "USD")
Example #6
0
def test_valid_voucher_min_amount_spent(min_amount_spent, value):
    voucher = Voucher(
        code="unique",
        type=VoucherType.SHIPPING,
        discount_value_type=DiscountValueType.FIXED,
        discount_value=Money(10, "USD"),
        min_amount_spent=min_amount_spent,
    )
    voucher.validate_min_amount_spent(value)
Example #7
0
def test_valid_voucher_min_amount_spent(settings, min_amount_spent, value):
    voucher = Voucher(
        code="unique",
        type=VoucherType.SHIPPING,
        discount_value_type=DiscountValueType.FIXED,
        discount_value=Money(10, "USD"),
        min_amount_spent=min_amount_spent,
    )
    voucher.validate_min_amount_spent(TaxedMoney(net=value, gross=value))
Example #8
0
def test_value_voucher_checkout_discount_not_applicable(settings):
    voucher = Voucher(code='unique',
                      type=VoucherType.VALUE,
                      discount_value_type=DiscountValueType.FIXED,
                      discount_value=10,
                      limit=100)
    checkout = Mock(get_subtotal=Mock(return_value=Price(10, currency='USD')))
    with pytest.raises(NotApplicable) as e:
        voucher.get_discount_for_checkout(checkout)
    assert e.value.limit == Price(100, currency='USD')
Example #9
0
def test_value_voucher_order_discount_not_applicable_returns_zero(settings):
    voucher = Voucher(code='unique',
                      type=VoucherType.VALUE,
                      discount_value_type=DiscountValueType.FIXED,
                      discount_value=10,
                      limit=Money(100, 'USD'))
    subtotal = TaxedMoney(net=Money(10, 'USD'), gross=Money(10, 'USD'))
    order = Mock(get_subtotal=Mock(return_value=subtotal), voucher=voucher)
    discount = get_voucher_discount_for_order(order)
    assert discount == Money(0, 'USD')
Example #10
0
def test_query_vouchers_with_filter_times_used(
    voucher_filter,
    count,
    staff_api_client,
    query_vouchers_with_filter,
    permission_manage_discounts,
):
    Voucher.objects.bulk_create([
        Voucher(name="Voucher1", discount_value=123, code="abc"),
        Voucher(name="Voucher2", discount_value=123, code="123", used=2),
    ])
    variables = {"filter": voucher_filter}
    response = staff_api_client.post_graphql(
        query_vouchers_with_filter,
        variables,
        permissions=[permission_manage_discounts])
    content = get_graphql_content(response)
    data = content["data"]["vouchers"]["edges"]
    assert len(data) == count
Example #11
0
def test_value_voucher_checkout_discount_not_applicable(settings):
    voucher = Voucher(code='unique',
                      type=Voucher.VALUE_TYPE,
                      discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
                      discount_value=10,
                      limit=100)
    checkout = Mock(get_subtotal=Mock(return_value=Price(10, currency='USD')))
    with pytest.raises(NotApplicable) as e:
        voucher.get_discount_for_checkout(checkout)
    assert str(e.value) == 'This offer is only valid for orders over $100.00.'
Example #12
0
def test_query_vouchers_with_filter_started(voucher_filter, count,
                                            staff_api_client,
                                            query_vouchers_with_filter,
                                            permission_manage_discounts):
    Voucher.objects.bulk_create([
        Voucher(name='Voucher1', discount_value=123, code='abc'),
        Voucher(name='Voucher2',
                discount_value=123,
                code='123',
                start_date=date(2012, 1, 5))
    ])
    variables = {'filter': voucher_filter}
    response = staff_api_client.post_graphql(
        query_vouchers_with_filter,
        variables,
        permissions=[permission_manage_discounts])
    content = get_graphql_content(response)
    data = content['data']['vouchers']['edges']
    assert len(data) == count
Example #13
0
def test_query_vouchers_with_sort(voucher_sort, result_order, staff_api_client,
                                  permission_manage_discounts):
    Voucher.objects.bulk_create([
        Voucher(
            name="Voucher1",
            discount_value=123,
            code="abc",
            discount_value_type=DiscountValueType.FIXED,
            type=VoucherType.ENTIRE_ORDER,
            usage_limit=10,
        ),
        Voucher(
            name="Voucher2",
            discount_value=23,
            code="123",
            discount_value_type=DiscountValueType.FIXED,
            type=VoucherType.ENTIRE_ORDER,
            start_date=timezone.now().replace(year=2012, month=1, day=5),
            end_date=timezone.now().replace(year=2013, month=1, day=5),
            min_spent_amount=50,
        ),
        Voucher(
            name="FreeShipping",
            discount_value=100,
            code="xyz",
            discount_value_type=DiscountValueType.PERCENTAGE,
            type=VoucherType.SHIPPING,
            start_date=timezone.now().replace(year=2011, month=1, day=5),
            end_date=timezone.now().replace(year=2015, month=12, day=31),
            usage_limit=1000,
            min_spent_amount=500,
        ),
    ])
    variables = {"sort_by": voucher_sort}
    staff_api_client.user.user_permissions.add(permission_manage_discounts)
    response = staff_api_client.post_graphql(QUERY_VOUCHER_WITH_SORT,
                                             variables)
    content = get_graphql_content(response)
    vouchers = content["data"]["vouchers"]["edges"]

    for order, voucher_name in enumerate(result_order):
        assert vouchers[order]["node"]["name"] == voucher_name
def test_get_discount_for_checkout_value_voucher_not_applicable():
    voucher = Voucher(code='unique',
                      type=VoucherType.VALUE,
                      discount_value_type=DiscountValueType.FIXED,
                      discount_value=10,
                      min_amount_spent=Money(100, 'USD'))
    subtotal = TaxedMoney(net=Money(10, 'USD'), gross=Money(10, 'USD'))
    checkout = Mock(get_subtotal=Mock(return_value=subtotal))
    with pytest.raises(NotApplicable) as e:
        get_voucher_discount_for_checkout(voucher, checkout)
    assert e.value.min_amount_spent == Money(100, 'USD')
Example #15
0
def test_value_voucher_order_discount(settings, total, discount_value,
                                      discount_type, limit, expected_value):
    voucher = Voucher(code='unique',
                      type=VoucherType.VALUE,
                      discount_value_type=discount_type,
                      discount_value=discount_value,
                      limit=Money(limit, 'USD') if limit is not None else None)
    subtotal = TaxedMoney(net=Money(total, 'USD'), gross=Money(total, 'USD'))
    order = Mock(get_subtotal=Mock(return_value=subtotal), voucher=voucher)
    discount = get_voucher_discount_for_order(order)
    assert discount == Money(expected_value, 'USD')
Example #16
0
def test_category_voucher_checkout_discount_not_applicable(monkeypatch):
    monkeypatch.setattr(
        'saleor.dashboard.order.utils.get_category_variants_and_prices',
        lambda order, product: [])
    voucher = Voucher(code='unique',
                      type=VoucherType.CATEGORY,
                      discount_value_type=DiscountValueType.FIXED,
                      discount_value=10)
    order = Mock(voucher=voucher)
    discount = get_voucher_discount_for_order(order)
    assert discount == Money(0, 'USD')
Example #17
0
def test_get_value_voucher_discount(
        total, limit, discount_value, discount_value_type, expected_value):
    voucher = Voucher(
        code='unique', type=VoucherType.VALUE,
        discount_value_type=discount_value_type,
        discount_value=discount_value,
        limit=Money(limit, 'USD') if limit is not None else None)
    total_price = TaxedMoney(
        net=Money(total, 'USD'), gross=Money(total, 'USD'))
    discount = get_value_voucher_discount(voucher, total_price)
    assert discount == Money(expected_value, 'USD')
Example #18
0
def test_value_voucher_checkout_discount(settings, total, discount_value,
                                         discount_type, limit, expected_value):
    voucher = Voucher(
        code='unique', type=Voucher.VALUE_TYPE,
        discount_value_type=discount_type,
        discount_value=discount_value,
        limit=Price(limit, currency='USD') if limit is not None else None)
    checkout = Mock(get_subtotal=Mock(return_value=Price(total,
                                                         currency='USD')))
    discount = voucher.get_discount_for_checkout(checkout)
    assert discount.amount == Price(expected_value, currency='USD')
Example #19
0
def test_shipping_voucher_checkout_discount_not_applicable_returns_zero():
    voucher = Voucher(code='unique',
                      type=VoucherType.SHIPPING,
                      discount_value_type=DiscountValueType.FIXED,
                      discount_value=10,
                      min_amount_spent=Money(20, 'USD'))
    price = TaxedMoney(net=Money(10, 'USD'), gross=Money(10, 'USD'))
    order = Mock(get_subtotal=Mock(return_value=price),
                 shipping_price=price,
                 voucher=voucher)
    with pytest.raises(NotApplicable):
        get_voucher_discount_for_order(order)
Example #20
0
def test_value_voucher_checkout_discount(
        settings, total, discount_value, discount_type, limit, expected_value):
    voucher = Voucher(
        code='unique', type=VoucherType.VALUE,
        discount_value_type=discount_type,
        discount_value=discount_value,
        limit=Money(limit, currency='USD') if limit is not None else None)
    subtotal = TaxedMoney(
        net=Money(total, currency='USD'), gross=Money(total, currency='USD'))
    checkout = Mock(get_subtotal=Mock(return_value=subtotal))
    discount = get_voucher_discount_for_checkout(voucher, checkout)
    assert discount == Money(expected_value, currency='USD')
Example #21
0
def test_shipping_voucher_checkout_discount_not_applicable_returns_zero():
    voucher = Voucher(code='unique',
                      type=VoucherType.SHIPPING,
                      discount_value_type=DiscountValueType.FIXED,
                      discount_value=10,
                      limit=Money(20, 'USD'))
    price = TaxedMoney(net=Money(10, 'USD'), gross=Money(10, 'USD'))
    order = Mock(get_subtotal=Mock(return_value=price),
                 shipping_price=price,
                 voucher=voucher)
    discount = get_voucher_discount_for_order(order)
    assert discount == Money(0, 'USD')
Example #22
0
def test_get_discount_for_cart_value_voucher(settings, total, discount_value,
                                             discount_type, limit,
                                             discount_amount):
    voucher = Voucher(code='unique',
                      type=VoucherType.VALUE,
                      discount_value_type=discount_type,
                      discount_value=discount_value,
                      limit=Money(limit, 'USD') if limit is not None else None)
    subtotal = TaxedMoney(net=Money(total, 'USD'), gross=Money(total, 'USD'))
    cart = Mock(get_subtotal=Mock(return_value=subtotal))
    discount = get_voucher_discount_for_cart(voucher, cart)
    assert discount == Money(discount_amount, 'USD')
Example #23
0
def test_get_discount_for_checkout_value_voucher_not_applicable():
    voucher = Voucher(
        code="unique",
        type=VoucherType.ENTIRE_ORDER,
        discount_value_type=DiscountValueType.FIXED,
        discount_value=10,
        min_amount_spent=Money(100, "USD"),
    )
    checkout = Mock(get_subtotal=Mock(return_value=Money(10, "USD")))
    with pytest.raises(NotApplicable) as e:
        get_voucher_discount_for_checkout(voucher, checkout)
    assert e.value.min_amount_spent == Money(100, "USD")
Example #24
0
def test_get_value_voucher_discount(total, min_amount_spent, discount_value,
                                    discount_value_type, expected_value):
    voucher = Voucher(code='unique',
                      type=VoucherType.VALUE,
                      discount_value_type=discount_value_type,
                      discount_value=discount_value,
                      min_amount_spent=get_min_amount_spent(min_amount_spent))
    voucher.save()
    total_price = TaxedMoney(net=Money(total, 'USD'),
                             gross=Money(total, 'USD'))
    discount = get_value_voucher_discount(voucher, total_price)
    assert discount == Money(expected_value, 'USD')
Example #25
0
def test_get_product_or_category_voucher_discount_all_products(
        prices, discount_value_type, discount_value, voucher_type, apply_to,
        expected_value):
    prices = [
        TaxedMoney(net=Money(price, 'USD'), gross=Money(price, 'USD'))
        for price in prices]
    voucher = Voucher(
        code='unique', type=voucher_type, apply_to=apply_to,
        discount_value_type=discount_value_type,
        discount_value=discount_value)

    discount = get_product_or_category_voucher_discount(voucher, prices)
    assert discount == Money(expected_value, 'USD')
def test_get_voucher_discount_all_products(
        prices, discount_value_type, discount_value, voucher_type,
        expected_value):
    prices = [
        TaxedMoney(net=Money(price, 'USD'), gross=Money(price, 'USD'))
        for price in prices]
    voucher = Voucher(
        code='unique', type=voucher_type,
        discount_value_type=discount_value_type,
        discount_value=discount_value, apply_once_per_order=True)
    voucher.save()
    discount = get_products_voucher_discount(voucher, prices)
    assert discount == Money(expected_value, 'USD')
Example #27
0
def test_get_voucher_discount_all_products(
    prices, discount_value_type, discount_value, voucher_type, expected_value
):
    prices = [Money(price, "USD") for price in prices]
    voucher = Voucher(
        code="unique",
        type=voucher_type,
        discount_value_type=discount_value_type,
        discount_value=discount_value,
    )
    voucher.save()
    discount = get_products_voucher_discount(voucher, prices)
    assert discount == Money(expected_value, "USD")
Example #28
0
def test_category_voucher_checkout_discount_not_applicable(
        settings, monkeypatch):
    monkeypatch.setattr(
        'saleor.discount.models.get_category_variants_and_prices',
        lambda cart, product: [])
    voucher = Voucher(code='unique',
                      type=VoucherType.CATEGORY,
                      discount_value_type=DiscountValueType.FIXED,
                      discount_value=10)
    checkout = Mock(cart=Mock())
    with pytest.raises(NotApplicable) as e:
        voucher.get_discount_for_checkout(checkout)
    assert str(e.value) == 'This offer is only valid for selected items.'
Example #29
0
def test_get_value_voucher_discount(
    total, min_amount_spent, discount_value, discount_value_type, expected_value
):
    voucher = Voucher(
        code="unique",
        type=VoucherType.ENTIRE_ORDER,
        discount_value_type=discount_value_type,
        discount_value=discount_value,
        min_amount_spent=get_min_amount_spent(min_amount_spent),
    )
    voucher.save()
    total_price = Money(total, "USD")
    discount = get_value_voucher_discount(voucher, total_price)
    assert discount == Money(expected_value, "USD")
def test_get_discount_for_checkout_product_voucher_not_applicable(monkeypatch):
    monkeypatch.setattr(
        'saleor.checkout.utils.get_prices_of_discounted_products',
        lambda checkout, product: [])
    voucher = Voucher(code='unique',
                      type=VoucherType.PRODUCT,
                      discount_value_type=DiscountValueType.FIXED,
                      discount_value=10)
    voucher.save()
    checkout = Mock()

    with pytest.raises(NotApplicable) as e:
        get_voucher_discount_for_checkout(voucher, checkout)
    assert str(e.value) == 'This offer is only valid for selected items.'