Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add tests
Mateusz Grzybad2613b50502
diff --git a/saleor/payment/gateways/stripe/tests/test_webhooks.py b/saleor/payment/gateways/stripe/tests/test_webhooks.pyindex 7c5d9e8881..cddaee536e 100644--- a/saleor/payment/gateways/stripe/tests/test_webhooks.py+++ b/saleor/payment/gateways/stripe/tests/test_webhooks.py@@ -6,6 +6,7 @@ import pytestfrom stripe.stripe_object import StripeObjectfrom .....checkout.complete_checkout import complete_checkout+from .....order.actions import order_captured, order_refunded, order_voidedfrom .... import ChargeStatus, TransactionKindfrom ....utils import price_to_minor_unitfrom ..consts import (@@ -20,6 +21,8 @@ from ..consts import (WEBHOOK_SUCCESS_EVENT,)from ..webhooks import (+ _process_payment_with_checkout,+ _update_payment_with_new_transaction,handle_authorized_payment_intent,handle_failed_payment_intent,handle_processing_payment_intent,@@ -120,7 +123,6 @@ def test_handle_successful_payment_intent_with_future_usage(def test_handle_successful_payment_intent_for_order(wrapped_checkout_complete, payment_stripe_for_order, stripe_plugin, channel_USD):-payment = payment_stripe_for_orderplugin = stripe_plugin()payment_intent = StripeObject(id="ABC", last_response={})@@ -193,6 +195,77 @@ def test_handle_successful_payment_intent_for_order_with_pending_payment(assert wrapped_checkout_complete.called is False[email protected]("called", [True, False])+@patch(+ "saleor.payment.gateways.stripe.webhooks._process_payment_with_checkout",+ wraps=_process_payment_with_checkout,+)+def test_handle_successful_payment_intent_different_checkout_channel_slug(+ wrapped_process_payment_with_checkout,+ payment_stripe_for_checkout,+ checkout_with_items,+ stripe_plugin,+ channel_USD,+ channel_PLN,+ called,+):+ # given+ channel = channel_USD if called else channel_PLN+ payment = payment_stripe_for_checkout+ payment.to_confirm = True+ payment.save()+ payment.transactions.create(+ is_success=True,+ action_required=True,+ kind=TransactionKind.ACTION_TO_CONFIRM,+ amount=payment.total,+ currency=payment.currency,+ token="ABC",+ gateway_response={},+ )+ plugin = stripe_plugin()+ payment_intent = StripeObject(id="ABC", last_response={})+ payment_intent["amount_received"] = price_to_minor_unit(+ payment.total, payment.currency+ )+ payment_intent["setup_future_usage"] = None+ payment_intent["currency"] = payment.currency+ payment_intent["status"] = SUCCESS_STATUS++ # when+ handle_successful_payment_intent(payment_intent, plugin.config, channel.slug)++ # then+ assert wrapped_process_payment_with_checkout.called == called++[email protected]("called", [True, False])+@patch("saleor.payment.gateways.stripe.webhooks.order_captured", wraps=order_captured)+def test_handle_successful_payment_intent_different_order_channel_slug(+ wrapped_order_captured,+ payment_stripe_for_order,+ stripe_plugin,+ channel_USD,+ channel_PLN,+ called,+):+ # given+ channel = channel_USD if called else channel_PLN+ payment = payment_stripe_for_order+ plugin = stripe_plugin()+ payment_intent = StripeObject(id="token", last_response={})+ payment_intent["amount_received"] = payment.total+ payment_intent["currency"] = payment.currency+ payment_intent["capture_method"] = "automatic"+ payment_intent["setup_future_usage"] = None++ # when+ handle_successful_payment_intent(payment_intent, plugin.config, channel.slug)++ # then+ assert wrapped_order_captured.called == called++@patch("saleor.payment.gateways.stripe.webhooks.complete_checkout", wraps=complete_checkout)@@ -242,7 +315,6 @@ def test_handle_authorized_payment_intent_for_order(stripe_plugin,channel_USD,):-payment = payment_stripe_for_orderplugin = stripe_plugin()payment_intent = StripeObject(id="ABC", last_response={})@@ -264,7 +336,6 @@ def test_handle_authorized_payment_intent_for_processing_order_payment(stripe_plugin,channel_USD,):-payment = payment_stripe_for_orderpayment.charge_status = ChargeStatus.PENDINGplugin = stripe_plugin()@@ -277,6 +348,79 @@ def test_handle_authorized_payment_intent_for_processing_order_payment(assert wrapped_checkout_complete.called is False[email protected]("called", [True, False])+@patch(+ "saleor.payment.gateways.stripe.webhooks._update_payment_with_new_transaction",+ wraps=_update_payment_with_new_transaction,+)+def test_handle_authorized_payment_intent_different_order_channel_slug(+ wrapped_update_payment_with_new_transaction,+ payment_stripe_for_order,+ checkout_with_items,+ stripe_plugin,+ channel_USD,+ channel_PLN,+ called,+):+ # given+ channel = channel_USD if called else channel_PLN+ payment = payment_stripe_for_order+ payment.charge_status = ChargeStatus.PENDING+ payment.checkout = None+ payment.save()+ plugin = stripe_plugin()+ payment_intent = StripeObject(id="token", last_response={})+ payment_intent["amount"] = payment.total+ payment_intent["currency"] = payment.currency+ payment_intent["status"] = AUTHORIZED_STATUS++ # when+ handle_authorized_payment_intent(payment_intent, plugin.config, channel.slug)++ # then+ assert wrapped_update_payment_with_new_transaction.called == called++[email protected]("called", [True, False])+@patch(+ "saleor.payment.gateways.stripe.webhooks._process_payment_with_checkout",+ wraps=_process_payment_with_checkout,+)+def test_handle_authorized_payment_intent_different_checkout_channel_slug(+ wrapped_process_payment_with_checkout,+ payment_stripe_for_checkout,+ stripe_plugin,+ channel_USD,+ channel_PLN,+ called,+):+ # given+ channel = channel_USD if called else channel_PLN+ payment = payment_stripe_for_checkout+ payment.to_confirm = True+ payment.save()+ payment.transactions.create(+ is_success=True,+ action_required=True,+ kind=TransactionKind.ACTION_TO_CONFIRM,+ amount=payment.total,+ currency=payment.currency,+ token="ABC",+ gateway_response={},+ )+ plugin = stripe_plugin()+ payment_intent = StripeObject(id="ABC", last_response={})+ payment_intent["amount"] = price_to_minor_unit(payment.total, payment.currency)+ payment_intent["currency"] = payment.currency+ payment_intent["status"] = AUTHORIZED_STATUS++ # when+ handle_authorized_payment_intent(payment_intent, plugin.config, channel.slug)++ # then+ assert wrapped_process_payment_with_checkout.called == called++@patch("saleor.payment.gateways.stripe.webhooks.complete_checkout", wraps=complete_checkout)@@ -287,7 +431,6 @@ def test_handle_processing_payment_intent_for_order(stripe_plugin,channel_USD,):-payment = payment_stripe_for_orderplugin = stripe_plugin()payment_intent = StripeObject(id="ABC", last_response={})@@ -338,6 +481,77 @@ def test_handle_processing_payment_intent_for_checkout(assert transaction.token == payment_intent.id[email protected]("called", [True, False])+@patch(+ "saleor.payment.gateways.stripe.webhooks._process_payment_with_checkout",+ wraps=_process_payment_with_checkout,+)+def test_handle_processing_payment_intent_different_order_channel_slug(+ wrapped_process_payment_with_checkout,+ payment_stripe_for_order,+ checkout_with_items,+ stripe_plugin,+ channel_USD,+ channel_PLN,+ called,+):+ # given+ channel = channel_USD if called else channel_PLN+ payment = payment_stripe_for_order+ plugin = stripe_plugin()+ payment_intent = StripeObject(id="ABC", last_response={})+ payment_intent["amount"] = payment.total+ payment_intent["currency"] = payment.currency+ payment_intent["status"] = PROCESSING_STATUS++ # when+ handle_processing_payment_intent(payment_intent, plugin.config, channel.slug)++ # then+ assert not wrapped_process_payment_with_checkout.called++[email protected]("called", [True, False])+@patch(+ "saleor.payment.gateways.stripe.webhooks._process_payment_with_checkout",+ wraps=_process_payment_with_checkout,+)+def test_handle_processing_payment_intent_different_checkout_channel_slug(+ wrapped_process_payment_with_checkout,+ payment_stripe_for_checkout,+ checkout_with_items,+ stripe_plugin,+ channel_USD,+ channel_PLN,+ called,+):+ # given+ channel = channel_USD if called else channel_PLN+ payment = payment_stripe_for_checkout+ payment.to_confirm = True+ payment.save()+ payment.transactions.create(+ is_success=True,+ action_required=True,+ kind=TransactionKind.ACTION_TO_CONFIRM,+ amount=payment.total,+ currency=payment.currency,+ token="ABC",+ gateway_response={},+ )+ plugin = stripe_plugin()+ payment_intent = StripeObject(id="ABC", last_response={})+ payment_intent["amount"] = price_to_minor_unit(payment.total, payment.currency)+ payment_intent["currency"] = payment.currency+ payment_intent["status"] = PROCESSING_STATUS++ # when+ handle_processing_payment_intent(payment_intent, plugin.config, channel.slug)++ # then+ assert wrapped_process_payment_with_checkout.called == called++def test_handle_failed_payment_intent_for_checkout(stripe_plugin, payment_stripe_for_checkout, channel_USD):@@ -399,6 +613,92 @@ def test_handle_failed_payment_intent_for_order(assert payment.transactions.filter(kind=TransactionKind.CANCEL).exists()[email protected]("called", [True, False])+@patch(+ "saleor.payment.gateways.stripe.webhooks._update_payment_with_new_transaction",+ wraps=_update_payment_with_new_transaction,+)+@patch("saleor.payment.gateways.stripe.webhooks.order_voided", wraps=order_voided)+def test_handle_failed_payment_intent_different_order_channel_slug(+ wrapped_update_payment_with_new_transaction,+ wrapped_order_voided,+ payment_stripe_for_order,+ stripe_plugin,+ channel_USD,+ channel_PLN,+ called,+):+ # given+ channel = channel_USD if called else channel_PLN+ payment = payment_stripe_for_order+ payment.transactions.create(+ is_success=True,+ action_required=True,+ kind=TransactionKind.ACTION_TO_CONFIRM,+ amount=payment.total,+ currency=payment.currency,+ token="ABC",+ gateway_response={},+ )++ plugin = stripe_plugin()++ payment_intent = StripeObject(id="ABC", last_response={})+ payment_intent["amount"] = payment.total+ payment_intent["currency"] = payment.currency+ payment_intent["status"] = FAILED_STATUSES[0]++ # when+ handle_failed_payment_intent(payment_intent, plugin.config, channel.slug)++ # then+ assert wrapped_update_payment_with_new_transaction.called == called+ assert wrapped_order_voided.called == called++[email protected]("called", [True, False])+@patch("saleor.payment.gateways.stripe.webhooks.order_voided", wraps=order_voided)+@patch(+ "saleor.payment.gateways.stripe.webhooks._update_payment_with_new_transaction",+ wraps=_update_payment_with_new_transaction,+)+def test_handle_failed_payment_intent_different_checkout_channel_slug(+ wrapped_update_payment_with_new_transaction,+ wrapped_order_voided,+ payment_stripe_for_checkout,+ stripe_plugin,+ channel_USD,+ channel_PLN,+ called,+):+ # given+ channel = channel_USD if called else channel_PLN+ payment = payment_stripe_for_checkout+ payment.transactions.create(+ is_success=True,+ action_required=True,+ kind=TransactionKind.ACTION_TO_CONFIRM,+ amount=payment.total,+ currency=payment.currency,+ token="ABC",+ gateway_response={},+ )++ plugin = stripe_plugin()++ payment_intent = StripeObject(id="ABC", last_response={})+ payment_intent["amount"] = payment.total+ payment_intent["currency"] = payment.currency+ payment_intent["status"] = FAILED_STATUSES[0]++ # when+ handle_failed_payment_intent(payment_intent, plugin.config, channel.slug)++ # then+ assert wrapped_update_payment_with_new_transaction.called == called+ assert not wrapped_order_voided.called++def test_handle_fully_refund(stripe_plugin, payment_stripe_for_order, channel_USD):payment = payment_stripe_for_orderpayment.captured_amount = payment.total@@ -506,6 +806,110 @@ def test_handle_refund_already_processed(assert payment.captured_amount == payment.total - Decimal("10")[email protected]("called", [True, False])+@patch(+ "saleor.payment.gateways.stripe.webhooks._update_payment_with_new_transaction",+ wraps=_update_payment_with_new_transaction,+)+@patch(+ "saleor.payment.gateways.stripe.webhooks.order_refunded",+ wraps=order_refunded,+)+def test_handle_refund_different_order_channel_slug(+ wrapped_update_payment_with_new_transaction,+ wrapped_order_refunded,+ stripe_plugin,+ payment_stripe_for_order,+ channel_USD,+ channel_PLN,+ called,+):+ # given+ channel = channel_USD if called else channel_PLN+ payment = payment_stripe_for_order+ payment.captured_amount = payment.total+ payment.save()+ payment.transactions.create(+ is_success=True,+ action_required=True,+ kind=TransactionKind.CAPTURE,+ amount=payment.total,+ currency=payment.currency,+ token="ABC",+ gateway_response={},+ )+ plugin = stripe_plugin()++ refund = StripeObject(id="refund_id")+ refund["amount"] = price_to_minor_unit(payment.total, payment.currency)+ refund["currency"] = payment.currency+ refund["last_response"] = None++ charge = StripeObject()+ charge["payment_intent"] = "ABC"+ charge["refunds"] = StripeObject()+ charge["refunds"]["data"] = [refund]++ # when+ handle_refund(charge, plugin.config, channel.slug)++ # then+ assert wrapped_update_payment_with_new_transaction.called == called+ assert wrapped_order_refunded.called == called++[email protected]("called", [True, False])+@patch(+ "saleor.payment.gateways.stripe.webhooks.order_refunded",+ wraps=order_refunded,+)+@patch(+ "saleor.payment.gateways.stripe.webhooks._update_payment_with_new_transaction",+ wraps=_update_payment_with_new_transaction,+)+def test_handle_refund_different_checkout_channel_slug(+ wrapped_update_payment_with_new_transaction,+ wrapped_order_refunded,+ stripe_plugin,+ payment_stripe_for_checkout,+ channel_USD,+ channel_PLN,+ called,+):+ # given+ channel = channel_USD if called else channel_PLN+ payment = payment_stripe_for_checkout+ payment.captured_amount = payment.total+ payment.save()+ payment.transactions.create(+ is_success=True,+ action_required=True,+ kind=TransactionKind.CAPTURE,+ amount=payment.total,+ currency=payment.currency,+ token="ABC",+ gateway_response={},+ )+ plugin = stripe_plugin()++ refund = StripeObject(id="refund_id")+ refund["amount"] = price_to_minor_unit(payment.total, payment.currency)+ refund["currency"] = payment.currency+ refund["last_response"] = None++ charge = StripeObject()+ charge["payment_intent"] = "ABC"+ charge["refunds"] = StripeObject()+ charge["refunds"]["data"] = [refund]++ # when+ handle_refund(charge, plugin.config, channel.slug)++ # then+ assert wrapped_update_payment_with_new_transaction.called == called+ assert not wrapped_order_refunded.called++@pytest.mark.parametrize("webhook_type, fun_to_mock",[diff --git a/saleor/payment/gateways/stripe/webhooks.py b/saleor/payment/gateways/stripe/webhooks.pyindex 5b64259282..acbda9b5e8 100644--- a/saleor/payment/gateways/stripe/webhooks.py+++ b/saleor/payment/gateways/stripe/webhooks.py@@ -99,16 +99,14 @@ def _channel_slug_is_different_from_payment_channel_slug(elif order is not None:return channel_slug != order.channel.slugelse:- raise AssertionError("Both payment.checkout and payment.order cannot be None")+ raise ValueError("Both payment.checkout and payment.order cannot be None")def _get_payment(payment_intent_id: str) -> Optional[Payment]:return (Payment.objects.prefetch_related(- Prefetch(- "checkout", queryset=Checkout.objects.select_related("channel__slug")- ),- Prefetch("order", queryset=Order.objects.select_related("channel__slug")),+ Prefetch("checkout", queryset=Checkout.objects.select_related("channel")),+ Prefetch("order", queryset=Order.objects.select_related("channel")),).select_for_update(of=("self",)).filter(transactions__token=payment_intent_id, is_active=True)