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 re |
---|
20 | import unittest |
---|
21 | from zope.component import getUtilitiesFor, getSiteManager |
---|
22 | from zope.interface import implements |
---|
23 | from zope.interface.verify import verifyClass, verifyObject |
---|
24 | from waeup.ikoba.payments.interfaces import ( |
---|
25 | IPayment, STATE_UNPAID, STATE_PAID, STATE_FAILED, |
---|
26 | IPaymentGatewayService, IPaymentItem |
---|
27 | ) |
---|
28 | from waeup.ikoba.payments.payment import ( |
---|
29 | Payment, get_payment_providers, PaymentItem, |
---|
30 | ) |
---|
31 | |
---|
32 | |
---|
33 | class HelperTests(unittest.TestCase): |
---|
34 | |
---|
35 | def tearDown(self): |
---|
36 | # unregister any IPaymentGatewayServices |
---|
37 | sm = getSiteManager(None) |
---|
38 | for name, util in getUtilitiesFor(IPaymentGatewayService): |
---|
39 | sm.unregisterUtility(util, name=name) |
---|
40 | |
---|
41 | def test_get_payment_providers_no_providers(self): |
---|
42 | # we can get a dict of all payment providers |
---|
43 | result = get_payment_providers() |
---|
44 | assert isinstance(result, dict) |
---|
45 | assert result == {} |
---|
46 | |
---|
47 | def test_get_payment_providers(self): |
---|
48 | # we get any payment providers registered |
---|
49 | sm = getSiteManager(None) |
---|
50 | |
---|
51 | class FakeUtil(object): |
---|
52 | implements(IPaymentGatewayService) |
---|
53 | |
---|
54 | fake_util = FakeUtil() |
---|
55 | sm.registerUtility(fake_util, name=u'some_name') |
---|
56 | result = get_payment_providers() |
---|
57 | assert isinstance(result, dict) |
---|
58 | assert result.keys() == ['some_name', ] |
---|
59 | assert result['some_name'] is fake_util |
---|
60 | |
---|
61 | |
---|
62 | class PaymentTests(unittest.TestCase): |
---|
63 | |
---|
64 | def test_iface(self): |
---|
65 | # Payments fullfill any interface contracts |
---|
66 | obj = Payment() |
---|
67 | verifyClass(IPayment, Payment) |
---|
68 | verifyObject(IPayment, obj) |
---|
69 | |
---|
70 | def test_payment_id_unique(self): |
---|
71 | # we get unique payment ids |
---|
72 | p1, p2 = Payment(), Payment() |
---|
73 | id1, id2 = p1.payment_id, p2.payment_id |
---|
74 | assert id1 != id2 |
---|
75 | |
---|
76 | def test_payment_id_format(self): |
---|
77 | # payment ids have a special format: "PAY_<32 hex digits>" |
---|
78 | id1 = Payment().payment_id |
---|
79 | assert isinstance(id1, basestring) |
---|
80 | assert re.match('PAY_[0-9a-f]{32}', id1) |
---|
81 | |
---|
82 | def test_initial_state_is_unpaid(self): |
---|
83 | # the initial state of payments is <unpaid> |
---|
84 | p1 = Payment() |
---|
85 | assert p1.state == STATE_UNPAID |
---|
86 | |
---|
87 | def test_approve(self): |
---|
88 | # we can approve payments |
---|
89 | p1 = Payment() |
---|
90 | p1.approve() |
---|
91 | assert p1.state == STATE_PAID |
---|
92 | assert p1.payment_date is not None |
---|
93 | assert isinstance(p1.payment_date, datetime.datetime) |
---|
94 | |
---|
95 | def test_approve_datetime_given(self): |
---|
96 | # we can give a datetime |
---|
97 | p1 = Payment() |
---|
98 | some_datetime = datetime.datetime(2014, 1, 1, 0, 0, 0) |
---|
99 | p1.approve(payment_date=some_datetime) |
---|
100 | assert p1.payment_date == some_datetime |
---|
101 | |
---|
102 | def test_approve_datetime_automatic(self): |
---|
103 | # if we do not give a datetime, current one will be used |
---|
104 | current = datetime.datetime.utcnow() |
---|
105 | p1 = Payment() |
---|
106 | p1.approve() |
---|
107 | assert p1.payment_date >= current |
---|
108 | |
---|
109 | def test_mark_failed(self): |
---|
110 | # we can mark payments as failed |
---|
111 | p1 = Payment() |
---|
112 | p1.mark_failed() |
---|
113 | assert p1.state == STATE_FAILED |
---|
114 | |
---|
115 | |
---|
116 | class PaymentItemTests(unittest.TestCase): |
---|
117 | |
---|
118 | def test_iface(self): |
---|
119 | # PaymentItems fullfill any interface contracts |
---|
120 | obj = PaymentItem() |
---|
121 | verifyClass(IPaymentItem, PaymentItem) |
---|
122 | verifyObject(IPaymentItem, obj) |
---|