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

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

Some paypal-related tests.

File size: 2.4 KB
Line 
1import os
2import shutil
3import tempfile
4import unittest
5from zope.component import getGlobalSiteManager, queryUtility
6from waeup.ikoba.interfaces import IPayPalConfig
7from waeup.ikoba.payments.paypal import (
8    get_paypal_config_file_path, parse_paypal_config, get_access_token,
9    )
10from waeup.ikoba.testing import (
11    FunctionalLayer, FunctionalTestCase,
12    )
13
14
15class HelperTests(unittest.TestCase):
16
17    def setUp(self):
18        self.workdir = tempfile.mkdtemp()
19
20    def tearDown(self):
21        shutil.rmtree(self.workdir)
22        # unregister any remaining utils registered during tests
23        util = queryUtility(IPayPalConfig)
24        if util is not None:
25            getGlobalSiteManager().unregisterUtility(util, IPayPalConfig)
26
27    def test_get_paypal_config_file_path(self):
28        # we can get a config file path (if registered)
29        path = os.path.join(self.workdir, 'sample.cfg')
30        getGlobalSiteManager().registerUtility(
31            {'path': path, }, IPayPalConfig, '')
32        result = get_paypal_config_file_path()
33        assert result == path
34
35    def test_get_paypal_config_file_path_no_file(self):
36        # without IPayPalConfig util set, we get `None`
37        result = get_paypal_config_file_path()
38        assert result is None
39
40    def test_parse_paypal_config(self):
41        # we can parse paypal configs
42        path = os.path.join(self.workdir, 'sample.cfg')
43        open(path, 'w').write(
44            "[rest-client]\n"
45            "id = myid\n"
46            "secret = mysecret\n"
47            "mode = live\n")
48        result = parse_paypal_config(path)
49        assert result['client_id'] == u'myid'
50        assert result['client_secret'] == u'mysecret'
51        assert result['mode'] == 'live'
52
53    def test_parse_paypal_config_defaults(self):
54        # we have fallback values (defaults) in place
55        path = os.path.join(self.workdir, 'sample.cfg')
56        open(path, 'w').write(
57            "[rest-client]\n"
58            )
59        result = parse_paypal_config(path)
60        assert result['client_id'] is None
61        assert result['client_secret'] is None
62        assert result['mode'] == "sandbox"
63
64
65class FunctionalPaypalTests(FunctionalTestCase):
66    # XXX: The functional paypal tests are expensive
67
68    layer = FunctionalLayer
69
70    def test_get_access_token(self):
71        # we can get an access token
72        result = get_access_token()
73        assert isinstance(result, unicode)
Note: See TracBrowser for help on using the repository browser.