Преглед на файлове

Merge pull request #1832 from regulusweb/fix/eventhandler_no_lines_validation

Fix validate_payment_event for payment events with no lines specified
master
Maik Hoepfel преди 10 години
родител
ревизия
00a6b597ca
променени са 2 файла, в които са добавени 67 реда и са изтрити 10 реда
  1. 11
    10
      src/oscar/apps/order/processing.py
  2. 56
    0
      tests/unit/order/processing_tests.py

+ 11
- 10
src/oscar/apps/order/processing.py Целия файл

@@ -90,16 +90,17 @@ class EventHandler(object):
90 90
         if errors:
91 91
             raise exceptions.InvalidShippingEvent(", ".join(errors))
92 92
 
93
-    def validate_payment_event(self, order, event_type, amount, lines,
94
-                               line_quantities, **kwargs):
95
-        errors = []
96
-        for line, qty in zip(lines, line_quantities):
97
-            if not line.is_payment_event_permitted(event_type, qty):
98
-                msg = _("The selected quantity for line #%(line_id)s is too"
99
-                        " large") % {'line_id': line.id}
100
-                errors.append(msg)
101
-        if errors:
102
-            raise exceptions.InvalidPaymentEvent(", ".join(errors))
93
+    def validate_payment_event(self, order, event_type, amount, lines=None,
94
+                               line_quantities=None, **kwargs):
95
+        if lines and line_quantities:
96
+            errors = []
97
+            for line, qty in zip(lines, line_quantities):
98
+                if not line.is_payment_event_permitted(event_type, qty):
99
+                    msg = _("The selected quantity for line #%(line_id)s is too"
100
+                            " large") % {'line_id': line.id}
101
+                    errors.append(msg)
102
+            if errors:
103
+                raise exceptions.InvalidPaymentEvent(", ".join(errors))
103 104
 
104 105
     # Query methods
105 106
     # -------------

+ 56
- 0
tests/unit/order/processing_tests.py Целия файл

@@ -0,0 +1,56 @@
1
+from decimal import Decimal as D
2
+
3
+from django.test import TestCase
4
+import mock
5
+import six
6
+
7
+from oscar.apps.order import processing
8
+from oscar.apps.order import exceptions
9
+from oscar.apps.order.models import Line
10
+from oscar.test.factories import create_order
11
+
12
+
13
+class TestValidatePaymentEvent(TestCase):
14
+
15
+    def setUp(self):
16
+        self.event_handler = processing.EventHandler()
17
+
18
+    def test_valid_lines(self):
19
+        order = mock.Mock()
20
+        lines = [mock.Mock() for r in range(3)]
21
+        line_quantities = [line.quantity for line in lines]
22
+        self.event_handler.validate_payment_event(order, 'pre-auth',
23
+                                                  D('10.00'), lines,
24
+                                                  line_quantities)
25
+        # Has each line has been checked
26
+        for line in lines:
27
+            line.is_payment_event_permitted.assert_called_with('pre-auth',
28
+                                                               line.quantity)
29
+
30
+    def test_invalid_lines(self):
31
+        order = mock.Mock()
32
+        invalid_line = mock.Mock()
33
+        invalid_line.is_payment_event_permitted.return_value = False
34
+        invalid_line.id = 6
35
+        lines = [
36
+            mock.Mock(),
37
+            invalid_line,
38
+            mock.Mock(),
39
+        ]
40
+        line_quantities = [line.quantity for line in lines]
41
+
42
+        error = "The selected quantity for line #6 is too large"
43
+
44
+        with six.assertRaisesRegex(self, exceptions.InvalidPaymentEvent, error):
45
+            self.event_handler.validate_payment_event(order, 'payment',
46
+                                                      D('10.00'), lines,
47
+                                                      line_quantities)
48
+
49
+    def test_no_lines(self):
50
+        order = mock.Mock()
51
+        lines = None
52
+        line_quantities = None
53
+        out = self.event_handler.validate_payment_event(order, 'payment',
54
+                                                  D('10.00'), lines,
55
+                                                  line_quantities)
56
+        self.assertIsNone(out)

Loading…
Отказ
Запис