From 02b3644e3c1f02959c3316d4291ec1c819c2baf7 Mon Sep 17 00:00:00 2001 From: David Winterbottom Date: Wed, 17 Apr 2013 09:45:23 +0100 Subject: [PATCH] Fix validation error in dashboard voucher form The clean method was raising a KeyError when the date fields were empty. --- oscar/apps/dashboard/vouchers/forms.py | 6 ++--- tests/unit/dashboard/voucher_form_tests.py | 22 +++++++++++++++++++ tests/unit/voucher/__init__.py | 0 .../model_tests.py} | 5 ++--- 4 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 tests/unit/dashboard/voucher_form_tests.py create mode 100644 tests/unit/voucher/__init__.py rename tests/unit/{voucher_tests.py => voucher/model_tests.py} (93%) diff --git a/oscar/apps/dashboard/vouchers/forms.py b/oscar/apps/dashboard/vouchers/forms.py index 79bee52bfe5..3ac2eefc8f1 100644 --- a/oscar/apps/dashboard/vouchers/forms.py +++ b/oscar/apps/dashboard/vouchers/forms.py @@ -63,9 +63,9 @@ def clean_code(self): def clean(self): cleaned_data = super(VoucherForm, self).clean() - start_date = cleaned_data['start_date'] - end_date = cleaned_data['end_date'] - if end_date < start_date: + start_date = cleaned_data.get('start_date', None) + end_date = cleaned_data.get('end_date', None) + if start_date and end_date and end_date < start_date: raise forms.ValidationError(_("The start date must be before the end date")) return cleaned_data diff --git a/tests/unit/dashboard/voucher_form_tests.py b/tests/unit/dashboard/voucher_form_tests.py new file mode 100644 index 00000000000..beaf43b73e2 --- /dev/null +++ b/tests/unit/dashboard/voucher_form_tests.py @@ -0,0 +1,22 @@ +from django import test + +from oscar.apps.dashboard.vouchers import forms + + +class TestVoucherForm(test.TestCase): + + def test_handles_empty_date_fields(self): + data = {'code': '', + 'name': '', + 'start_date': '', + 'end_date': '', + 'benefit_range': '', + 'benefit_type': 'Percentage', + 'usage': 'Single use'} + form = forms.VoucherForm(data=data) + try: + form.is_valid() + except Exception, e: + import traceback + self.fail("Validating form failed: %s\n\n%s" % ( + e.message, traceback.format_exc())) diff --git a/tests/unit/voucher/__init__.py b/tests/unit/voucher/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/unit/voucher_tests.py b/tests/unit/voucher/model_tests.py similarity index 93% rename from tests/unit/voucher_tests.py rename to tests/unit/voucher/model_tests.py index 742a0224bb9..908752bc272 100644 --- a/tests/unit/voucher_tests.py +++ b/tests/unit/voucher/model_tests.py @@ -23,9 +23,8 @@ def test_saves_code_as_uppercase(self): def test_verifies_dates_are_sensible(self): with self.assertRaises(exceptions.ValidationError): - voucher = Voucher.objects.create(code='lower', - start_date=END_DATE, - end_date=START_DATE) + voucher = Voucher.objects.create( + code='lower', start_date=END_DATE, end_date=START_DATE) voucher.clean()