Skip to content

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

Add test for ready_to_place_order

fowczarek346a73d8221e
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index b0b0bfafef..aeb21993f1 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -894,7 +894,6 @@ def is_fully_paid(cart: Cart):
def ready_to_place_order(cart: Cart, taxes, discounts):
- # FIXME test me
if cart.is_shipping_required():
if not cart.shipping_method:
return False, pgettext_lazy(
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index 169c225d24..9e4e259fbd 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -3,11 +3,11 @@ from unittest.mock import ANY, patch
import graphene
import pytest
+from tests.api.utils import get_graphql_content
from saleor.checkout.models import Cart
+from saleor.checkout.utils import ready_to_place_order
from saleor.order.models import Order
-from tests.api.utils import get_graphql_content
-
MUTATION_CHECKOUT_CREATE = """
mutation createCheckout($checkoutInput: CheckoutCreateInput!) {
@@ -589,6 +589,22 @@ def test_checkout_email_update(user_api_client, cart_with_item):
assert cart.email == email
+MUTATION_CHECKOUT_COMPLEATE = """
+ mutation checkoutComplete($checkoutId: ID!) {
+ checkoutComplete(checkoutId: $checkoutId) {
+ order {
+ id,
+ token
+ },
+ errors {
+ field,
+ message
+ }
+ }
+ }
+ """
+
+
@pytest.mark.integration
def test_checkout_complete(
user_api_client, cart_with_item, payment_dummy, address,
@@ -607,23 +623,10 @@ def test_checkout_complete(
payment.save()
checkout_id = graphene.Node.to_global_id('Checkout', checkout.pk)
assert not payment.transactions.exists()
- query = """
- mutation checkoutComplete($checkoutId: ID!) {
- checkoutComplete(checkoutId: $checkoutId) {
- order {
- id,
- token
- },
- errors {
- field,
- message
- }
- }
- }
- """
variables = {'checkoutId': checkout_id}
orders_count = Order.objects.count()
- response = user_api_client.post_graphql(query, variables)
+ response = user_api_client.post_graphql(
+ MUTATION_CHECKOUT_COMPLEATE, variables)
content = get_graphql_content(response)
data = content['data']['checkoutComplete']
assert not data['errors']
@@ -819,3 +822,65 @@ def test_query_checkout_lines(
graphene.Node.to_global_id('CheckoutLine', item.pk)
for item in checkout]
assert expected_lines_ids == checkout_lines_ids
+
+
+def test_ready_to_place_order(
+ cart_with_item, payment_dummy, address, shipping_method):
+ checkout = cart_with_item
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.save()
+ total = checkout.get_total()
+ payment = payment_dummy
+ payment.is_active = True
+ payment.order = None
+ payment.total = total.gross.amount
+ payment.currency = total.gross.currency
+ payment.checkout = checkout
+ payment.save()
+ ready, error = ready_to_place_order(checkout, None, None)
+ assert ready
+ assert not error
+
+
+def test_checkout_complete_no_shipping_method(
+ cart_with_item, address):
+ checkout = cart_with_item
+ checkout.shipping_address = address
+ checkout.save()
+ ready, error = ready_to_place_order(checkout, None, None)
+ assert not ready
+ assert error == 'Shipping method is not set'
+
+
+def test_checkout_complete_no_shipping_addres(
+ cart_with_item, shipping_method):
+ checkout = cart_with_item
+ checkout.shipping_method = shipping_method
+ checkout.save()
+ ready, error = ready_to_place_order(checkout, None, None)
+ assert not ready
+ assert error == 'Shipping address is not set'
+
+
+def test_checkout_complete_invalid_shipping_method(
+ cart_with_item, address, shipping_zone_without_countries):
+ checkout = cart_with_item
+ checkout.shipping_address = address
+ shipping_method = shipping_zone_without_countries.shipping_methods.first()
+ checkout.shipping_method = shipping_method
+ checkout.save()
+ ready, error = ready_to_place_order(checkout, None, None)
+ assert not ready
+ assert error == 'Shipping method is not valid for your shipping address'
+
+
+def test_checkout_complete_no_payment(
+ cart_with_item, shipping_method, address):
+ checkout = cart_with_item
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.save()
+ ready, error = ready_to_place_order(checkout, None, None)
+ assert not ready
+ assert error == 'Checkout is not fully paid'
diff --git a/tests/conftest.py b/tests/conftest.py
index f62c3221dd..c68a9a8fe4 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -195,6 +195,16 @@ def shipping_zone(db): # pylint: disable=W0613
return shipping_zone
+def shipping_zone_without_countries(db): # pylint: disable=W0613
+ shipping_zone = ShippingZone.objects.create(
+ name='Europe', countries=[])
+ shipping_zone.shipping_methods.create(
+ name='DHL', minimum_order_price=Money(0, 'USD'),
+ type=ShippingMethodType.PRICE_BASED, price=Money(10, 'USD'),
+ shipping_zone=shipping_zone)
+ return shipping_zone
+
@pytest.fixture
def shipping_method(shipping_zone):
return ShippingMethod.objects.create(