source: main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/payments/tests/test_paypal.py @ 12132

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

Merge changes from uli-payments back into trunk.

  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1## $Id: test_paypal.py 12060 2014-11-25 18:44:01Z uli $
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 os
19import shutil
20import tempfile
21import unittest
22import paypalrestsdk
23from zope.component import getGlobalSiteManager, queryUtility
24from waeup.ikoba.interfaces import IPayPalConfig
25from waeup.ikoba.payments.paypal import (
26    get_paypal_config_file_path, parse_paypal_config, get_access_token,
27    configure_sdk, get_payment,
28    )
29from waeup.ikoba.testing import (
30    FunctionalLayer, FunctionalTestCase,
31    )
32
33#
34# PayPal test config
35#
36
37EXTERNAL_PAYPAL_TESTS = False
38
39#
40# End of PayPal test config
41#
42
43
44def external_paypal_test(func):
45    """A decorator that can block test functions.
46    """
47    if not EXTERNAL_PAYPAL_TESTS:
48        myself = __file__
49        if myself.endswith('.pyc'):
50            myself = myself[:-2]
51        print "WARNING: external paypal tests are skipped!"
52        print "WARNING: edit %s to enable them." % myself
53        return
54    return func
55
56
57class HelperTests(unittest.TestCase):
58
59    def setUp(self):
60        self.workdir = tempfile.mkdtemp()
61
62    def tearDown(self):
63        shutil.rmtree(self.workdir)
64        # unregister any remaining utils registered during tests
65        util = queryUtility(IPayPalConfig)
66        if util is not None:
67            getGlobalSiteManager().unregisterUtility(util, IPayPalConfig)
68
69    def test_get_paypal_config_file_path(self):
70        # we can get a config file path (if registered)
71        path = os.path.join(self.workdir, 'sample.cfg')
72        getGlobalSiteManager().registerUtility(
73            {'path': path, }, IPayPalConfig, '')
74        result = get_paypal_config_file_path()
75        assert result == path
76
77    def test_get_paypal_config_file_path_no_file(self):
78        # without IPayPalConfig util set, we get `None`
79        result = get_paypal_config_file_path()
80        assert result is None
81
82    def test_parse_paypal_config(self):
83        # we can parse paypal configs
84        path = os.path.join(self.workdir, 'sample.cfg')
85        open(path, 'w').write(
86            "[rest-client]\n"
87            "id = myid\n"
88            "secret = mysecret\n"
89            "mode = live\n")
90        result = parse_paypal_config(path)
91        assert result['client_id'] == u'myid'
92        assert result['client_secret'] == u'mysecret'
93        assert result['mode'] == 'live'
94
95    def test_parse_paypal_config_defaults(self):
96        # we have fallback values (defaults) in place
97        path = os.path.join(self.workdir, 'sample.cfg')
98        open(path, 'w').write(
99            "[rest-client]\n"
100            )
101        result = parse_paypal_config(path)
102        assert result['client_id'] is None
103        assert result['client_secret'] is None
104        assert result['mode'] == "sandbox"
105
106    def test_configure_sdk(self):
107        # we can configure the paypal sdk
108        path = os.path.join(self.workdir, 'sample.cfg')
109        open(path, 'w').write(
110            "[rest-client]\n"
111            "id = my-special-id\n"
112            )
113        result = configure_sdk(path)
114        assert result['client_id'] == 'my-special-id'
115        assert result['client_secret'] is None
116        assert result['mode'] == "sandbox"
117        assert paypalrestsdk.api.__api__.mode == 'sandbox'
118        assert paypalrestsdk.api.__api__.client_id == 'my-special-id'
119
120
121class FunctionalPaypalTests(FunctionalTestCase):
122
123    layer = FunctionalLayer
124
125    @external_paypal_test
126    def test_get_access_token(self):
127        # we can get an access token
128        result = get_access_token()
129        assert isinstance(result, unicode)
130
131    @external_paypal_test
132    def test_get_payment(self):
133        # we can construct (paypal-)payment objects
134        payment = get_payment()
135        assert isinstance(payment, paypalrestsdk.Payment)
136        result = payment.create()
137        assert result is True
Note: See TracBrowser for help on using the repository browser.