Skip to content

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

[Test] Use external shipping methods in the checkout (#14820)

Anna Szczęchac2d2804a600
diff --git a/saleor/tests/e2e/apps/__init__.py b/saleor/tests/e2e/apps/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/tests/e2e/apps/utils/__init__.py b/saleor/tests/e2e/apps/utils/__init__.py
new file mode 100644
index 0000000000..c7cab1d63c
--- /dev/null
+++ b/saleor/tests/e2e/apps/utils/__init__.py
@@ -0,0 +1,5 @@
+from .app_create import add_app
+
+__all__ = [
+ "add_app",
+]
diff --git a/saleor/tests/e2e/apps/utils/app_create.py b/saleor/tests/e2e/apps/utils/app_create.py
new file mode 100644
index 0000000000..7d58581fb9
--- /dev/null
+++ b/saleor/tests/e2e/apps/utils/app_create.py
@@ -0,0 +1,35 @@
+from ...utils import get_graphql_content
+
+APP_CREATE_MUTATION = """
+mutation AppCreate($input: AppInput!) {
+ appCreate(input: $input) {
+ errors {
+ field
+ code
+ message
+ }
+ authToken
+ app {
+ id
+ isActive
+ }
+ }
+}
+
+"""
+
+
+def add_app(
+ staff_api_client,
+ input,
+):
+ variables = {"input": input}
+
+ response = staff_api_client.post_graphql(APP_CREATE_MUTATION, variables)
+ content = get_graphql_content(response)
+
+ assert content["data"]["appCreate"]["errors"] == []
+ data = content["data"]["appCreate"]
+ assert data["app"]["id"] is not None
+
+ return data
diff --git a/saleor/tests/e2e/checkout/shipping/__init__.py b/saleor/tests/e2e/checkout/shipping/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/tests/e2e/checkout/shipping/cassettes/test_use_external_shipping_methods_in_checkout/test_use_external_shipping_methods_in_checkout_core_1652.yaml b/saleor/tests/e2e/checkout/shipping/cassettes/test_use_external_shipping_methods_in_checkout/test_use_external_shipping_methods_in_checkout_core_1652.yaml
new file mode 100644
index 0000000000..f8a94165b8
--- /dev/null
+++ b/saleor/tests/e2e/checkout/shipping/cassettes/test_use_external_shipping_methods_in_checkout/test_use_external_shipping_methods_in_checkout_core_1652.yaml
@@ -0,0 +1,100 @@
+interactions:
+- request:
+ body: '{"__typename": "ShippingListMethodsForCheckout", "checkout": {"shippingAddress":
+ {"country": {"code": "US"}}}}'
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate, br
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '110'
+ Content-Type:
+ - application/json
+ Saleor-Api-Url:
+ - http://mirumee.com/graphql/
+ Saleor-Domain:
+ - mirumee.com
+ Saleor-Event:
+ - shipping_list_methods_for_checkout
+ Saleor-Signature:
+ - ''
+ User-Agent:
+ - Saleor/3.19
+ X-Saleor-Domain:
+ - mirumee.com
+ X-Saleor-Event:
+ - shipping_list_methods_for_checkout
+ X-Saleor-Signature:
+ - ''
+ method: POST
+ uri: http://localhost:8080/
+ response:
+ body:
+ string: '[{"id": "Provider", "name": "Provider - Economy", "amount": "100",
+ "currency": "USD", "maximum_delivery_days": "7"}, {"id": 1, "name": "Pocztex",
+ "amount": "21.37", "currency": "USD", "maximum_delivery_days": 7}]'
+ headers:
+ Content-Length:
+ - '212'
+ Content-Type:
+ - application/json
+ Date:
+ - Tue, 21 Nov 2023 16:14:43 GMT
+ Server:
+ - TwistedWeb/21.7.0
+ status:
+ code: 200
+ message: OK
+- request:
+ body: '{"__typename": "ShippingListMethodsForCheckout", "checkout": {"shippingAddress":
+ {"country": {"code": "US"}}}}'
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate, br
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '110'
+ Content-Type:
+ - application/json
+ Saleor-Api-Url:
+ - http://mirumee.com/graphql/
+ Saleor-Domain:
+ - mirumee.com
+ Saleor-Event:
+ - shipping_list_methods_for_checkout
+ Saleor-Signature:
+ - ''
+ User-Agent:
+ - Saleor/3.19
+ X-Saleor-Domain:
+ - mirumee.com
+ X-Saleor-Event:
+ - shipping_list_methods_for_checkout
+ X-Saleor-Signature:
+ - ''
+ method: POST
+ uri: http://localhost:8080/
+ response:
+ body:
+ string: '[{"id": "Provider", "name": "Provider - Economy", "amount": "100",
+ "currency": "USD", "maximum_delivery_days": "7"}, {"id": 1, "name": "Pocztex",
+ "amount": "21.37", "currency": "USD", "maximum_delivery_days": 7}]'
+ headers:
+ Content-Length:
+ - '212'
+ Content-Type:
+ - application/json
+ Date:
+ - Tue, 21 Nov 2023 16:14:44 GMT
+ Server:
+ - TwistedWeb/21.7.0
+ status:
+ code: 200
+ message: OK
+version: 1
diff --git a/saleor/tests/e2e/checkout/shipping/test_use_external_shipping_methods_in_checkout.py b/saleor/tests/e2e/checkout/shipping/test_use_external_shipping_methods_in_checkout.py
new file mode 100644
index 0000000000..a34b88ea20
--- /dev/null
+++ b/saleor/tests/e2e/checkout/shipping/test_use_external_shipping_methods_in_checkout.py
@@ -0,0 +1,145 @@
+import pytest
+import vcr
+
+from ...apps.utils import add_app
+from ...product.utils.preparing_product import prepare_product
+from ...shop.utils.preparing_shop import prepare_default_shop
+from ...utils import assign_permissions, request_matcher
+from ...webhooks.utils import create_webhook
+from ..utils import (
+ checkout_complete,
+ checkout_create,
+ checkout_delivery_method_update,
+ checkout_dummy_payment_create,
+)
+
+
+def test_use_external_shipping_methods_in_checkout_core_1652(
+ e2e_not_logged_api_client,
+ e2e_staff_api_client,
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_shipping,
+ permission_manage_product_types_and_attributes,
+ permission_manage_apps,
+ settings,
+ vcr_cassette_dir,
+):
+ # Before
+ settings.PLUGINS = [
+ "saleor.plugins.webhook.plugin.WebhookPlugin",
+ "saleor.payment.gateways.dummy.plugin.DummyGatewayPlugin",
+ ]
+ permissions = [
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_shipping,
+ permission_manage_product_types_and_attributes,
+ permission_manage_apps,
+ ]
+ assign_permissions(e2e_staff_api_client, permissions)
+
+ shop_data = prepare_default_shop(e2e_staff_api_client)
+ warehouse_id = shop_data["warehouse"]["id"]
+ channel_id = shop_data["channel"]["id"]
+ channel_slug = shop_data["channel"]["slug"]
+
+ app_input = {"name": "external_shipping", "permissions": ["MANAGE_SHIPPING"]}
+ app_data = add_app(e2e_staff_api_client, app_input)
+ app_id = app_data["app"]["id"]
+
+ target_url = "http://localhost:8080"
+ webhook_input = {
+ "app": app_id,
+ "syncEvents": ["SHIPPING_LIST_METHODS_FOR_CHECKOUT"],
+ "asyncEvents": [],
+ "isActive": True,
+ "name": "external shipping methods",
+ "targetUrl": target_url,
+ "query": "subscription {\n event {\n ... on ShippingListMethodsForCheckout {\n __typename\n checkout{\n shippingAddress{\n country{\n code\n }\n }\n }\n }\n }\n}\n",
+ "customHeaders": "{}",
+ }
+ create_webhook(e2e_staff_api_client, webhook_input)
+
+ (
+ _product_id,
+ product_variant_id,
+ product_variant_price,
+ ) = prepare_product(
+ e2e_staff_api_client,
+ warehouse_id,
+ channel_id,
+ variant_price=9.99,
+ )
+ product_variant_price = float(product_variant_price)
+
+ # Step 1 - Create checkout for product
+ lines = [
+ {
+ "variantId": product_variant_id,
+ "quantity": 4,
+ },
+ ]
+ my_vcr = vcr.VCR()
+ my_vcr.register_matcher("shipping_cassette", request_matcher)
+ with my_vcr.use_cassette(
+ f"{vcr_cassette_dir}/test_use_external_shipping_methods_in_checkout_core_1652.yaml",
+ match_on=["shipping_cassette"],
+ ):
+ checkout_data = checkout_create(
+ e2e_not_logged_api_client,
+ lines,
+ channel_slug,
+ set_default_billing_address=True,
+ set_default_shipping_address=True,
+ )
+ checkout_id = checkout_data["id"]
+ assert checkout_data["isShippingRequired"] is True
+ calculated_subtotal = product_variant_price * 4
+ assert checkout_data["totalPrice"]["gross"]["amount"] == calculated_subtotal
+ shipping_methods = checkout_data["shippingMethods"]
+ assert len(shipping_methods) == 3
+ saleor_shipping = shipping_methods[0]
+ assert saleor_shipping["name"] == "Test shipping method"
+ assert saleor_shipping["price"]["amount"] == 10
+ external_shipping_1 = shipping_methods[1]
+ assert external_shipping_1["name"] == "Provider - Economy"
+ assert external_shipping_1["price"]["amount"] == 100
+ assert external_shipping_1["maximumDeliveryDays"] == 7
+ external_shipping_2 = shipping_methods[2]
+ assert external_shipping_2["name"] == "Pocztex"
+ assert external_shipping_2["price"]["amount"] == 21.37
+ assert external_shipping_2["maximumDeliveryDays"] == 7
+
+ external_shipping_2_id = external_shipping_2["id"]
+ shipping_price = external_shipping_2["price"]["amount"]
+
+ # Step 2 - Set DeliveryMethod for checkout.
+ checkout_data = checkout_delivery_method_update(
+ e2e_not_logged_api_client,
+ checkout_id,
+ external_shipping_2_id,
+ )
+ assert checkout_data["deliveryMethod"]["id"] == external_shipping_2_id
+ total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]
+ calculated_total = calculated_subtotal + shipping_price
+ assert total_gross_amount == calculated_total
+
+ # Step 3 - Create payment for checkout.
+ checkout_dummy_payment_create(
+ e2e_not_logged_api_client, checkout_id, total_gross_amount
+ )
+
+ # Step 4 - Complete checkout and check total
+ order_data = checkout_complete(
+ e2e_not_logged_api_client,
+ checkout_id,
+ )
+ assert order_data["status"] == "UNFULFILLED"
+ assert order_data["total"]["gross"]["amount"] == calculated_total
+ assert order_data["deliveryMethod"]["id"] == external_shipping_2_id
+ assert order_data["deliveryMethod"]["price"]["amount"] == shipping_price
+ assert order_data["deliveryMethod"]["name"] == "Pocztex"
diff --git a/saleor/tests/e2e/checkout/utils/checkout_complete.py b/saleor/tests/e2e/checkout/utils/checkout_complete.py
index 00c8c4b4a2..f338f0f9eb 100644
--- a/saleor/tests/e2e/checkout/utils/checkout_complete.py
+++ b/saleor/tests/e2e/checkout/utils/checkout_complete.py
@@ -38,6 +38,7 @@ mutation CheckoutComplete($checkoutId: ID!) {
deliveryMethod {
... on ShippingMethod {
id
+ name
price {
amount
}
diff --git a/saleor/tests/e2e/checkout/utils/checkout_create.py b/saleor/tests/e2e/checkout/utils/checkout_create.py
index a586184a6c..d92e309ef3 100644
--- a/saleor/tests/e2e/checkout/utils/checkout_create.py
+++ b/saleor/tests/e2e/checkout/utils/checkout_create.py
@@ -44,6 +44,12 @@ mutation CreateCheckout($input: CheckoutCreateInput!) {
}
shippingMethods {
id
+ name
+ price {
+ amount
+ currency
+ }
+ maximumDeliveryDays
}
deliveryMethod {
... on ShippingMethod {
diff --git a/saleor/tests/e2e/test_utils.py b/saleor/tests/e2e/test_utils.py
new file mode 100644
index 0000000000..d391b0d020
--- /dev/null
+++ b/saleor/tests/e2e/test_utils.py
@@ -0,0 +1,124 @@
+import json
+from unittest.mock import Mock
+
+from .utils import request_matcher
+
+
+def test_equal_dicts():
+ r1 = Mock(
+ body=Mock(
+ decode=Mock(return_value=json.dumps({"key1": "value1", "key2": "value2"}))
+ )
+ )
+ r2 = Mock(
+ body=Mock(
+ decode=Mock(return_value=json.dumps({"key1": "value1", "key2": "value2"}))
+ )
+ )
+
+ result = request_matcher(r1, r2)
+ assert result is True
+
+
+def test_unequal_dicts():
+ r1 = Mock(
+ body=Mock(
+ decode=Mock(return_value=json.dumps({"key1": "value1", "key2": "value2"}))
+ )
+ )
+ r2 = Mock(
+ body=Mock(
+ decode=Mock(return_value=json.dumps({"key1": "value1", "key2": "value3"}))
+ )
+ )
+
+ result = request_matcher(r1, r2)
+ assert result is False
+
+
+def test_missing_key_in_r2():
+ r1 = Mock(
+ body=Mock(
+ decode=Mock(return_value=json.dumps({"key1": "value1", "key2": "value2"}))
+ )
+ )
+ r2 = Mock(body=Mock(decode=Mock(return_value=json.dumps({"key1": "value1"}))))
+
+ result = request_matcher(r1, r2)
+ assert result is False
+
+
+def test_extra_key_in_r2():
+ r1 = Mock(
+ body=Mock(
+ decode=Mock(return_value=json.dumps({"key1": "value1", "key2": "value2"}))
+ )
+ )
+ r2 = Mock(
+ body=Mock(
+ decode=Mock(
+ return_value=json.dumps(
+ {"key1": "value1", "key2": "value2", "key3": "value3"}
+ )
+ )
+ )
+ )
+
+ result = request_matcher(r1, r2)
+ assert result is False
+
+
+def test_different_sorting_order_in_r2():
+ r1 = Mock(
+ body=Mock(
+ decode=Mock(return_value=json.dumps({"key1": "value1", "key2": "value2"}))
+ )
+ )
+ r2 = Mock(
+ body=Mock(
+ decode=Mock(return_value=json.dumps({"key2": "value2", "key1": "value1"}))
+ )
+ )
+
+ result = request_matcher(r1, r2)
+ assert result is True
+
+
+def test_nested_keys():
+ r1 = Mock(
+ body=Mock(
+ decode=Mock(
+ return_value=json.dumps(
+ {
+ "key1": "value1",
+ "key2": {
+ "key3": "value3",
+ "key4": {
+ "key5": "value5",
+ },
+ },
+ }
+ )
+ )
+ )
+ )
+ r2 = Mock(
+ body=Mock(
+ decode=Mock(
+ return_value=json.dumps(
+ {
+ "key1": "value1",
+ "key2": {
+ "key3": "value3",
+ "key4": {
+ "key5": "value5",
+ },
+ },
+ }
+ )
+ )
+ )
+ )
+
+ result = request_matcher(r1, r2)
+ assert result is True
diff --git a/saleor/tests/e2e/utils.py b/saleor/tests/e2e/utils.py
index 7fe08febdd..e877359aaa 100644
--- a/saleor/tests/e2e/utils.py
+++ b/saleor/tests/e2e/utils.py
@@ -1,3 +1,5 @@
+import json
+
from ...account.models import Group
from ...graphql.tests.utils import (
get_graphql_content, # noqa: F401
@@ -18,3 +20,23 @@ def assign_permissions(api_client, permissions):
app = api_client.app
if app:
app.permissions.add(*permissions)
+
+
+def request_matcher(r1, r2):
+ body1 = json.loads(r1.body.decode("utf-8"))
+ body2 = json.loads(r2.body.decode("utf-8"))
+
+ # Check if all key-value pairs in body1 are present in body2
+ if set(body1.keys()) != set(body2.keys()):
+ return False
+
+ for key, value in body1.items():
+ if key not in body2 or body2[key] != value:
+ return False
+
+ # Check if there are any extra key-value pairs in body2
+ for key in body2:
+ if key not in body1:
+ return False
+
+ return True
diff --git a/saleor/tests/e2e/webhooks/__init__.py b/saleor/tests/e2e/webhooks/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/tests/e2e/webhooks/utils.py b/saleor/tests/e2e/webhooks/utils.py
new file mode 100644
index 0000000000..e7366d13b9
--- /dev/null
+++ b/saleor/tests/e2e/webhooks/utils.py
@@ -0,0 +1,50 @@
+from ..utils import get_graphql_content
+
+WEBHOOK_CREATE_MUTATION = """
+mutation WebhookCreate($input: WebhookCreateInput!) {
+ webhookCreate(input: $input) {
+ errors {
+ field
+ code
+ message
+ }
+ webhook {
+ id
+ name
+ isActive
+ targetUrl
+ syncEvents{
+ eventType
+ }
+ asyncEvents{
+ eventType
+ }
+ subscriptionQuery
+ customHeaders
+ }
+ __typename
+ }
+}
+"""
+
+
+def create_webhook(
+ staff_api_client,
+ input,
+):
+ variables = {
+ "input": input,
+ }
+
+ response = staff_api_client.post_graphql(
+ WEBHOOK_CREATE_MUTATION,
+ variables,
+ )
+
+ content = get_graphql_content(response)
+
+ assert content["data"]["webhookCreate"]["errors"] == []
+ data = content["data"]["webhookCreate"]["webhook"]
+ assert data["id"] is not None
+
+ return data