Changeset 11999


Ignore:
Timestamp:
20 Nov 2014, 03:06:02 (10 years ago)
Author:
uli
Message:

Require the paypal config file to exists when declared.

Location:
main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba
Files:
1 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/tests/test_zcml.py

    r11998 r11999  
     1import os
    12import tempfile
    23import shutil
     
    5152            gsm.unregisterUtility(conf, iface)
    5253
     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
    5361    def test_datacenter_config_directive(self):
    5462        # the 'datacenter' directive can be used in ZCML
     
    7179        # the 'paypalconf' ZCML directive works
    7280        assert queryUtility(IPayPalConfig) is None
    73         zcfile("sample-paypalconf.zcml", tests)  # 'execute' the ZCML file
     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
    7486        result = getUtility(IPayPalConfig)
    75         assert result == {'path': u'/path/to/some/paypal.conf'}
     87        assert result == {'path': self.conf1_path}
    7688
    7789    def test_paypal_config_singleton(self):
    7890        # we can define only one paypal config
     91        assert queryUtility(IPayPalConfig) is None
     92        self.create_fake_paypal_configs()
    7993        config = get_zcml_conf(
    80             '<ikoba:paypalconf path="/some/path" />'
    81             '<ikoba:paypalconf path="/other/path" />'
    82             )
     94            '<ikoba:paypalconf path="%s" />'
     95            '<ikoba:paypalconf path="%s" />'
     96            ) % (self.conf1_path, self.conf2_path)
    8397        assert queryUtility(IPayPalConfig) is None
    8498        self.assertRaises(
    8599            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)
  • main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/zcml.py

    r11993 r11999  
    1616## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    1717##
     18import os
    1819from zope.component.zcml import handler
     20from zope.configuration.exceptions import ConfigurationError
    1921from waeup.ikoba.interfaces import IDataCenterConfig, IPayPalConfig
    2022
     
    5254
    5355
     56def paypal_handler(path):
     57    """ZCML handler that registers paypal configuration.
     58
     59    We expect paypal credentials written down in a config file. The
     60    path to this config file can be set with the ZCML `paypalconf`
     61    directive, which is handled here.
     62
     63    This handler requires the given path to exist and to be a file.
     64
     65    If the file exists and is readable, a dict with ``path`` set to
     66    the given path is registered as a global unnamed utility for the
     67    `IPayPalConfig` interface.
     68
     69    We do not check more (syntax, etc. of a found config) as this is
     70    beyond the scope of ZCML configuration. ZCML is responsible for
     71    finding _some_ paypal configuration file, not for the values found
     72    (or not found) within this file.
     73    """
     74    if not os.path.exists(path):
     75        raise ConfigurationError("No such file: %s" % path)
     76    if not os.path.isfile(path):
     77        raise ConfigurationError("Not a regular file: %s" % path)
     78    return handler(
     79        'registerUtility', {'path': path}, IPayPalConfig, '')
     80
     81
    5482def paypal_conf(context, path):
    5583    """Handler for ZCML paypalconf directive.
     84
     85    This handler registers the `paypal_handler` above to perform the
     86    real configuration action.
    5687    """
    5788    context.action(
    5889        discriminator=('utility', IPayPalConfig, ''),
    59         callable=handler,
    60         args=(
    61             'registerUtility',
    62             {'path': path}, IPayPalConfig, '')
     90        callable=paypal_handler,
     91        args=(path, )
    6392        )
Note: See TracChangeset for help on using the changeset viewer.