Skip to content

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

[TEST] adding test for order with invalid shipping method CORE 0203 (#13864)

Renata008e7aebbf17
diff --git a/saleor/tests/e2e/checkout/test_checkout_create_from_order.py b/saleor/tests/e2e/checkout/test_checkout_create_from_order.py
index a99229a82d..7358154cd5 100644
--- a/saleor/tests/e2e/checkout/test_checkout_create_from_order.py
+++ b/saleor/tests/e2e/checkout/test_checkout_create_from_order.py
@@ -1,7 +1,7 @@
import pytest
-from ..orders.utils.draft_order import draft_order_create
-from ..orders.utils.order_lines import order_lines_create
+from ..orders.utils.draft_order_create import draft_order_create
+from ..orders.utils.order_lines_create import order_lines_create
from ..product.utils.preparing_product import prepare_product
from ..shop.utils.preparing_shop import prepare_shop
from ..utils import assign_permissions
@@ -43,9 +43,10 @@ def test_checkout_create_from_order_core_0104(
)
# Step 1 - Create checkout from order
+ channel_id = {"channelId": result_channel_id}
data = draft_order_create(
e2e_staff_api_client,
- result_channel_id,
+ channel_id,
)
order_id = data["order"]["id"]
diff --git a/saleor/tests/e2e/checkout/utils/__init__.py b/saleor/tests/e2e/checkout/utils/__init__.py
index d11291f6e8..02f6130fc3 100644
--- a/saleor/tests/e2e/checkout/utils/__init__.py
+++ b/saleor/tests/e2e/checkout/utils/__init__.py
@@ -1,5 +1,5 @@
-from ...orders.utils.draft_order import draft_order_create
-from ...orders.utils.order_lines import order_lines_create
+from ...orders.utils.draft_order_create import draft_order_create
+from ...orders.utils.order_lines_create import order_lines_create
from ...product.utils.product_channel_listing import raw_create_product_channel_listing
from .checkout_add_promo_code import (
checkout_add_promo_code,
diff --git a/saleor/tests/e2e/orders/__init__.py b/saleor/tests/e2e/orders/__init__.py
index 35bffa4f2b..e69de29bb2 100644
--- a/saleor/tests/e2e/orders/__init__.py
+++ b/saleor/tests/e2e/orders/__init__.py
@@ -1,7 +0,0 @@
-from .utils.draft_order import draft_order_create
-from .utils.order_lines import order_lines_create
-
-__all__ = [
- "draft_order_create",
- "order_lines_create",
-]
diff --git a/saleor/tests/e2e/orders/test_cannot_fullfill_order_with_invalid_shipping_method.py b/saleor/tests/e2e/orders/test_cannot_fullfill_order_with_invalid_shipping_method.py
new file mode 100644
index 0000000000..39a21f4791
--- /dev/null
+++ b/saleor/tests/e2e/orders/test_cannot_fullfill_order_with_invalid_shipping_method.py
@@ -0,0 +1,200 @@
+import base64
+
+import pytest
+
+from .. import DEFAULT_ADDRESS
+from ..channel.utils import create_channel
+from ..product.utils.preparing_product import prepare_product
+from ..shipping_zone.utils import (
+ create_shipping_method,
+ create_shipping_method_channel_listing,
+ create_shipping_zone,
+)
+from ..utils import assign_permissions
+from ..warehouse.utils import create_warehouse
+from .utils.draft_order_complete import raw_draft_order_complete
+from .utils.draft_order_create import draft_order_create
+from .utils.draft_order_update import draft_order_update
+from .utils.order_lines_create import order_lines_create
+from .utils.order_query import order_query
+
+# Please note: decoding won't be necessary once
+# https://github.com/saleor/saleor/issues/13675 is fixed
+
+
+def decode_base64_and_get_last_2_chars(encoded_string):
+ base64_bytes = encoded_string.encode("ascii")
+ decoded_bytes = base64.b64decode(base64_bytes)
+ decoded_string = decoded_bytes.decode("ascii")
+ return decoded_string[-2:]
+
+
+first_address = DEFAULT_ADDRESS
+
+
+def prepare_shipping_methods(
+ e2e_staff_api_client,
+):
+ warehouse_data = create_warehouse(
+ e2e_staff_api_client,
+ name="Warehouse 1",
+ slug="warehouse-1",
+ address=first_address,
+ )
+ warehouse_id = warehouse_data["id"]
+
+ channel_data = create_channel(
+ e2e_staff_api_client,
+ warehouse_ids=[warehouse_id],
+ )
+ channel_id = channel_data["id"]
+
+ first_shipping_zone_data = create_shipping_zone(
+ e2e_staff_api_client,
+ warehouse_ids=[warehouse_id],
+ channel_ids=[channel_id],
+ )
+ first_shipping_zone_id = first_shipping_zone_data["id"]
+
+ first_shipping_method_data = create_shipping_method(
+ e2e_staff_api_client,
+ first_shipping_zone_id,
+ name="First shipping method",
+ type="PRICE",
+ )
+ first_shipping_method_id = first_shipping_method_data["id"]
+
+ create_shipping_method_channel_listing(
+ e2e_staff_api_client,
+ first_shipping_method_id,
+ channel_id,
+ )
+
+ second_shipping_zone_data = create_shipping_zone(
+ e2e_staff_api_client,
+ name="second shipping zone",
+ countries="PL",
+ warehouse_ids=[warehouse_id],
+ channel_ids=[channel_id],
+ )
+ second_shipping_zone_id = second_shipping_zone_data["id"]
+
+ second_shipping_method_data = create_shipping_method(
+ e2e_staff_api_client,
+ second_shipping_zone_id,
+ name="Second shipping method",
+ type="PRICE",
+ )
+ second_shipping_method_id = second_shipping_method_data["id"]
+
+ create_shipping_method_channel_listing(
+ e2e_staff_api_client,
+ second_shipping_method_id,
+ channel_id,
+ )
+
+ return (
+ warehouse_id,
+ channel_id,
+ first_shipping_method_id,
+ second_shipping_method_id,
+ )
+
+
+def test_cannot_fullfill_order_with_invalid_shipping_method_core_0203(
+ e2e_staff_api_client,
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_product_types_and_attributes,
+ permission_manage_shipping,
+ permission_manage_orders,
+):
+ # Before
+ permissions = [
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_shipping,
+ permission_manage_product_types_and_attributes,
+ permission_manage_orders,
+ ]
+ assign_permissions(e2e_staff_api_client, permissions)
+
+ (
+ warehouse_id,
+ channel_id,
+ first_shipping_method_id,
+ second_shipping_method_id,
+ ) = prepare_shipping_methods(e2e_staff_api_client)
+
+ price = 2
+ (
+ _,
+ result_product_variant_id,
+ _,
+ ) = prepare_product(e2e_staff_api_client, warehouse_id, channel_id, price)
+
+ # Step 1 - Create draft order and add lines
+ draft_order_input = {
+ "channelId": channel_id,
+ }
+ data = draft_order_create(
+ e2e_staff_api_client,
+ draft_order_input,
+ )
+ order_id = data["order"]["id"]
+ assert order_id is not None
+
+ lines = [{"variantId": result_product_variant_id, "quantity": 1}]
+ order_lines = order_lines_create(e2e_staff_api_client, order_id, lines)
+ order_product_variant_id = order_lines["order"]["lines"][0]["variant"]["id"]
+ assert order_product_variant_id == result_product_variant_id
+
+ # Step 2 - Update order's shipping method
+ input = {
+ "shippingMethod": first_shipping_method_id,
+ "shippingAddress": first_address,
+ "billingAddress": first_address,
+ }
+ draft_order = draft_order_update(e2e_staff_api_client, order_id, input)
+ order_shipping_id = draft_order["order"]["deliveryMethod"]["id"]
+ first_shipping_id_number = decode_base64_and_get_last_2_chars(
+ first_shipping_method_id
+ )
+ shipping_id_number = decode_base64_and_get_last_2_chars(order_shipping_id)
+
+ assert shipping_id_number == first_shipping_id_number
+
+ # Step 3 - Update order's shipping address for country PL
+ second_address = {
+ "firstName": "Jan",
+ "lastName": "Kowalski",
+ "phone": "+48123456787",
+ "companyName": "Saleor PL",
+ "country": "PL",
+ "countryArea": "",
+ "city": "WROCLAW",
+ "postalCode": "53-346",
+ "streetAddress1": "Smolna",
+ "streetAddress2": "13/1",
+ }
+ update_input = {"shippingAddress": second_address}
+ draft_update = draft_order_update(e2e_staff_api_client, order_id, update_input)
+ draft_order_shipping_id = draft_update["order"]["deliveryMethod"]["id"]
+ assert draft_order_shipping_id == order_shipping_id
+
+ # Step 4 - Complete the order and check that 2nd shipping method is now available
+ order_complete = raw_draft_order_complete(e2e_staff_api_client, order_id)
+ assert (
+ order_complete["errors"][0]["message"]
+ == "Shipping method is not valid for chosen shipping address"
+ )
+ order_details = order_query(e2e_staff_api_client, order_id)
+ order_shipping_method = order_details["availableShippingMethods"][0]["id"]
+ second_shipping_id_number = decode_base64_and_get_last_2_chars(
+ second_shipping_method_id
+ )
+ order_shipping_method_number = decode_base64_and_get_last_2_chars(
+ order_shipping_method
+ )
+ assert order_shipping_method_number == second_shipping_id_number
diff --git a/saleor/tests/e2e/orders/utils/__init_.py b/saleor/tests/e2e/orders/utils/__init_.py
index e69de29bb2..2876a8d865 100644
--- a/saleor/tests/e2e/orders/utils/__init_.py
+++ b/saleor/tests/e2e/orders/utils/__init_.py
@@ -0,0 +1,14 @@
+from draft_order_complete import draft_order_complete, raw_draft_order_complete
+from draft_order_create import draft_order_create
+from draft_order_update import draft_order_update
+from order_lines_create import order_lines_create
+from order_query import order_query
+
+__all__ = [
+ "raw_draft_order_complete",
+ "draft_order_create",
+ "order_lines_create",
+ "draft_order_complete",
+ "draft_order_update",
+ "order_query",
+]
diff --git a/saleor/tests/e2e/orders/utils/draft_order.py b/saleor/tests/e2e/orders/utils/draft_order.py
deleted file mode 100644
index 72e1f844f2..0000000000
--- a/saleor/tests/e2e/orders/utils/draft_order.py
+++ /dev/null
@@ -1,42 +0,0 @@
-from saleor.graphql.tests.utils import get_graphql_content
-
-DRAFT_ORDER_CREATE_MUTATION = """
-mutation OrderDraftCreate($input: DraftOrderCreateInput!){
- draftOrderCreate(input: $input) {
- errors {
- message
- field
- }
- order {
- id
- }
- }
-}
-"""
-
-
-def draft_order_create(
- api_client,
- channel_id,
-):
- variables = {
- "input": {
- "channelId": channel_id,
- }
- }
-
- response = api_client.post_graphql(
- DRAFT_ORDER_CREATE_MUTATION,
- variables=variables,
- )
- content = get_graphql_content(response)
-
- data = content["data"]["draftOrderCreate"]
-
- order_id = data["order"]["id"]
- errors = data["errors"]
-
- assert errors == []
- assert order_id is not None
-
- return data
diff --git a/saleor/tests/e2e/orders/utils/draft_order_complete.py b/saleor/tests/e2e/orders/utils/draft_order_complete.py
new file mode 100644
index 0000000000..c7c766783b
--- /dev/null
+++ b/saleor/tests/e2e/orders/utils/draft_order_complete.py
@@ -0,0 +1,87 @@
+from saleor.graphql.tests.utils import get_graphql_content
+
+DRAFT_ORDER_COMPLETE_MUTATION = """
+mutation DraftOrderComplete($id: ID!) {
+ draftOrderComplete(id: $id) {
+ errors {
+ message
+ field
+ code
+ }
+ order {
+ id
+ undiscountedTotal {
+ gross {
+ amount
+ }
+ }
+ totalBalance {
+ amount
+ }
+ total {
+ gross {
+ amount
+ }
+ }
+ subtotal {
+ gross {
+ amount
+ }
+ }
+ shippingPrice {
+ gross {
+ amount
+ }
+ }
+ displayGrossPrices
+ status
+ lines {
+ productVariantId
+ quantity
+ unitDiscount {
+ amount
+ }
+ undiscountedUnitPrice {
+ gross {
+ amount
+ }
+ }
+ unitPrice {
+ gross {
+ amount
+ }
+ }
+ unitDiscountReason
+ unitDiscountType
+ unitDiscountValue
+ }
+ }
+ }
+}
+"""
+
+
+def raw_draft_order_complete(api_client, id):
+ variables = {"id": id}
+
+ response = api_client.post_graphql(
+ DRAFT_ORDER_COMPLETE_MUTATION,
+ variables=variables,
+ )
+ content = get_graphql_content(response)
+
+ data = content["data"]["draftOrderComplete"]
+
+ return data
+
+
+def draft_order_complete(api_client, id):
+ response = raw_draft_order_complete(api_client, id)
+
+ assert response["order"] is not None
+
+ errors = response["errors"]
+
+ assert errors == []
+
+ return response
diff --git a/saleor/tests/e2e/orders/utils/draft_order_create.py b/saleor/tests/e2e/orders/utils/draft_order_create.py
new file mode 100644
index 0000000000..2a1dfb06e9
--- /dev/null
+++ b/saleor/tests/e2e/orders/utils/draft_order_create.py
@@ -0,0 +1,70 @@
+from saleor.graphql.tests.utils import get_graphql_content
+
+DRAFT_ORDER_CREATE_MUTATION = """
+mutation OrderDraftCreate($input: DraftOrderCreateInput!) {
+ draftOrderCreate(input: $input) {
+ errors {
+ message
+ field
+ code
+ }
+ order {
+ id
+ created
+ discounts {
+ amount {
+ amount
+ }
+ }
+ billingAddress {
+ streetAddress1
+ }
+ shippingAddress {
+ streetAddress1
+ }
+ shippingMethods {
+ id
+ }
+ lines {
+ productVariantId
+ quantity
+ undiscountedUnitPrice {
+ gross {
+ amount
+ }
+ }
+ unitPrice {
+ gross {
+ amount
+ }
+ }
+ totalPrice {
+ gross {
+ amount
+ }
+ }
+ }
+ }
+ }
+}
+"""
+
+
+def draft_order_create(api_client, input):
+ variables = {"input": input}
+
+ response = api_client.post_graphql(
+ DRAFT_ORDER_CREATE_MUTATION,
+ variables=variables,
+ )
+ content = get_graphql_content(response)
+
+ data = content["data"]["draftOrderCreate"]
+
+ order_id = data["order"]["id"]
+ errors = data["errors"]
+
+ assert errors == []
+ assert order_id is not None
+
+ return data
diff --git a/saleor/tests/e2e/orders/utils/draft_order_update.py b/saleor/tests/e2e/orders/utils/draft_order_update.py
new file mode 100644
index 0000000000..5bd3ed8488
--- /dev/null
+++ b/saleor/tests/e2e/orders/utils/draft_order_update.py
@@ -0,0 +1,99 @@
+from saleor.graphql.tests.utils import get_graphql_content
+
+DRAFT_ORDER_UPDATE_MUTATION = """
+mutation DraftOrderUpdate($input: DraftOrderInput!, $id: ID!) {
+ draftOrderUpdate(input: $input, id: $id) {
+ errors {
+ message
+ field
+ code
+ }
+ order {
+ id
+ lines {
+ totalPrice {
+ gross {
+ amount
+ }
+ }
+ unitPrice {
+ gross {
+ amount
+ }
+ }
+ unitDiscountReason
+ }
+ subtotal {
+ gross {
+ amount
+ }
+ }
+ totalBalance {
+ amount
+ }
+ total {
+ gross {
+ amount
+ }
+ }
+ shippingAddress {
+ firstName
+ lastName
+ companyName
+ streetAddress1
+ streetAddress2
+ postalCode
+ country{ code }
+ city
+ countryArea
+ phone
+ }
+ isShippingRequired
+ shippingPrice {
+ gross {
+ amount
+ }
+ }
+ shippingMethod {
+ id
+ }
+ shippingMethods {
+ id
+ }
+ channel {
+ id
+ name
+ }
+ deliveryMethod {
+ __typename
+ ... on ShippingMethod {
+ id
+ __typename
+ }
+ }
+ }
+ }
+}
+"""
+
+
+def draft_order_update(
+ api_client,
+ id,
+ input,
+):
+ variables = {"id": id, "input": input}
+
+ response = api_client.post_graphql(
+ DRAFT_ORDER_UPDATE_MUTATION,
+ variables=variables,
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["draftOrderUpdate"]
+ order_id = data["order"]["id"]
+ errors = data["errors"]
+
+ assert errors == []
+ assert order_id == id
+
+ return data
diff --git a/saleor/tests/e2e/orders/utils/order_lines.py b/saleor/tests/e2e/orders/utils/order_lines_create.py
similarity index 100%
rename from saleor/tests/e2e/orders/utils/order_lines.py
rename to saleor/tests/e2e/orders/utils/order_lines_create.py
diff --git a/saleor/tests/e2e/orders/utils/order_query.py b/saleor/tests/e2e/orders/utils/order_query.py
new file mode 100644
index 0000000000..d2bff3a69d
--- /dev/null
+++ b/saleor/tests/e2e/orders/utils/order_query.py
@@ -0,0 +1,56 @@
+from saleor.graphql.tests.utils import get_graphql_content
+
+ORDER_QUERY = """
+query OrderDetails($id:ID!) {
+ order(id:$id) {
+ availableShippingMethods {
+ id
+ active
+ }
+ channel {
+ id
+ name
+ }
+ fulfillments {
+ created
+ id
+ }
+ deliveryMethod {
+ ... on ShippingMethod {
+ id
+ name
+ active
+ }
+ }
+ shippingMethods {
+ id
+ }
+ shippingAddress {
+ country {
+ code
+ }
+ countryArea
+ firstName
+ cityArea
+ city
+ phone
+ postalCode
+ streetAddress1
+ streetAddress2
+ }
+ statusDisplay
+ }
+}
+"""
+
+
+def order_query(
+ api_client,
+ order_id,
+):
+ variables = {"id": order_id}
+
+ response = api_client.post_graphql(ORDER_QUERY, variables)
+ content = get_graphql_content(response)
+
+ return content["data"]["order"]
diff --git a/saleor/tests/e2e/shipping_zone/utils/shipping_zone.py b/saleor/tests/e2e/shipping_zone/utils/shipping_zone.py
index 4b38129f0e..c86fe573e4 100644
--- a/saleor/tests/e2e/shipping_zone/utils/shipping_zone.py
+++ b/saleor/tests/e2e/shipping_zone/utils/shipping_zone.py
@@ -27,6 +27,7 @@ mutation createShipping($input: ShippingZoneCreateInput!) {
def create_shipping_zone(
staff_api_client,
name="Test shipping zone",
+ countries="US",
warehouse_ids=None,
channel_ids=None,
):
@@ -38,7 +39,7 @@ def create_shipping_zone(
variables = {
"input": {
"name": name,
- "countries": "US",
+ "countries": [countries],
"addWarehouses": warehouse_ids,
"addChannels": channel_ids,
}