Skip to content

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

Test voucher validation

Maciek Jabłońskid7f3b914b2b8
diff --git a/tests/conftest.py b/tests/conftest.py
index e8b61db9ce..66bb18b143 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -2,9 +2,13 @@
from __future__ import unicode_literals
import pytest
+from django.contrib.auth.models import AnonymousUser
+from mock import Mock
from saleor.cart import decorators
from saleor.cart.models import Cart
+from saleor.checkout.core import Checkout
+from saleor.discount.models import Voucher
from saleor.product.models import Product, ProductVariant, Stock
from saleor.shipping.models import ShippingMethod
from saleor.userprofile.models import Address, User
@@ -16,7 +20,7 @@ def cart(db): # pylint: disable=W0613
@pytest.fixture
-def customer_user(db):
+def customer_user(db): # pylint: disable=W0613
return User.objects.create_user('[email protected]', 'password')
@@ -37,7 +41,7 @@ def request_cart_with_item(product_in_stock, request_cart):
@pytest.fixture()
-def admin_user(db):
+def admin_user(db): # pylint: disable=W0613
"""A Django admin user.
"""
return User.objects.create_superuser('[email protected]', 'password')
@@ -70,7 +74,7 @@ def billing_address(db): # pylint: disable=W0613
@pytest.fixture
-def shipping_method(db):
+def shipping_method(db): # pylint: disable=W0613
shipping_method = ShippingMethod.objects.create(name='DHL')
shipping_method.price_per_country.create(price=10)
return shipping_method
@@ -91,3 +95,13 @@ def product_in_stock(db): # pylint: disable=W0613
variant=variant, cost_price=10, quantity=5, quantity_allocated=0,
location='Warehouse 3')
return product
+
+
+def anonymous_checkout():
+ return Checkout(Mock(), AnonymousUser(), 'tracking_code')
+
+
+def voucher(db): # pylint: disable=W0613
+ return Voucher.objects.create(code='mirumee', discount_value=20)
diff --git a/tests/test_checkout_integration.py b/tests/test_checkout_integration.py
index 19cee8a177..7590355971 100644
--- a/tests/test_checkout_integration.py
+++ b/tests/test_checkout_integration.py
@@ -6,7 +6,7 @@ from saleor.userprofile.models import User
from tests.utils import get_redirect_location
-def test_checkout_flow(request_cart_with_item, client, shipping_method): # pylint: disable=W0613
+def test_checkout_flow(request_cart_with_item, client, shipping_method): # pylint: disable=W0613,R0914
"""
Basic test case that confirms if core checkout flow works
"""
@@ -65,7 +65,7 @@ def test_checkout_flow(request_cart_with_item, client, shipping_method): # pyli
assert get_redirect_location(payment_response) == order_details
-def test_checkout_flow_authenticated_user(authorized_client, billing_address, request_cart_with_item,
+def test_checkout_flow_authenticated_user(authorized_client, billing_address, request_cart_with_item, # pylint: disable=R0914
customer_user, shipping_method):
"""
Checkout with authenticated user and previously saved address
@@ -239,3 +239,53 @@ def test_email_is_saved_in_order(authorized_client, billing_address, customer_us
assert updated_user.email != customer_user.email
assert order.user_email == customer_user.email
assert order.get_user_current_email() == updated_user.email
+
+
+def test_voucher_invalid(client, request_cart_with_item, shipping_method, voucher): # pylint: disable=W0613,R0914
+ """
+ Look: #549 #544
+ """
+ voucher.usage_limit = 3
+ voucher.save()
+ # Enter checkout
+ checkout_index = client.get(reverse('checkout:index'), follow=True)
+ # Checkout index redirects directly to shipping address step
+ shipping_address = client.get(checkout_index.request['PATH_INFO'])
+
+ # Enter shipping address data
+ shipping_data = {
+ 'email': '[email protected]',
+ 'first_name': 'John',
+ 'last_name': 'Doe',
+ 'street_address_1': 'Aleje Jerozolimskie 2',
+ 'street_address_2': '',
+ 'city': 'Warszawa',
+ 'city_area': '',
+ 'country_area': '',
+ 'postal_code': '00-374',
+ 'country': 'PL'}
+ shipping_response = client.post(shipping_address.request['PATH_INFO'],
+ data=shipping_data, follow=True)
+
+ # Select shipping method
+ shipping_method_page = client.get(shipping_response.request['PATH_INFO'])
+
+ # Redirect to summary after shipping method selection
+ shipping_method_data = {'method': shipping_method.pk}
+ shipping_method_response = client.post(shipping_method_page.request['PATH_INFO'],
+ data=shipping_method_data, follow=True)
+
+ # Summary page asks for Billing address, default is the same as shipping
+ url = shipping_method_response.request['PATH_INFO']
+ discount_data = {'discount-voucher': voucher.code}
+ voucher_response = client.post('{url}?next={url}'.format(url=url),
+ follow=True, data=discount_data, HTTP_REFERER=url)
+ assert voucher_response.context['checkout'].voucher_code == voucher.code
+ voucher.used = 3
+ voucher.save()
+ address_data = {'address': 'shipping_address'}
+ summary_response = client.post(url, data=address_data, follow=True)
+ assert summary_response.context['checkout'].voucher_code is None
+
+ summary_response = client.post(url, data=address_data, follow=True)
+ assert summary_response.context['order'].voucher is None