Skip to content

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

Fix discount error message and add a test

Marco Badan58b360f2e9b9
diff --git a/saleor/discount/forms.py b/saleor/discount/forms.py
index 397a80f751..fb1920e03e 100644
--- a/saleor/discount/forms.py
+++ b/saleor/discount/forms.py
@@ -1,4 +1,5 @@
from django import forms
+from django.utils import six
from django.utils.translation import pgettext_lazy
from .models import Voucher, NotApplicable
@@ -38,7 +39,7 @@ class CheckoutDiscountForm(forms.Form):
discount = voucher.get_discount_for_checkout(self.checkout)
cleaned_data['discount'] = discount
except NotApplicable as e:
- self.add_error('voucher', unicode(e))
+ self.add_error('voucher', six.text_type(e))
return cleaned_data
def apply_discount(self):
diff --git a/tests/test_discounts.py b/tests/test_discounts.py
index 6c7d76186a..05064e080e 100644
--- a/tests/test_discounts.py
+++ b/tests/test_discounts.py
@@ -6,6 +6,7 @@ from prices import FixedDiscount, FractionalDiscount, Price
from django_prices.templatetags.prices_i18n import net
+from saleor.discount.forms import CheckoutDiscountForm
from saleor.discount.models import NotApplicable, Sale, Voucher
from saleor.product.models import Product, ProductVariant
@@ -166,6 +167,7 @@ def test_product_voucher_checkout_discount_not_applicable(settings,
discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
discount_value=10)
checkout = Mock(cart=Mock())
+
with pytest.raises(NotApplicable) as e:
voucher.get_discount_for_checkout(checkout)
assert str(e.value) == 'This offer is only valid for selected items.'
@@ -186,6 +188,16 @@ 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):
+ checkout = Mock(cart=Mock())
+ form = CheckoutDiscountForm({'voucher': voucher.code}, checkout=checkout)
+ monkeypatch.setattr(
+ 'saleor.discount.models.Voucher.get_discount_for_checkout',
+ Mock(side_effect=NotApplicable('Not applicable')))
+ assert not form.is_valid()
+ assert 'voucher' in form.errors
+
+
@pytest.mark.parametrize(
'prices, discount_value, discount_type, apply_to, expected_value', [
([10], 10, Voucher.DISCOUNT_VALUE_FIXED, Voucher.APPLY_TO_ONE_PRODUCT, 10), # noqa