Skip to content

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

Add some tests for sqs and google pub sub

Maciej Korycinskieca19e8bf3bf
diff --git a/.isort.cfg b/.isort.cfg
index e81cdb9065..12b48e10d6 100644
--- a/.isort.cfg
+++ b/.isort.cfg
@@ -1,2 +1,2 @@
[settings]
-known_third_party = PIL,babel,boto3,braintree,celery,dj_database_url,dj_email_url,django,django_cache_url,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,geolite2,google,google_measurement_protocol,graphene,graphene_django,graphene_federation,graphql,graphql_relay,html_to_draftjs,i18naddress,jaeger_client,jwt,markdown,measurement,mptt,oauthlib,openpyxl,opentracing,petl,phonenumber_field,phonenumbers,pkg_resources,prices,promise,pytest,pythonjsonlogger,pytimeparse,pytz,razorpay,requests,sentry_sdk,storages,stripe,templated_email,tqdm,versatileimagefield,weasyprint
+known_third_party = PIL,babel,boto3,braintree,celery,dj_database_url,dj_email_url,django,django_cache_url,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,geolite2,google,google_measurement_protocol,graphene,graphene_django,graphene_federation,graphql,graphql_relay,html_to_draftjs,i18naddress,jaeger_client,jwt,kombu,markdown,measurement,mptt,oauthlib,openpyxl,opentracing,petl,phonenumber_field,phonenumbers,pkg_resources,prices,promise,pytest,pythonjsonlogger,pytimeparse,pytz,razorpay,requests,sentry_sdk,storages,stripe,templated_email,tqdm,versatileimagefield,weasyprint
diff --git a/saleor/plugins/webhook/tasks.py b/saleor/plugins/webhook/tasks.py
index 2ede935e88..7b1d58374a 100644
--- a/saleor/plugins/webhook/tasks.py
+++ b/saleor/plugins/webhook/tasks.py
@@ -83,13 +83,16 @@ def send_webhook_using_aws_sqs(target_url, message, domain, signature, event_typ
queue_url = urlunparse(
("https", parts.hostname, parts.path, parts.params, parts.query, parts.fragment)
)
+
+ msg_attributes = {
+ "SaleorDomain": {"DataType": "String", "StringValue": domain},
+ "EventType": {"DataType": "String", "StringValue": event_type},
+ }
+ if signature:
+ msg_attributes["Signature"] = {"DataType": "String", "StringValue": signature}
client.send_message(
QueueUrl=queue_url,
- MessageAttributes={
- "SaleorDomain": {"DataType": "String", "StringValue": domain},
- "EventType": {"DataType": "String", "StringValue": event_type},
- "Signature": {"DataType": "String", "StringValue": signature},
- },
+ MessageAttributes=msg_attributes,
MessageBody=message.decode("utf-8"),
)
diff --git a/saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_for_event.yaml b/saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_with_http.yaml
similarity index 100%
rename from saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_for_event.yaml
rename to saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_with_http.yaml
diff --git a/saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_for_event_with_secret_key.yaml b/saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_with_http_and_secret_key.yaml
similarity index 100%
rename from saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_for_event_with_secret_key.yaml
rename to saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_with_http_and_secret_key.yaml
diff --git a/saleor/plugins/webhook/tests/test_webhook.py b/saleor/plugins/webhook/tests/test_webhook.py
index 0eb1852325..d8902b1dbd 100644
--- a/saleor/plugins/webhook/tests/test_webhook.py
+++ b/saleor/plugins/webhook/tests/test_webhook.py
@@ -1,8 +1,6 @@
from unittest import mock
import pytest
-import requests
-from django.core.serializers import serialize
from ....app.models import App
from ....webhook.event_types import WebhookEventType
@@ -14,7 +12,6 @@ from ....webhook.payloads import (
generate_product_payload,
)
from ...manager import get_plugins_manager
-from ...webhook import signature_for_payload
from ...webhook.tasks import trigger_webhooks_for_event
first_url = "http://www.example.com/first/"
@@ -77,38 +74,6 @@ def test_trigger_webhooks_for_event_calls_expected_events(
assert target_url_calls == expected_target_urls
[email protected]("saleor.plugins.webhook.tasks.requests.post", wraps=requests.post)
-def test_trigger_webhooks_for_event_with_secret_key(
- mock_request, webhook, order_with_lines, permission_manage_orders
-):
- webhook.app.permissions.add(permission_manage_orders)
- webhook.target_url = "https://webhook.site/48978b64-4efb-43d5-a334-451a1d164009"
- webhook.secret_key = "secret_key"
- webhook.save()
-
- expected_data = serialize("json", [order_with_lines])
- trigger_webhooks_for_event(WebhookEventType.ORDER_CREATED, expected_data)
-
- expected_signature = signature_for_payload(
- expected_data.encode("utf-8"), webhook.secret_key
- )
- expected_headers = {
- "Content-Type": "application/json",
- "X-Saleor-Event": "order_created",
- "X-Saleor-Domain": "mirumee.com",
- "X-Saleor-Signature": expected_signature,
- "X-Saleor-HMAC-SHA256": f"sha1={expected_signature}",
- }
-
- mock_request.assert_called_once_with(
- webhook.target_url,
- data=bytes(expected_data, "utf-8"),
- headers=expected_headers,
- timeout=10,
- )
-
-
@mock.patch("saleor.plugins.webhook.plugin.trigger_webhooks_for_event.delay")
def test_order_created(mocked_webhook_trigger, settings, order_with_lines):
settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
diff --git a/saleor/plugins/webhook/tests/test_webhook_protocols.py b/saleor/plugins/webhook/tests/test_webhook_protocols.py
index 8774165e73..cc136356ec 100644
--- a/saleor/plugins/webhook/tests/test_webhook_protocols.py
+++ b/saleor/plugins/webhook/tests/test_webhook_protocols.py
@@ -1,13 +1,114 @@
from unittest.mock import MagicMock, patch
+import boto3
import pytest
import requests
from django.core.serializers import serialize
+from google.cloud.pubsub_v1 import PublisherClient
+from kombu.asynchronous.aws.sqs.connection import AsyncSQSConnection
from ....webhook.event_types import WebhookEventType
+from ...webhook import signature_for_payload
from ...webhook.tasks import trigger_webhooks_for_event
+def test_trigger_webhooks_with_aws_sqs(
+ webhook,
+ order_with_lines,
+ permission_manage_orders,
+ permission_manage_users,
+ permission_manage_products,
+ monkeypatch,
+):
+ mocked_client = MagicMock(spec=AsyncSQSConnection)
+ mocked_client_constructor = MagicMock(spec=boto3.client, return_value=mocked_client)
+
+ monkeypatch.setattr(
+ "saleor.plugins.webhook.tasks.boto3.client", mocked_client_constructor,
+ )
+
+ webhook.app.permissions.add(permission_manage_orders)
+ access_key = "access_key_id"
+ secret_key = "secret_access"
+ region = "us-east-1"
+
+ webhook.target_url = (
+ f"awssqs://{access_key}:{secret_key}@sqs.{region}.amazonaws.com/account_id/"
+ f"queue_name"
+ )
+ webhook.save()
+
+ expected_data = serialize("json", [order_with_lines])
+
+ trigger_webhooks_for_event(WebhookEventType.ORDER_CREATED, expected_data)
+
+ mocked_client_constructor.assert_called_once_with(
+ "sqs",
+ region_name=region,
+ aws_access_key_id=access_key,
+ aws_secret_access_key=secret_key,
+ )
+ mocked_client.send_message.assert_called_once_with(
+ QueueUrl="https://sqs.us-east-1.amazonaws.com/account_id/queue_name",
+ MessageAttributes={
+ "SaleorDomain": {"DataType": "String", "StringValue": "mirumee.com"},
+ "EventType": {"DataType": "String", "StringValue": "order_created"},
+ },
+ MessageBody=expected_data,
+ )
+
+
+def test_trigger_webhooks_with_aws_sqs_and_secret_key(
+ webhook,
+ order_with_lines,
+ permission_manage_orders,
+ permission_manage_users,
+ permission_manage_products,
+ monkeypatch,
+):
+ mocked_client = MagicMock(spec=AsyncSQSConnection)
+ mocked_client_constructor = MagicMock(spec=boto3.client, return_value=mocked_client)
+
+ monkeypatch.setattr(
+ "saleor.plugins.webhook.tasks.boto3.client", mocked_client_constructor,
+ )
+
+ webhook.app.permissions.add(permission_manage_orders)
+ access_key = "access_key_id"
+ secret_key = "secret_access"
+ region = "us-east-1"
+
+ webhook.target_url = (
+ f"awssqs://{access_key}:{secret_key}@sqs.{region}.amazonaws.com/account_id/"
+ f"queue_name"
+ )
+ webhook.secret_key = "secret_key"
+ webhook.save()
+
+ expected_data = serialize("json", [order_with_lines])
+ expected_signature = signature_for_payload(
+ expected_data.encode("utf-8"), webhook.secret_key
+ )
+
+ trigger_webhooks_for_event(WebhookEventType.ORDER_CREATED, expected_data)
+
+ mocked_client_constructor.assert_called_once_with(
+ "sqs",
+ region_name=region,
+ aws_access_key_id=access_key,
+ aws_secret_access_key=secret_key,
+ )
+ mocked_client.send_message.assert_called_once_with(
+ QueueUrl="https://sqs.us-east-1.amazonaws.com/account_id/queue_name",
+ MessageAttributes={
+ "SaleorDomain": {"DataType": "String", "StringValue": "mirumee.com"},
+ "EventType": {"DataType": "String", "StringValue": "order_created"},
+ "Signature": {"DataType": "String", "StringValue": expected_signature},
+ },
+ MessageBody=expected_data,
+ )
+
+
def test_trigger_webhooks_with_google_pub_sub(
webhook,
order_with_lines,
@@ -16,7 +117,7 @@ def test_trigger_webhooks_with_google_pub_sub(
permission_manage_products,
monkeypatch,
):
- mocked_publisher = MagicMock()
+ mocked_publisher = MagicMock(spec=PublisherClient)
monkeypatch.setattr(
"saleor.plugins.webhook.tasks.pubsub_v1.PublisherClient",
lambda: mocked_publisher,
@@ -37,6 +138,38 @@ def test_trigger_webhooks_with_google_pub_sub(
)
+def test_trigger_webhooks_with_google_pub_sub_and_secret_key(
+ webhook,
+ order_with_lines,
+ permission_manage_orders,
+ permission_manage_users,
+ permission_manage_products,
+ monkeypatch,
+):
+ mocked_publisher = MagicMock(spec=PublisherClient)
+ monkeypatch.setattr(
+ "saleor.plugins.webhook.tasks.pubsub_v1.PublisherClient",
+ lambda: mocked_publisher,
+ )
+ webhook.app.permissions.add(permission_manage_orders)
+ webhook.target_url = "gcpubsub://cloud.google.com/projects/saleor/topics/test"
+ webhook.secret_key = "secret_key"
+ webhook.save()
+
+ expected_data = serialize("json", [order_with_lines])
+ expected_signature = signature_for_payload(
+ expected_data.encode("utf-8"), webhook.secret_key
+ )
+ trigger_webhooks_for_event(WebhookEventType.ORDER_CREATED, expected_data)
+ mocked_publisher.publish.assert_called_once_with(
+ "projects/saleor/topics/test",
+ expected_data.encode("utf-8"),
+ saleorDomain="mirumee.com",
+ eventType=WebhookEventType.ORDER_CREATED,
+ signature=expected_signature,
+ )
+
+
@pytest.mark.vcr
@patch("saleor.plugins.webhook.tasks.requests.post", wraps=requests.post)
def test_trigger_webhooks_with_http(
@@ -68,3 +201,35 @@ def test_trigger_webhooks_with_http(
headers=expected_headers,
timeout=10,
)
+
+
+@patch("saleor.plugins.webhook.tasks.requests.post", wraps=requests.post)
+def test_trigger_webhooks_with_http_and_secret_key(
+ mock_request, webhook, order_with_lines, permission_manage_orders
+):
+ webhook.app.permissions.add(permission_manage_orders)
+ webhook.target_url = "https://webhook.site/48978b64-4efb-43d5-a334-451a1d164009"
+ webhook.secret_key = "secret_key"
+ webhook.save()
+
+ expected_data = serialize("json", [order_with_lines])
+ trigger_webhooks_for_event(WebhookEventType.ORDER_CREATED, expected_data)
+
+ expected_signature = signature_for_payload(
+ expected_data.encode("utf-8"), webhook.secret_key
+ )
+ expected_headers = {
+ "Content-Type": "application/json",
+ "X-Saleor-Event": "order_created",
+ "X-Saleor-Domain": "mirumee.com",
+ "X-Saleor-Signature": expected_signature,
+ "X-Saleor-HMAC-SHA256": f"sha1={expected_signature}",
+ }
+
+ mock_request.assert_called_once_with(
+ webhook.target_url,
+ data=bytes(expected_data, "utf-8"),
+ headers=expected_headers,
+ timeout=10,
+ )