Skip to content

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

Restructure webhook tests (#16583)

Marcin Gębalaa76e77e495a0
diff --git a/conftest.py b/conftest.py
index 00415b0f53..d3c1ce6dc5 100644
--- a/conftest.py
+++ b/conftest.py
@@ -34,8 +34,8 @@ pytest_plugins = [
"saleor.graphql.order.tests.benchmark.fixtures",
"saleor.graphql.giftcard.tests.benchmark.fixtures",
"saleor.graphql.webhook.tests.benchmark.fixtures",
- "saleor.plugins.webhook.tests.subscription_webhooks.fixtures",
"saleor.tax.tests.fixtures",
+ "saleor.webhook.tests.subscription_webhooks.fixtures",
]
diff --git a/saleor/plugins/webhook/conftest.py b/saleor/plugins/webhook/conftest.py
index 932f56c6c4..17bea31a22 100644
--- a/saleor/plugins/webhook/conftest.py
+++ b/saleor/plugins/webhook/conftest.py
@@ -1,43 +1,8 @@
import pytest
-from ...webhook.event_types import WebhookEventSyncType
-from ...webhook.models import Webhook
from ..manager import get_plugins_manager
-def tax_app(app, permission_handle_taxes, webhook):
- app.permissions.add(permission_handle_taxes)
- return app
-
-
-def tax_checkout_webhook(tax_app):
- webhook = Webhook.objects.create(
- name="Tax checkout webhook",
- app=tax_app,
- target_url="https://www.example.com/tax-checkout",
- )
- webhook.events.create(event_type=WebhookEventSyncType.CHECKOUT_CALCULATE_TAXES)
- return webhook
-
-
-def tax_order_webhook(tax_app):
- webhook = Webhook.objects.create(
- name="Tax order webhook",
- app=tax_app,
- target_url="https://www.example.com/tax-order",
- )
- webhook.events.create(event_type=WebhookEventSyncType.ORDER_CALCULATE_TAXES)
- return webhook
-
-
-def tax_app_with_webhooks(tax_app, tax_checkout_webhook, tax_order_webhook):
- return tax_app
-
-
@pytest.fixture
def webhook_plugin(settings):
def factory():
diff --git a/saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_with_http.yaml b/saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_with_http.yaml
deleted file mode 100644
index 0d57522a4d..0000000000
--- a/saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_with_http.yaml
+++ /dev/null
@@ -1,71 +0,0 @@
-interactions:
-- request:
- body: '[{"model": "order.order", "pk": 187, "fields": {"private_metadata": {},
- "metadata": {}, "created": "2020-08-07T09:05:59.192Z", "status": "unfulfilled",
- "user": 202, "language_code": "en", "tracking_client_id": "", "billing_address":
- 825, "shipping_address": 826, "user_email": "[email protected]", "currency":
- "USD", "shipping_method": 235, "shipping_method_name": "DHL", "shipping_price_net_amount":
- "10.00", "shipping_price_gross_amount": "12.30", "token": "aad0c768-c6d8-41ee-a25f-73b7538941a8",
- "checkout_token": "", "total_net_amount": "80.00", "total_gross_amount": "98.40",
- "voucher": null, "discount_amount": "0.00", "discount_name": null, "translated_discount_name":
- null, "display_gross_prices": true, "customer_note": "", "weight": "0.0:kg",
- "gift_cards": []}}]'
- headers:
- Accept:
- - '*/*'
- Accept-Encoding:
- - gzip, deflate
- Connection:
- - keep-alive
- Content-Length:
- - '771'
- Content-Type:
- - application/json
- User-Agent:
- - python-requests/2.24.0
-
- # X- headers will be deprecated in Saleor 4.0, proper headers are without X-
- X-Saleor-Domain:
- - mirumee.com
- X-Saleor-Event:
- - order_created
- X-Saleor-Signature:
- - ''
- Saleor-Domain:
- - mirumee.com
- Saleor-Event:
- - order_created
- Saleor-Signature:
- - ''
- method: POST
- uri: https://webhook.site/48978b64-4efb-43d5-a334-451a1d164009
- response:
- body:
- string: !!binary |
- H4sIAAAAAAAAAwMAAAAAAAAAAAA=
- headers:
- Cache-Control:
- - no-cache, private
- Content-Encoding:
- - gzip
- Content-Type:
- - text/plain; charset=UTF-8
- Date:
- - Fri, 07 Aug 2020 09:05:59 GMT
- Server:
- - nginx/1.14.2
- Set-Cookie:
- - laravel_session=ylfogNz3uixEXKaOfz1tmSob8nISj4pH8vFG5XRc; expires=Fri, 07-Aug-2020
- 11:05:59 GMT; Max-Age=7200; path=/; httponly
- Transfer-Encoding:
- - chunked
- Vary:
- - Accept-Encoding
- X-Request-Id:
- - 0e944b79-e2e2-4f52-9879-1a9635416fb9
- X-Token-Id:
- - 48978b64-4efb-43d5-a334-451a1d164009
- status:
- code: 200
- message: OK
-version: 1
diff --git a/saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_with_http_and_secret_key.yaml b/saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_with_http_and_secret_key.yaml
deleted file mode 100644
index 7620b433e5..0000000000
--- a/saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_with_http_and_secret_key.yaml
+++ /dev/null
@@ -1,75 +0,0 @@
-interactions:
-- request:
- body: '[{"model": "order.order", "pk": 195, "fields": {"private_metadata": {},
- "metadata": {}, "created": "2020-08-07T09:06:01.255Z", "status": "unfulfilled",
- "user": 210, "language_code": "en", "tracking_client_id": "", "billing_address":
- 857, "shipping_address": 858, "user_email": "[email protected]", "currency":
- "USD", "shipping_method": 243, "shipping_method_name": "DHL", "shipping_price_net_amount":
- "10.00", "shipping_price_gross_amount": "12.30", "token": "ee6320cf-3080-4578-bab2-520e93b4b065",
- "checkout_token": "", "total_net_amount": "80.00", "total_gross_amount": "98.40",
- "voucher": null, "discount_amount": "0.00", "discount_name": null, "translated_discount_name":
- null, "display_gross_prices": true, "customer_note": "", "weight": "0.0:kg",
- "gift_cards": []}}]'
- headers:
- Accept:
- - '*/*'
- Accept-Encoding:
- - gzip, deflate
- Connection:
- - keep-alive
- Content-Length:
- - '771'
- Content-Type:
- - application/json
- User-Agent:
- - python-requests/2.24.0
-
- # X- headers will be deprecated in Saleor 4.0, proper headers are without X-
- X-Saleor-Domain:
- - mirumee.com
- X-Saleor-Event:
- - order_created
- X-Saleor-HMAC-SHA256:
- - sha1=cce609bb36a9aa6b54af010a2aef40979ddd56a309590a470a9c4602226324b4
- X-Saleor-Signature:
- - cce609bb36a9aa6b54af010a2aef40979ddd56a309590a470a9c4602226324b4
- Saleor-Domain:
- - mirumee.com
- Saleor-Event:
- - order_created
- Saleor-HMAC-SHA256:
- - sha1=cce609bb36a9aa6b54af010a2aef40979ddd56a309590a470a9c4602226324b4
- Saleor-Signature:
- - cce609bb36a9aa6b54af010a2aef40979ddd56a309590a470a9c4602226324b4
- method: POST
- uri: https://webhook.site/48978b64-4efb-43d5-a334-451a1d164009
- response:
- body:
- string: !!binary |
- H4sIAAAAAAAAAwMAAAAAAAAAAAA=
- headers:
- Cache-Control:
- - no-cache, private
- Content-Encoding:
- - gzip
- Content-Type:
- - text/plain; charset=UTF-8
- Date:
- - Fri, 07 Aug 2020 09:06:01 GMT
- Server:
- - nginx/1.14.2
- Set-Cookie:
- - laravel_session=RJ3x6kNdLBbDUIJXj2TAYiiIlfP2pV0jBXWtUGjM; expires=Fri, 07-Aug-2020
- 11:06:01 GMT; Max-Age=7200; path=/; httponly
- Transfer-Encoding:
- - chunked
- Vary:
- - Accept-Encoding
- X-Request-Id:
- - 1d00628d-31d9-4636-b6b8-af7e35e3dde9
- X-Token-Id:
- - 48978b64-4efb-43d5-a334-451a1d164009
- status:
- code: 200
- message: OK
-version: 1
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_webhook.py b/saleor/plugins/webhook/tests/subscription_webhooks/test_webhook.py
deleted file mode 100644
index db44935558..0000000000
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_webhook.py
+++ /dev/null
@@ -1,181 +0,0 @@
-import dataclasses
-import json
-from decimal import Decimal
-from unittest import mock
-
-import graphene
-
-from .....core.models import EventDelivery
-from .....graphql.discount.enums import DiscountValueTypeEnum
-from .....graphql.order.tests.mutations.test_order_discount import ORDER_DISCOUNT_ADD
-from .....graphql.product.tests.mutations.test_product_create import (
- CREATE_PRODUCT_MUTATION,
-)
-from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
-from .....webhook.models import Webhook
-from .....webhook.transport.asynchronous.transport import trigger_webhooks_async
-from .....webhook.transport.synchronous.transport import trigger_webhook_sync
-from .payloads import generate_payment_payload
-
-TEST_ID = "test_id"
-
-
-class FakeDelivery:
- id = TEST_ID
-
-
- "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
-)
-def test_trigger_webhooks_async(
- mocked_send_webhook_request,
- webhook,
- subscription_order_created_webhook,
- order,
-):
- webhook_type = WebhookEventAsyncType.ORDER_CREATED
- webhooks, payload = Webhook.objects.all(), '{"example": "payload"}'
-
- trigger_webhooks_async(payload, webhook_type, webhooks, order, allow_replica=False)
-
- deliveries = EventDelivery.objects.all()
- assert deliveries.count() == 2
- assert deliveries[0].webhook == subscription_order_created_webhook
- assert deliveries[1].webhook == webhook
- for delivery in deliveries:
- assert (
- mock.call(
- kwargs={"event_delivery_id": delivery.id},
- queue=None,
- bind=True,
- retry_backoff=10,
- retry_kwargs={"max_retries": 5},
- )
- in mocked_send_webhook_request.mock_calls
- )
-
-
- "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
-)
- "saleor.webhook.transport.asynchronous.transport.create_deliveries_for_subscriptions"
-)
-def test_trigger_webhooks_async_no_subscription_webhooks(
- mocked_create_deliveries_for_subscriptions,
- mocked_send_webhook_request,
- webhook,
- order,
-):
- webhook_type = WebhookEventAsyncType.ORDER_UPDATED
- webhooks = Webhook.objects.all()
- data = '{"regular_webhook": "data"}'
- trigger_webhooks_async(data, webhook_type, webhooks, order, allow_replica=False)
- mocked_create_deliveries_for_subscriptions.assert_not_called()
-
-
[email protected]("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
-def test_trigger_webhook_sync_with_subscription(
- mock_request, payment_app_with_subscription_webhooks, payment
-):
- # given
- payment_app = payment_app_with_subscription_webhooks
- data = '{"key": "value"}'
- expected_payment_payload = generate_payment_payload(payment)
- # when
- trigger_webhook_sync(
- WebhookEventSyncType.PAYMENT_AUTHORIZE,
- data,
- payment_app.webhooks.first(),
- False,
- payment,
- )
- event_delivery = EventDelivery.objects.first()
-
- # then
- assert json.loads(event_delivery.payload.get_payload()) == expected_payment_payload
- mock_request.assert_called_once_with(event_delivery)
-
-
[email protected]("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
[email protected]("saleor.webhook.transport.synchronous.transport.get_webhooks_for_event")
- "saleor.webhook.transport.synchronous.transport.generate_payload_from_subscription"
-)
-def test_trigger_webhook_sync_with_subscription_within_mutation_use_default_db(
- mocked_generate_payload,
- mocked_get_webhooks_for_event,
- mocked_request,
- draft_order,
- app_api_client,
- permission_manage_orders,
- settings,
- subscription_calculate_taxes_for_order,
-):
- # given
- webhook = subscription_calculate_taxes_for_order
- settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
- mocked_get_webhooks_for_event.return_value = [webhook]
- variables = {
- "orderId": graphene.Node.to_global_id("Order", draft_order.pk),
- "input": {
- "valueType": DiscountValueTypeEnum.PERCENTAGE.name,
- "value": Decimal("50"),
- },
- }
- app_api_client.app.permissions.add(permission_manage_orders)
-
- # when
- app_api_client.post_graphql(ORDER_DISCOUNT_ADD, variables)
-
- # then
- mocked_generate_payload.assert_called_once()
- assert not mocked_generate_payload.call_args[1]["request"].allow_replica
-
-
- "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async"
-)
[email protected]("saleor.plugins.webhook.plugin.get_webhooks_for_event")
- "saleor.webhook.transport.asynchronous.transport.generate_payload_from_subscription"
-)
-def test_trigger_webhook_async_with_subscription_use_main_db(
- mocked_generate_payload,
- mocked_get_webhooks_for_event,
- mocked_request,
- staff_api_client,
- product_type,
- category,
- permission_manage_products,
- subscription_product_created_webhook,
- settings,
-):
- # given
- webhook = subscription_product_created_webhook
- settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
- mocked_get_webhooks_for_event.return_value = [webhook]
-
- product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
- category_id = graphene.Node.to_global_id("Category", category.pk)
- product_name = "test name"
- product_slug = "product-test-slug"
-
- variables = {
- "input": {
- "productType": product_type_id,
- "category": category_id,
- "name": product_name,
- "slug": product_slug,
- }
- }
-
- # when
- staff_api_client.post_graphql(
- CREATE_PRODUCT_MUTATION, variables, permissions=[permission_manage_products]
- )
-
- # then
- mocked_generate_payload.assert_called_once()
- assert not mocked_generate_payload.call_args[1]["request"].allow_replica
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 0c8cf90523..97b437b8e8 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
@@ -8,13 +8,13 @@ from ....payment.interface import ListStoredPaymentMethodsRequestData
from ....settings import WEBHOOK_SYNC_TIMEOUT
from ....webhook.const import WEBHOOK_CACHE_DEFAULT_TIMEOUT
from ....webhook.event_types import WebhookEventSyncType
+from ....webhook.tests.subscription_webhooks.subscription_queries import (
+ LIST_STORED_PAYMENT_METHODS as LIST_STORED_PAYMENT_METHODS_SUBSCRIPTION,
+)
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 {
diff --git a/saleor/plugins/webhook/tests/test_tax_webhook.py b/saleor/plugins/webhook/tests/test_tax_webhook.py
index 41ac6cc564..ede131dd3b 100644
--- a/saleor/plugins/webhook/tests/test_tax_webhook.py
+++ b/saleor/plugins/webhook/tests/test_tax_webhook.py
@@ -11,7 +11,6 @@ from ....core.models import EventDelivery, EventPayload
from ....core.taxes import TaxType
from ....graphql.webhook.utils import get_subscription_query_hash
from ....webhook.event_types import WebhookEventSyncType
-from ....webhook.models import Webhook
from ....webhook.payloads import generate_order_payload_for_tax_calculation
from ....webhook.transport.utils import (
DEFAULT_TAX_CODE,
@@ -51,15 +50,17 @@ def test_get_taxes_for_order(
mock_fetch,
permission_handle_taxes,
webhook_plugin,
- tax_order_webhook,
tax_data_response,
order,
- tax_app_with_webhooks,
+ tax_app,
):
# given
mock_request.return_value = tax_data_response
plugin = webhook_plugin()
app_identifier = None
+ webhook = tax_app.webhooks.get(name="tax-webhook-1")
+ webhook.subscription_query = None
+ webhook.save(update_fields=["subscription_query"])
# when
tax_data = plugin.get_taxes_for_order(order, app_identifier, None)
@@ -71,7 +72,7 @@ def test_get_taxes_for_order(
assert delivery.status == EventDeliveryStatus.PENDING
assert delivery.event_type == WebhookEventSyncType.ORDER_CALCULATE_TAXES
assert delivery.payload == payload
- assert delivery.webhook == tax_order_webhook
+ assert delivery.webhook == webhook
mock_request.assert_called_once_with(delivery)
mock_fetch.assert_not_called()
assert tax_data == parse_tax_data(tax_data_response)
@@ -120,15 +121,15 @@ def test_get_tax_code_from_object_meta_no_app(
def test_get_tax_code_from_object_meta(
webhook_plugin,
- tax_app_with_webhooks,
+ tax_app,
tax_type,
product,
):
# given
plugin = webhook_plugin()
product.metadata = {
- f"{tax_app_with_webhooks.identifier}.code": tax_type.code,
- f"{tax_app_with_webhooks.identifier}.description": tax_type.description,
+ f"{tax_app.identifier}.code": tax_type.code,
+ f"{tax_app.identifier}.description": tax_type.description,
}
# when
@@ -140,7 +141,7 @@ def test_get_tax_code_from_object_meta(
def test_get_tax_code_from_object_meta_default_code(
webhook_plugin,
- tax_app_with_webhooks,
+ tax_app,
product,
):
# given
@@ -170,15 +171,11 @@ def test_get_taxes_for_order_with_sync_subscription(
# given
mock_request.return_value = tax_data_response
plugin = webhook_plugin()
- webhook = Webhook.objects.create(
- name="Tax checkout webhook",
- app=tax_app,
- target_url="https://localhost:8888/tax-order",
- subscription_query=(
- "subscription{event{... on CalculateTaxes{taxBase{currency}}}}"
- ),
+ webhook = tax_app.webhooks.get(name="tax-webhook-1")
+ webhook.subscription_query = (
+ "subscription{event{... on CalculateTaxes{taxBase{currency}}}}"
)
- webhook.events.create(event_type=WebhookEventSyncType.ORDER_CALCULATE_TAXES)
+ webhook.save(update_fields=["subscription_query"])
app_identifier = None
# when
@@ -221,13 +218,9 @@ def test_get_taxes_for_checkout_with_sync_subscription(
mock_request.return_value = tax_data_response
mock_generate_payload.return_value = expected_payload
plugin = webhook_plugin()
- webhook = Webhook.objects.create(
- name="Tax checkout webhook",
- app=tax_app,
- target_url="https://localhost:8888/tax-order",
- subscription_query=subscription_query,
- )
- webhook.events.create(event_type=WebhookEventSyncType.CHECKOUT_CALCULATE_TAXES)
+ webhook = tax_app.webhooks.get(name="tax-webhook-1")
+ webhook.subscription_query = subscription_query
+ webhook.save(update_fields=["subscription_query"])
app_identifier = None
# when
@@ -277,16 +270,12 @@ def test_get_taxes_for_checkout_with_sync_subscription_with_pregenerated_payload
)
mock_request.return_value = tax_data_response
plugin = webhook_plugin()
- webhook = Webhook.objects.create(
- name="Tax checkout webhook",
- app=tax_app,
- target_url="https://localhost:8888/tax-order",
- subscription_query=subscription_query,
- )
- webhook.events.create(event_type=WebhookEventSyncType.CHECKOUT_CALCULATE_TAXES)
+ webhook = tax_app.webhooks.get(name="tax-webhook-1")
+ webhook.subscription_query = subscription_query
+ webhook.save(update_fields=["subscription_query"])
app_identifier = None
pregenerated_subscription_payloads = {
- tax_app.id: {subscription_query_hash: expected_payload}
+ webhook.app.id: {subscription_query_hash: expected_payload}
}
# when
diff --git a/saleor/plugins/webhook/tests/test_utils.py b/saleor/plugins/webhook/tests/test_utils.py
deleted file mode 100644
index 8b9bdf5e6c..0000000000
--- a/saleor/plugins/webhook/tests/test_utils.py
+++ /dev/null
@@ -1,403 +0,0 @@
-import copy
-
-import pytest
-
-from ....payment.interface import PaymentGateway
-from ....webhook.event_types import WebhookEventSyncType
-from ....webhook.transport.list_stored_payment_methods import (
- get_credit_card_info,
- get_list_stored_payment_methods_from_response,
- get_payment_method_from_response,
-)
-from ....webhook.transport.utils import (
- generate_cache_key_for_webhook,
- to_payment_app_id,
-)
-
-
-def payment_method_response():
- return {
- "id": "method-1",
- "supportedPaymentFlows": ["INTERACTIVE"],
- "type": "Credit Card",
- "creditCardInfo": {
- "brand": "visa",
- "lastDigits": "1234",
- "expMonth": 1,
- "expYear": 2023,
- "firstDigits": "123456",
- },
- "name": "***1234",
- "data": {"some": "data"},
- }
-
-
-def test_different_target_urls_produce_different_cache_key(checkout_with_item):
- # given
- target_url_1 = "http://example.com/1"
- target_url_2 = "http://example.com/2"
-
- payload = {"field": "1", "field2": "2"}
-
- # when
- cache_key_1 = generate_cache_key_for_webhook(
- payload,
- target_url_1,
- WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
- 1,
- )
- cache_key_2 = generate_cache_key_for_webhook(
- payload,
- target_url_2,
- WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
- 1,
- )
-
- # then
- assert cache_key_1 != cache_key_2
-
-
-def test_different_payload_produce_different_cache_key(checkout_with_item):
- # given
- target_url = "http://example.com/1"
-
- payload_1 = {"field": "1", "field2": "2"}
- payload_2 = {"field": "1", "field2": "3"}
-
- # when
- cache_key_1 = generate_cache_key_for_webhook(
- payload_1,
- target_url,
- WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
- 1,
- )
- cache_key_2 = generate_cache_key_for_webhook(
- payload_2,
- target_url,
- WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
- 1,
- )
-
- # then
- assert cache_key_1 != cache_key_2
-
-
-def test_different_event_produce_different_cache_key(checkout_with_item):
- # given
- target_url = "http://example.com/1"
-
- payload = {"field": "1", "field2": "2"}
-
- # when
- cache_key_1 = generate_cache_key_for_webhook(
- payload, target_url, WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT, 1
- )
- cache_key_2 = generate_cache_key_for_webhook(
- payload, target_url, WebhookEventSyncType.LIST_STORED_PAYMENT_METHODS, 1
- )
-
- # then
- assert cache_key_1 != cache_key_2
-
-
-def test_different_app_produce_different_cache_key():
- # given
- target_url = "http://example.com/1"
- first_app_id = 1
- second_app_id = 2
- payload = {"field": "1", "field2": "2"}
-
- # when
- cache_key_1 = generate_cache_key_for_webhook(
- payload,
- target_url,
- WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
- first_app_id,
- )
- cache_key_2 = generate_cache_key_for_webhook(
- payload,
- target_url,
- WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
- second_app_id,
- )
-
- # then
- assert cache_key_1 != cache_key_2
-
-
-def test_get_credit_card_info(app):
- # given
- brand = "VISA"
- last_digits = "1234"
- exp_year = "2023"
- exp_month = 1
- first_digits = "4321"
-
- credit_card_info = {
- "brand": brand,
- "lastDigits": last_digits,
- "expYear": exp_year,
- "expMonth": exp_month,
- "firstDigits": first_digits,
- }
-
- # when
- response = get_credit_card_info(app, credit_card_info)
-
- # then
- assert response.brand == brand
- assert response.last_digits == last_digits
- assert response.exp_year == int(exp_year)
- assert response.exp_month == exp_month
- assert response.first_digits == first_digits
-
-
-def test_get_credit_card_info_without_first_digits_field(app):
- # given
- brand = "VISA"
- last_digits = "1234"
- exp_year = 2023
- exp_month = 1
-
- credit_card_info = {
- "brand": brand,
- "lastDigits": last_digits,
- "expYear": exp_year,
- "expMonth": exp_month,
- }
-
- # when
- response = get_credit_card_info(app, credit_card_info)
-
- # then
- assert response.brand == brand
- assert response.last_digits == last_digits
- assert response.exp_year == exp_year
- assert response.exp_month == exp_month
- assert response.first_digits is None
-
-
- "field",
- [
- "brand",
- "lastDigits",
- "expYear",
- "expMonth",
- ],
-)
-def test_get_credit_card_info_missing_required_field(field, app):
- # given
- brand = "VISA"
- last_digits = "1234"
- exp_year = 2023
- exp_month = 1
- first_digits = "4321"
-
- credit_card_info = {
- "brand": brand,
- "lastDigits": last_digits,
- "expYear": exp_year,
- "expMonth": exp_month,
- "firstDigits": first_digits,
- }
- del credit_card_info[field]
-
- # when
- response = get_credit_card_info(app, credit_card_info)
-
- # then
- assert response is None
-
-
- "field",
- [
- "brand",
- "lastDigits",
- "expYear",
- "expMonth",
- ],
-)
-def test_get_credit_card_info_required_field_is_none(field, app):
- # given
- brand = "VISA"
- last_digits = "1234"
- exp_year = 2023
- exp_month = 1
- first_digits = "4321"
-
- credit_card_info = {
- "brand": brand,
- "lastDigits": last_digits,
- "expYear": exp_year,
- "expMonth": exp_month,
- "firstDigits": first_digits,
- }
- credit_card_info[field] = None
-
- # when
- response = get_credit_card_info(app, credit_card_info)
-
- # then
- assert response is None
-
-
[email protected]("exp_year", [None, "", "str"])
-def test_get_credit_card_info_incorrect_exp_year(exp_year, app):
- # given
- brand = "VISA"
- last_digits = "1234"
- exp_month = 1
- first_digits = "4321"
-
- credit_card_info = {
- "brand": brand,
- "lastDigits": last_digits,
- "expYear": exp_year,
- "expMonth": exp_month,
- "firstDigits": first_digits,
- }
-
- # when
- response = get_credit_card_info(app, credit_card_info)
-
- # then
- assert response is None
-
-
[email protected]("exp_month", [None, "", "str"])
-def test_get_credit_card_info_incorrect_exp_month(exp_month, app):
- # given
- brand = "VISA"
- last_digits = "1234"
- exp_year = 2023
- first_digits = "4321"
-
- credit_card_info = {
- "brand": brand,
- "lastDigits": last_digits,
- "expYear": exp_year,
- "expMonth": exp_month,
- "firstDigits": first_digits,
- }
-
- # when
- response = get_credit_card_info(app, credit_card_info)
-
- # then
- assert response is None
-
-
-def test_get_payment_method_from_response(payment_method_response, app):
- # when
- payment_method = get_payment_method_from_response(
- app, payment_method_response, "usd"
- )
-
- # then
- assert payment_method.id == to_payment_app_id(app, payment_method_response["id"])
- assert payment_method.external_id == payment_method_response["id"]
- assert payment_method.type == payment_method_response["type"]
- assert payment_method.gateway == PaymentGateway(
- id=app.identifier, name=app.name, currencies=["usd"], config=[]
- )
- assert payment_method.supported_payment_flows == [
- flow.lower() for flow in payment_method_response["supportedPaymentFlows"]
- ]
- assert payment_method.credit_card_info == get_credit_card_info(
- app, payment_method_response["creditCardInfo"]
- )
- assert payment_method.name == payment_method_response["name"]
- assert payment_method.data == payment_method_response["data"]
-
-
[email protected]("field", ["id", "type", "supportedPaymentFlows"])
-def test_get_payment_method_from_response_missing_required_field(
- field, payment_method_response, app
-):
- # given
- del payment_method_response[field]
-
- # when
- payment_method = get_payment_method_from_response(
- app, payment_method_response, "usd"
- )
-
- # then
- assert payment_method is None
-
-
[email protected]("field", ["creditCardInfo", "name", "data"])
-def test_get_payment_method_from_response_optional_field(
- field, payment_method_response, app
-):
- del payment_method_response[field]
-
- # when
- payment_method = get_payment_method_from_response(
- app, payment_method_response, "usd"
- )
-
- # then
- assert payment_method.id == to_payment_app_id(app, payment_method_response["id"])
- assert payment_method.external_id == payment_method_response["id"]
- assert payment_method.type == payment_method_response["type"]
- assert payment_method.gateway == PaymentGateway(
- id=app.identifier, name=app.name, currencies=["usd"], config=[]
- )
- assert payment_method.supported_payment_flows == [
- flow.lower() for flow in payment_method_response["supportedPaymentFlows"]
- ]
-
-
[email protected]("field", ["id", "type", "supportedPaymentFlows"])
-def test_get_payment_method_from_response_required_field_is_none(
- field, payment_method_response, app
-):
- # given
- payment_method_response[field] = None
-
- # when
- payment_method = get_payment_method_from_response(
- app, payment_method_response, "usd"
- )
-
- # then
- assert payment_method is None
-
-
-def test_get_payment_method_from_response_incorrect_payment_flow_choices(
- payment_method_response, app
-):
- # given
- payment_method_response["supportedPaymentFlows"] = ["incorrect", "INTERACTIVE"]
-
- # when
- payment_method = get_payment_method_from_response(
- app, payment_method_response, "usd"
- )
-
- # then
- assert payment_method is None
-
-
-def test_get_list_stored_payment_methods_from_response(payment_method_response, app):
- # given
- second_payment_method = copy.deepcopy(payment_method_response)
- del second_payment_method["id"]
- list_stored_payment_methods_response = {
- "paymentMethods": [payment_method_response, second_payment_method]
- }
-
- # when
- response = get_list_stored_payment_methods_from_response(
- app, list_stored_payment_methods_response, "usd"
- )
-
- # then
- assert len(response) == 1
- assert response == [
- get_payment_method_from_response(app, payment_method_response, "usd")
- ]
diff --git a/saleor/plugins/webhook/tests/test_webhook.py b/saleor/plugins/webhook/tests/test_webhook.py
index c9a7d70d05..94e7baa20c 100644
--- a/saleor/plugins/webhook/tests/test_webhook.py
+++ b/saleor/plugins/webhook/tests/test_webhook.py
@@ -37,7 +37,12 @@ from ....discount.interface import VariantPromotionRuleInfo
from ....discount.utils.checkout import (
create_or_update_discount_objects_from_promotion_for_checkout,
)
+from ....graphql.discount.enums import DiscountValueTypeEnum
from ....graphql.discount.utils import convert_migrated_sale_predicate_to_catalogue_info
+from ....graphql.order.tests.mutations.test_order_discount import ORDER_DISCOUNT_ADD
+from ....graphql.product.tests.mutations.test_product_create import (
+ CREATE_PRODUCT_MUTATION,
+)
from ....payment import TransactionAction, TransactionEventType
from ....payment.interface import TransactionActionData
from ....payment.models import TransactionItem
@@ -2371,3 +2376,86 @@ def test_is_event_active(settings, webhook, permission_manage_orders):
# then
assert is_active == expected_is_active
+
+
[email protected]("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
[email protected]("saleor.webhook.transport.synchronous.transport.get_webhooks_for_event")
+ "saleor.webhook.transport.synchronous.transport.generate_payload_from_subscription"
+)
+def test_trigger_webhook_sync_with_subscription_within_mutation_use_default_db(
+ mocked_generate_payload,
+ mocked_get_webhooks_for_event,
+ mocked_request,
+ draft_order,
+ app_api_client,
+ permission_manage_orders,
+ settings,
+ subscription_calculate_taxes_for_order,
+):
+ # given
+ webhook = subscription_calculate_taxes_for_order
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ mocked_get_webhooks_for_event.return_value = [webhook]
+ variables = {
+ "orderId": graphene.Node.to_global_id("Order", draft_order.pk),
+ "input": {
+ "valueType": DiscountValueTypeEnum.PERCENTAGE.name,
+ "value": Decimal("50"),
+ },
+ }
+ app_api_client.app.permissions.add(permission_manage_orders)
+
+ # when
+ app_api_client.post_graphql(ORDER_DISCOUNT_ADD, variables)
+
+ # then
+ mocked_generate_payload.assert_called_once()
+ assert not mocked_generate_payload.call_args[1]["request"].allow_replica
+
+
+ "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async"
+)
[email protected]("saleor.plugins.webhook.plugin.get_webhooks_for_event")
+ "saleor.webhook.transport.asynchronous.transport.generate_payload_from_subscription"
+)
+def test_trigger_webhook_async_with_subscription_use_main_db(
+ mocked_generate_payload,
+ mocked_get_webhooks_for_event,
+ mocked_request,
+ staff_api_client,
+ product_type,
+ category,
+ permission_manage_products,
+ subscription_product_created_webhook,
+ settings,
+):
+ # given
+ webhook = subscription_product_created_webhook
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ mocked_get_webhooks_for_event.return_value = [webhook]
+
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+ product_name = "test name"
+ product_slug = "product-test-slug"
+
+ variables = {
+ "input": {
+ "productType": product_type_id,
+ "category": category_id,
+ "name": product_name,
+ "slug": product_slug,
+ }
+ }
+
+ # when
+ staff_api_client.post_graphql(
+ CREATE_PRODUCT_MUTATION, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ mocked_generate_payload.assert_called_once()
+ assert not mocked_generate_payload.call_args[1]["request"].allow_replica
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 72ff239a2f..c9cc6c5e35 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -116,7 +116,6 @@ from ..payment.utils import create_manual_adjustment_events
from ..permission.enums import get_permissions
from ..permission.models import Permission
from ..plugins.manager import get_plugins_manager
-from ..plugins.webhook.tests.subscription_webhooks import subscription_queries
from ..product import ProductMediaTypes, ProductTypeKind
from ..product.models import (
Category,
@@ -162,6 +161,7 @@ from ..warehouse.models import (
from ..webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ..webhook.models import Webhook, WebhookEvent
from ..webhook.observability import WebhookData
+from ..webhook.tests.subscription_webhooks import subscription_queries
from ..webhook.transport.utils import WebhookResponse, to_payment_app_id
from .utils import dummy_editorjs
diff --git a/saleor/plugins/webhook/tests/test_observability.py b/saleor/webhook/observability/tests/test_observability.py
similarity index 96%
rename from saleor/plugins/webhook/tests/test_observability.py
rename to saleor/webhook/observability/tests/test_observability.py
index 0b0c62f9a8..cfcac03a01 100644
--- a/saleor/plugins/webhook/tests/test_observability.py
+++ b/saleor/webhook/observability/tests/test_observability.py
@@ -3,13 +3,13 @@ from unittest.mock import Mock, patch
from celery.canvas import Signature
from ....core import EventDeliveryStatus
-from ....webhook.event_types import WebhookEventAsyncType
-from ....webhook.observability import concatenate_json_events
-from ....webhook.transport.asynchronous.transport import (
+from ...event_types import WebhookEventAsyncType
+from ...transport.asynchronous.transport import (
observability_reporter_task,
observability_send_events,
send_observability_events,
)
+from .. import concatenate_json_events
@patch("saleor.webhook.transport.asynchronous.transport.group")
diff --git a/saleor/webhook/tests/subscription_webhooks/__init__.py b/saleor/webhook/tests/subscription_webhooks/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py b/saleor/webhook/tests/subscription_webhooks/fixtures.py
similarity index 99%
rename from saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
rename to saleor/webhook/tests/subscription_webhooks/fixtures.py
index 00c6e43beb..4f09280657 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
+++ b/saleor/webhook/tests/subscription_webhooks/fixtures.py
@@ -1,7 +1,7 @@
import pytest
-from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
-from .....webhook.models import Webhook
+from ...event_types import WebhookEventAsyncType, WebhookEventSyncType
+from ...models import Webhook
from . import subscription_queries as queries
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py b/saleor/webhook/tests/subscription_webhooks/payloads.py
similarity index 97%
rename from saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
rename to saleor/webhook/tests/subscription_webhooks/payloads.py
index 0ba778c59b..f80a13e857 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
+++ b/saleor/webhook/tests/subscription_webhooks/payloads.py
@@ -3,13 +3,13 @@ import json
import graphene
from django.utils import timezone
-from ..... import __version__
-from .....core.utils import build_absolute_uri
-from .....discount.models import Promotion
-from .....graphql.attribute.enums import AttributeInputTypeEnum, AttributeTypeEnum
-from .....graphql.discount.utils import get_categories_from_predicate
-from .....graphql.shop.types import SHOP_ID
-from .....product.models import Product
+from .... import __version__
+from ....core.utils import build_absolute_uri
+from ....discount.models import Promotion
+from ....graphql.attribute.enums import AttributeInputTypeEnum, AttributeTypeEnum
+from ....graphql.discount.utils import get_categories_from_predicate
+from ....graphql.shop.types import SHOP_ID
+from ....product.models import Product
def generate_account_events_payload(customer_user):
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py b/saleor/webhook/tests/subscription_webhooks/subscription_queries.py
similarity index 99%
rename from saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
rename to saleor/webhook/tests/subscription_webhooks/subscription_queries.py
index c58ca0f15d..751d711d77 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
+++ b/saleor/webhook/tests/subscription_webhooks/subscription_queries.py
@@ -2,8 +2,8 @@ from enum import Enum
from graphene.utils.str_converters import to_snake_case
-from .....graphql.tests.queries import fragments
-from .....graphql.webhook.subscription_types import TRANSLATIONS_TYPES_MAP
+from ....graphql.tests.queries import fragments
+from ....graphql.webhook.subscription_types import TRANSLATIONS_TYPES_MAP
ACCOUNT_CONFIRMATION_REQUESTED = (
fragments.CUSTOMER_DETAILS
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_list_payment_methods.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_list_payment_methods.py
similarity index 85%
rename from saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_list_payment_methods.py
rename to saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_list_payment_methods.py
index 0a7a44f23b..80fd65435c 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_list_payment_methods.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_list_payment_methods.py
@@ -2,9 +2,9 @@ import json
import graphene
-from .....payment.interface import ListStoredPaymentMethodsRequestData
-from .....webhook.event_types import WebhookEventSyncType
-from .....webhook.transport.asynchronous.transport import (
+from ....payment.interface import ListStoredPaymentMethodsRequestData
+from ...event_types import WebhookEventSyncType
+from ...transport.asynchronous.transport import (
create_deliveries_for_subscriptions,
)
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_gateway_initialize_session.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_gateway_initialize_session.py
similarity index 95%
rename from saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_gateway_initialize_session.py
rename to saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_gateway_initialize_session.py
index cd4459dc2f..a51cee9421 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_gateway_initialize_session.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_gateway_initialize_session.py
@@ -3,11 +3,9 @@ from decimal import Decimal
import graphene
-from .....webhook.event_types import WebhookEventSyncType
-from .....webhook.models import Webhook
-from .....webhook.transport.asynchronous.transport import (
- create_deliveries_for_subscriptions,
-)
+from ...event_types import WebhookEventSyncType
+from ...models import Webhook
+from ...transport.asynchronous import create_deliveries_for_subscriptions
PAYMENT_GATEWAY_INITIALIZE_SESSION = """
subscription {
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_gateway_initialize_tokenization_session.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_gateway_initialize_tokenization_session.py
similarity index 91%
rename from saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_gateway_initialize_tokenization_session.py
rename to saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_gateway_initialize_tokenization_session.py
index 602dfb4834..6e20b1be30 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_gateway_initialize_tokenization_session.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_gateway_initialize_tokenization_session.py
@@ -2,11 +2,9 @@ import json
import graphene
-from .....payment.interface import PaymentGatewayInitializeTokenizationRequestData
-from .....webhook.event_types import WebhookEventSyncType
-from .....webhook.transport.asynchronous.transport import (
- create_deliveries_for_subscriptions,
-)
+from ....payment.interface import PaymentGatewayInitializeTokenizationRequestData
+from ...event_types import WebhookEventSyncType
+from ...transport.asynchronous import create_deliveries_for_subscriptions
PAYMENT_GATEWAY_INITIALIZE_TOKENIZATION = """
subscription {
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_initialize_tokenization.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_initialize_tokenization.py
similarity index 90%
rename from saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_initialize_tokenization.py
rename to saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_initialize_tokenization.py
index d4fb8173b1..d57a11807b 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_initialize_tokenization.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_initialize_tokenization.py
@@ -2,12 +2,10 @@ import json
import graphene
-from .....payment import TokenizedPaymentFlow
-from .....payment.interface import PaymentMethodInitializeTokenizationRequestData
-from .....webhook.event_types import WebhookEventSyncType
-from .....webhook.transport.asynchronous.transport import (
- create_deliveries_for_subscriptions,
-)
+from ....payment import TokenizedPaymentFlow
+from ....payment.interface import PaymentMethodInitializeTokenizationRequestData
+from ...event_types import WebhookEventSyncType
+from ...transport.asynchronous import create_deliveries_for_subscriptions
PAYMENT_METHOD_INITIALIZE_TOKENIZATION_SESSION = """
subscription{
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_process_tokenization.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_process_tokenization.py
similarity index 91%
rename from saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_process_tokenization.py
rename to saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_process_tokenization.py
index 9eb87dbae6..cffcde90ea 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_process_tokenization.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_process_tokenization.py
@@ -2,11 +2,9 @@ import json
import graphene
-from .....payment.interface import PaymentMethodProcessTokenizationRequestData
-from .....webhook.event_types import WebhookEventSyncType
-from .....webhook.transport.asynchronous.transport import (
- create_deliveries_for_subscriptions,
-)
+from ....payment.interface import PaymentMethodProcessTokenizationRequestData
+from ...event_types import WebhookEventSyncType
+from ...transport.asynchronous import create_deliveries_for_subscriptions
PAYMENT_METHOD_PROCESS_TOKENIZATION_SESSION = """
subscription{
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_request_delete.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_request_delete.py
similarity index 84%
rename from saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_request_delete.py
rename to saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_request_delete.py
index f45ef827aa..c12a2d0dbd 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_request_delete.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_payment_method_request_delete.py
@@ -2,11 +2,9 @@ import json
import graphene
-from .....payment.interface import StoredPaymentMethodRequestDeleteData
-from .....webhook.event_types import WebhookEventSyncType
-from .....webhook.transport.asynchronous.transport import (
- create_deliveries_for_subscriptions,
-)
+from ....payment.interface import StoredPaymentMethodRequestDeleteData
+from ...event_types import WebhookEventSyncType
+from ...transport.asynchronous import create_deliveries_for_subscriptions
STORED_PAYMENT_METHOD_DELETE_REQUESTED = """
subscription {
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
similarity index 99%
rename from saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
rename to saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
index 5f5455c5ff..d210623863 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
@@ -7,19 +7,19 @@ import pytest
from django.core.files import File
from freezegun import freeze_time
-from .....channel.models import Channel
-from .....giftcard.models import GiftCard
-from .....graphql.webhook.subscription_query import SubscriptionQuery
-from .....menu.models import Menu, MenuItem
-from .....product.models import Category
-from .....shipping.models import ShippingMethod, ShippingZone
-from .....site.models import SiteSettings
-from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
-from .....webhook.transport.asynchronous.transport import (
+from ....channel.models import Channel
+from ....giftcard.models import GiftCard
+from ....graphql.webhook.subscription_query import SubscriptionQuery
+from ....menu.models import Menu, MenuItem
+from ....product.models import Category
+from ....shipping.models import ShippingMethod, ShippingZone
+from ....site.models import SiteSettings
+from ...event_types import WebhookEventAsyncType, WebhookEventSyncType
+from ...transport.asynchronous.transport import (
create_deliveries_for_subscriptions,
logger,
)
-from .....webhook.transport.synchronous.transport import (
+from ...transport.synchronous.transport import (
create_delivery_for_subscription_sync_event,
)
from . import subscription_queries
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscriptions_payments.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscriptions_payments.py
similarity index 96%
rename from saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscriptions_payments.py
rename to saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscriptions_payments.py
index 0d8be0772b..37f63124db 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscriptions_payments.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscriptions_payments.py
@@ -2,10 +2,8 @@ import json
import graphene
-from .....webhook.event_types import WebhookEventSyncType
-from .....webhook.transport.asynchronous.transport import (
- create_deliveries_for_subscriptions,
-)
+from ...event_types import WebhookEventSyncType
+from ...transport.asynchronous import create_deliveries_for_subscriptions
from .payloads import generate_payment_payload
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.py
similarity index 98%
rename from saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.py
rename to saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.py
index d953e46f06..16766966cd 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.py
@@ -7,19 +7,19 @@ import pytest
from freezegun import freeze_time
from prices import Money, TaxedMoney, fixed_discount
-from .....core.prices import quantize_price
-from .....discount import DiscountValueType, VoucherType
-from .....discount.models import PromotionRule
-from .....graphql.core.utils import to_global_id_or_none
-from .....order import OrderStatus
-from .....order.calculations import fetch_order_prices_if_expired
-from .....order.models import Order
-from .....order.utils import update_discount_for_order_line
-from .....plugins.manager import get_plugins_manager
-from .....tests.fixtures import recalculate_order
-from .....webhook.event_types import WebhookEventSyncType
-from .....webhook.models import Webhook
-from .....webhook.transport.synchronous.transport import (
+from ....core.prices import quantize_price
+from ....discount import DiscountValueType, VoucherType
+from ....discount.models import PromotionRule
+from ....graphql.core.utils import to_global_id_or_none
+from ....order import OrderStatus
+from ....order.calculations import fetch_order_prices_if_expired
+from ....order.models import Order
+from ....order.utils import update_discount_for_order_line
+from ....plugins.manager import get_plugins_manager
+from ....tests.fixtures import recalculate_order
+from ...event_types import WebhookEventSyncType
+from ...models import Webhook
+from ...transport.synchronous.transport import (
create_delivery_for_subscription_sync_event,
)
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_cancelation_requested.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_cancelation_requested.py
similarity index 92%
rename from saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_cancelation_requested.py
rename to saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_cancelation_requested.py
index 1eb37a4e33..3d165ad209 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_cancelation_requested.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_cancelation_requested.py
@@ -5,16 +5,14 @@ import graphene
from django.utils import timezone
from freezegun import freeze_time
-from .....core.prices import quantize_price
-from .....graphql.tests.queries import fragments
-from .....payment import TransactionAction, TransactionEventType
-from .....payment.interface import TransactionActionData
-from .....payment.models import TransactionItem
-from .....webhook.event_types import WebhookEventSyncType
-from .....webhook.models import Webhook
-from .....webhook.transport.asynchronous.transport import (
- create_deliveries_for_subscriptions,
-)
+from ....core.prices import quantize_price
+from ....graphql.tests.queries import fragments
+from ....payment import TransactionAction, TransactionEventType
+from ....payment.interface import TransactionActionData
+from ....payment.models import TransactionItem
+from ...event_types import WebhookEventSyncType
+from ...models import Webhook
+from ...transport.asynchronous import create_deliveries_for_subscriptions
TRANSACTION_CANCELATION_REQUESTED_SUBSCRIPTION = (
fragments.TRANSACTION_ITEM_DETAILS
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_charge_requested.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_charge_requested.py
similarity index 92%
rename from saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_charge_requested.py
rename to saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_charge_requested.py
index 86b6fe23b7..9f823ca50b 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_charge_requested.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_charge_requested.py
@@ -5,16 +5,14 @@ import graphene
from django.utils import timezone
from freezegun import freeze_time
-from .....core.prices import quantize_price
-from .....graphql.tests.queries import fragments
-from .....payment import TransactionAction, TransactionEventType
-from .....payment.interface import TransactionActionData
-from .....payment.models import TransactionItem
-from .....webhook.event_types import WebhookEventSyncType
-from .....webhook.models import Webhook
-from .....webhook.transport.asynchronous.transport import (
- create_deliveries_for_subscriptions,
-)
+from ....core.prices import quantize_price
+from ....graphql.tests.queries import fragments
+from ....payment import TransactionAction, TransactionEventType
+from ....payment.interface import TransactionActionData
+from ....payment.models import TransactionItem
+from ...event_types import WebhookEventSyncType
+from ...models import Webhook
+from ...transport.asynchronous import create_deliveries_for_subscriptions
TRANSACTION_CHARGE_REQUESTED_SUBSCRIPTION = (
fragments.TRANSACTION_ITEM_DETAILS
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_initialize_session.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_initialize_session.py
similarity index 97%
rename from saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_initialize_session.py
rename to saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_initialize_session.py
index eb9c58ed2f..f45fee69a1 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_initialize_session.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_initialize_session.py
@@ -4,17 +4,15 @@ from decimal import Decimal
import graphene
-from .....channel import TransactionFlowStrategy
-from .....payment.interface import (
+from ....channel import TransactionFlowStrategy
+from ....payment.interface import (
PaymentGatewayData,
TransactionProcessActionData,
TransactionSessionData,
)
-from .....webhook.event_types import WebhookEventSyncType
-from .....webhook.models import Webhook
-from .....webhook.transport.asynchronous.transport import (
- create_deliveries_for_subscriptions,
-)
+from ...event_types import WebhookEventSyncType
+from ...models import Webhook
+from ...transport.asynchronous import create_deliveries_for_subscriptions
TRANSACTION_INITIALIZE_SESSION = """
subscription {
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_process_session.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_process_session.py
similarity index 97%
rename from saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_process_session.py
rename to saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_process_session.py
index 40c82a55bb..63f9c957ea 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_process_session.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_process_session.py
@@ -3,17 +3,15 @@ from decimal import Decimal
import graphene
-from .....channel import TransactionFlowStrategy
-from .....payment.interface import (
+from ....channel import TransactionFlowStrategy
+from ....payment.interface import (
PaymentGatewayData,
TransactionProcessActionData,
TransactionSessionData,
)
-from .....webhook.event_types import WebhookEventSyncType
-from .....webhook.models import Webhook
-from .....webhook.transport.asynchronous.transport import (
- create_deliveries_for_subscriptions,
-)
+from ...event_types import WebhookEventSyncType
+from ...models import Webhook
+from ...transport.asynchronous.transport import create_deliveries_for_subscriptions
TRANSACTION_PROCESS_SESSION = """
subscription {
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_refund_requested.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_refund_requested.py
similarity index 95%
rename from saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_refund_requested.py
rename to saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_refund_requested.py
index ffd898a268..fa9b30e29d 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_refund_requested.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_transaction_refund_requested.py
@@ -5,16 +5,14 @@ import graphene
from django.utils import timezone
from freezegun import freeze_time
-from .....core.prices import quantize_price
-from .....graphql.tests.queries import fragments
-from .....payment import TransactionAction, TransactionEventType
-from .....payment.interface import TransactionActionData
-from .....payment.models import TransactionItem
-from .....webhook.event_types import WebhookEventSyncType
-from .....webhook.models import Webhook
-from .....webhook.transport.asynchronous.transport import (
- create_deliveries_for_subscriptions,
-)
+from ....core.prices import quantize_price
+from ....graphql.tests.queries import fragments
+from ....payment import TransactionAction, TransactionEventType
+from ....payment.interface import TransactionActionData
+from ....payment.models import TransactionItem
+from ...event_types import WebhookEventSyncType
+from ...models import Webhook
+from ...transport.asynchronous.transport import create_deliveries_for_subscriptions
TRANSACTION_REFUND_REQUESTED_SUBSCRIPTION = (
fragments.TRANSACTION_ITEM_DETAILS
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_translation_subscription.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_translation_subscription.py
similarity index 99%
rename from saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_translation_subscription.py
rename to saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_translation_subscription.py
index 4a322f5481..e2ea48a9c9 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_translation_subscription.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_translation_subscription.py
@@ -2,10 +2,8 @@ import json
import graphene
-from .....webhook.event_types import WebhookEventAsyncType
-from .....webhook.transport.asynchronous.transport import (
- create_deliveries_for_subscriptions,
-)
+from ...event_types import WebhookEventAsyncType
+from ...transport.asynchronous.transport import create_deliveries_for_subscriptions
def test_translation_created_product(
diff --git a/saleor/webhook/tests/subscription_webhooks/test_webhook.py b/saleor/webhook/tests/subscription_webhooks/test_webhook.py
new file mode 100644
index 0000000000..22b0686b00
--- /dev/null
+++ b/saleor/webhook/tests/subscription_webhooks/test_webhook.py
@@ -0,0 +1,82 @@
+import json
+from unittest import mock
+
+from ....core.models import EventDelivery
+from ...event_types import WebhookEventAsyncType, WebhookEventSyncType
+from ...models import Webhook
+from ...transport.asynchronous import trigger_webhooks_async
+from ...transport.synchronous import trigger_webhook_sync
+from .payloads import generate_payment_payload
+
+
+ "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
+)
+def test_trigger_webhooks_async(
+ mocked_send_webhook_request,
+ webhook,
+ subscription_order_created_webhook,
+ order,
+):
+ webhook_type = WebhookEventAsyncType.ORDER_CREATED
+ webhooks, payload = Webhook.objects.all(), '{"example": "payload"}'
+
+ trigger_webhooks_async(payload, webhook_type, webhooks, order, allow_replica=False)
+
+ deliveries = EventDelivery.objects.all()
+ assert deliveries.count() == 2
+ assert deliveries[0].webhook == subscription_order_created_webhook
+ assert deliveries[1].webhook == webhook
+ for delivery in deliveries:
+ assert (
+ mock.call(
+ kwargs={"event_delivery_id": delivery.id},
+ queue=None,
+ bind=True,
+ retry_backoff=10,
+ retry_kwargs={"max_retries": 5},
+ )
+ in mocked_send_webhook_request.mock_calls
+ )
+
+
+ "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
+)
+ "saleor.webhook.transport.asynchronous.transport.create_deliveries_for_subscriptions"
+)
+def test_trigger_webhooks_async_no_subscription_webhooks(
+ mocked_create_deliveries_for_subscriptions,
+ mocked_send_webhook_request,
+ webhook,
+ order,
+):
+ webhook_type = WebhookEventAsyncType.ORDER_UPDATED
+ webhooks = Webhook.objects.all()
+ data = '{"regular_webhook": "data"}'
+ trigger_webhooks_async(data, webhook_type, webhooks, order, allow_replica=False)
+ mocked_create_deliveries_for_subscriptions.assert_not_called()
+
+
[email protected]("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
+def test_trigger_webhook_sync_with_subscription(
+ mock_request, payment_app_with_subscription_webhooks, payment
+):
+ # given
+ payment_app = payment_app_with_subscription_webhooks
+ data = '{"key": "value"}'
+ expected_payment_payload = generate_payment_payload(payment)
+ # when
+ trigger_webhook_sync(
+ WebhookEventSyncType.PAYMENT_AUTHORIZE,
+ data,
+ payment_app.webhooks.first(),
+ False,
+ payment,
+ )
+ event_delivery = EventDelivery.objects.first()
+
+ # then
+ assert json.loads(event_delivery.payload.get_payload()) == expected_payment_payload
+ mock_request.assert_called_once_with(event_delivery)
diff --git a/saleor/plugins/webhook/tests/test_payment_webhook_utils.py b/saleor/webhook/tests/test_payment_webhook_utils.py
similarity index 97%
rename from saleor/plugins/webhook/tests/test_payment_webhook_utils.py
rename to saleor/webhook/tests/test_payment_webhook_utils.py
index c31a928dba..e76a2f528c 100644
--- a/saleor/plugins/webhook/tests/test_payment_webhook_utils.py
+++ b/saleor/webhook/tests/test_payment_webhook_utils.py
@@ -1,10 +1,10 @@
import pytest
-from ....core import EventDeliveryStatus, private_storage
-from ....core.models import EventDelivery, EventPayload
-from ....payment import TransactionKind
-from ....webhook.const import APP_ID_PREFIX
-from ....webhook.transport.utils import (
+from ...core import EventDeliveryStatus, private_storage
+from ...core.models import EventDelivery, EventPayload
+from ...payment import TransactionKind
+from ..const import APP_ID_PREFIX
+from ..transport.utils import (
clear_successful_delivery,
from_payment_app_id,
parse_list_payment_gateways_response,
diff --git a/saleor/plugins/webhook/tests/test_sync_webhooks_for_removed_app.py b/saleor/webhook/tests/test_sync_webhooks_for_removed_app.py
similarity index 58%
rename from saleor/plugins/webhook/tests/test_sync_webhooks_for_removed_app.py
rename to saleor/webhook/tests/test_sync_webhooks_for_removed_app.py
index 2fd269ef8a..43f724041b 100644
--- a/saleor/plugins/webhook/tests/test_sync_webhooks_for_removed_app.py
+++ b/saleor/webhook/tests/test_sync_webhooks_for_removed_app.py
@@ -2,21 +2,20 @@ from unittest import mock
from django.utils import timezone
-from ....core.models import EventDelivery, EventPayload
-from ....webhook.event_types import WebhookEventSyncType
-from ....webhook.transport.synchronous.transport import trigger_all_webhooks_sync
-from ....webhook.transport.utils import parse_tax_data
+from ...core.models import EventDelivery, EventPayload
+from ..event_types import WebhookEventSyncType
+from ..transport.synchronous.transport import trigger_all_webhooks_sync
+from ..transport.utils import parse_tax_data
@mock.patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
def test_not_trigger_sync_webhook_for_removed_app(
mock_request,
- tax_checkout_webhook,
+ tax_app,
):
# given
- app = tax_checkout_webhook.app
- app.removed_at = timezone.now()
- app.save(update_fields=["removed_at"])
+ tax_app.removed_at = timezone.now()
+ tax_app.save(update_fields=["removed_at"])
event_type = WebhookEventSyncType.CHECKOUT_CALCULATE_TAXES
data = '{"key": "value"}'
diff --git a/saleor/plugins/webhook/tests/test_tasks.py b/saleor/webhook/tests/test_tasks.py
similarity index 98%
rename from saleor/plugins/webhook/tests/test_tasks.py
rename to saleor/webhook/tests/test_tasks.py
index bc55f0ec84..0dc6263188 100644
--- a/saleor/plugins/webhook/tests/test_tasks.py
+++ b/saleor/webhook/tests/test_tasks.py
@@ -8,17 +8,17 @@ from freezegun import freeze_time
from graphene import Node
from requests_hardened import HTTPSession
-from ....core import EventDeliveryStatus
-from ....core.models import EventDelivery, EventPayload
-from ....payment import TransactionEventType
-from ....payment.interface import TransactionActionData
-from ....payment.models import TransactionEvent
-from ....payment.transaction_item_calculations import recalculate_transaction_amounts
-from ....tests.utils import flush_post_commit_hooks
-from ....webhook.event_types import WebhookEventSyncType
-from ....webhook.payloads import generate_transaction_action_request_payload
-from ....webhook.transport.synchronous.transport import handle_transaction_request_task
-from ....webhook.transport.utils import trigger_transaction_request
+from ...core import EventDeliveryStatus
+from ...core.models import EventDelivery, EventPayload
+from ...payment import TransactionEventType
+from ...payment.interface import TransactionActionData
+from ...payment.models import TransactionEvent
+from ...payment.transaction_item_calculations import recalculate_transaction_amounts
+from ...tests.utils import flush_post_commit_hooks
+from ..event_types import WebhookEventSyncType
+from ..payloads import generate_transaction_action_request_payload
+from ..transport.synchronous.transport import handle_transaction_request_task
+from ..transport.utils import trigger_transaction_request
@pytest.fixture
diff --git a/saleor/plugins/webhook/tests/test_tax_webhook_tasks.py b/saleor/webhook/tests/test_tax_webhook_tasks.py
similarity index 88%
rename from saleor/plugins/webhook/tests/test_tax_webhook_tasks.py
rename to saleor/webhook/tests/test_tax_webhook_tasks.py
index 1277216d68..3f0b1fdb9b 100644
--- a/saleor/plugins/webhook/tests/test_tax_webhook_tasks.py
+++ b/saleor/webhook/tests/test_tax_webhook_tasks.py
@@ -2,16 +2,17 @@ from unittest import mock
import pytest
-from ....core import EventDeliveryStatus
-from ....core.models import EventDelivery, EventPayload
-from ....webhook.event_types import WebhookEventSyncType
-from ....webhook.models import Webhook, WebhookEvent
-from ....webhook.transport.synchronous.transport import trigger_all_webhooks_sync
-from ....webhook.transport.utils import parse_tax_data
+from ...core import EventDeliveryStatus
+from ...core.models import EventDelivery, EventPayload
+from ..event_types import WebhookEventSyncType
+from ..models import Webhook, WebhookEvent
+from ..transport.synchronous import trigger_all_webhooks_sync
+from ..transport.utils import parse_tax_data
@pytest.fixture
def tax_checkout_webhooks(tax_app):
+ tax_app.webhooks.all().delete()
webhooks = [
Webhook(
name=f"Tax checkout webhook no {i}",
@@ -39,13 +40,16 @@ def tax_checkout_webhooks(tax_app):
@mock.patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
def test_trigger_tax_webhook_sync(
mock_request,
- tax_checkout_webhook,
+ tax_app,
tax_data_response,
):
# given
mock_request.return_value = tax_data_response
event_type = WebhookEventSyncType.CHECKOUT_CALCULATE_TAXES
data = '{"key": "value"}'
+ webhook = tax_app.webhooks.get(name="tax-webhook-1")
+ webhook.subscription_query = None
+ webhook.save(update_fields=["subscription_query"])
# when
tax_data = trigger_all_webhooks_sync(event_type, lambda: data, parse_tax_data)
@@ -57,7 +61,7 @@ def test_trigger_tax_webhook_sync(
assert delivery.status == EventDeliveryStatus.PENDING
assert delivery.event_type == event_type
assert delivery.payload == payload
- assert delivery.webhook == tax_checkout_webhook
+ assert delivery.webhook == webhook
mock_request.assert_called_once_with(delivery)
assert tax_data == parse_tax_data(tax_data_response)
diff --git a/saleor/plugins/webhook/tests/test_tax_webhook_utils.py b/saleor/webhook/tests/test_tax_webhook_utils.py
similarity index 97%
rename from saleor/plugins/webhook/tests/test_tax_webhook_utils.py
rename to saleor/webhook/tests/test_tax_webhook_utils.py
index 8a34a87cd1..af0e867aba 100644
--- a/saleor/plugins/webhook/tests/test_tax_webhook_utils.py
+++ b/saleor/webhook/tests/test_tax_webhook_utils.py
@@ -2,8 +2,8 @@ import decimal
import pytest
-from ....core.taxes import TaxData
-from ....webhook.transport.utils import (
+from ...core.taxes import TaxData
+from ..transport.utils import (
_unsafe_parse_tax_data,
_unsafe_parse_tax_line_data,
parse_tax_data,
diff --git a/saleor/webhook/tests/test_utils.py b/saleor/webhook/tests/test_utils.py
index 9dc0fbe9e7..0ede1a0d67 100644
--- a/saleor/webhook/tests/test_utils.py
+++ b/saleor/webhook/tests/test_utils.py
@@ -1,6 +1,9 @@
+import copy
+
import pytest
from ...app.models import App
+from ...payment.interface import PaymentGateway
from ..event_types import WebhookEventAsyncType, WebhookEventSyncType
from ..models import Webhook
from ..observability.exceptions import (
@@ -9,6 +12,15 @@ from ..observability.exceptions import (
TruncationError,
)
from ..observability.payload_schema import ObservabilityEventTypes
+from ..transport.list_stored_payment_methods import (
+ get_credit_card_info,
+ get_list_stored_payment_methods_from_response,
+ get_payment_method_from_response,
+)
+from ..transport.utils import (
+ generate_cache_key_for_webhook,
+ to_payment_app_id,
+)
from ..utils import get_webhooks_for_event, get_webhooks_for_multiple_events
@@ -211,3 +223,391 @@ def test_get_webhooks_for_multiple_events(
shipping_filter_webhook
},
}
+
+
+def payment_method_response():
+ return {
+ "id": "method-1",
+ "supportedPaymentFlows": ["INTERACTIVE"],
+ "type": "Credit Card",
+ "creditCardInfo": {
+ "brand": "visa",
+ "lastDigits": "1234",
+ "expMonth": 1,
+ "expYear": 2023,
+ "firstDigits": "123456",
+ },
+ "name": "***1234",
+ "data": {"some": "data"},
+ }
+
+
+def test_different_target_urls_produce_different_cache_key(checkout_with_item):
+ # given
+ target_url_1 = "http://example.com/1"
+ target_url_2 = "http://example.com/2"
+
+ payload = {"field": "1", "field2": "2"}
+
+ # when
+ cache_key_1 = generate_cache_key_for_webhook(
+ payload,
+ target_url_1,
+ WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
+ 1,
+ )
+ cache_key_2 = generate_cache_key_for_webhook(
+ payload,
+ target_url_2,
+ WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
+ 1,
+ )
+
+ # then
+ assert cache_key_1 != cache_key_2
+
+
+def test_different_payload_produce_different_cache_key(checkout_with_item):
+ # given
+ target_url = "http://example.com/1"
+
+ payload_1 = {"field": "1", "field2": "2"}
+ payload_2 = {"field": "1", "field2": "3"}
+
+ # when
+ cache_key_1 = generate_cache_key_for_webhook(
+ payload_1,
+ target_url,
+ WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
+ 1,
+ )
+ cache_key_2 = generate_cache_key_for_webhook(
+ payload_2,
+ target_url,
+ WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
+ 1,
+ )
+
+ # then
+ assert cache_key_1 != cache_key_2
+
+
+def test_different_event_produce_different_cache_key(checkout_with_item):
+ # given
+ target_url = "http://example.com/1"
+
+ payload = {"field": "1", "field2": "2"}
+
+ # when
+ cache_key_1 = generate_cache_key_for_webhook(
+ payload, target_url, WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT, 1
+ )
+ cache_key_2 = generate_cache_key_for_webhook(
+ payload, target_url, WebhookEventSyncType.LIST_STORED_PAYMENT_METHODS, 1
+ )
+
+ # then
+ assert cache_key_1 != cache_key_2
+
+
+def test_different_app_produce_different_cache_key():
+ # given
+ target_url = "http://example.com/1"
+ first_app_id = 1
+ second_app_id = 2
+ payload = {"field": "1", "field2": "2"}
+
+ # when
+ cache_key_1 = generate_cache_key_for_webhook(
+ payload,
+ target_url,
+ WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
+ first_app_id,
+ )
+ cache_key_2 = generate_cache_key_for_webhook(
+ payload,
+ target_url,
+ WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
+ second_app_id,
+ )
+
+ # then
+ assert cache_key_1 != cache_key_2
+
+
+def test_get_credit_card_info(app):
+ # given
+ brand = "VISA"
+ last_digits = "1234"
+ exp_year = "2023"
+ exp_month = 1
+ first_digits = "4321"
+
+ credit_card_info = {
+ "brand": brand,
+ "lastDigits": last_digits,
+ "expYear": exp_year,
+ "expMonth": exp_month,
+ "firstDigits": first_digits,
+ }
+
+ # when
+ response = get_credit_card_info(app, credit_card_info)
+
+ # then
+ assert response.brand == brand
+ assert response.last_digits == last_digits
+ assert response.exp_year == int(exp_year)
+ assert response.exp_month == exp_month
+ assert response.first_digits == first_digits
+
+
+def test_get_credit_card_info_without_first_digits_field(app):
+ # given
+ brand = "VISA"
+ last_digits = "1234"
+ exp_year = 2023
+ exp_month = 1
+
+ credit_card_info = {
+ "brand": brand,
+ "lastDigits": last_digits,
+ "expYear": exp_year,
+ "expMonth": exp_month,
+ }
+
+ # when
+ response = get_credit_card_info(app, credit_card_info)
+
+ # then
+ assert response.brand == brand
+ assert response.last_digits == last_digits
+ assert response.exp_year == exp_year
+ assert response.exp_month == exp_month
+ assert response.first_digits is None
+
+
+ "field",
+ [
+ "brand",
+ "lastDigits",
+ "expYear",
+ "expMonth",
+ ],
+)
+def test_get_credit_card_info_missing_required_field(field, app):
+ # given
+ brand = "VISA"
+ last_digits = "1234"
+ exp_year = 2023
+ exp_month = 1
+ first_digits = "4321"
+
+ credit_card_info = {
+ "brand": brand,
+ "lastDigits": last_digits,
+ "expYear": exp_year,
+ "expMonth": exp_month,
+ "firstDigits": first_digits,
+ }
+ del credit_card_info[field]
+
+ # when
+ response = get_credit_card_info(app, credit_card_info)
+
+ # then
+ assert response is None
+
+
+ "field",
+ [
+ "brand",
+ "lastDigits",
+ "expYear",
+ "expMonth",
+ ],
+)
+def test_get_credit_card_info_required_field_is_none(field, app):
+ # given
+ brand = "VISA"
+ last_digits = "1234"
+ exp_year = 2023
+ exp_month = 1
+ first_digits = "4321"
+
+ credit_card_info = {
+ "brand": brand,
+ "lastDigits": last_digits,
+ "expYear": exp_year,
+ "expMonth": exp_month,
+ "firstDigits": first_digits,
+ }
+ credit_card_info[field] = None
+
+ # when
+ response = get_credit_card_info(app, credit_card_info)
+
+ # then
+ assert response is None
+
+
[email protected]("exp_year", [None, "", "str"])
+def test_get_credit_card_info_incorrect_exp_year(exp_year, app):
+ # given
+ brand = "VISA"
+ last_digits = "1234"
+ exp_month = 1
+ first_digits = "4321"
+
+ credit_card_info = {
+ "brand": brand,
+ "lastDigits": last_digits,
+ "expYear": exp_year,
+ "expMonth": exp_month,
+ "firstDigits": first_digits,
+ }
+
+ # when
+ response = get_credit_card_info(app, credit_card_info)
+
+ # then
+ assert response is None
+
+
[email protected]("exp_month", [None, "", "str"])
+def test_get_credit_card_info_incorrect_exp_month(exp_month, app):
+ # given
+ brand = "VISA"
+ last_digits = "1234"
+ exp_year = 2023
+ first_digits = "4321"
+
+ credit_card_info = {
+ "brand": brand,
+ "lastDigits": last_digits,
+ "expYear": exp_year,
+ "expMonth": exp_month,
+ "firstDigits": first_digits,
+ }
+
+ # when
+ response = get_credit_card_info(app, credit_card_info)
+
+ # then
+ assert response is None
+
+
+def test_get_payment_method_from_response(payment_method_response, app):
+ # when
+ payment_method = get_payment_method_from_response(
+ app, payment_method_response, "usd"
+ )
+
+ # then
+ assert payment_method.id == to_payment_app_id(app, payment_method_response["id"])
+ assert payment_method.external_id == payment_method_response["id"]
+ assert payment_method.type == payment_method_response["type"]
+ assert payment_method.gateway == PaymentGateway(
+ id=app.identifier, name=app.name, currencies=["usd"], config=[]
+ )
+ assert payment_method.supported_payment_flows == [
+ flow.lower() for flow in payment_method_response["supportedPaymentFlows"]
+ ]
+ assert payment_method.credit_card_info == get_credit_card_info(
+ app, payment_method_response["creditCardInfo"]
+ )
+ assert payment_method.name == payment_method_response["name"]
+ assert payment_method.data == payment_method_response["data"]
+
+
[email protected]("field", ["id", "type", "supportedPaymentFlows"])
+def test_get_payment_method_from_response_missing_required_field(
+ field, payment_method_response, app
+):
+ # given
+ del payment_method_response[field]
+
+ # when
+ payment_method = get_payment_method_from_response(
+ app, payment_method_response, "usd"
+ )
+
+ # then
+ assert payment_method is None
+
+
[email protected]("field", ["creditCardInfo", "name", "data"])
+def test_get_payment_method_from_response_optional_field(
+ field, payment_method_response, app
+):
+ del payment_method_response[field]
+
+ # when
+ payment_method = get_payment_method_from_response(
+ app, payment_method_response, "usd"
+ )
+
+ # then
+ assert payment_method.id == to_payment_app_id(app, payment_method_response["id"])
+ assert payment_method.external_id == payment_method_response["id"]
+ assert payment_method.type == payment_method_response["type"]
+ assert payment_method.gateway == PaymentGateway(
+ id=app.identifier, name=app.name, currencies=["usd"], config=[]
+ )
+ assert payment_method.supported_payment_flows == [
+ flow.lower() for flow in payment_method_response["supportedPaymentFlows"]
+ ]
+
+
[email protected]("field", ["id", "type", "supportedPaymentFlows"])
+def test_get_payment_method_from_response_required_field_is_none(
+ field, payment_method_response, app
+):
+ # given
+ payment_method_response[field] = None
+
+ # when
+ payment_method = get_payment_method_from_response(
+ app, payment_method_response, "usd"
+ )
+
+ # then
+ assert payment_method is None
+
+
+def test_get_payment_method_from_response_incorrect_payment_flow_choices(
+ payment_method_response, app
+):
+ # given
+ payment_method_response["supportedPaymentFlows"] = ["incorrect", "INTERACTIVE"]
+
+ # when
+ payment_method = get_payment_method_from_response(
+ app, payment_method_response, "usd"
+ )
+
+ # then
+ assert payment_method is None
+
+
+def test_get_list_stored_payment_methods_from_response(payment_method_response, app):
+ # given
+ second_payment_method = copy.deepcopy(payment_method_response)
+ del second_payment_method["id"]
+ list_stored_payment_methods_response = {
+ "paymentMethods": [payment_method_response, second_payment_method]
+ }
+
+ # when
+ response = get_list_stored_payment_methods_from_response(
+ app, list_stored_payment_methods_response, "usd"
+ )
+
+ # then
+ assert len(response) == 1
+ assert response == [
+ get_payment_method_from_response(app, payment_method_response, "usd")
+ ]
diff --git a/saleor/plugins/webhook/tests/test_webhook_ip_filtering.py b/saleor/webhook/tests/test_webhook_ip_filtering.py
similarity index 82%
rename from saleor/plugins/webhook/tests/test_webhook_ip_filtering.py
rename to saleor/webhook/tests/test_webhook_ip_filtering.py
index ccf31436db..5998f21215 100644
--- a/saleor/plugins/webhook/tests/test_webhook_ip_filtering.py
+++ b/saleor/webhook/tests/test_webhook_ip_filtering.py
@@ -1,6 +1,6 @@
-from ....core.http_client import HTTPClient
-from ....core.models import EventDeliveryAttempt
-from ....webhook.transport.asynchronous.transport import trigger_webhooks_async
+from ...core.http_client import HTTPClient
+from ...core.models import EventDeliveryAttempt
+from ..transport.asynchronous import trigger_webhooks_async
def test_rejects_private_ips(webhook, monkeypatch):
diff --git a/saleor/plugins/webhook/tests/test_webhook_protocols.py b/saleor/webhook/tests/test_webhook_protocols.py
similarity index 98%
rename from saleor/plugins/webhook/tests/test_webhook_protocols.py
rename to saleor/webhook/tests/test_webhook_protocols.py
index 0cbcdf6581..0d3db9ef70 100644
--- a/saleor/plugins/webhook/tests/test_webhook_protocols.py
+++ b/saleor/webhook/tests/test_webhook_protocols.py
@@ -9,10 +9,10 @@ from django.core.serializers import serialize
from google.cloud.pubsub_v1 import PublisherClient
from requests_hardened import HTTPSession
-from ....core.models import EventDelivery
-from ....webhook.event_types import WebhookEventAsyncType
-from ....webhook.transport import signature_for_payload
-from ....webhook.transport.asynchronous.transport import trigger_webhooks_async
+from ...core.models import EventDelivery
+from ..event_types import WebhookEventAsyncType
+from ..transport import signature_for_payload
+from ..transport.asynchronous import trigger_webhooks_async
@pytest.mark.parametrize(
diff --git a/saleor/plugins/webhook/tests/test_webhook_tasks.py b/saleor/webhook/tests/test_webhook_tasks.py
similarity index 83%
rename from saleor/plugins/webhook/tests/test_webhook_tasks.py
rename to saleor/webhook/tests/test_webhook_tasks.py
index c99b403fb6..a2b55439d2 100644
--- a/saleor/plugins/webhook/tests/test_webhook_tasks.py
+++ b/saleor/webhook/tests/test_webhook_tasks.py
@@ -1,9 +1,9 @@
import pytest
-from ....app.models import App
-from ....webhook.event_types import WebhookEventAsyncType
-from ....webhook.models import Webhook, WebhookEvent
-from ....webhook.utils import get_webhooks_for_event
+from ...app.models import App
+from ..event_types import WebhookEventAsyncType
+from ..models import Webhook, WebhookEvent
+from ..utils import get_webhooks_for_event
@pytest.fixture
diff --git a/saleor/webhook/transport/asynchronous/__init__.py b/saleor/webhook/transport/asynchronous/__init__.py
index e69de29bb2..808720f94b 100644
--- a/saleor/webhook/transport/asynchronous/__init__.py
+++ b/saleor/webhook/transport/asynchronous/__init__.py
@@ -0,0 +1,11 @@
+from .transport import (
+ create_deliveries_for_subscriptions,
+ send_webhook_request_async,
+ trigger_webhooks_async,
+)
+
+__all__ = [
+ "create_deliveries_for_subscriptions",
+ "trigger_webhooks_async",
+ "send_webhook_request_async",
+]
diff --git a/saleor/webhook/transport/synchronous/__init__.py b/saleor/webhook/transport/synchronous/__init__.py
index e69de29bb2..91a0906f5c 100644
--- a/saleor/webhook/transport/synchronous/__init__.py
+++ b/saleor/webhook/transport/synchronous/__init__.py
@@ -0,0 +1,11 @@
+from .transport import (
+ trigger_all_webhooks_sync,
+ trigger_webhook_sync,
+ trigger_webhook_sync_if_not_cached,
+)
+
+__all__ = [
+ "trigger_all_webhooks_sync",
+ "trigger_webhook_sync",
+ "trigger_webhook_sync_if_not_cached",
+]