Skip to content

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

[Test] Should be able to pay for the checkout using gift card (#13998)

Anna Szczęch8edce265ff60
diff --git a/saleor/tests/e2e/checkout/test_should_pay_for_chckout_with_gift_card.py b/saleor/tests/e2e/checkout/test_should_pay_for_chckout_with_gift_card.py
new file mode 100644
index 0000000000..4a86bcb923
--- /dev/null
+++ b/saleor/tests/e2e/checkout/test_should_pay_for_chckout_with_gift_card.py
@@ -0,0 +1,119 @@
+import pytest
+
+from ..gift_cards.utils import create_gift_card
+from ..product.utils.preparing_product import prepare_product
+from ..shop.utils.preparing_shop import prepare_shop
+from ..utils import assign_permissions
+from .utils import (
+ checkout_add_promo_code,
+ checkout_complete,
+ checkout_create,
+ checkout_delivery_method_update,
+ raw_checkout_dummy_payment_create,
+)
+
+
+def test_pay_for_checkout_with_gift_card_core_1101(
+ e2e_logged_api_client,
+ e2e_staff_api_client,
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_shipping,
+ permission_manage_product_types_and_attributes,
+ permission_manage_gift_card,
+):
+ # Before
+ permissions = [
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_shipping,
+ permission_manage_product_types_and_attributes,
+ permission_manage_gift_card,
+ ]
+ assign_permissions(e2e_staff_api_client, permissions)
+
+ (
+ warehouse_id,
+ channel_id,
+ channel_slug,
+ shipping_method_id,
+ ) = prepare_shop(e2e_staff_api_client)
+
+ (
+ _product_id,
+ product_variant_id,
+ product_variant_price,
+ ) = prepare_product(
+ e2e_staff_api_client,
+ warehouse_id,
+ channel_id,
+ variant_price=24.99,
+ )
+ gift_card = create_gift_card(e2e_staff_api_client, 100, "USD", active=True)
+ gift_card_code = gift_card["code"]
+ gift_card_id = gift_card["id"]
+
+ # Step 1 - Create checkout for product
+ lines = [
+ {
+ "variantId": product_variant_id,
+ "quantity": 3,
+ },
+ ]
+ checkout_data = checkout_create(
+ e2e_logged_api_client,
+ lines,
+ channel_slug,
+ set_default_billing_address=True,
+ set_default_shipping_address=True,
+ )
+ checkout_id = checkout_data["id"]
+ shipping_method_id = checkout_data["shippingMethods"][0]["id"]
+ assert checkout_data["isShippingRequired"] is True
+ assert checkout_data["totalPrice"]["gross"]["amount"] == float(
+ product_variant_price * 3
+ )
+
+ # Step 2 - Set DeliveryMethod for checkout.
+ checkout_data = checkout_delivery_method_update(
+ e2e_logged_api_client,
+ checkout_id,
+ shipping_method_id,
+ )
+ assert checkout_data["deliveryMethod"]["id"] == shipping_method_id
+ total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]
+ shipping_price = checkout_data["deliveryMethod"]["price"]["amount"]
+ assert shipping_price == 10
+ assert total_gross_amount == float(product_variant_price * 3) + shipping_price
+
+ # Step 3 Add gift card to checkout
+ checkout_data = checkout_add_promo_code(
+ e2e_logged_api_client,
+ checkout_id,
+ gift_card_code,
+ )
+ total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]
+ assert total_gross_amount == 0
+ assert checkout_data["giftCards"][0]["id"] == gift_card_id
+ assert checkout_data["giftCards"][0]["last4CodeChars"] == gift_card_code[-4:]
+
+ # Step 4 - Create payment for checkout.
+ create_payment = raw_checkout_dummy_payment_create(
+ e2e_logged_api_client,
+ checkout_id,
+ total_gross_amount,
+ )
+ assert create_payment["errors"] == []
+ assert create_payment["checkout"]["id"] == checkout_id
+
+ # Step 5 - Complete checkout.
+ order_data = checkout_complete(
+ e2e_logged_api_client,
+ checkout_id,
+ )
+ assert order_data["status"] == "UNFULFILLED"
+ assert order_data["total"]["gross"]["amount"] == 0
+ assert order_data["giftCards"][0]["id"] == gift_card_id
+ assert order_data["giftCards"][0]["last4CodeChars"] == gift_card_code[-4:]
diff --git a/saleor/tests/e2e/checkout/utils/checkout_add_promo_code.py b/saleor/tests/e2e/checkout/utils/checkout_add_promo_code.py
index c8890f05c6..21398beb96 100644
--- a/saleor/tests/e2e/checkout/utils/checkout_add_promo_code.py
+++ b/saleor/tests/e2e/checkout/utils/checkout_add_promo_code.py
@@ -32,6 +32,10 @@ mutation AddCheckoutPromoCode($checkoutId: ID!, $promoCode: String!) {
amount
}
}
+ giftCards {
+ id
+ last4CodeChars
+ }
}
errors {
code
diff --git a/saleor/tests/e2e/checkout/utils/checkout_complete.py b/saleor/tests/e2e/checkout/utils/checkout_complete.py
index 4882a56730..019e524cb3 100644
--- a/saleor/tests/e2e/checkout/utils/checkout_complete.py
+++ b/saleor/tests/e2e/checkout/utils/checkout_complete.py
@@ -74,6 +74,10 @@ mutation CheckoutComplete($checkoutId: ID!) {
voucher {
code
}
+ giftCards {
+ id
+ last4CodeChars
+ }
}
}
}
diff --git a/saleor/tests/e2e/gift_cards/__init__.py b/saleor/tests/e2e/gift_cards/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/tests/e2e/gift_cards/utils/__init__.py b/saleor/tests/e2e/gift_cards/utils/__init__.py
new file mode 100644
index 0000000000..e2847dc704
--- /dev/null
+++ b/saleor/tests/e2e/gift_cards/utils/__init__.py
@@ -0,0 +1,5 @@
+from .gift_card_create import create_gift_card
+
+__all__ = [
+ "create_gift_card",
+]
diff --git a/saleor/tests/e2e/gift_cards/utils/gift_card_create.py b/saleor/tests/e2e/gift_cards/utils/gift_card_create.py
new file mode 100644
index 0000000000..7b662ba935
--- /dev/null
+++ b/saleor/tests/e2e/gift_cards/utils/gift_card_create.py
@@ -0,0 +1,51 @@
+from ...utils import get_graphql_content
+
+GIFT_CARD_CREATE_MUTATION = """
+mutation GiftCardCreate($input: GiftCardCreateInput!) {
+ giftCardCreate(input: $input) {
+ giftCard {
+ id
+ code
+ }
+ errors {
+ code
+ field
+ message
+ }
+ }
+}
+"""
+
+
+def create_gift_card(
+ staff_api_client,
+ amount,
+ currency="USD",
+ active=True,
+ email=None,
+ channel=None,
+):
+ variables = {
+ "input": {
+ "note": "note",
+ "addTags": ["tag_test"],
+ "userEmail": email,
+ "channel": channel,
+ "balance": {"amount": amount, "currency": currency},
+ "isActive": active,
+ }
+ }
+
+ response = staff_api_client.post_graphql(
+ GIFT_CARD_CREATE_MUTATION,
+ variables,
+ )
+ content = get_graphql_content(response)
+
+ assert content["data"]["giftCardCreate"]["errors"] == []
+
+ data = content["data"]["giftCardCreate"]["giftCard"]
+ assert data["id"] is not None
+ assert data["code"] is not None
+
+ return data