Skip to content

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

Add tests for adding checkout gift card code

IKarbowiak442622409657
diff --git a/saleor/giftcard/tests/__init__.py b/saleor/giftcard/tests/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/giftcard/tests/test_utils.py b/saleor/giftcard/tests/test_utils.py
new file mode 100644
index 0000000000..46129c9b51
--- /dev/null
+++ b/saleor/giftcard/tests/test_utils.py
@@ -0,0 +1,68 @@
+from datetime import date, timedelta
+
+import pytest
+
+from ...core.utils.promo_code import InvalidPromoCode
+from ..utils import add_gift_card_code_to_checkout, remove_gift_card_code_from_checkout
+
+
+def test_add_gift_card_code_to_checkout(checkout, gift_card):
+ # given
+ assert checkout.gift_cards.count() == 0
+
+ # when
+ add_gift_card_code_to_checkout(checkout, gift_card.code)
+
+ # then
+ assert checkout.gift_cards.count() == 1
+
+
+def test_add_gift_card_code_to_checkout_inactive_card(checkout, gift_card):
+ # given
+ gift_card.is_active = False
+ gift_card.save(update_fields=["is_active"])
+
+ assert checkout.gift_cards.count() == 0
+
+ # when
+ # then
+ with pytest.raises(InvalidPromoCode):
+ add_gift_card_code_to_checkout(checkout, gift_card.code)
+
+
+def test_add_gift_card_code_to_checkout_expired_card(checkout, gift_card):
+ # given
+ gift_card.expiry_date = date.today() - timedelta(days=10)
+ gift_card.save(update_fields=["expiry_date"])
+
+ assert checkout.gift_cards.count() == 0
+
+ # when
+ # then
+ with pytest.raises(InvalidPromoCode):
+ add_gift_card_code_to_checkout(checkout, gift_card.code)
+
+
+def test_remove_gift_card_code_from_checkout(checkout, gift_card):
+ # given
+ checkout.gift_cards.add(gift_card)
+ assert checkout.gift_cards.count() == 1
+
+ # when
+ remove_gift_card_code_from_checkout(checkout, gift_card.code)
+
+ # then
+ assert checkout.gift_cards.count() == 0
+
+
+def test_remove_gift_card_code_from_checkout_no_checkout_gift_cards(
+ checkout, gift_card
+):
+ # given
+ assert checkout.gift_cards.count() == 0
+
+ # when
+ remove_gift_card_code_from_checkout(checkout, gift_card.code)
+
+ # then
+ assert checkout.gift_cards.count() == 0
diff --git a/saleor/graphql/checkout/tests/deprecated/test_checkout_promo_codes.py b/saleor/graphql/checkout/tests/deprecated/test_checkout_promo_codes.py
index fe317703bb..23fecc407f 100644
--- a/saleor/graphql/checkout/tests/deprecated/test_checkout_promo_codes.py
+++ b/saleor/graphql/checkout/tests/deprecated/test_checkout_promo_codes.py
@@ -1,3 +1,5 @@
+from datetime import date, timedelta
+
import graphene
from .....checkout.error_codes import CheckoutErrorCode
@@ -16,6 +18,9 @@ MUTATION_CHECKOUT_ADD_PROMO_CODE = """
id
token
voucherCode
+ giftCards {
+ code
+ }
giftCards {
id
displayCode
@@ -31,8 +36,10 @@ MUTATION_CHECKOUT_ADD_PROMO_CODE = """
"""
-def _mutate_checkout_add_promo_code(client, variables):
- response = client.post_graphql(MUTATION_CHECKOUT_ADD_PROMO_CODE, variables)
+def _mutate_checkout_add_promo_code(client, variables, permissions=[]):
+ response = client.post_graphql(
+ MUTATION_CHECKOUT_ADD_PROMO_CODE, variables, permissions=permissions
+ )
content = get_graphql_content(response)
return content["data"]["checkoutAddPromoCode"]
@@ -74,6 +81,66 @@ def test_checkout_add_voucher_code_both_token_and_id_given(
assert data["errors"][0]["code"] == CheckoutErrorCode.GRAPHQL_ERROR.name
+def test_checkout_add_gift_card_code_by_id(
+ staff_api_client, checkout_with_item, gift_card, permission_manage_gift_card
+):
+ # given
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout_with_item.pk)
+ variables = {"checkoutId": checkout_id, "promoCode": gift_card.code}
+
+ # when
+ data = _mutate_checkout_add_promo_code(
+ staff_api_client, variables, [permission_manage_gift_card]
+ )
+
+ # then
+ assert not data["errors"]
+ assert data["checkout"]["id"] == checkout_id
+ assert data["checkout"]["voucherCode"] is None
+ assert len(data["checkout"]["giftCards"]) == 1
+ assert data["checkout"]["giftCards"][0]["code"] == gift_card.code
+
+
+def test_checkout_add_inactive_gift_card_code_by_id(
+ staff_api_client, checkout_with_item, gift_card, permission_manage_gift_card
+):
+ # given
+ gift_card.is_active = False
+ gift_card.save(update_fields=["is_active"])
+
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout_with_item.pk)
+ variables = {"checkoutId": checkout_id, "promoCode": gift_card.code}
+
+ # when
+ data = _mutate_checkout_add_promo_code(staff_api_client, variables)
+
+ # then
+ assert not data["checkout"]
+ assert len(data["errors"]) == 1
+ assert data["errors"][0]["code"] == CheckoutErrorCode.INVALID.name
+ assert data["errors"][0]["field"] == "promoCode"
+
+
+def test_checkout_add_expired_gift_card_code_by_id(
+ staff_api_client, checkout_with_item, gift_card, permission_manage_gift_card
+):
+ # given
+ gift_card.expiry_date = date.today() - timedelta(days=10)
+ gift_card.save(update_fields=["expiry_date"])
+
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout_with_item.pk)
+ variables = {"checkoutId": checkout_id, "promoCode": gift_card.code}
+
+ # when
+ data = _mutate_checkout_add_promo_code(staff_api_client, variables)
+
+ # then
+ assert not data["checkout"]
+ assert len(data["errors"]) == 1
+ assert data["errors"][0]["code"] == CheckoutErrorCode.INVALID.name
+ assert data["errors"][0]["field"] == "promoCode"
+
+
MUTATION_CHECKOUT_REMOVE_PROMO_CODE = """
mutation($checkoutId: ID, $token: UUID, $promoCode: String!) {
checkoutRemovePromoCode(