source: main/waeup.ikoba/trunk/src/waeup/ikoba/tests/test_zcml.py @ 17397

Last change on this file since 17397 was 12288, checked in by Henrik Bettermann, 10 years ago

propset svn:keywords "Id"

  • Property svn:keywords set to Id
File size: 4.3 KB
RevLine 
[11999]1import os
[11992]2import tempfile
3import shutil
4import unittest
5from waeup.ikoba import tests
6from waeup.ikoba.interfaces import IDataCenterConfig, IPayPalConfig
7from zope.component import getUtility, queryUtility, getGlobalSiteManager
8from zope.configuration.exceptions import ConfigurationError
9from zope.configuration.xmlconfig import file as zcfile
10from zope.configuration.xmlconfig import string as zcstring
11
12
13def get_zcml_conf(xml_code):
14    """Get a working ZCML configuration file with `xml_code` embedded.
15
16    The returned ZCML declares an ikoba namespace for local ZCML
17    extensions and includes the `waeup.ikoba.meta` declarations before
18    embedding `xml_code`.
19
20    The returned string can be 'executed' with `string` or `file` from
21    `zope.configuration.xmlconfig.
22    """
23    config = (
24        '<configure '
25        '   xmlns="http://namespaces.zope.org/zope"'
26        '   xmlns:ikoba="http://namespaces.waeup.org/ikoba"'
27        '   i18n_domain="waeup.ikoba"'
28        '   package="waeup.ikoba.tests"'
29        '>'
30        '<include package="waeup.ikoba" file="meta.zcml" />'
31        ''
32        '%s'
33        ''
34        '</configure>'
35        ) % xml_code
36    return config
37
38
39class ZCMLTests(unittest.TestCase):
40
41    def setUp(self):
42        self.workdir = tempfile.mkdtemp()
43
44    def tearDown(self):
45        shutil.rmtree(self.workdir)
46        # unregister any remaining utils registered during tests
47        gsm = getGlobalSiteManager()
48        for iface in (IDataCenterConfig, IPayPalConfig):
49            conf = queryUtility(iface)
50            if conf is None:
51                continue
52            gsm.unregisterUtility(conf, iface)
53
[11999]54    def create_fake_paypal_configs(self):
55        # create two regular files in workdir
56        self.conf1_path = os.path.join(self.workdir, 'paypal1.conf')
57        self.conf2_path = os.path.join(self.workdir, 'paypal2.conf')
[12030]58        open(self.conf1_path, 'w').write('[rest-client]\n')
59        open(self.conf2_path, 'w').write('[rest-client]\n')
[11999]60
[11992]61    def test_datacenter_config_directive(self):
62        # the 'datacenter' directive can be used in ZCML
63        assert queryUtility(IDataCenterConfig) is None
[11998]64        zcfile("sample-datacenter.zcml", tests)  # 'execute' the ZCML
[11992]65        result = getUtility(IDataCenterConfig)
66        assert result == {'path': u'/some/path'}
67
68    def test_datacenter_config_singleton(self):
69        # we can define only one datacenter config
70        config = get_zcml_conf(
71            '<ikoba:datacenter path="/some/path" />'
72            '<ikoba:datacenter path="/other/path" />'
73            )
74        assert queryUtility(IDataCenterConfig) is None
75        self.assertRaises(
76            ConfigurationError, zcstring, config)
77
78    def test_paypal_config_directive(self):
79        # the 'paypalconf' ZCML directive works
80        assert queryUtility(IPayPalConfig) is None
[11999]81        self.create_fake_paypal_configs()
82        config = get_zcml_conf(
83            '<ikoba:paypalconf path="%s" />' % self.conf1_path
84            )
85        zcstring(config)  # execute ZCML
[11992]86        result = getUtility(IPayPalConfig)
[12030]87        assert result == {
88            'path': self.conf1_path,
89            'client_id': None,
90            'client_secret': None,
91            'mode': 'sandbox'
92            }
[11992]93
94    def test_paypal_config_singleton(self):
95        # we can define only one paypal config
[11999]96        assert queryUtility(IPayPalConfig) is None
97        self.create_fake_paypal_configs()
[11992]98        config = get_zcml_conf(
[11999]99            '<ikoba:paypalconf path="%s" />'
100            '<ikoba:paypalconf path="%s" />'
101            ) % (self.conf1_path, self.conf2_path)
[11992]102        assert queryUtility(IPayPalConfig) is None
103        self.assertRaises(
104            ConfigurationError, zcstring, config)
[11999]105
106    def test_paypal_config_no_such_file(self):
107        # a set paypal config file must exist
108        config = get_zcml_conf(
109            '<ikoba:paypalconf path="/not/eXiStInG/path" />')
110        self.assertRaises(
111            ConfigurationError, zcstring, config)
112
113    def test_paypal_config_not_a_regular_file(self):
114        # a paypal config file must be a regular file
115        dirpath = os.path.join(self.workdir, 'mydir')
116        os.mkdir(dirpath)
117        config = get_zcml_conf(
118            '<ikoba:paypalconf path="%s" />' % dirpath)
119        self.assertRaises(
120            ConfigurationError, zcstring, config)
Note: See TracBrowser for help on using the repository browser.