Skip to content

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

Add test for query gift cards from checkout

fowczarek1d3553df1028
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index c3cb92dfa4..22538b2157 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -1504,3 +1504,59 @@ def test_checkout_totals_use_discounts(api_client, checkout_with_item, sale):
data["lines"][0]["totalPrice"]["gross"]["amount"]
== line.get_total(discounts=discounts).gross.amount
)
+
+
+QUERY_GET_CHECKOUT_GIFT_CARD_CODES = """
+query getCheckout($token: UUID!) {
+ checkout(token: $token) {
+ token
+ giftCards {
+ displayCode
+ currentBalance {
+ amount
+ }
+ }
+ }
+}
+"""
+
+
+def test_checkout_get_gift_card_code(user_api_client, checkout_with_gift_card):
+ gift_card = checkout_with_gift_card.gift_cards.first()
+ variables = {"token": str(checkout_with_gift_card.token)}
+ response = user_api_client.post_graphql(
+ QUERY_GET_CHECKOUT_GIFT_CARD_CODES, variables
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["checkout"]["giftCards"][0]
+ assert data["displayCode"] == gift_card.display_code
+ assert data["currentBalance"]["amount"] == gift_card.current_balance.amount
+
+
+def test_checkout_get_gift_card_codes(
+ user_api_client, checkout_with_gift_card, gift_card_created_by_staff
+):
+ checkout_with_gift_card.gift_cards.add(gift_card_created_by_staff)
+ checkout_with_gift_card.save()
+ gift_card_0 = checkout_with_gift_card.gift_cards.first()
+ gift_card_1 = checkout_with_gift_card.gift_cards.last()
+ variables = {"token": str(checkout_with_gift_card.token)}
+ response = user_api_client.post_graphql(
+ QUERY_GET_CHECKOUT_GIFT_CARD_CODES, variables
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["checkout"]["giftCards"]
+ assert data[0]["displayCode"] == gift_card_0.display_code
+ assert data[0]["currentBalance"]["amount"] == gift_card_0.current_balance.amount
+ assert data[1]["displayCode"] == gift_card_1.display_code
+ assert data[1]["currentBalance"]["amount"] == gift_card_1.current_balance.amount
+
+
+def test_checkout_get_gift_card_code_without_gift_card(user_api_client, checkout):
+ variables = {"token": str(checkout.token)}
+ response = user_api_client.post_graphql(
+ QUERY_GET_CHECKOUT_GIFT_CARD_CODES, variables
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["checkout"]["giftCards"]
+ assert not data
diff --git a/tests/conftest.py b/tests/conftest.py
index e9cdbedeb0..8729585e03 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -103,6 +103,13 @@ def checkout_with_voucher(checkout, product, voucher):
return checkout
+def checkout_with_gift_card(checkout_with_item, gift_card):
+ checkout_with_item.gift_cards.add(gift_card)
+ checkout_with_item.save()
+ return checkout_with_item
+
+
@pytest.fixture
def address(db): # pylint: disable=W0613
return Address.objects.create(
@@ -349,7 +356,7 @@ def permission_manage_discounts():
@pytest.fixture
def permission_manage_gift_card():
- return Permission.objects.get(codename='manage_gift_card')
+ return Permission.objects.get(codename="manage_gift_card")
@pytest.fixture
@@ -619,15 +626,19 @@ def order_line(order, variant, vatlayer):
@pytest.fixture
def gift_card(customer_user, staff_user):
return GiftCard.objects.create(
- code='mirumee_giftcard', buyer=customer_user, initial_balance=10,
- current_balance=10, creator=staff_user)
+ code="mirumee_giftcard",
+ buyer=customer_user,
+ initial_balance=10,
+ current_balance=10,
+ creator=staff_user,
+ )
@pytest.fixture
def gift_card_created_by_staff(staff_user):
return GiftCard.objects.create(
- code='mirumee_staff', creator=staff_user, initial_balance=100,
- current_balance=100)
+ code="mirumee_staff", creator=staff_user, initial_balance=5, current_balance=5
+ )
@pytest.fixture()