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