Skip to content

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

Fix issuingPrincipal field resolution (#16159)

Tomasz Szymańskic5828cf62020
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index a15a6e5551..fc56db138f 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -2399,6 +2399,7 @@ class WebhookPlugin(BasePlugin):
channel=request_delete_data.channel,
),
timeout=WEBHOOK_SYNC_TIMEOUT,
+ requestor=self.requestor,
)
if response_data:
invalidate_cache_for_stored_payment_methods(
@@ -2436,6 +2437,7 @@ class WebhookPlugin(BasePlugin):
subscribable_object=list_payment_method_data,
request_timeout=WEBHOOK_SYNC_TIMEOUT,
cache_timeout=WEBHOOK_CACHE_DEFAULT_TIMEOUT,
+ requestor=self.requestor,
)
if response_data:
previous_value.extend(
@@ -2470,6 +2472,7 @@ class WebhookPlugin(BasePlugin):
False,
subscribable_object=request_data,
timeout=WEBHOOK_SYNC_TIMEOUT,
+ requestor=self.requestor,
)
return response_data
@@ -2702,6 +2705,7 @@ class WebhookPlugin(BasePlugin):
webhook,
False,
subscribable_object=payment,
+ requestor=self.requestor,
)
if response_data is None:
continue
@@ -2754,6 +2758,7 @@ class WebhookPlugin(BasePlugin):
allow_replica=False,
subscribable_object=subscribable_object,
request=request,
+ requestor=self.requestor,
)
error_msg = None
if response_data is None:
@@ -2841,6 +2846,7 @@ class WebhookPlugin(BasePlugin):
webhook=webhook,
allow_replica=False,
subscribable_object=transaction_session_data,
+ requestor=self.requestor,
)
error_msg = None
if response_data is None:
@@ -2897,6 +2903,7 @@ class WebhookPlugin(BasePlugin):
webhook=webhook,
allow_replica=False,
subscribable_object=checkout,
+ requestor=self.requestor,
)
if response_data:
app_gateways = parse_list_payment_gateways_response(
@@ -3042,6 +3049,7 @@ class WebhookPlugin(BasePlugin):
allow_replica=False,
subscribable_object=subscriptable_object,
request=request_context,
+ requestor=self.requestor,
)
return parse_tax_data(response)
@@ -3109,6 +3117,7 @@ class WebhookPlugin(BasePlugin):
subscribable_object=checkout,
request_timeout=WEBHOOK_SYNC_TIMEOUT,
cache_timeout=CACHE_TIME_SHIPPING_LIST_METHODS_FOR_CHECKOUT,
+ requestor=self.requestor,
)
if response_data:
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
index 2a141a325b..c58ca0f15d 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
@@ -1966,6 +1966,21 @@ TRANSACTION_ITEM_METADATA_UPDATED = """
"""
+LIST_STORED_PAYMENT_METHODS = """
+ subscription {
+ event {
+ ... on ListStoredPaymentMethods {
+ issuingPrincipal {
+ ... on Node {
+ id
+ }
+ }
+ }
+ }
+ }
+"""
+
+
MULTIPLE_EVENTS = """
subscription{
event{
diff --git a/saleor/plugins/webhook/tests/test_list_stored_payment_methods_webhook.py b/saleor/plugins/webhook/tests/test_list_stored_payment_methods_webhook.py
index f9ee6f5611..51b6c50c49 100644
--- a/saleor/plugins/webhook/tests/test_list_stored_payment_methods_webhook.py
+++ b/saleor/plugins/webhook/tests/test_list_stored_payment_methods_webhook.py
@@ -1,3 +1,4 @@
+import json
from unittest import mock
import graphene
@@ -11,6 +12,9 @@ from ....webhook.transport.list_stored_payment_methods import (
get_list_stored_payment_methods_from_response,
)
from ....webhook.transport.utils import generate_cache_key_for_webhook
+from .subscription_webhooks.subscription_queries import (
+ LIST_STORED_PAYMENT_METHODS as LIST_STORED_PAYMENT_METHODS_SUBSCRIPTION,
+)
LIST_STORED_PAYMENT_METHODS = """
subscription {
@@ -85,6 +89,100 @@ def test_list_stored_payment_methods_with_static_payload(
)
[email protected]("saleor.webhook.transport.synchronous.transport.cache.get")
[email protected]("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
+def test_list_stored_payment_methods_subscription_issuing_principal(
+ mock_request,
+ mocked_cache_get,
+ channel_USD,
+ customer_user,
+ webhook_plugin,
+ list_stored_payment_methods_app,
+ webhook_list_stored_payment_methods_response,
+):
+ # given
+ mock_request.return_value = webhook_list_stored_payment_methods_response
+ mocked_cache_get.return_value = None
+ webhook = list_stored_payment_methods_app.webhooks.first()
+ webhook.subscription_query = LIST_STORED_PAYMENT_METHODS_SUBSCRIPTION
+ webhook.save(update_fields=["subscription_query"])
+
+ plugin = webhook_plugin()
+ plugin.requestor = customer_user
+
+ data = ListStoredPaymentMethodsRequestData(
+ channel=channel_USD,
+ user=customer_user,
+ )
+
+ # when
+ response = plugin.list_stored_payment_methods(data, [])
+
+ # then
+ delivery = EventDelivery.objects.get()
+ mock_request.assert_called_once_with(delivery, timeout=WEBHOOK_SYNC_TIMEOUT)
+
+ delivery_subscription_payload = json.loads(delivery.payload.payload)
+ assert delivery_subscription_payload == {
+ "issuingPrincipal": {"id": graphene.Node.to_global_id("User", customer_user.pk)}
+ }
+
+ assert response
+ assert response == get_list_stored_payment_methods_from_response(
+ list_stored_payment_methods_app,
+ webhook_list_stored_payment_methods_response,
+ channel_USD.currency_code,
+ )
+
+
[email protected]("saleor.webhook.transport.synchronous.transport.cache.get")
[email protected]("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
+def test_list_stored_payment_methods_subscription_issuing_principal_as_app(
+ mock_request,
+ mocked_cache_get,
+ channel_USD,
+ customer_user,
+ webhook_plugin,
+ list_stored_payment_methods_app,
+ webhook_list_stored_payment_methods_response,
+):
+ # given
+ mock_request.return_value = webhook_list_stored_payment_methods_response
+ mocked_cache_get.return_value = None
+ webhook = list_stored_payment_methods_app.webhooks.first()
+ webhook.subscription_query = LIST_STORED_PAYMENT_METHODS_SUBSCRIPTION
+ webhook.save(update_fields=["subscription_query"])
+
+ plugin = webhook_plugin()
+ plugin.requestor = list_stored_payment_methods_app
+
+ data = ListStoredPaymentMethodsRequestData(
+ channel=channel_USD,
+ user=customer_user,
+ )
+
+ # when
+ response = plugin.list_stored_payment_methods(data, [])
+
+ # then
+ delivery = EventDelivery.objects.get()
+ mock_request.assert_called_once_with(delivery, timeout=WEBHOOK_SYNC_TIMEOUT)
+
+ delivery_subscription_payload = json.loads(delivery.payload.payload)
+ assert delivery_subscription_payload == {
+ "issuingPrincipal": {
+ "id": graphene.Node.to_global_id("App", list_stored_payment_methods_app.pk)
+ }
+ }
+
+ assert response
+ assert response == get_list_stored_payment_methods_from_response(
+ list_stored_payment_methods_app,
+ webhook_list_stored_payment_methods_response,
+ channel_USD.currency_code,
+ )
+
+
@mock.patch("saleor.webhook.transport.synchronous.transport.cache.set")
@mock.patch("saleor.webhook.transport.synchronous.transport.cache.get")
@mock.patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
diff --git a/saleor/plugins/webhook/tests/test_transaction_initialize_session_webhook.py b/saleor/plugins/webhook/tests/test_transaction_initialize_session_webhook.py
index 7a6e76c018..1290646d0c 100644
--- a/saleor/plugins/webhook/tests/test_transaction_initialize_session_webhook.py
+++ b/saleor/plugins/webhook/tests/test_transaction_initialize_session_webhook.py
@@ -39,6 +39,11 @@ subscription {
id
}
}
+ issuingPrincipal {
+ ... on Node {
+ id
+ }
+ }
}
}
}
@@ -56,6 +61,8 @@ def _assert_with_subscription(
response,
mock_request,
idempotency_key,
+ requestor=None,
+ requestor_type=None,
):
object_id = graphene.Node.to_global_id(
source_object.__class__.__name__, source_object.pk
@@ -75,6 +82,11 @@ def _assert_with_subscription(
"__typename": source_object.__class__.__name__,
"id": object_id,
},
+ "issuingPrincipal": (
+ {"id": graphene.Node.to_global_id(requestor_type, requestor.pk)}
+ if requestor
+ else None
+ ),
}
_assert_fields(payload, webhook, expected_response, response, mock_request)
@@ -265,14 +277,93 @@ def test_transaction_initialize_checkout_without_request_data(
webhook_plugin,
webhook_app,
checkout,
+ staff_user,
permission_manage_payments,
transaction_session_response,
transaction_item_generator,
):
# given
+
+ expected_response_data = transaction_session_response
+ mock_request.return_value = expected_response_data
+ plugin = webhook_plugin()
+ plugin.requestor = staff_user
+
+ webhook_app.identifier = "app.identifier"
+ webhook_app.save()
+ webhook_app.permissions.add(permission_manage_payments)
+ webhook = Webhook.objects.create(
+ name="Webhook",
+ app=webhook_app,
+ subscription_query=TRANSACTION_INITIALIZE_SESSION,
+ )
+ event_type = WebhookEventSyncType.TRANSACTION_INITIALIZE_SESSION
+ webhook.events.create(event_type=event_type)
+ amount = Decimal("10.00")
+
+ transaction = transaction_item_generator(
+ checkout_id=checkout.pk,
+ app=webhook_app,
+ psp_reference=None,
+ name=None,
+ message=None,
+ )
+ action_type = TransactionFlowStrategy.CHARGE
+ idempotency_key = str(uuid.uuid4())
+
+ # when
+ response = plugin.transaction_initialize_session(
+ transaction_session_data=TransactionSessionData(
+ transaction=transaction,
+ source_object=checkout,
+ action=TransactionProcessActionData(
+ amount=amount,
+ currency=transaction.currency,
+ action_type=action_type,
+ ),
+ customer_ip_address="127.0.0.1",
+ payment_gateway_data=PaymentGatewayData(
+ app_identifier=webhook_app.identifier, data=None, error=None
+ ),
+ idempotency_key=idempotency_key,
+ ),
+ previous_value=None,
+ )
+
+ # then
+ _assert_with_subscription(
+ checkout,
+ transaction,
+ None,
+ amount,
+ action_type,
+ webhook,
+ expected_response_data,
+ response,
+ mock_request,
+ idempotency_key,
+ requestor=staff_user,
+ requestor_type="User",
+ )
+
+
+@freeze_time()
[email protected]("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
+def test_transaction_initialize_checkout_without_request_data_app_requestor(
+ mock_request,
+ webhook_plugin,
+ webhook_app,
+ checkout,
+ permission_manage_payments,
+ transaction_session_response,
+ transaction_item_generator,
+):
+ # given
+
expected_response_data = transaction_session_response
mock_request.return_value = expected_response_data
plugin = webhook_plugin()
+ plugin.requestor = webhook_app
webhook_app.identifier = "app.identifier"
webhook_app.save()
@@ -327,6 +418,8 @@ def test_transaction_initialize_checkout_without_request_data(
response,
mock_request,
idempotency_key,
+ requestor=webhook_app,
+ requestor_type="App",
)
diff --git a/saleor/webhook/transport/synchronous/transport.py b/saleor/webhook/transport/synchronous/transport.py
index 90eb1c66e6..0623e65b70 100644
--- a/saleor/webhook/transport/synchronous/transport.py
+++ b/saleor/webhook/transport/synchronous/transport.py
@@ -175,6 +175,7 @@ def trigger_webhook_sync_if_not_cached(
request_timeout=None,
cache_timeout=None,
request=None,
+ requestor=None,
) -> Optional[dict]:
"""Get response for synchronous webhook.
@@ -195,6 +196,7 @@ def trigger_webhook_sync_if_not_cached(
subscribable_object=subscribable_object,
timeout=request_timeout,
request=request,
+ requestor=requestor,
)
if response_data is not None:
cache.set(
@@ -273,6 +275,7 @@ def trigger_webhook_sync(
subscribable_object=None,
timeout=None,
request=None,
+ requestor=None,
) -> Optional[dict[Any, Any]]:
"""Send a synchronous webhook request."""
if webhook.subscription_query:
@@ -280,6 +283,7 @@ def trigger_webhook_sync(
event_type=event_type,
subscribable_object=subscribable_object,
webhook=webhook,
+ requestor=requestor,
request=request,
allow_replica=allow_replica,
)