Skip to content

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

Add test for checkout get total with gift card

fowczarek766885914480
diff --git a/tests/api/test_checkout_promo_codes.py b/tests/api/test_checkout_promo_codes.py
index 337b685f1b..1cc2a9cddd 100644
--- a/tests/api/test_checkout_promo_codes.py
+++ b/tests/api/test_checkout_promo_codes.py
@@ -270,6 +270,11 @@ MUTATION_CHECKOUT_ADD_PROMO_CODE = """
giftCards {
code
}
+ totalPrice {
+ gross {
+ amount
+ }
+ }
}
}
}
@@ -332,6 +337,55 @@ def test_checkout_add_many_gift_card_code(
assert data["checkout"]["giftCards"][-1]["code"] == gift_card_created_by_staff.code
+def test_checkout_get_total_with_gift_card(api_client, checkout_with_item, gift_card):
+ total_with_gift_card = (
+ checkout_with_item.get_total().gross.amount - gift_card.current_balance
+ )
+
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout_with_item.pk)
+ variables = {"checkoutId": checkout_id, "promoCode": gift_card.code}
+ data = _mutate_checkout_add_promo_code(api_client, variables)
+
+ assert not data["errors"]
+ assert data["checkout"]["id"] == checkout_id
+ assert not data["checkout"]["giftCards"] == []
+ assert data["checkout"]["totalPrice"]["gross"]["amount"] == total_with_gift_card
+
+
+def test_checkout_get_total_with_many_gift_card(
+ api_client, checkout_with_gift_card, gift_card_created_by_staff
+):
+ total_with_gift_card = (
+ checkout_with_gift_card.get_total().gross.amount
+ - gift_card_created_by_staff.current_balance
+ )
+
+ assert checkout_with_gift_card.gift_cards.count() > 0
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout_with_gift_card.pk)
+ variables = {
+ "checkoutId": checkout_id,
+ "promoCode": gift_card_created_by_staff.code,
+ }
+ data = _mutate_checkout_add_promo_code(api_client, variables)
+
+ assert not data["errors"]
+ assert data["checkout"]["id"] == checkout_id
+ assert data["checkout"]["totalPrice"]["gross"]["amount"] == total_with_gift_card
+
+
+def test_checkout_get_total_with_more_money_on_gift_card(
+ api_client, checkout_with_item, gift_card_used
+):
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout_with_item.pk)
+ variables = {"checkoutId": checkout_id, "promoCode": gift_card_used.code}
+ data = _mutate_checkout_add_promo_code(api_client, variables)
+
+ assert not data["errors"]
+ assert data["checkout"]["id"] == checkout_id
+ assert not data["checkout"]["giftCards"] == []
+ assert data["checkout"]["totalPrice"]["gross"]["amount"] == 0
+
+
def test_checkout_add_same_gift_card_code(api_client, checkout_with_gift_card):
gift_card = checkout_with_gift_card.gift_cards.first()
gift_card_count = checkout_with_gift_card.gift_cards.count()
diff --git a/tests/conftest.py b/tests/conftest.py
index 8729585e03..cd38ce70d5 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -634,6 +634,16 @@ def gift_card(customer_user, staff_user):
)
+def gift_card_used(staff_user):
+ return GiftCard.objects.create(
+ code="used_gift_card",
+ initial_balance=150,
+ current_balance=100,
+ creator=staff_user,
+ )
+
+
@pytest.fixture
def gift_card_created_by_staff(staff_user):
return GiftCard.objects.create(