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 getUtilitiesFor, getSiteManager, queryUtility |
---|
23 | from zope.component.hooks import setSite |
---|
24 | from zope.interface import implements |
---|
25 | from zope.interface.verify import verifyClass, verifyObject |
---|
26 | from waeup.ikoba.payments.interfaces import ( |
---|
27 | IPayment, STATE_UNPAID, STATE_PAID, STATE_FAILED, |
---|
28 | IPaymentGatewayService, IPaymentItem, IPaymentGatewayServicesLister, |
---|
29 | ) |
---|
30 | from waeup.ikoba.app import Company |
---|
31 | from waeup.ikoba.payments.payment import ( |
---|
32 | Payment, get_payment_providers, PaymentItem, format_payment_item_values, |
---|
33 | get_payment, |
---|
34 | ) |
---|
35 | from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase) |
---|
36 | |
---|
37 | |
---|
38 | class HelperTests(unittest.TestCase): |
---|
39 | |
---|
40 | def tearDown(self): |
---|
41 | # unregister any IPaymentGatewayServices |
---|
42 | sm = getSiteManager(None) |
---|
43 | for name, util in getUtilitiesFor(IPaymentGatewayService): |
---|
44 | sm.unregisterUtility(util, name=name) |
---|
45 | |
---|
46 | def test_get_payment_providers_no_providers(self): |
---|
47 | # we can get a dict of all payment providers |
---|
48 | result = get_payment_providers() |
---|
49 | assert isinstance(result, dict) |
---|
50 | assert result == {} |
---|
51 | |
---|
52 | def test_get_payment_providers(self): |
---|
53 | # we get any payment providers registered |
---|
54 | sm = getSiteManager(None) |
---|
55 | |
---|
56 | class FakeUtil(object): |
---|
57 | implements(IPaymentGatewayService) |
---|
58 | |
---|
59 | fake_util = FakeUtil() |
---|
60 | sm.registerUtility(fake_util, name=u'some_name') |
---|
61 | result = get_payment_providers() |
---|
62 | assert isinstance(result, dict) |
---|
63 | assert result.keys() == ['some_name', ] |
---|
64 | assert result['some_name'] is fake_util |
---|
65 | |
---|
66 | def test_format_payment_item_values(self): |
---|
67 | # we can format lists of payment item values |
---|
68 | result = format_payment_item_values( |
---|
69 | [(u'Item 1', 'USD', decimal.Decimal("12.123")), |
---|
70 | (u'Item 2', 'USD', decimal.Decimal("12.002")), |
---|
71 | ], 'USD') |
---|
72 | self.assertEqual( |
---|
73 | result, [(u'Item 1', 'USD 12.12'), |
---|
74 | (u'Item 2', 'USD 12.00'), |
---|
75 | (u'Total', 'USD 24.12')] |
---|
76 | ) |
---|
77 | |
---|
78 | def test_format_payment_item_values_req_single_currency(self): |
---|
79 | # we require one currency for all items, yet. |
---|
80 | self.assertRaises( |
---|
81 | ValueError, format_payment_item_values, |
---|
82 | [(u'Item 1', 'USD', decimal.Decimal("12.12")), |
---|
83 | (u'Item 2', 'EUR', decimal.Decimal("50")), |
---|
84 | ], |
---|
85 | 'USD') |
---|
86 | |
---|
87 | |
---|
88 | class FunctionalHelperTests(FunctionalTestCase): |
---|
89 | |
---|
90 | layer = FunctionalLayer |
---|
91 | |
---|
92 | def test_services_lister_is_registered(self): |
---|
93 | # a lister of gateway services is registered on startup |
---|
94 | util = queryUtility(IPaymentGatewayServicesLister) |
---|
95 | assert util is not None |
---|
96 | |
---|
97 | def test_services_are_really_listed(self): |
---|
98 | # we can really get locally registered gateways when calling |
---|
99 | util = queryUtility(IPaymentGatewayServicesLister) |
---|
100 | assert len(util()) > 0 |
---|
101 | |
---|
102 | def test_add_payment_item(self): |
---|
103 | # we can add payment items |
---|
104 | p1 = Payment() |
---|
105 | item1 = PaymentItem() |
---|
106 | result = p1.add_payment_item(item1) |
---|
107 | assert len(p1) == 1 # do not make assumptions about result content |
---|
108 | assert isinstance(result, basestring) |
---|
109 | |
---|
110 | def test_get_payment(self): |
---|
111 | # we can lookup payments. |
---|
112 | self.getRootFolder()['app'] = Company() |
---|
113 | app = self.getRootFolder()['app'] |
---|
114 | setSite(app) |
---|
115 | p1 = Payment() |
---|
116 | item1 = PaymentItem() |
---|
117 | app['payments']['1'] = p1 |
---|
118 | p1.add_payment_item(item1) |
---|
119 | p_id = p1.payment_id |
---|
120 | result= get_payment(p_id) |
---|
121 | self.assertTrue(result is p1) |
---|
122 | self.assertTrue(get_payment('not-valid') is None) |
---|
123 | |
---|
124 | |
---|
125 | class PaymentTests(unittest.TestCase): |
---|
126 | |
---|
127 | def test_iface(self): |
---|
128 | # Payments fullfill any interface contracts |
---|
129 | obj = Payment() |
---|
130 | verifyClass(IPayment, Payment) |
---|
131 | verifyObject(IPayment, obj) |
---|
132 | |
---|
133 | def test_payment_id_unique(self): |
---|
134 | # we get unique payment ids |
---|
135 | p1, p2 = Payment(), Payment() |
---|
136 | id1, id2 = p1.payment_id, p2.payment_id |
---|
137 | assert id1 != id2 |
---|
138 | |
---|
139 | def test_payment_id_format(self): |
---|
140 | # payment ids have a special format: "PAY_<32 hex digits>" |
---|
141 | id1 = Payment().payment_id |
---|
142 | assert isinstance(id1, basestring) |
---|
143 | assert re.match('PAY_[0-9a-f]{32}', id1) |
---|
144 | |
---|
145 | def test_initial_state_is_unpaid(self): |
---|
146 | # the initial state of payments is <unpaid> |
---|
147 | p1 = Payment() |
---|
148 | assert p1.state == STATE_UNPAID |
---|
149 | |
---|
150 | def test_approve(self): |
---|
151 | # we can approve payments |
---|
152 | p1 = Payment() |
---|
153 | p1.approve() |
---|
154 | assert p1.state == STATE_PAID |
---|
155 | assert p1.payment_date is not None |
---|
156 | assert isinstance(p1.payment_date, datetime.datetime) |
---|
157 | |
---|
158 | def test_approve_datetime_given(self): |
---|
159 | # we can give a datetime |
---|
160 | p1 = Payment() |
---|
161 | some_datetime = datetime.datetime(2014, 1, 1, 0, 0, 0) |
---|
162 | p1.approve(payment_date=some_datetime) |
---|
163 | assert p1.payment_date == some_datetime |
---|
164 | |
---|
165 | def test_approve_datetime_automatic(self): |
---|
166 | # if we do not give a datetime, current one will be used |
---|
167 | current = datetime.datetime.utcnow() |
---|
168 | p1 = Payment() |
---|
169 | p1.approve() |
---|
170 | assert p1.payment_date >= current |
---|
171 | |
---|
172 | def test_mark_failed(self): |
---|
173 | # we can mark payments as failed |
---|
174 | p1 = Payment() |
---|
175 | p1.mark_failed() |
---|
176 | assert p1.state == STATE_FAILED |
---|
177 | |
---|
178 | def test_add_payment_item(self): |
---|
179 | # we can add payment items |
---|
180 | p1 = Payment() |
---|
181 | item1 = PaymentItem() |
---|
182 | result = p1.add_payment_item(item1) |
---|
183 | assert len(p1) == 1 # do not make assumptions about result content |
---|
184 | assert isinstance(result, basestring) |
---|
185 | |
---|
186 | def test_add_payment_item_multiple(self): |
---|
187 | # we can add several items |
---|
188 | p1 = Payment() |
---|
189 | item1 = PaymentItem() |
---|
190 | item2 = PaymentItem() |
---|
191 | result1 = p1.add_payment_item(item1) |
---|
192 | result2 = p1.add_payment_item(item2) |
---|
193 | assert len(p1) == 2 # do not make assumptions about result content |
---|
194 | assert isinstance(result1, basestring) |
---|
195 | assert isinstance(result2, basestring) |
---|
196 | |
---|
197 | def test_amount(self): |
---|
198 | # the amount of a payment is the sum of amounts of its items |
---|
199 | p1 = Payment() |
---|
200 | item1 = PaymentItem() |
---|
201 | item2 = PaymentItem() |
---|
202 | p1.add_payment_item(item1) |
---|
203 | p1.add_payment_item(item2) |
---|
204 | item1.amount = decimal.Decimal("12.25") |
---|
205 | item2.amount = decimal.Decimal("0.5") |
---|
206 | assert p1.amount == decimal.Decimal("12.75") |
---|
207 | |
---|
208 | def test_amount_negative(self): |
---|
209 | # we can sum up negative numbers |
---|
210 | p1 = Payment() |
---|
211 | item1 = PaymentItem() |
---|
212 | item2 = PaymentItem() |
---|
213 | p1.add_payment_item(item1) |
---|
214 | p1.add_payment_item(item2) |
---|
215 | item1.amount = decimal.Decimal("2.21") |
---|
216 | item2.amount = decimal.Decimal("-3.23") |
---|
217 | assert p1.amount == decimal.Decimal("-1.02") |
---|
218 | |
---|
219 | def test_amount_empty(self): |
---|
220 | # the amount of zero items is 0.00. |
---|
221 | p1 = Payment() |
---|
222 | assert p1.amount == decimal.Decimal("0.00") |
---|
223 | assert isinstance(p1.amount, decimal.Decimal) |
---|
224 | |
---|
225 | |
---|
226 | class PaymentItemTests(unittest.TestCase): |
---|
227 | |
---|
228 | def test_iface(self): |
---|
229 | # PaymentItems fullfill any interface contracts |
---|
230 | obj = PaymentItem() |
---|
231 | verifyClass(IPaymentItem, PaymentItem) |
---|
232 | verifyObject(IPaymentItem, obj) |
---|