Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add test for create order with gift cards

fowczarekaae411439943
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index 94d9bbcb8a..24695aec06 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -7,6 +7,7 @@ from django.core.exceptions import ValidationError
from saleor.checkout.models import Checkout
from saleor.checkout.utils import clean_checkout, is_fully_paid
+from saleor.core.utils.taxes import ZERO_MONEY
from saleor.graphql.core.utils import str_to_enum
from saleor.order.models import Order
from tests.api.utils import get_graphql_content
@@ -873,9 +874,14 @@ MUTATION_CHECKOUT_COMPLETE = """
@pytest.mark.integration
def test_checkout_complete(
- user_api_client, checkout_with_item, payment_dummy, address, shipping_method
+ user_api_client,
+ checkout_with_gift_card,
+ gift_card,
+ payment_dummy,
+ address,
+ shipping_method,
):
- checkout = checkout_with_item
+ checkout = checkout_with_gift_card
checkout.shipping_address = address
checkout.shipping_method = shipping_method
checkout.billing_address = address
@@ -918,6 +924,8 @@ def test_checkout_complete(
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 that the checkout instance has been deleted after checkout
with pytest.raises(Checkout.DoesNotExist):
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index da0ceed3e5..1898300e0b 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -660,6 +660,108 @@ def test_create_order_doesnt_duplicate_order(
assert mocked_delete.call_count == 1
[email protected]("is_anonymous_user", (True, False))
+def test_create_order_with_gift_card(
+ checkout_with_gift_card, customer_user, shipping_method, is_anonymous_user
+):
+ checkout_user = None if is_anonymous_user else customer_user
+ checkout = checkout_with_gift_card
+ checkout.user = checkout_user
+ checkout.billing_address = customer_user.default_billing_address
+ checkout.shipping_address = customer_user.default_billing_address
+ checkout.shipping_method = shipping_method
+ checkout.save()
+
+ total_gross_without_gift_cards = checkout.get_total_without_gift_cards().gross
+ gift_cards_balance = checkout.get_total_gift_cards_balance()
+
+ order = create_order(
+ checkout=checkout,
+ tracking_code="tracking_code",
+ discounts=None,
+ taxes=None,
+ user=customer_user if not is_anonymous_user else AnonymousUser(),
+ )
+
+ assert order.gift_cards.count() > 0
+ assert order.gift_cards.first().current_balance.amount == 0
+ assert order.total.gross == (total_gross_without_gift_cards - gift_cards_balance)
+
+
+def test_create_order_with_gift_card_partial_use(
+ checkout_with_item, gift_card_used, 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 = checkout.get_total()
+ gift_card_balance_before_order = gift_card_used.current_balance
+
+ checkout.gift_cards.add(gift_card_used)
+ checkout.save()
+
+ order = create_order(
+ checkout=checkout,
+ tracking_code="tracking_code",
+ discounts=None,
+ taxes=None,
+ user=customer_user,
+ )
+
+ gift_card_used.refresh_from_db()
+ assert order.gift_cards.count() > 0
+ assert order.total == ZERO_TAXED_MONEY
+ assert (
+ gift_card_balance_before_order
+ == (price_without_gift_card + gift_card_used.current_balance).gross.amount
+ )
+
+
+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 = checkout.get_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,
+ tracking_code="tracking_code",
+ discounts=None,
+ taxes=None,
+ user=customer_user,
+ )
+
+ gift_card_created_by_staff.refresh_from_db()
+ gift_card.refresh_from_db()
+ assert order.gift_cards.count() > 0
+ assert gift_card_created_by_staff.current_balance == ZERO_MONEY
+ assert gift_card.current_balance == ZERO_MONEY
+ assert price_without_gift_card.gross.amount == (
+ gift_cards_balance_befor_order + order.total.gross.amount
+ )
+
+
def test_note_in_created_order(request_checkout_with_item, address, customer_user):
request_checkout_with_item.shipping_address = address
request_checkout_with_item.note = "test_note"