Skip to content

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

Add tests for remove gift card from checkout

fowczarek1cef377a2713
diff --git a/tests/api/test_checkout_promo_codes.py b/tests/api/test_checkout_promo_codes.py
index b8bc91905b..f8db5dda99 100644
--- a/tests/api/test_checkout_promo_codes.py
+++ b/tests/api/test_checkout_promo_codes.py
@@ -378,20 +378,79 @@ MUTATION_CHECKOUT_REMOVE_PROMO_CODE = """
"""
-def test_checkout_remove_promo_code_voucher(api_client, checkout_with_voucher):
+def _mutate_checkout_remove_promo_code(client, variables):
+ response = client.post_graphql(MUTATION_CHECKOUT_REMOVE_PROMO_CODE, variables)
+ content = get_graphql_content(response)
+ return content["data"]["checkoutRemovePromoCode"]
+
+
+def test_checkout_remove_voucher_code(api_client, checkout_with_voucher):
assert checkout_with_voucher.voucher_code is not None
+
checkout_id = graphene.Node.to_global_id("Checkout", checkout_with_voucher.pk)
variables = {
"checkoutId": checkout_id,
"promoCode": checkout_with_voucher.voucher_code,
}
- response = api_client.post_graphql(MUTATION_CHECKOUT_REMOVE_PROMO_CODE, variables)
- content = get_graphql_content(response)
- data = content["data"]["checkoutRemovePromoCode"]
+ data = _mutate_checkout_remove_promo_code(api_client, variables)
checkout_with_voucher.refresh_from_db()
assert not data["errors"]
assert data["checkout"]["id"] == checkout_id
assert data["checkout"]["voucherCode"] is None
assert checkout_with_voucher.voucher_code is None
+
+
+def test_checkout_remove_gift_card_code(api_client, checkout_with_gift_card):
+ assert checkout_with_gift_card.gift_cards.count() == 1
+
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout_with_gift_card.pk)
+ variables = {
+ "checkoutId": checkout_id,
+ "promoCode": checkout_with_gift_card.gift_cards.first().code,
+ }
+
+ data = _mutate_checkout_remove_promo_code(api_client, variables)
+
+ assert data["checkout"]["id"] == checkout_id
+ assert data["checkout"]["giftCards"] == []
+ assert not checkout_with_gift_card.gift_cards.all().exists()
+
+
+def test_checkout_remove_one_of_gift_cards(
+ 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_first = checkout_with_gift_card.gift_cards.first()
+ gift_card_last = checkout_with_gift_card.gift_cards.last()
+
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout_with_gift_card.pk)
+ variables = {"checkoutId": checkout_id, "promoCode": gift_card_first.code}
+
+ data = _mutate_checkout_remove_promo_code(api_client, variables)
+
+ checkout_gift_cards = checkout_with_gift_card.gift_cards
+ assert data["checkout"]["id"] == checkout_id
+ assert checkout_gift_cards.filter(code=gift_card_last.code).exists()
+ assert not checkout_gift_cards.filter(code=gift_card_first.code).exists()
+
+
+def test_checkout_remove_promo_code_invalid_promo_code(api_client, checkout_with_item):
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout_with_item.pk)
+ variables = {"checkoutId": checkout_id, "promoCode": "unexisting_code"}
+
+ data = _mutate_checkout_remove_promo_code(api_client, variables)
+
+ assert not data["errors"]
+ assert data["checkout"]["id"] == checkout_id
+
+
+def test_checkout_remove_promo_code_invalid_checkout(api_client, voucher):
+ variables = {"checkoutId": "unexisting_checkout", "promoCode": voucher.code}
+
+ data = _mutate_checkout_remove_promo_code(api_client, variables)
+
+ assert data["errors"]
+ assert data["errors"][0]["field"] == "checkoutId"