Skip to content
This repository
Browse code

Virtual barcode errors are fatal

Raise an exception if something bad happens
instead of hiding the error. This may crash
makebills if there's an error in the code (which
is actually a good thing) instead of sending
invalid bills.
  • Loading branch information...
commit 2f96fb40f44447d3ca1b19d976b81f8c937b9489 1 parent d54a886
Joonas Kuorilehto authored March 14, 2011
36  membership/reference_numbers.py
@@ -2,6 +2,12 @@
2 2
 
3 3
 from decimal import Decimal
4 4
 
  5
+class ReferenceNumberException(Exception): pass
  6
+class ReferenceNumberFormatException(ReferenceNumberException): pass
  7
+class IBANFormatException(ReferenceNumberException): pass
  8
+class InvalidAmountException(ReferenceNumberException): pass
  9
+class DueDateFormatException(ReferenceNumberException): pass
  10
+
5 11
 def generate_membership_bill_reference_number(membership_id, bill_year):
6 12
     # [jäsennumero] yyxxz
7 13
     # jossa yy=vuosi kahdella numerolla, xx=maksutapahtumakoodi ja z tarkistenumero
@@ -57,20 +63,20 @@ def canonize_iban(iban):
57 63
         iban = iban[2:]
58 64
     if iban.isdigit() and len(iban) == 16:
59 65
         return iban
60  
-    # TODO: Raise exception instead
61  
-    return 'X' * 16
  66
+    raise IBANFormatException("IBAN format is invalid")
62 67
 
63 68
 def canonize_refnum(refnum, digits = 20):
64 69
     """Removes any whitespace makes sure the number is 20 digits long"""
  70
+    if refnum == None:
  71
+        return '0' * digits
65 72
     refnum = refnum.replace(' ', '')
66 73
     if len(refnum) < digits:
67  
-        # zero pad to enough digits
68  
-        refnum = ("0" * digits) + refnum
69  
-        refnum = refnum [-digits:]
  74
+        refnum = refnum.zfill(digits) # zero pad
70 75
     if refnum.isdigit() and len(refnum) == digits and check_checknumber(refnum):
71 76
         return refnum
72  
-    # TODO: Raise exception instead
73  
-    return '0' * 20
  77
+    else:
  78
+        raise ReferenceNumberFormatException("Reference number '%s' invalid" %
  79
+            refnum)
74 80
 
75 81
 def canonize_sum(euros, cents=0):
76 82
     if cents == 0:
@@ -84,13 +90,15 @@ def canonize_sum(euros, cents=0):
84 90
         # Amount too big, return 0
85 91
         return '000000' + '00'
86 92
     if cents > 99 or euros < 0 or cents < 0:
87  
-        # Invalid amount
88  
-        # TODO: Raise exception instead
89  
-        return '000000' + '00'
  93
+        raise InvalidAmountException("Amount %s euros %s cents invalid" % (
  94
+            euros, cents))
90 95
     return '%06u%02u' % (euros, cents)
91 96
 
92 97
 def canonize_duedate(duedate):
93  
-    if hasattr(duedate, 'strftime'):
94  
-        return duedate.strftime('%y%m%d')
95  
-    # TODO: Raise exception instead
96  
-    return '000000'
  98
+    if duedate == None:
  99
+        return '000000'
  100
+    else:
  101
+        try:
  102
+            return duedate.strftime('%y%m%d')
  103
+        except AttributeError:
  104
+            raise DueDateFormatException("Invalid type for canonize_duedate")
23  membership/tests.py
@@ -29,6 +29,10 @@
29 29
 from reference_numbers import generate_membership_bill_reference_number
30 30
 from reference_numbers import generate_checknumber, add_checknumber, check_checknumber, group_right
31 31
 from reference_numbers import barcode_4, canonize_iban, canonize_refnum, canonize_sum, canonize_duedate
  32
+from reference_numbers import ReferenceNumberException
  33
+from reference_numbers import ReferenceNumberFormatException
  34
+from reference_numbers import IBANFormatException, InvalidAmountException
  35
+from reference_numbers import DueDateFormatException
32 36
 
33 37
 from management.commands.makebills import logger as makebills_logger
34 38
 from management.commands.makebills import makebills
@@ -84,35 +88,36 @@ def test_canonize_iban(self):
84 88
         self.assertEqual(canonize_iban('FI16 5741 3620 4069 56'), '1657413620406956')
85 89
         self.assertEqual(canonize_iban(' 16 5741 3620 4069 56 '), '1657413620406956')
86 90
         self.assertEqual(canonize_iban('1657413620406956'),       '1657413620406956')
87  
-        self.assertEqual(canonize_iban('31231231657413620406956'),'XXXXXXXXXXXXXXXX')
88  
-        self.assertEqual(canonize_iban('SE16 5741 3620 4069 56'), 'XXXXXXXXXXXXXXXX')
89  
-        self.assertEqual(canonize_iban('foobar?'),                'XXXXXXXXXXXXXXXX')
  91
+        self.assertRaises(IBANFormatException, canonize_iban, '31231231657413620406956')
  92
+        self.assertRaises(IBANFormatException, canonize_iban, 'SE16 5741 3620 4069 56')
  93
+        self.assertRaises(IBANFormatException, canonize_iban, 'foobar?')
90 94
 
91 95
     def test_canonize_refnum(self):
92 96
         self.assertEqual(canonize_refnum('42'),                   '00000000000000000042')
93 97
         self.assertEqual(canonize_refnum('32287 22205 1'),        '00000000032287222051')
94  
-        self.assertEqual(canonize_refnum('32287 22205 0'),        '00000000000000000000')
95 98
         self.assertEqual(canonize_refnum('86851 62596 19897'),    '00000868516259619897')
96 99
         self.assertEqual(canonize_refnum('559582243294671'),      '00000559582243294671')
97 100
         self.assertEqual(canonize_refnum('69 87567 20834 35364'), '00069875672083435364')
98 101
         self.assertEqual(canonize_refnum('7 75847 47906 47489'),  '00007758474790647489')
99 102
         self.assertEqual(canonize_refnum('78 77767 96566 28687'), '00078777679656628687')
100  
-        self.assertEqual(canonize_refnum('000000000078777679656628687'), '00078777679656628687')
101 103
         self.assertEqual(canonize_refnum('8 68624'),              '00000000000000868624')
102  
-        self.assertEqual(canonize_refnum('not a ref num'),        '00000000000000000000')
  104
+        self.assertEqual(canonize_refnum(None),                   '00000000000000000000')
  105
+        self.assertRaises(ReferenceNumberException, canonize_refnum, '32287 22205 0')
  106
+        self.assertRaises(ReferenceNumberFormatException, canonize_refnum, '000000000078777679656628687')
  107
+        self.assertRaises(ReferenceNumberFormatException, canonize_refnum, 'not refnum')
103 108
 
104 109
     def test_canonize_sum(self):
105 110
         self.assertEquals(canonize_sum(euros=35, cents=00), '00003500')
106 111
         self.assertEquals(canonize_sum(euros=35, cents=15), '00003515')
107 112
         self.assertEquals(canonize_sum(30), '00003000')
108 113
         self.assertEquals(canonize_sum(123456), '12345600')
109  
-        self.assertEquals(canonize_sum(123456, 101), '00000000')
110 114
         self.assertEquals(canonize_sum(1000000),     '00000000')
111 115
         self.assertEquals(canonize_sum("35"), '00003500')
112 116
         self.assertEquals(canonize_sum("250", "99"), '00025099')
113  
-        self.assertEquals(canonize_sum(-1), '00000000')
114 117
         self.assertEquals(canonize_sum("2.10"), '00000210')
115 118
         self.assertEquals(canonize_sum(Decimal("150.99")), '00015099')
  119
+        self.assertRaises(InvalidAmountException, canonize_sum, -1)
  120
+        self.assertRaises(InvalidAmountException, canonize_sum, 123456, 101)
116 121
 
117 122
     def test_canonize_duedate(self):
118 123
         self.assertEquals(canonize_duedate(datetime(2011, 03 ,20)), '110320')
@@ -122,7 +127,7 @@ def test_canonize_duedate(self):
122 127
         self.assertEquals(canonize_duedate(datetime(2010, 10, 10, 23, 59, 59)), '101010')
123 128
         self.assertEquals(canonize_duedate(datetime(2010, 10, 10, 0, 1, 0)),    '101010')
124 129
         self.assertEquals(canonize_duedate(None), '000000')
125  
-        self.assertEquals(canonize_duedate('HETI'), '000000')
  130
+        self.assertRaises(ReferenceNumberException, canonize_duedate, ('HETI'))
126 131
 
127 132
     # http://www.fkl.fi/www/page/fk_www_1293
128 133
     def test_barcode_4(self):

0 notes on commit 2f96fb4

Please sign in to comment.
Something went wrong with that request. Please try again.