1 | ## $Id$ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | import datetime |
---|
19 | import decimal |
---|
20 | import re |
---|
21 | import unittest |
---|
22 | from zope.component import ( |
---|
23 | getUtilitiesFor, getSiteManager, queryUtility, getGlobalSiteManager, |
---|
24 | ) |
---|
25 | from zope.component.hooks import setSite |
---|
26 | from zope.interface import implements |
---|
27 | from zope.interface.verify import verifyClass, verifyObject |
---|
28 | from waeup.ikoba.payments.interfaces import ( |
---|
29 | IPayment, STATE_UNPAID, STATE_PAID, STATE_FAILED, |
---|
30 | IPaymentGatewayService, IPaymentItem, IPaymentGatewayServicesLister, |
---|
31 | IPayableFinder, |
---|
32 | ) |
---|
33 | from waeup.ikoba.app import Company |
---|
34 | from waeup.ikoba.payments.payment import ( |
---|
35 | Payment, get_payment_providers, PaymentItem, format_payment_item_values, |
---|
36 | get_payment, find_payable_from_payable_id, |
---|
37 | ) |
---|
38 | from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase) |
---|
39 | |
---|
40 | |
---|
41 | class HelperTests(unittest.TestCase): |
---|
42 | |
---|
43 | def tearDown(self): |
---|
44 | # unregister any IPaymentGatewayServices |
---|
45 | sm = getSiteManager(None) |
---|
46 | for name, util in getUtilitiesFor(IPaymentGatewayService): |
---|
47 | sm.unregisterUtility(util, name=name) |
---|
48 | |
---|
49 | def test_get_payment_providers_no_providers(self): |
---|
50 | # we can get a dict of all payment providers |
---|
51 | result = get_payment_providers() |
---|
52 | assert isinstance(result, dict) |
---|
53 | assert result == {} |
---|
54 | |
---|
55 | def test_get_payment_providers(self): |
---|
56 | # we get any payment providers registered |
---|
57 | sm = getSiteManager(None) |
---|
58 | |
---|
59 | class FakeUtil(object): |
---|
60 | implements(IPaymentGatewayService) |
---|
61 | |
---|
62 | fake_util = FakeUtil() |
---|
63 | sm.registerUtility(fake_util, name=u'some_name') |
---|
64 | result = get_payment_providers() |
---|
65 | assert isinstance(result, dict) |
---|
66 | assert result.keys() == ['some_name', ] |
---|
67 | assert result['some_name'] is fake_util |
---|
68 | |
---|
69 | def test_format_payment_item_values(self): |
---|
70 | # we can format lists of payment item values |
---|
71 | result = format_payment_item_values( |
---|
72 | [(u'Item 1', 'USD', decimal.Decimal("12.123")), |
---|
73 | (u'Item 2', 'USD', decimal.Decimal("12.002")), |
---|
74 | ], 'USD') |
---|
75 | self.assertEqual( |
---|
76 | result, [(u'Item 1', 'USD 12.12'), |
---|
77 | (u'Item 2', 'USD 12.00'), |
---|
78 | (u'Total', 'USD 24.12')] |
---|
79 | ) |
---|
80 | |
---|
81 | def test_format_payment_item_values_req_single_currency(self): |
---|
82 | # we require one currency for all items, yet. |
---|
83 | self.assertRaises( |
---|
84 | ValueError, format_payment_item_values, |
---|
85 | [(u'Item 1', 'USD', decimal.Decimal("12.12")), |
---|
86 | (u'Item 2', 'EUR', decimal.Decimal("50")), |
---|
87 | ], |
---|
88 | 'USD') |
---|
89 | |
---|
90 | |
---|
91 | class FunctionalHelperTests(FunctionalTestCase): |
---|
92 | |
---|
93 | layer = FunctionalLayer |
---|
94 | |
---|
95 | def test_services_lister_is_registered(self): |
---|
96 | # a lister of gateway services is registered on startup |
---|
97 | util = queryUtility(IPaymentGatewayServicesLister) |
---|
98 | assert util is not None |
---|
99 | |
---|
100 | def test_services_are_really_listed(self): |
---|
101 | # we can really get locally registered gateways when calling |
---|
102 | util = queryUtility(IPaymentGatewayServicesLister) |
---|
103 | assert len(util()) > 0 |
---|
104 | |
---|
105 | def test_add_payment_item(self): |
---|
106 | # we can add payment items |
---|
107 | p1 = Payment() |
---|
108 | item1 = PaymentItem() |
---|
109 | result = p1.add_payment_item(item1) |
---|
110 | assert len(p1) == 1 # do not make assumptions about result content |
---|
111 | assert isinstance(result, basestring) |
---|
112 | |
---|
113 | def test_get_payment(self): |
---|
114 | # we can lookup payments. |
---|
115 | self.getRootFolder()['app'] = Company() |
---|
116 | app = self.getRootFolder()['app'] |
---|
117 | setSite(app) |
---|
118 | p1 = Payment() |
---|
119 | item1 = PaymentItem() |
---|
120 | app['payments']['1'] = p1 |
---|
121 | p1.add_payment_item(item1) |
---|
122 | p_id = p1.payment_id |
---|
123 | result = get_payment(p_id) |
---|
124 | self.assertTrue(result is p1) |
---|
125 | self.assertTrue(get_payment('not-valid') is None) |
---|
126 | |
---|
127 | def test_find_payable_from_payable_id(self): |
---|
128 | # we can find payables. |
---|
129 | obj1 = object() |
---|
130 | obj2 = object() |
---|
131 | |
---|
132 | class FakeFinder(object): |
---|
133 | valid = {'id1': obj1, 'id3': obj2} |
---|
134 | |
---|
135 | def get_payable_by_id(self, the_id): |
---|
136 | return self.valid.get(the_id) |
---|
137 | |
---|
138 | finder1 = FakeFinder() |
---|
139 | finder1.valid = {'id1': obj1} |
---|
140 | finder2 = FakeFinder() |
---|
141 | finder2.valid = {'id2': obj2} |
---|
142 | gsm = getGlobalSiteManager() |
---|
143 | try: |
---|
144 | gsm.registerUtility(finder1, provided=IPayableFinder, name='f1') |
---|
145 | gsm.registerUtility(finder2, provided=IPayableFinder, name='f2') |
---|
146 | result1 = find_payable_from_payable_id('id1') |
---|
147 | result2 = find_payable_from_payable_id('id2') |
---|
148 | result3 = find_payable_from_payable_id('id3') |
---|
149 | finally: |
---|
150 | gsm.unregisterUtility(finder1, IPayableFinder) |
---|
151 | gsm.unregisterUtility(finder2, IPayableFinder) |
---|
152 | self.assertTrue(result1 is obj1) |
---|
153 | self.assertTrue(result2 is obj2) |
---|
154 | self.assertTrue(result3 is None) |
---|
155 | |
---|
156 | |
---|
157 | class PaymentTests(unittest.TestCase): |
---|
158 | |
---|
159 | def test_iface(self): |
---|
160 | # Payments fullfill any interface contracts |
---|
161 | obj = Payment() |
---|
162 | verifyClass(IPayment, Payment) |
---|
163 | verifyObject(IPayment, obj) |
---|
164 | |
---|
165 | def test_payment_id_unique(self): |
---|
166 | # we get unique payment ids |
---|
167 | p1, p2 = Payment(), Payment() |
---|
168 | id1, id2 = p1.payment_id, p2.payment_id |
---|
169 | assert id1 != id2 |
---|
170 | |
---|
171 | def test_payment_id_format(self): |
---|
172 | # payment ids have a special format: "PAY_<32 hex digits>" |
---|
173 | id1 = Payment().payment_id |
---|
174 | assert isinstance(id1, basestring) |
---|
175 | assert re.match('PAY_[0-9a-f]{32}', id1) |
---|
176 | |
---|
177 | def test_initial_state_is_unpaid(self): |
---|
178 | # the initial state of payments is <unpaid> |
---|
179 | p1 = Payment() |
---|
180 | assert p1.state == STATE_UNPAID |
---|
181 | |
---|
182 | def test_approve(self): |
---|
183 | # we can approve payments |
---|
184 | p1 = Payment() |
---|
185 | p1.approve() |
---|
186 | assert p1.state == STATE_PAID |
---|
187 | assert p1.payment_date is not None |
---|
188 | assert isinstance(p1.payment_date, datetime.datetime) |
---|
189 | |
---|
190 | def test_approve_datetime_given(self): |
---|
191 | # we can give a datetime |
---|
192 | p1 = Payment() |
---|
193 | some_datetime = datetime.datetime(2014, 1, 1, 0, 0, 0) |
---|
194 | p1.approve(payment_date=some_datetime) |
---|
195 | assert p1.payment_date == some_datetime |
---|
196 | |
---|
197 | def test_approve_datetime_automatic(self): |
---|
198 | # if we do not give a datetime, current one will be used |
---|
199 | current = datetime.datetime.utcnow() |
---|
200 | p1 = Payment() |
---|
201 | p1.approve() |
---|
202 | assert p1.payment_date >= current |
---|
203 | |
---|
204 | def test_mark_failed(self): |
---|
205 | # we can mark payments as failed |
---|
206 | p1 = Payment() |
---|
207 | p1.mark_failed() |
---|
208 | assert p1.state == STATE_FAILED |
---|
209 | |
---|
210 | def test_add_payment_item(self): |
---|
211 | # we can add payment items |
---|
212 | p1 = Payment() |
---|
213 | item1 = PaymentItem() |
---|
214 | result = p1.add_payment_item(item1) |
---|
215 | assert len(p1) == 1 # do not make assumptions about result content |
---|
216 | assert isinstance(result, basestring) |
---|
217 | |
---|
218 | def test_add_payment_item_multiple(self): |
---|
219 | # we can add several items |
---|
220 | p1 = Payment() |
---|
221 | item1 = PaymentItem() |
---|
222 | item2 = PaymentItem() |
---|
223 | result1 = p1.add_payment_item(item1) |
---|
224 | result2 = p1.add_payment_item(item2) |
---|
225 | assert len(p1) == 2 # do not make assumptions about result content |
---|
226 | assert isinstance(result1, basestring) |
---|
227 | assert isinstance(result2, basestring) |
---|
228 | |
---|
229 | def test_amount(self): |
---|
230 | # the amount of a payment is the sum of amounts of its items |
---|
231 | p1 = Payment() |
---|
232 | item1 = PaymentItem() |
---|
233 | item2 = PaymentItem() |
---|
234 | p1.add_payment_item(item1) |
---|
235 | p1.add_payment_item(item2) |
---|
236 | item1.amount = decimal.Decimal("12.25") |
---|
237 | item2.amount = decimal.Decimal("0.5") |
---|
238 | assert p1.amount == decimal.Decimal("12.75") |
---|
239 | |
---|
240 | def test_amount_negative(self): |
---|
241 | # we can sum up negative numbers |
---|
242 | p1 = Payment() |
---|
243 | item1 = PaymentItem() |
---|
244 | item2 = PaymentItem() |
---|
245 | p1.add_payment_item(item1) |
---|
246 | p1.add_payment_item(item2) |
---|
247 | item1.amount = decimal.Decimal("2.21") |
---|
248 | item2.amount = decimal.Decimal("-3.23") |
---|
249 | assert p1.amount == decimal.Decimal("-1.02") |
---|
250 | |
---|
251 | def test_amount_empty(self): |
---|
252 | # the amount of zero items is 0.00. |
---|
253 | p1 = Payment() |
---|
254 | assert p1.amount == decimal.Decimal("0.00") |
---|
255 | assert isinstance(p1.amount, decimal.Decimal) |
---|
256 | |
---|
257 | |
---|
258 | class PaymentItemTests(unittest.TestCase): |
---|
259 | |
---|
260 | def test_iface(self): |
---|
261 | # PaymentItems fullfill any interface contracts |
---|
262 | obj = PaymentItem() |
---|
263 | verifyClass(IPaymentItem, PaymentItem) |
---|
264 | verifyObject(IPaymentItem, obj) |
---|