source: main/waeup.ikoba/trunk/src/waeup/ikoba/payments/tests/test_payment.py @ 12444

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

pep8 (and test for svn+ssh commit).

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