Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Assign app and user to transactionEvent request (#12865)
Maciej Korycinski6c614db0602e
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.pyindex 3ab5040b6e..af8755f799 100644--- a/saleor/graphql/payment/mutations.py+++ b/saleor/graphql/payment/mutations.py@@ -1365,12 +1365,17 @@ class TransactionRequestAction(BaseMutation):@classmethoddef handle_transaction_action(- cls, action, action_kwargs, action_value: Optional[Decimal]+ cls,+ action,+ action_kwargs,+ action_value: Optional[Decimal],+ user: Optional["User"],+ app: Optional[App],):if action == TransactionAction.VOID or action == TransactionAction.CANCEL:transaction = action_kwargs["transaction"]request_event = cls.create_transaction_event_requested(- transaction, 0, action+ transaction, 0, action, user=user, app=app)request_cancelation_action(**action_kwargs,@@ -1383,7 +1388,7 @@ class TransactionRequestAction(BaseMutation):action_value = action_value or transaction.authorized_valueaction_value = min(action_value, transaction.authorized_value)request_event = cls.create_transaction_event_requested(- transaction, action_value, TransactionAction.CHARGE+ transaction, action_value, TransactionAction.CHARGE, user=user, app=app)request_charge_action(**action_kwargs, charge_value=action_value, request_event=request_event@@ -1393,14 +1398,16 @@ class TransactionRequestAction(BaseMutation):action_value = action_value or transaction.charged_valueaction_value = min(action_value, transaction.charged_value)request_event = cls.create_transaction_event_requested(- transaction, action_value, TransactionAction.REFUND+ transaction, action_value, TransactionAction.REFUND, user=user, app=app)request_refund_action(**action_kwargs, refund_value=action_value, request_event=request_event)@classmethod- def create_transaction_event_requested(cls, transaction, action_value, action):+ def create_transaction_event_requested(+ cls, transaction, action_value, action, user=None, app=None+ ):if action in (TransactionAction.CANCEL, TransactionAction.VOID):type = TransactionEventType.CANCEL_REQUESTelif action == TransactionAction.CHARGE:@@ -1420,6 +1427,9 @@ class TransactionRequestAction(BaseMutation):amount_value=action_value,currency=transaction.currency,type=type,+ user=user,+ app=app,+ app_identifier=app.identifier if app else None,)@classmethod@@ -1436,18 +1446,21 @@ class TransactionRequestAction(BaseMutation):channel = checkout.channelcls.check_channel_permissions(info, [channel.id])channel_slug = channel.slug+ user = info.context.userapp = get_app_promise(info.context).get()manager = get_plugin_manager_promise(info.context).get()action_kwargs = {"channel_slug": channel_slug,- "user": info.context.user,+ "user": user,"app": app,"transaction": transaction,"manager": manager,}try:- cls.handle_transaction_action(action_type, action_kwargs, action_value)+ cls.handle_transaction_action(+ action_type, action_kwargs, action_value, user, app+ )except PaymentError as e:error_enum = TransactionRequestActionErrorCodecode = error_enum.MISSING_TRANSACTION_ACTION_REQUEST_WEBHOOK.valuediff --git a/saleor/graphql/payment/tests/mutations/test_transaction_request_action.py b/saleor/graphql/payment/tests/mutations/test_transaction_request_action.pyindex e118e3a511..574a47c668 100644--- a/saleor/graphql/payment/tests/mutations/test_transaction_request_action.py+++ b/saleor/graphql/payment/tests/mutations/test_transaction_request_action.py@@ -1360,3 +1360,296 @@ def test_transaction_request_missing_event(event_type=WebhookEventSyncType.TRANSACTION_CANCELATION_REQUESTED,apps_ids=[app.id],)+++@patch("saleor.plugins.manager.PluginsManager.is_event_active_for_any_plugin")+@patch("saleor.plugins.manager.PluginsManager.transaction_refund_requested")+def test_transaction_request_refund_sets_app_to_request_event(+ mocked_payment_action_request,+ mocked_is_active,+ checkout,+ app_api_client,+ permission_manage_payments,+ transaction_request_webhook,+ transaction_item_generator,+):+ # given+ mocked_is_active.return_value = False++ transaction_request_webhook.events.create(+ event_type=WebhookEventSyncType.TRANSACTION_REFUND_REQUESTED+ )+ transaction = transaction_item_generator(+ checkout_id=checkout.pk,+ charged_value=Decimal(10),+ app=transaction_request_webhook.app,+ )+ refund_amount = Decimal("1")+ variables = {+ "id": graphene.Node.to_global_id("TransactionItem", transaction.token),+ "action_type": TransactionActionEnum.REFUND.name,+ "amount": refund_amount,+ }++ # when+ response = app_api_client.post_graphql(+ MUTATION_TRANSACTION_REQUEST_ACTION,+ variables,+ permissions=[permission_manage_payments],+ )++ # then+ get_graphql_content(response)++ request_event = TransactionEvent.objects.filter(+ type=TransactionEventType.REFUND_REQUEST,+ ).first()++ assert request_event+ assert request_event.app.id == app_api_client.app.id+ assert request_event.app_identifier == app_api_client.app.identifier+ assert request_event.user is None+++@patch("saleor.plugins.manager.PluginsManager.is_event_active_for_any_plugin")+@patch("saleor.plugins.manager.PluginsManager.transaction_refund_requested")+def test_transaction_request_refund_sets_user_to_request_event(+ mocked_payment_action_request,+ mocked_is_active,+ checkout,+ staff_api_client,+ permission_manage_payments,+ transaction_request_webhook,+ transaction_item_generator,+ permission_group_handle_payments,+):+ # given+ mocked_is_active.return_value = False++ transaction_request_webhook.events.create(+ event_type=WebhookEventSyncType.TRANSACTION_REFUND_REQUESTED+ )+ transaction = transaction_item_generator(+ checkout_id=checkout.pk,+ charged_value=Decimal(10),+ app=transaction_request_webhook.app,+ )+ refund_amount = Decimal("1")+ variables = {+ "id": graphene.Node.to_global_id("TransactionItem", transaction.token),+ "action_type": TransactionActionEnum.REFUND.name,+ "amount": refund_amount,+ }+ staff_api_client.user.groups.add(permission_group_handle_payments)++ # when+ response = staff_api_client.post_graphql(+ MUTATION_TRANSACTION_REQUEST_ACTION,+ variables,+ )++ # then+ get_graphql_content(response)++ request_event = TransactionEvent.objects.filter(+ type=TransactionEventType.REFUND_REQUEST,+ ).first()++ assert request_event+ assert request_event.app is None+ assert request_event.app_identifier is None+ assert request_event.user == staff_api_client.user+++@patch("saleor.plugins.manager.PluginsManager.is_event_active_for_any_plugin")+@patch("saleor.plugins.manager.PluginsManager.transaction_charge_requested")+def test_transaction_request_charge_sets_app_to_request_event(+ mocked_payment_action_request,+ mocked_is_active,+ checkout,+ app_api_client,+ permission_manage_payments,+ transaction_request_webhook,+ transaction_item_generator,+):+ # given+ mocked_is_active.return_value = False++ transaction_request_webhook.events.create(+ event_type=WebhookEventSyncType.TRANSACTION_CHARGE_REQUESTED+ )+ transaction = transaction_item_generator(+ checkout_id=checkout.pk,+ authorized_value=Decimal(10),+ app=transaction_request_webhook.app,+ )+ amount = Decimal("1")+ variables = {+ "id": graphene.Node.to_global_id("TransactionItem", transaction.token),+ "action_type": TransactionActionEnum.CHARGE.name,+ "amount": amount,+ }++ # when+ response = app_api_client.post_graphql(+ MUTATION_TRANSACTION_REQUEST_ACTION,+ variables,+ permissions=[permission_manage_payments],+ )++ # then+ get_graphql_content(response)++ request_event = TransactionEvent.objects.filter(+ type=TransactionEventType.CHARGE_REQUEST,+ ).first()++ assert request_event+ assert request_event.app.id == app_api_client.app.id+ assert request_event.app_identifier == app_api_client.app.identifier+ assert request_event.user is None+++@patch("saleor.plugins.manager.PluginsManager.is_event_active_for_any_plugin")+@patch("saleor.plugins.manager.PluginsManager.transaction_charge_requested")+def test_transaction_request_charge_sets_user_to_request_event(+ mocked_payment_action_request,+ mocked_is_active,+ checkout,+ staff_api_client,+ permission_manage_payments,+ transaction_request_webhook,+ transaction_item_generator,+ permission_group_handle_payments,+):+ # given+ mocked_is_active.return_value = False++ transaction_request_webhook.events.create(+ event_type=WebhookEventSyncType.TRANSACTION_CHARGE_REQUESTED+ )+ transaction = transaction_item_generator(+ checkout_id=checkout.pk,+ authorized_value=Decimal(10),+ app=transaction_request_webhook.app,+ )+ amount = Decimal("1")+ variables = {+ "id": graphene.Node.to_global_id("TransactionItem", transaction.token),+ "action_type": TransactionActionEnum.CHARGE.name,+ "amount": amount,+ }+ staff_api_client.user.groups.add(permission_group_handle_payments)++ # when+ response = staff_api_client.post_graphql(+ MUTATION_TRANSACTION_REQUEST_ACTION,+ variables,+ )++ # then+ get_graphql_content(response)++ request_event = TransactionEvent.objects.filter(+ type=TransactionEventType.CHARGE_REQUEST,+ ).first()++ assert request_event+ assert request_event.app is None+ assert request_event.app_identifier is None+ assert request_event.user == staff_api_client.user+++@patch("saleor.plugins.manager.PluginsManager.is_event_active_for_any_plugin")+@patch("saleor.plugins.manager.PluginsManager.transaction_cancelation_requested")+def test_transaction_request_cancel_sets_app_to_request_event(+ mocked_payment_action_request,+ mocked_is_active,+ checkout,+ app_api_client,+ permission_manage_payments,+ transaction_request_webhook,+ transaction_item_generator,+):+ # given+ mocked_is_active.return_value = False++ transaction_request_webhook.events.create(+ event_type=WebhookEventSyncType.TRANSACTION_CANCELATION_REQUESTED+ )+ transaction = transaction_item_generator(+ checkout_id=checkout.pk,+ authorized_value=Decimal(10),+ app=transaction_request_webhook.app,+ )+ variables = {+ "id": graphene.Node.to_global_id("TransactionItem", transaction.token),+ "action_type": TransactionActionEnum.CANCEL.name,+ }++ # when+ response = app_api_client.post_graphql(+ MUTATION_TRANSACTION_REQUEST_ACTION,+ variables,+ permissions=[permission_manage_payments],+ )++ # then+ get_graphql_content(response)++ request_event = TransactionEvent.objects.filter(+ type=TransactionEventType.CANCEL_REQUEST,+ ).first()++ assert request_event+ assert request_event.app.id == app_api_client.app.id+ assert request_event.app_identifier == app_api_client.app.identifier+ assert request_event.user is None+++@patch("saleor.plugins.manager.PluginsManager.is_event_active_for_any_plugin")+@patch("saleor.plugins.manager.PluginsManager.transaction_cancelation_requested")+def test_transaction_request_cancel_sets_user_to_request_event(+ mocked_payment_action_request,+ mocked_is_active,+ checkout,+ staff_api_client,+ permission_manage_payments,+ transaction_request_webhook,+ transaction_item_generator,+ permission_group_handle_payments,+):+ # given+ mocked_is_active.return_value = False++ transaction_request_webhook.events.create(+ event_type=WebhookEventSyncType.TRANSACTION_CANCELATION_REQUESTED+ )+ transaction = transaction_item_generator(+ checkout_id=checkout.pk,+ authorized_value=Decimal(10),+ app=transaction_request_webhook.app,+ )+ variables = {+ "id": graphene.Node.to_global_id("TransactionItem", transaction.token),+ "action_type": TransactionActionEnum.CANCEL.name,+ }+ staff_api_client.user.groups.add(permission_group_handle_payments)++ # when+ response = staff_api_client.post_graphql(+ MUTATION_TRANSACTION_REQUEST_ACTION,+ variables,+ )++ # then+ get_graphql_content(response)++ request_event = TransactionEvent.objects.filter(+ type=TransactionEventType.CANCEL_REQUEST,+ ).first()++ assert request_event+ assert request_event.app is None+ assert request_event.app_identifier is None+ assert request_event.user == staff_api_client.userdiff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.pyindex 79003b41eb..1abb4cfbf5 100644--- a/saleor/tests/fixtures.py+++ b/saleor/tests/fixtures.py@@ -5113,6 +5113,17 @@ def permission_group_manage_apps(permission_manage_apps, staff_users):return group+def permission_group_handle_payments(permission_manage_payments, staff_users):+ group = Group.objects.create(+ name="Manage apps group.", restricted_access_to_channels=False+ )+ group.permissions.add(permission_manage_payments)++ group.user_set.add(staff_users[1])+ return group++@pytest.fixturedef permission_group_all_perms_all_channels(permission_manage_users, staff_users, channel_USD, channel_PLN