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