Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add used by email gift card valdiation
IKarbowiak7dd122265f68
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex 97cc2eb08d..12ea310f7d 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -487,7 +487,10 @@ def add_promo_code_to_checkout()elif promo_code_is_gift_card(promo_code):add_gift_card_code_to_checkout(- checkout_info.checkout, promo_code, checkout_info.channel.currency_code+ checkout_info.checkout,+ checkout_info.get_customer_email(),+ promo_code,+ checkout_info.channel.currency_code,)else:raise InvalidPromoCode()diff --git a/saleor/giftcard/tests/test_utils.py b/saleor/giftcard/tests/test_utils.pyindex 2022355acb..829299dffb 100644--- a/saleor/giftcard/tests/test_utils.py+++ b/saleor/giftcard/tests/test_utils.py@@ -11,7 +11,9 @@ def test_add_gift_card_code_to_checkout(checkout, gift_card):assert checkout.gift_cards.count() == 0# when- add_gift_card_code_to_checkout(checkout, gift_card.code, gift_card.currency)+ add_gift_card_code_to_checkout(+ checkout, "[email protected]", gift_card.code, gift_card.currency+ )# thenassert checkout.gift_cards.count() == 1@@ -27,7 +29,9 @@ def test_add_gift_card_code_to_checkout_inactive_card(checkout, gift_card):# when# thenwith pytest.raises(InvalidPromoCode):- add_gift_card_code_to_checkout(checkout, gift_card.code, gift_card.currency)+ add_gift_card_code_to_checkout(+ checkout, "[email protected]", gift_card.code, gift_card.currency+ )def test_add_gift_card_code_to_checkout_expired_card(checkout, gift_card):@@ -40,7 +44,9 @@ def test_add_gift_card_code_to_checkout_expired_card(checkout, gift_card):# when# thenwith pytest.raises(InvalidPromoCode):- add_gift_card_code_to_checkout(checkout, gift_card.code, gift_card.currency)+ add_gift_card_code_to_checkout(+ checkout, "[email protected]", gift_card.code, gift_card.currency+ )def test_add_gift_card_code_to_checkout_invalid_currency(checkout, gift_card):@@ -53,7 +59,43 @@ def test_add_gift_card_code_to_checkout_invalid_currency(checkout, gift_card):# when# thenwith pytest.raises(InvalidPromoCode):- add_gift_card_code_to_checkout(checkout, gift_card.code, currency)+ add_gift_card_code_to_checkout(+ checkout, "[email protected]", gift_card.code, currency+ )+++def test_add_gift_card_code_to_checkout_used_gift_card(checkout, gift_card_used):+ # given+ assert gift_card_used.used_by_email+ assert checkout.gift_cards.count() == 0++ # when+ add_gift_card_code_to_checkout(+ checkout,+ gift_card_used.used_by_email,+ gift_card_used.code,+ gift_card_used.currency,+ )++ # then+ assert checkout.gift_cards.count() == 1+++def test_add_gift_card_code_to_checkout_used_gift_card_invalid_user(+ checkout, gift_card_used+):+ # given+ email = "[email protected]"+ assert gift_card_used.used_by_email+ assert gift_card_used.used_by_email != email+ assert checkout.gift_cards.count() == 0++ # when+ # then+ with pytest.raises(InvalidPromoCode):+ add_gift_card_code_to_checkout(+ checkout, email, gift_card_used.code, gift_card_used.currency+ )def test_remove_gift_card_code_from_checkout(checkout, gift_card):diff --git a/saleor/giftcard/utils.py b/saleor/giftcard/utils.pyindex 3fa645a807..40b70b5db6 100644--- a/saleor/giftcard/utils.py+++ b/saleor/giftcard/utils.py@@ -5,7 +5,9 @@ from ..core.utils.promo_code import InvalidPromoCodefrom .models import GiftCard-def add_gift_card_code_to_checkout(checkout: Checkout, promo_code: str, currency: str):+def add_gift_card_code_to_checkout(+ checkout: Checkout, email: str, promo_code: str, currency: str+):"""Add gift card data to checkout by code.Raise InvalidPromoCode if gift card cannot be applied.@@ -19,6 +21,12 @@ def add_gift_card_code_to_checkout(checkout: Checkout, promo_code: str, currency)except GiftCard.DoesNotExist:raise InvalidPromoCode()++ used_by_email = gift_card.used_by_email+ # gift card can be used only by one user+ if used_by_email and used_by_email != email:+ raise InvalidPromoCode()+checkout.gift_cards.add(gift_card)diff --git a/saleor/graphql/checkout/tests/deprecated/test_checkout_promo_codes.py b/saleor/graphql/checkout/tests/deprecated/test_checkout_promo_codes.pyindex 23fecc407f..e92dec0d0d 100644--- a/saleor/graphql/checkout/tests/deprecated/test_checkout_promo_codes.py+++ b/saleor/graphql/checkout/tests/deprecated/test_checkout_promo_codes.py@@ -1,5 +1,3 @@-from datetime import date, timedelta-import graphenefrom .....checkout.error_codes import CheckoutErrorCode@@ -36,10 +34,8 @@ MUTATION_CHECKOUT_ADD_PROMO_CODE = """"""-def _mutate_checkout_add_promo_code(client, variables, permissions=[]):- response = client.post_graphql(- MUTATION_CHECKOUT_ADD_PROMO_CODE, variables, permissions=permissions- )+def _mutate_checkout_add_promo_code(client, variables):+ response = client.post_graphql(MUTATION_CHECKOUT_ADD_PROMO_CODE, variables)content = get_graphql_content(response)return content["data"]["checkoutAddPromoCode"]@@ -81,66 +77,6 @@ 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(diff --git a/saleor/graphql/checkout/tests/test_checkout_promo_codes.py b/saleor/graphql/checkout/tests/test_checkout_promo_codes.pyindex ef0901a96e..1bae1b428c 100644--- a/saleor/graphql/checkout/tests/test_checkout_promo_codes.py+++ b/saleor/graphql/checkout/tests/test_checkout_promo_codes.py@@ -557,6 +557,86 @@ def test_checkout_add_many_gift_card_code(assert gift_card_id in {gift_card["id"] for gift_card in gift_card_data}+def test_checkout_add_inactive_gift_card_code(+ staff_api_client, checkout_with_item, gift_card+):+ # given+ gift_card.is_active = False+ gift_card.save(update_fields=["is_active"])++ variables = {"token": checkout_with_item.token, "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(+ staff_api_client, checkout_with_item, gift_card+):+ # given+ gift_card.expiry_date = date.today() - timedelta(days=10)+ gift_card.save(update_fields=["expiry_date"])++ variables = {"token": checkout_with_item.token, "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_used_gift_card_code(+ staff_api_client, checkout_with_item, gift_card_used, customer_user+):+ # given+ checkout_with_item.email = gift_card_used.used_by_email+ checkout_with_item.save(update_fields=["email"])++ gift_card_id = graphene.Node.to_global_id("GiftCard", gift_card_used.pk)++ variables = {"token": checkout_with_item.token, "promoCode": gift_card_used.code}++ # when+ data = _mutate_checkout_add_promo_code(staff_api_client, variables)++ # then+ assert not data["errors"]+ assert data["checkout"]["token"] == str(checkout_with_item.token)+ assert data["checkout"]["giftCards"][0]["id"] == gift_card_id+ assert (+ data["checkout"]["giftCards"][0]["displayCode"] == gift_card_used.display_code+ )+++def test_checkout_add_used_gift_card_code_invalid_user(+ staff_api_client, checkout_with_item, gift_card_used, staff_user+):+ # given+ checkout_with_item.user = staff_user+ assert gift_card_used.used_by_email != checkout_with_item.user.email++ variables = {"token": checkout_with_item.token, "promoCode": gift_card_used.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_get_total_with_gift_card(api_client, checkout_with_item, gift_card):manager = get_plugins_manager()lines = fetch_checkout_lines(checkout_with_item)@@ -609,8 +689,11 @@ def test_checkout_get_total_with_many_gift_card(def test_checkout_get_total_with_more_money_on_gift_card(- api_client, checkout_with_item, gift_card_used+ api_client, checkout_with_item, gift_card_used, customer_user):+ checkout_with_item.email = gift_card_used.used_by_email+ checkout_with_item.save(update_fields=["email"])+variables = {"token": checkout_with_item.token, "promoCode": gift_card_used.code}data = _mutate_checkout_add_promo_code(api_client, variables)