Procházet zdrojové kódy

Fix input validation for BenefitForm in dashboard.offers (#2762)

master
Philip Kazmeier před 7 roky
rodič
revize
57e71b8fda

+ 4
- 0
src/oscar/apps/dashboard/offers/forms.py Zobrazit soubor

@@ -144,6 +144,10 @@ class BenefitForm(forms.ModelForm):
144 144
                 raise forms.ValidationError(
145 145
                     _("No other options can be set if you are using a "
146 146
                       "custom incentive"))
147
+        elif not data.get('type'):
148
+            raise forms.ValidationError(
149
+                _("Please either choose a range, type and value OR "
150
+                  "select a custom incentive"))
147 151
 
148 152
         return data
149 153
 

+ 161
- 0
tests/integration/dashboard/test_offer_forms.py Zobrazit soubor

@@ -0,0 +1,161 @@
1
+from decimal import Decimal as D
2
+
3
+from django.core.exceptions import ValidationError
4
+from django.test import TestCase
5
+from oscar.apps.dashboard.offers import forms
6
+from oscar.apps.offer.custom import create_benefit
7
+from oscar.apps.offer.models import Range, Benefit
8
+from oscar.test.factories import create_product
9
+
10
+from tests._site.model_tests_app.models import CustomBenefitModel
11
+
12
+
13
+class TestBenefitForm(TestCase):
14
+
15
+    def setUp(self):
16
+        self.range = Range.objects.create(
17
+            name="All products", includes_all_products=True)
18
+        self.prod = create_product()
19
+
20
+    def test_init_without_custom_benefit(self):
21
+        """
22
+        If no custom benefit exists, the type field should be required.
23
+        """
24
+        form = forms.BenefitForm()
25
+        self.assertTrue(form.fields['type'].required)
26
+
27
+    def test_init_with_custom_benefit(self):
28
+        """
29
+        If a custom benefit exists, the type field should not be required.
30
+        """
31
+        create_benefit(CustomBenefitModel)
32
+        form = forms.BenefitForm()
33
+        self.assertFalse(form.fields['type'].required)
34
+        self.assertEqual(form.fields['custom_benefit'].initial, None)
35
+
36
+    def test_init_with_custom_benefit_with_instance(self):
37
+        """
38
+        If a custom benefit exists and the kwargs instance is passed to init, the initial value for the custom_benefit
39
+        should be the instance.
40
+        """
41
+        benefit = create_benefit(CustomBenefitModel)
42
+        form = forms.BenefitForm(instance=benefit)
43
+        self.assertFalse(form.fields['type'].required)
44
+        self.assertEqual(form.fields['custom_benefit'].initial, benefit.id)
45
+
46
+    def test_is_valid_no_data(self):
47
+        """
48
+        If not data is supplied, is_valid should evaluate to false
49
+        """
50
+        form = forms.BenefitForm()
51
+        self.assertFalse(form.is_valid())
52
+
53
+    def test_clean_no_value_data(self):
54
+        """
55
+        If data is supplied without any values, the form should evaluate to not valid +
56
+        and the clean method should throw a ValidationError
57
+        """
58
+        form = forms.BenefitForm(data={
59
+            'range': '',
60
+            'type': '',
61
+            'value': '',
62
+            'custom_benefit': ''
63
+        })
64
+        self.assertFalse(form.is_valid())
65
+        self.assertRaises(ValidationError, form.clean)
66
+
67
+    def test_clean_new_incentive(self):
68
+        """
69
+        If a range, type and value is supplied, the clean method should return the cleaned data without errors.
70
+        """
71
+        form = forms.BenefitForm(data={
72
+            'range': self.range.id,
73
+            'type': Benefit.FIXED,
74
+            'value': 5,
75
+            'custom_benefit': ''
76
+        })
77
+
78
+        self.assertTrue(form.is_valid())
79
+        self.assertEqual({
80
+            'range': self.range,
81
+            'type': Benefit.FIXED,
82
+            'value': D('5'),
83
+            'custom_benefit': '',
84
+            'max_affected_items': None
85
+        }, form.clean())
86
+
87
+    def test_clean_new_incentive_only_range(self):
88
+        """
89
+        If only a range is supplied, the clean method should throw a ValidationError.
90
+        """
91
+        form = forms.BenefitForm(data={
92
+            'range': self.range.id,
93
+            'type': '',
94
+            'value': '',
95
+            'custom_benefit': ''
96
+        })
97
+        self.assertFalse(form.is_valid())
98
+        self.assertRaises(ValidationError, form.clean)
99
+
100
+    def test_clean_validation_with_custom_benefit(self):
101
+        """
102
+        If a custom benefit is selected, the form should be valid.
103
+        """
104
+        benefit = create_benefit(CustomBenefitModel)
105
+
106
+        form = forms.BenefitForm(data={
107
+            'range': '',
108
+            'type': '',
109
+            'value': '',
110
+            'custom_benefit': benefit.id
111
+        })
112
+
113
+        self.assertTrue(form.is_valid())
114
+        self.assertEqual({
115
+            'range': None,
116
+            'type': '',
117
+            'value': None,
118
+            'custom_benefit': str(benefit.id),
119
+            'max_affected_items': None
120
+        }, form.clean())
121
+
122
+    def test_clean_only_range_custom_exists(self):
123
+        """
124
+        If a custom benefit exists, the type field is not required. Still, the clean method should throw a
125
+        ValidationError, if only the range is supplied.
126
+        """
127
+        create_benefit(CustomBenefitModel)
128
+
129
+        form = forms.BenefitForm(data={
130
+            'range': self.range,
131
+            'type': '',
132
+            'value': '',
133
+            'custom_benefit': ''
134
+        })
135
+
136
+        self.assertFalse(form.is_valid())
137
+        self.assertRaises(ValidationError, form.clean)
138
+
139
+    def test_clean_validation_custom_exists(self):
140
+        """
141
+        If a custom benefit exists, and the data for range, type and value is supplied, the form should validate.
142
+        Clean should return the cleaned data.
143
+        """
144
+
145
+        create_benefit(CustomBenefitModel)
146
+
147
+        form = forms.BenefitForm(data={
148
+            'range': self.range.id,
149
+            'type': Benefit.FIXED,
150
+            'value': 5,
151
+            'custom_benefit': ''
152
+        })
153
+
154
+        self.assertTrue(form.is_valid())
155
+        self.assertEqual({
156
+            'range': self.range,
157
+            'type': Benefit.FIXED,
158
+            'value': D('5'),
159
+            'custom_benefit': '',
160
+            'max_affected_items': None
161
+        }, form.clean())

Načítá se…
Zrušit
Uložit