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