Skip to content

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

Fix showstopper, add unit and integration test for user entering invalid voucher code

Rafał Pitońdfd042fb8df8
diff --git a/saleor/discount/forms.py b/saleor/discount/forms.py
index 81c5b8bd84..ce5c5bb964 100644
--- a/saleor/discount/forms.py
+++ b/saleor/discount/forms.py
@@ -12,8 +12,7 @@ class VoucherField(forms.ModelChoiceField):
default_error_messages = {
'invalid_choice': pgettext_lazy(
- 'voucher', pgettext_lazy(
- 'Voucher form error', 'Discount code incorrect or expired')),
+ 'Voucher form error', 'Discount code incorrect or expired'),
}
diff --git a/tests/test_checkout_integration.py b/tests/test_checkout_integration.py
index f1d4316be4..7c8dc4a438 100644
--- a/tests/test_checkout_integration.py
+++ b/tests/test_checkout_integration.py
@@ -261,6 +261,49 @@ def test_voucher_invalid(
assert summary_response.context['order'].voucher is None
+def test_voucher_code_invalid(
+ client, request_cart_with_item, shipping_method):
+ # 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.price_per_country.first().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': 'invalid-code'}
+ voucher_response = client.post(
+ '{url}?next={url}'.format(url=url), follow=True, data=discount_data,
+ HTTP_REFERER=url)
+ assert voucher_response.status_code == 200
+ assert 'voucher' in voucher_response.context['voucher_form'].errors
+ assert voucher_response.context['checkout'].voucher_code is None
+
+
def test_remove_voucher(
client, request_cart_with_item, shipping_method, voucher):
# Enter checkout
diff --git a/tests/test_discounts.py b/tests/test_discounts.py
index 0e702bbda4..034cda9020 100644
--- a/tests/test_discounts.py
+++ b/tests/test_discounts.py
@@ -183,7 +183,14 @@ def test_category_voucher_checkout_discount_not_applicable(settings,
assert str(e.value) == 'This offer is only valid for selected items.'
-def test_invalid_checkout_discount_form(monkeypatch, voucher):
+def test_checkout_discount_form_invalid_voucher_code(monkeypatch):
+ checkout = Mock(cart=Mock())
+ form = CheckoutDiscountForm({'voucher': 'invalid'}, checkout=checkout)
+ assert not form.is_valid()
+ assert 'voucher' in form.errors
+
+
+def test_checkout_discount_form_not_applicable_voucher(monkeypatch, voucher):
checkout = Mock(cart=Mock())
form = CheckoutDiscountForm({'voucher': voucher.code}, checkout=checkout)
monkeypatch.setattr(