Skip to content

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

Add test for google pub sub

Maciej Korycinskifeb42fb879c0
diff --git a/saleor/plugins/webhook/__init__.py b/saleor/plugins/webhook/__init__.py
index 244a00da3a..06141d4fa6 100644
--- a/saleor/plugins/webhook/__init__.py
+++ b/saleor/plugins/webhook/__init__.py
@@ -3,5 +3,7 @@ import hmac
def signature_for_payload(body: bytes, secret_key):
+ if not secret_key:
+ return ""
hash = hmac.new(bytes(secret_key, "utf-8"), body, hashlib.sha256)
return hash.hexdigest()
diff --git a/saleor/plugins/webhook/tasks.py b/saleor/plugins/webhook/tasks.py
index 66fc35392f..2ede935e88 100644
--- a/saleor/plugins/webhook/tasks.py
+++ b/saleor/plugins/webhook/tasks.py
@@ -1,4 +1,3 @@
-import json
import logging
from enum import Enum
from urllib.parse import urlparse, urlunparse
@@ -56,8 +55,13 @@ def send_webhook_using_http(target_url, message, domain, signature, event_type):
"Content-Type": "application/json",
"X-Saleor-Event": event_type,
"X-Saleor-Domain": domain,
- "X-Saleor-HMAC-SHA256": signature,
+ "X-Saleor-Signature": signature,
}
+
+ if signature:
+ # This header is depreceated and will be removed in Saleor3.0
+ headers["X-Saleor-HMAC-SHA256"] = f"sha1={signature}"
+
response = requests.post(
target_url, data=message, headers=headers, timeout=WEBHOOK_TIMEOUT
)
@@ -113,7 +117,7 @@ def send_webhook_using_google_cloud_pubsub(
def send_webhook_request(webhook_id, target_url, secret, event_type, data):
parts = urlparse(target_url)
domain = Site.objects.get_current().domain
- message = json.dumps(data).encode("utf-8")
+ message = data.encode("utf-8")
signature = signature_for_payload(message, secret)
if parts.scheme.lower() in [WebhookSchemes.HTTP, WebhookSchemes.HTTPS]:
send_webhook_using_http(target_url, message, domain, signature, event_type)
diff --git a/saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_for_event.yaml b/saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_for_event.yaml
index d719bc601a..ede1e07424 100644
--- a/saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_for_event.yaml
+++ b/saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_for_event.yaml
@@ -1,11 +1,11 @@
interactions:
- request:
- body: '[{"model": "order.order", "pk": 18, "fields": {"private_metadata": {},
- "metadata": {}, "created": "2020-07-10T12:33:07.638Z", "status": "unfulfilled",
- "user": 19, "language_code": "en", "tracking_client_id": "", "billing_address":
- 75, "shipping_address": 76, "user_email": "[email protected]", "currency": "USD",
- "shipping_method": 20, "shipping_method_name": "DHL", "shipping_price_net_amount":
- "10.00", "shipping_price_gross_amount": "12.30", "token": "cabcc857-e7ba-4415-9bf8-56b1a8d805a5",
+ 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",
@@ -18,7 +18,7 @@ interactions:
Connection:
- keep-alive
Content-Length:
- - '766'
+ - '771'
Content-Type:
- application/json
User-Agent:
@@ -27,8 +27,10 @@ interactions:
- mirumee.com
X-Saleor-Event:
- order_created
+ X-Saleor-Signature:
+ - ''
method: POST
- uri: https://webhook.site/75048668-439a-4e54-853b-6f081a6fb6cf
+ uri: https://webhook.site/48978b64-4efb-43d5-a334-451a1d164009
response:
body:
string: !!binary |
@@ -41,20 +43,20 @@ interactions:
Content-Type:
- text/plain; charset=UTF-8
Date:
- - Fri, 10 Jul 2020 12:33:08 GMT
+ - Fri, 07 Aug 2020 09:05:59 GMT
Server:
- nginx/1.14.2
Set-Cookie:
- - laravel_session=PVibhFdXrFzqlnOPwr4CMbPpCR9PqFxSBeisJ5Bp; expires=Fri, 10-Jul-2020
- 14:33:08 GMT; Max-Age=7200; path=/; httponly
+ - 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:
- - 61f48569-6ca6-43c6-a996-ad6a7e7984aa
+ - 0e944b79-e2e2-4f52-9879-1a9635416fb9
X-Token-Id:
- - 75048668-439a-4e54-853b-6f081a6fb6cf
+ - 48978b64-4efb-43d5-a334-451a1d164009
status:
code: 200
message: OK
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_for_event_with_secret_key.yaml
index 3a25c5675e..cdd096f049 100644
--- a/saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_for_event_with_secret_key.yaml
+++ b/saleor/plugins/webhook/tests/cassettes/test_trigger_webhooks_for_event_with_secret_key.yaml
@@ -1,11 +1,11 @@
interactions:
- request:
- body: '[{"model": "order.order", "pk": 26, "fields": {"private_metadata": {},
- "metadata": {}, "created": "2020-07-10T12:33:10.479Z", "status": "unfulfilled",
- "user": 27, "language_code": "en", "tracking_client_id": "", "billing_address":
- 107, "shipping_address": 108, "user_email": "[email protected]", "currency":
- "USD", "shipping_method": 28, "shipping_method_name": "DHL", "shipping_price_net_amount":
- "10.00", "shipping_price_gross_amount": "12.30", "token": "30a6f051-ae24-4bda-9cd0-732699fd7049",
+ 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",
@@ -18,7 +18,7 @@ interactions:
Connection:
- keep-alive
Content-Length:
- - '768'
+ - '771'
Content-Type:
- application/json
User-Agent:
@@ -28,9 +28,11 @@ interactions:
X-Saleor-Event:
- order_created
X-Saleor-HMAC-SHA256:
- - sha1=aa3028c8e235adc0224b20ed92e6161aa917476452df5a8aef18af68533cae25
+ - sha1=cce609bb36a9aa6b54af010a2aef40979ddd56a309590a470a9c4602226324b4
+ X-Saleor-Signature:
+ - cce609bb36a9aa6b54af010a2aef40979ddd56a309590a470a9c4602226324b4
method: POST
- uri: https://webhook.site/75048668-439a-4e54-853b-6f081a6fb6cf
+ uri: https://webhook.site/48978b64-4efb-43d5-a334-451a1d164009
response:
body:
string: !!binary |
@@ -43,20 +45,20 @@ interactions:
Content-Type:
- text/plain; charset=UTF-8
Date:
- - Fri, 10 Jul 2020 12:33:11 GMT
+ - Fri, 07 Aug 2020 09:06:01 GMT
Server:
- nginx/1.14.2
Set-Cookie:
- - laravel_session=J2Z8HoMlOVIhLGE6eSAvcSrdC6l2zGtj0jRvkRwt; expires=Fri, 10-Jul-2020
- 14:33:11 GMT; Max-Age=7200; path=/; httponly
+ - 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:
- - 54babb5f-57c1-47d1-a004-e4d7f8edca46
+ - 1d00628d-31d9-4636-b6b8-af7e35e3dde9
X-Token-Id:
- - 75048668-439a-4e54-853b-6f081a6fb6cf
+ - 48978b64-4efb-43d5-a334-451a1d164009
status:
code: 200
message: OK
diff --git a/saleor/plugins/webhook/tests/test_webhook.py b/saleor/plugins/webhook/tests/test_webhook.py
index 2d3215db7f..0eb1852325 100644
--- a/saleor/plugins/webhook/tests/test_webhook.py
+++ b/saleor/plugins/webhook/tests/test_webhook.py
@@ -17,36 +17,6 @@ from ...manager import get_plugins_manager
from ...webhook import signature_for_payload
from ...webhook.tasks import trigger_webhooks_for_event
-
[email protected]("saleor.plugins.webhook.tasks.requests.post", wraps=requests.post)
-def test_trigger_webhooks_for_event(
- mock_request,
- webhook,
- order_with_lines,
- permission_manage_orders,
- permission_manage_users,
- permission_manage_products,
-):
- webhook.app.permissions.add(permission_manage_orders)
- webhook.target_url = "https://webhook.site/75048668-439a-4e54-853b-6f081a6fb6cf"
- webhook.save()
-
- expected_data = serialize("json", [order_with_lines])
-
- trigger_webhooks_for_event(WebhookEventType.ORDER_CREATED, expected_data)
-
- expected_headers = {
- "Content-Type": "application/json",
- "X-Saleor-Event": "order_created",
- "X-Saleor-Domain": "mirumee.com",
- }
-
- mock_request.assert_called_once_with(
- webhook.target_url, data=expected_data, headers=expected_headers, timeout=10
- )
-
-
first_url = "http://www.example.com/first/"
third_url = "http://www.example.com/third/"
@@ -113,23 +83,29 @@ 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/75048668-439a-4e54-853b-6f081a6fb6cf"
+ 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, webhook.secret_key)
+ 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-HMAC-SHA256": expected_signature,
+ "X-Saleor-Signature": expected_signature,
+ "X-Saleor-HMAC-SHA256": f"sha1={expected_signature}",
}
mock_request.assert_called_once_with(
- webhook.target_url, data=expected_data, headers=expected_headers, timeout=10
+ webhook.target_url,
+ data=bytes(expected_data, "utf-8"),
+ headers=expected_headers,
+ timeout=10,
)
diff --git a/saleor/plugins/webhook/tests/test_webhook_protocols.py b/saleor/plugins/webhook/tests/test_webhook_protocols.py
new file mode 100644
index 0000000000..8774165e73
--- /dev/null
+++ b/saleor/plugins/webhook/tests/test_webhook_protocols.py
@@ -0,0 +1,70 @@
+from unittest.mock import MagicMock, patch
+
+import pytest
+import requests
+from django.core.serializers import serialize
+
+from ....webhook.event_types import WebhookEventType
+from ...webhook.tasks import trigger_webhooks_for_event
+
+
+def test_trigger_webhooks_with_google_pub_sub(
+ webhook,
+ order_with_lines,
+ permission_manage_orders,
+ permission_manage_users,
+ permission_manage_products,
+ monkeypatch,
+):
+ mocked_publisher = MagicMock()
+ 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.save()
+
+ expected_data = serialize("json", [order_with_lines])
+
+ 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="",
+ )
+
+
+@patch("saleor.plugins.webhook.tasks.requests.post", wraps=requests.post)
+def test_trigger_webhooks_with_http(
+ mock_request,
+ webhook,
+ order_with_lines,
+ permission_manage_orders,
+ permission_manage_users,
+ permission_manage_products,
+):
+ webhook.app.permissions.add(permission_manage_orders)
+ webhook.target_url = "https://webhook.site/48978b64-4efb-43d5-a334-451a1d164009"
+ webhook.save()
+
+ expected_data = serialize("json", [order_with_lines])
+
+ trigger_webhooks_for_event(WebhookEventType.ORDER_CREATED, expected_data)
+
+ expected_headers = {
+ "Content-Type": "application/json",
+ "X-Saleor-Event": "order_created",
+ "X-Saleor-Domain": "mirumee.com",
+ "X-Saleor-Signature": "",
+ }
+
+ mock_request.assert_called_once_with(
+ webhook.target_url,
+ data=bytes(expected_data, "utf-8"),
+ headers=expected_headers,
+ timeout=10,
+ )