source: main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/tests/test_zcml.py @ 12000

Last change on this file since 12000 was 11999, checked in by uli, 10 years ago

Require the paypal config file to exists when declared.

File size: 4.1 KB
Line 
1import os
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
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')
58        open(self.conf1_path, 'w').write('Fake')
59        open(self.conf2_path, 'w').write('Fake')
60
61    def test_datacenter_config_directive(self):
62        # the 'datacenter' directive can be used in ZCML
63        assert queryUtility(IDataCenterConfig) is None
64        zcfile("sample-datacenter.zcml", tests)  # 'execute' the ZCML
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
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
86        result = getUtility(IPayPalConfig)
87        assert result == {'path': self.conf1_path}
88
89    def test_paypal_config_singleton(self):
90        # we can define only one paypal config
91        assert queryUtility(IPayPalConfig) is None
92        self.create_fake_paypal_configs()
93        config = get_zcml_conf(
94            '<ikoba:paypalconf path="%s" />'
95            '<ikoba:paypalconf path="%s" />'
96            ) % (self.conf1_path, self.conf2_path)
97        assert queryUtility(IPayPalConfig) is None
98        self.assertRaises(
99            ConfigurationError, zcstring, config)
100
101    def test_paypal_config_no_such_file(self):
102        # a set paypal config file must exist
103        config = get_zcml_conf(
104            '<ikoba:paypalconf path="/not/eXiStInG/path" />')
105        self.assertRaises(
106            ConfigurationError, zcstring, config)
107
108    def test_paypal_config_not_a_regular_file(self):
109        # a paypal config file must be a regular file
110        dirpath = os.path.join(self.workdir, 'mydir')
111        os.mkdir(dirpath)
112        config = get_zcml_conf(
113            '<ikoba:paypalconf path="%s" />' % dirpath)
114        self.assertRaises(
115            ConfigurationError, zcstring, config)
Note: See TracBrowser for help on using the repository browser.