Пример #1
0
def test_remove_voucher_from_checkout(checkout_with_voucher, voucher_translation_fr):
    checkout = checkout_with_voucher
    remove_voucher_from_checkout(checkout)

    assert not checkout.voucher_code
    assert not checkout.discount_name
    assert not checkout.translated_discount_name
    assert checkout.discount == zero_money()
Пример #2
0
def test_recalculate_checkout_discount_free_shipping_for_checkout_without_shipping(
        checkout_with_voucher_percentage, voucher_free_shipping):
    checkout = checkout_with_voucher_percentage

    recalculate_checkout_discount(checkout, list(checkout), None)

    assert not checkout.discount_name
    assert not checkout.voucher_code
    assert checkout.discount == zero_money()
Пример #3
0
def test_recalculate_checkout_discount_expired_voucher(checkout_with_voucher, voucher):
    checkout = checkout_with_voucher
    date_yesterday = timezone.now() - datetime.timedelta(days=1)
    voucher.end_date = date_yesterday
    voucher.save()

    recalculate_checkout_discount(checkout_with_voucher, None)

    assert not checkout.voucher_code
    assert not checkout.discount_name
    assert checkout.discount == zero_money()
Пример #4
0
def test_recalculate_checkout_discount_voucher_not_applicable(
        checkout_with_voucher, voucher):
    checkout = checkout_with_voucher
    voucher.min_spent = Money(100, "USD")
    voucher.save(update_fields=["min_spent_amount", "currency"])

    recalculate_checkout_discount(checkout_with_voucher, None)

    assert not checkout.voucher_code
    assert not checkout.discount_name
    assert checkout.discount == zero_money()
def test_sort_product_not_having_attribute_data(api_client, category,
                                                count_queries):
    """Test the case where a product has a given attribute assigned to their
    product type but no attribute data assigned, i.e. the product's PT was changed
    after the product creation.
    """
    expected_results = ["Z", "Y", "A"]
    product_create_kwargs = {
        "category": category,
        "price": zero_money(),
        "is_published": True,
    }

    # Create two product types, with one forced to be at the bottom (no such attribute)
    product_type = product_models.ProductType.objects.create(name="Apples",
                                                             slug="apples")
    other_product_type = product_models.ProductType.objects.create(
        name="Chocolates", slug="chocolates")

    # Assign an attribute to the product type
    attribute = product_models.Attribute.objects.create(name="Kind",
                                                        slug="kind")
    value = product_models.AttributeValue.objects.create(name="Value",
                                                         slug="value",
                                                         attribute=attribute)
    product_type.product_attributes.add(attribute)

    # Create a product with a value
    product_having_attr_value = product_models.Product.objects.create(
        name="Z", slug="z", product_type=product_type, **product_create_kwargs)
    associate_attribute_values_to_instance(product_having_attr_value,
                                           attribute, value)

    # Create a product having the same product type but no attribute data
    product_models.Product.objects.create(name="Y",
                                          slug="y",
                                          product_type=product_type,
                                          **product_create_kwargs)

    # Create a new product having a name that would be ordered first in ascending
    # as the default ordering is by name for non matching products
    product_models.Product.objects.create(name="A",
                                          slug="a",
                                          product_type=other_product_type,
                                          **product_create_kwargs)

    # Sort the products
    qs = product_models.Product.objects.sort_by_attribute(
        attribute_pk=attribute.pk)
    qs = qs.values_list("name", flat=True)

    # Compare the results
    sorted_results = list(qs)
    assert sorted_results == expected_results
Пример #6
0
def test_recalculate_checkout_discount_voucher_not_applicable(
        checkout_with_voucher, voucher):
    checkout = checkout_with_voucher
    voucher.min_amount_spent = 100
    voucher.save()

    recalculate_checkout_discount(checkout_with_voucher, None)

    assert not checkout.voucher_code
    assert not checkout.discount_name
    assert checkout.discount_amount == zero_money()
Пример #7
0
def test_create_order_with_many_gift_cards(
    checkout_with_item,
    gift_card_created_by_staff,
    gift_card,
    customer_user,
    shipping_method,
):
    checkout = checkout_with_item
    checkout.user = customer_user
    checkout.billing_address = customer_user.default_billing_address
    checkout.shipping_address = customer_user.default_billing_address
    checkout.shipping_method = shipping_method
    checkout.save()

    price_without_gift_card = calculations.checkout_total(
        checkout=checkout, lines=list(checkout)
    )
    gift_cards_balance_before_order = (
        gift_card_created_by_staff.current_balance.amount
        + gift_card.current_balance.amount
    )

    checkout.gift_cards.add(gift_card_created_by_staff)
    checkout.gift_cards.add(gift_card)
    checkout.save()

    order = create_order(
        checkout=checkout,
        order_data=prepare_order_data(
            checkout=checkout,
            lines=list(checkout),
            tracking_code="tracking_code",
            discounts=None,
        ),
        user=customer_user,
        redirect_url="https://www.example.com",
    )

    gift_card_created_by_staff.refresh_from_db()
    gift_card.refresh_from_db()
    zero_price = zero_money()
    assert order.gift_cards.count() > 0
    assert gift_card_created_by_staff.current_balance == zero_price
    assert gift_card.current_balance == zero_price
    assert price_without_gift_card.gross.amount == (
        gift_cards_balance_before_order + order.total.gross.amount
    )
Пример #8
0
def test_create_order_with_many_gift_cards(
    checkout_with_item,
    gift_card_created_by_staff,
    gift_card,
    customer_user,
    shipping_method,
):
    checkout = checkout_with_item
    checkout.user = customer_user
    checkout.billing_address = customer_user.default_billing_address
    checkout.shipping_address = customer_user.default_billing_address
    checkout.shipping_method = shipping_method
    checkout.save()

    checkout_total = checkout.get_total()
    price_without_gift_card = TaxedMoney(net=checkout_total,
                                         gross=checkout_total)
    gift_cards_balance_befor_order = (
        gift_card_created_by_staff.current_balance + gift_card.current_balance)

    checkout.gift_cards.add(gift_card_created_by_staff)
    checkout.gift_cards.add(gift_card)
    checkout.save()

    order = create_order(
        checkout=checkout,
        order_data=prepare_order_data(checkout=checkout,
                                      tracking_code="tracking_code",
                                      discounts=None),
        user=customer_user,
    )

    gift_card_created_by_staff.refresh_from_db()
    gift_card.refresh_from_db()
    zero_price = zero_money()
    assert order.gift_cards.count() > 0
    assert gift_card_created_by_staff.current_balance == zero_price
    assert gift_card.current_balance == zero_price
    assert price_without_gift_card.gross.amount == (
        gift_cards_balance_befor_order + order.total.gross.amount)
Пример #9
0
def test_view_order_voucher_remove(admin_client, draft_order, settings, voucher):
    increase_voucher_usage(voucher)
    draft_order.voucher = voucher
    discount = Money(voucher.discount_value, settings.DEFAULT_CURRENCY)
    draft_order.discount = discount
    draft_order.total -= discount
    draft_order.save()
    total_before = draft_order.total
    url = reverse("dashboard:order-voucher-remove", kwargs={"order_pk": draft_order.pk})
    data = {"csrfmiddlewaretoken": "hello"}

    response = admin_client.post(url, data)

    assert response.status_code == 302
    redirect_url = reverse(
        "dashboard:order-details", kwargs={"order_pk": draft_order.pk}
    )
    assert get_redirect_location(response) == redirect_url

    draft_order.refresh_from_db()
    assert draft_order.discount == zero_money()
    assert draft_order.total == total_before + discount
Пример #10
0
def products_structures(category):
    def attr_value(attribute, *values):
        return [attribute.values.get_or_create(name=v, slug=v)[0] for v in values]

    assert product_models.Product.objects.count() == 0

    in_multivals = product_models.AttributeInputType.MULTISELECT

    pt_apples, pt_oranges, pt_other = list(
        product_models.ProductType.objects.bulk_create(
            [
                product_models.ProductType(
                    name="Apples", slug="apples", has_variants=False
                ),
                product_models.ProductType(
                    name="Oranges", slug="oranges", has_variants=False
                ),
                product_models.ProductType(
                    name="Other attributes", slug="other", has_variants=False
                ),
            ]
        )
    )

    colors_attr, trademark_attr, dummy_attr = list(
        product_models.Attribute.objects.bulk_create(
            [
                product_models.Attribute(
                    name="Colors", slug="colors", input_type=in_multivals
                ),
                product_models.Attribute(name="Trademark", slug="trademark"),
                product_models.Attribute(name="Dummy", slug="dummy"),
            ]
        )
    )

    # Manually add every attribute to given product types
    # to force the preservation of ordering
    pt_apples.product_attributes.add(colors_attr)
    pt_apples.product_attributes.add(trademark_attr)

    pt_oranges.product_attributes.add(colors_attr)
    pt_oranges.product_attributes.add(trademark_attr)

    pt_other.product_attributes.add(dummy_attr)

    assert len(COLORS) == len(TRADEMARKS)

    apples = list(
        product_models.Product.objects.bulk_create(
            [
                product_models.Product(
                    name=f"{attrs[0]} Apple - {attrs[1]} ({i})",
                    slug=f"{attrs[0]}-apple-{attrs[1]}-({i})",
                    product_type=pt_apples,
                    category=category,
                    price=zero_money(),
                    is_published=True,
                )
                for i, attrs in enumerate(zip(COLORS, TRADEMARKS))
            ]
        )
    )

    oranges = list(
        product_models.Product.objects.bulk_create(
            [
                product_models.Product(
                    name=f"{attrs[0]} Orange - {attrs[1]} ({i})",
                    slug=f"{attrs[0]}-orange-{attrs[1]}-({i})",
                    product_type=pt_oranges,
                    category=category,
                    price=zero_money(),
                    is_published=True,
                )
                for i, attrs in enumerate(zip(COLORS, TRADEMARKS))
            ]
        )
    )

    dummy = product_models.Product.objects.create(
        name=f"Oopsie Dummy",
        slug="oopsie-dummy",
        product_type=pt_other,
        category=category,
        price=zero_money(),
        is_published=True,
    )
    product_models.Product.objects.create(
        name=f"Another Dummy but first in ASC and has no attribute value",
        slug="another-dummy",
        product_type=pt_other,
        category=category,
        price=zero_money(),
        is_published=True,
    )
    dummy_attr_value = attr_value(dummy_attr, DUMMIES[0])
    associate_attribute_values_to_instance(dummy, dummy_attr, *dummy_attr_value)

    for products in (apples, oranges):
        for product, attr_values in zip(products, COLORS):
            attr_values = attr_value(colors_attr, *attr_values)
            associate_attribute_values_to_instance(product, colors_attr, *attr_values)

        for product, attr_values in zip(products, TRADEMARKS):
            attr_values = attr_value(trademark_attr, attr_values)
            associate_attribute_values_to_instance(
                product, trademark_attr, *attr_values
            )

    return colors_attr, trademark_attr, dummy_attr
Пример #11
0
def test_checkout_complete(
    user_api_client,
    checkout_with_gift_card,
    gift_card,
    payment_dummy,
    address,
    shipping_method,
):
    assert not gift_card.last_used_on

    checkout = checkout_with_gift_card
    checkout.shipping_method = shipping_method
    checkout.address = address
    checkout.save()

    checkout_line = checkout.lines.first()
    checkout_line_quantity = checkout_line.quantity
    checkout_line_variant = checkout_line.variant

    gift_current_balance = checkout.get_total_gift_cards_balance()
    total = checkout.get_total()
    total = TaxedMoney(total, total)
    payment = payment_dummy
    payment.is_active = True
    payment.order = None
    payment.total = total.gross.amount
    payment.currency = total.gross.currency
    payment.checkout = checkout
    payment.save()
    assert not payment.transactions.exists()

    orders_count = Order.objects.count()
    checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
    variables = {"checkoutId": checkout_id}
    response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
    content = get_graphql_content(response)
    data = content["data"]["checkoutComplete"]
    assert not data["errors"]

    order_token = data["order"]["token"]
    assert Order.objects.count() == orders_count + 1
    order = Order.objects.first()
    assert order.token == order_token
    assert order.total.gross == total.gross - gift_current_balance

    order_line = order.lines.first()
    assert checkout_line_quantity == order_line.quantity
    assert checkout_line_variant == order_line.variant
    assert order.address == address
    assert order.shipping_method == checkout.shipping_method
    assert order.payments.exists()
    order_payment = order.payments.first()
    assert order_payment == payment
    assert payment.transactions.count() == 1

    gift_card.refresh_from_db()
    assert gift_card.current_balance == zero_money()
    assert gift_card.last_used_on

    assert not Checkout.objects.filter(
        pk=checkout.pk
    ).exists(), "Checkout should have been deleted"