source: main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/payments/tests/test_payment.py @ 12696

Last change on this file since 12696 was 12696, checked in by uli, 10 years ago

payment item transformer.

File size: 7.1 KB
Line 
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##
18import datetime
19import decimal
20import re
21import unittest
22from zope.component import getUtilitiesFor, getSiteManager, queryUtility
23from zope.interface import implements
24from zope.interface.verify import verifyClass, verifyObject
25from waeup.ikoba.payments.interfaces import (
26    IPayment, STATE_UNPAID, STATE_PAID, STATE_FAILED,
27    IPaymentGatewayService, IPaymentItem, IPaymentGatewayServicesLister,
28    )
29from waeup.ikoba.payments.payment import (
30    Payment, get_payment_providers, PaymentItem, format_payment_item_values,
31    )
32from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase)
33
34
35class 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    def test_format_payment_item_values(self):
64        # we can format lists of payment item values
65        result = format_payment_item_values(
66            [(u'Item 1', 'USD', decimal.Decimal("12.123")),
67             (u'Item 2', 'USD', decimal.Decimal("12.002")),
68             ], 'USD')
69        self.assertEqual(
70            result, [(u'Item 1', 'USD 12.12'),
71                     (u'Item 2', 'USD 12.00'),
72                     (u'Total', 'USD 24.12')]
73            )
74
75    def test_format_payment_item_values_req_single_currency(self):
76        # we require one currency for all items, yet.
77        self.assertRaises(
78            ValueError, format_payment_item_values,
79            [(u'Item 1', 'USD', decimal.Decimal("12.12")),
80             (u'Item 2', 'EUR', decimal.Decimal("50")),
81             ],
82            'USD')
83
84
85class FunctionalHelperTests(FunctionalTestCase):
86
87    layer = FunctionalLayer
88
89    def test_services_lister_is_registered(self):
90        # a lister of gateway services is registered on startup
91        util = queryUtility(IPaymentGatewayServicesLister)
92        assert util is not None
93
94    def test_services_are_really_listed(self):
95        # we can really get locally registered gateways when calling
96        util = queryUtility(IPaymentGatewayServicesLister)
97        assert len(util()) > 0
98
99
100class PaymentTests(unittest.TestCase):
101
102    def test_iface(self):
103        # Payments fullfill any interface contracts
104        obj = Payment()
105        verifyClass(IPayment, Payment)
106        verifyObject(IPayment, obj)
107
108    def test_payment_id_unique(self):
109        # we get unique payment ids
110        p1, p2 = Payment(), Payment()
111        id1, id2 = p1.payment_id, p2.payment_id
112        assert id1 != id2
113
114    def test_payment_id_format(self):
115        # payment ids have a special format: "PAY_<32 hex digits>"
116        id1 = Payment().payment_id
117        assert isinstance(id1, basestring)
118        assert re.match('PAY_[0-9a-f]{32}', id1)
119
120    def test_initial_state_is_unpaid(self):
121        # the initial state of payments is <unpaid>
122        p1 = Payment()
123        assert p1.state == STATE_UNPAID
124
125    def test_approve(self):
126        # we can approve payments
127        p1 = Payment()
128        p1.approve()
129        assert p1.state == STATE_PAID
130        assert p1.payment_date is not None
131        assert isinstance(p1.payment_date, datetime.datetime)
132
133    def test_approve_datetime_given(self):
134        # we can give a datetime
135        p1 = Payment()
136        some_datetime = datetime.datetime(2014, 1, 1, 0, 0, 0)
137        p1.approve(payment_date=some_datetime)
138        assert p1.payment_date == some_datetime
139
140    def test_approve_datetime_automatic(self):
141        # if we do not give a datetime, current one will be used
142        current = datetime.datetime.utcnow()
143        p1 = Payment()
144        p1.approve()
145        assert p1.payment_date >= current
146
147    def test_mark_failed(self):
148        # we can mark payments as failed
149        p1 = Payment()
150        p1.mark_failed()
151        assert p1.state == STATE_FAILED
152
153    def test_add_payment_item(self):
154        # we can add payment items
155        p1 = Payment()
156        item1 = PaymentItem()
157        result = p1.add_payment_item(item1)
158        assert len(p1) == 1  # do not make assumptions about result content
159        assert isinstance(result, basestring)
160
161    def test_add_payment_item_multiple(self):
162        # we can add several items
163        p1 = Payment()
164        item1 = PaymentItem()
165        item2 = PaymentItem()
166        result1 = p1.add_payment_item(item1)
167        result2 = p1.add_payment_item(item2)
168        assert len(p1) == 2  # do not make assumptions about result content
169        assert isinstance(result1, basestring)
170        assert isinstance(result2, basestring)
171
172    def test_amount(self):
173        # the amount of a payment is the sum of amounts of its items
174        p1 = Payment()
175        item1 = PaymentItem()
176        item2 = PaymentItem()
177        p1.add_payment_item(item1)
178        p1.add_payment_item(item2)
179        item1.amount = decimal.Decimal("12.25")
180        item2.amount = decimal.Decimal("0.5")
181        assert p1.amount == decimal.Decimal("12.75")
182
183    def test_amount_negative(self):
184        # we can sum up negative numbers
185        p1 = Payment()
186        item1 = PaymentItem()
187        item2 = PaymentItem()
188        p1.add_payment_item(item1)
189        p1.add_payment_item(item2)
190        item1.amount = decimal.Decimal("2.21")
191        item2.amount = decimal.Decimal("-3.23")
192        assert p1.amount == decimal.Decimal("-1.02")
193
194    def test_amount_empty(self):
195        # the amount of zero items is 0.00.
196        p1 = Payment()
197        assert p1.amount == decimal.Decimal("0.00")
198        assert isinstance(p1.amount, decimal.Decimal)
199
200
201class PaymentItemTests(unittest.TestCase):
202
203    def test_iface(self):
204        # PaymentItems fullfill any interface contracts
205        obj = PaymentItem()
206        verifyClass(IPaymentItem, PaymentItem)
207        verifyObject(IPaymentItem, obj)
Note: See TracBrowser for help on using the repository browser.