Skip to content

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

Implemented tests for order placement partials to ensure: - expired voucher aborts order placement; - out of stock lines aborts order.

NyanKiyoshib0b2deea81ae
diff --git a/saleor/checkout/views/summary.py b/saleor/checkout/views/summary.py
index 7350907f8e..59d5544442 100644
--- a/saleor/checkout/views/summary.py
+++ b/saleor/checkout/views/summary.py
@@ -28,6 +28,7 @@ def handle_order_placement(request, cart):
except InsufficientStock:
return redirect('cart:index')
+ # happens when a voucher is now invalid
if not order:
msg = pgettext('Checkout warning', 'Please review your checkout.')
messages.warning(request, msg)
diff --git a/tests/conftest.py b/tests/conftest.py
index 8789ee747f..8b3c909f01 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -155,8 +155,7 @@ def request_cart(cart, monkeypatch):
@pytest.fixture
-def request_cart_with_item(product, request_cart):
- variant = product.variants.get()
+def request_cart_with_item(variant, request_cart):
add_variant_to_cart(request_cart, variant)
return request_cart
@@ -343,6 +342,7 @@ def product(product_type, default_category):
cost_price=Money('1.00', 'USD'), quantity=10, quantity_allocated=1)
return product
+
@pytest.fixture
def variant(product):
product_variant = ProductVariant.objects.create(
@@ -350,6 +350,7 @@ def variant(product):
quantity_allocated=3)
return product_variant
+
@pytest.fixture
def product_without_shipping(default_category):
product_type = ProductType.objects.create(
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index 648b5d5351..d474a6e5c1 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -332,6 +332,73 @@ def test_view_checkout_summary_with_invalid_voucher_code(
assert response.context['cart'].voucher_code is None
+def test_view_checkout_place_order_with_expired_voucher_code(
+ client, request_cart_with_item, shipping_method, address, voucher):
+
+ cart = request_cart_with_item
+
+ # add shipping information to the cart
+ cart.shipping_address = address
+ cart.email = '[email protected]'
+ cart.shipping_method = (
+ shipping_method.price_per_country.first())
+
+ # set voucher to be expired
+ yesterday = datetime.date.today() - datetime.timedelta(days=1)
+ voucher.end_date = yesterday
+ voucher.save()
+
+ # put the voucher code to cart
+ cart.voucher_code = voucher.code
+
+ # save the cart
+ cart.save()
+
+ checkout_url = reverse('checkout:summary')
+
+ # place order
+ data = {'address': 'shipping_address'}
+ response = client.post(checkout_url, data, follow=True)
+
+ # order should not have been placed
+ assert response.request['PATH_INFO'] == checkout_url
+
+ # ensure the voucher was removed
+ cart.refresh_from_db()
+ assert not cart.voucher_code
+
+
+def test_view_checkout_place_order_with_item_out_of_stock(
+ client, request_cart_with_item,
+ shipping_method, address, voucher, variant):
+
+ cart = request_cart_with_item
+
+ # add shipping information to the cart
+ cart.shipping_address = address
+ cart.email = '[email protected]'
+ cart.shipping_method = (
+ shipping_method.price_per_country.first())
+
+ # save the cart
+ cart.save()
+
+ # make the variant be out of stock
+ variant.quantity = 0
+ variant.save()
+
+ checkout_url = reverse('checkout:summary')
+ cart_url = reverse('cart:index')
+
+ # place order
+ data = {'address': 'shipping_address'}
+ response = client.post(checkout_url, data, follow=True)
+
+ # order should have been aborted,
+ # and user should have been redirected to its cart
+ assert response.request['PATH_INFO'] == cart_url
+
+
def test_view_checkout_summary_remove_voucher(
client, request_cart_with_item, shipping_method, voucher, address):
request_cart_with_item.shipping_address = address