From ddf5b28f7db5f2fd312f2a9e6a901f3bd9a8e6f3 Mon Sep 17 00:00:00 2001 From: sagi Date: Mon, 16 Nov 2015 20:06:16 +0000 Subject: [PATCH] fix tests and make linter happy --- letsencrypt/client.py | 36 ++++++++++++++++++-------------- letsencrypt/tests/client_test.py | 28 ++++++++++++++++++++----- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/letsencrypt/client.py b/letsencrypt/client.py index e009400d2..cc1f2aadb 100644 --- a/letsencrypt/client.py +++ b/letsencrypt/client.py @@ -359,7 +359,7 @@ class Client(object): :param list domains: list of domains to configure - :ivar namespace: Namespace typically produced by + :ivar config: Namespace typically produced by :meth:`argparse.ArgumentParser.parse_args`. :type namespace: :class:`argparse.Namespace` @@ -367,29 +367,32 @@ class Client(object): client. """ - redirect = config.redirect - hsts = config.hsts - uir = config.uir # Upgrade Insecure Requests - if self.installer is None: logger.warning("No installer is specified, there isn't any " "configuration to enhance.") raise errors.Error("No installer available") - if redirect is None: + if config is None: redirect = enhancements.ask("redirect") + if redirect: + self.apply_enhancement(domains, "redirect") + else: + redirect = config.redirect + hsts = config.hsts + uir = config.uir # Upgrade Insecure Requests - if redirect: - self.apply_enhancement(domains, "redirect") + if redirect: + self.apply_enhancement(domains, "redirect") - if hsts: - self.apply_enhancement(domains, "http-header", - "Strict-Transport-Security") - if uir: - self.apply_enhancement(domains, "http-header", - "Upgrade-Insecure-Requests") + if hsts: + self.apply_enhancement(domains, "http-header", + "Strict-Transport-Security") + if uir: + self.apply_enhancement(domains, "http-header", + "Upgrade-Insecure-Requests") + msg = ("We were unable to restart web server") if redirect or hsts or uir: with error_handler.ErrorHandler(self._rollback_and_restart, msg): self.installer.restart() @@ -408,8 +411,9 @@ class Client(object): :type str """ - msg = ("We were unable to set up a redirect for your server, " - "however, we successfully installed your certificate.") + msg = ("We were unable to set up enhancement %s for your server, " + "however, we successfully installed your certificate." + % (enhancement)) with error_handler.ErrorHandler(self._recovery_routine_with_msg, msg): for dom in domains: try: diff --git a/letsencrypt/tests/client_test.py b/letsencrypt/tests/client_test.py index d396e25bc..8b84fd3e2 100644 --- a/letsencrypt/tests/client_test.py +++ b/letsencrypt/tests/client_test.py @@ -20,6 +20,15 @@ KEY = test_util.load_vector("rsa512_key.pem") CSR_SAN = test_util.load_vector("csr-san.der") +class ConfigHelper(object): + """Creates a dummy object to imitate a namespace object + + Example: cfg = ConfigHelper(redirect=True, hsts=False, uir=False) + will result in: cfg.redirect=True, cfg.hsts=False, etc. + """ + def __init__(self, **kwds): + self.__dict__.update(kwds) + class RegisterTest(unittest.TestCase): """Tests for letsencrypt.client.register.""" @@ -219,7 +228,7 @@ class ClientTest(unittest.TestCase): self.client.installer = installer self.client.enhance_config(["foo.bar"]) - installer.enhance.assert_called_once_with("foo.bar", "redirect") + installer.enhance.assert_called_once_with("foo.bar", "redirect", None) self.assertEqual(installer.save.call_count, 1) installer.restart.assert_called_once_with() @@ -236,8 +245,10 @@ class ClientTest(unittest.TestCase): self.client.installer = installer installer.enhance.side_effect = errors.PluginError + config = ConfigHelper(redirect=True, hsts=False, uir=False) + self.assertRaises(errors.PluginError, - self.client.enhance_config, ["foo.bar"], True) + self.client.enhance_config, ["foo.bar"], config) installer.recovery_routine.assert_called_once_with() self.assertEqual(mock_get_utility().add_message.call_count, 1) @@ -250,8 +261,10 @@ class ClientTest(unittest.TestCase): self.client.installer = installer installer.save.side_effect = errors.PluginError + config = ConfigHelper(redirect=True, hsts=False, uir=False) + self.assertRaises(errors.PluginError, - self.client.enhance_config, ["foo.bar"], True) + self.client.enhance_config, ["foo.bar"], config) installer.recovery_routine.assert_called_once_with() self.assertEqual(mock_get_utility().add_message.call_count, 1) @@ -264,8 +277,11 @@ class ClientTest(unittest.TestCase): self.client.installer = installer installer.restart.side_effect = [errors.PluginError, None] + config = ConfigHelper(redirect=True, hsts=False, uir=False) + self.assertRaises(errors.PluginError, - self.client.enhance_config, ["foo.bar"], True) + self.client.enhance_config, ["foo.bar"], config) + self.assertEqual(mock_get_utility().add_message.call_count, 1) installer.rollback_checkpoints.assert_called_once_with() self.assertEqual(installer.restart.call_count, 2) @@ -280,8 +296,10 @@ class ClientTest(unittest.TestCase): installer.restart.side_effect = errors.PluginError installer.rollback_checkpoints.side_effect = errors.ReverterError + config = ConfigHelper(redirect=True, hsts=False, uir=False) + self.assertRaises(errors.PluginError, - self.client.enhance_config, ["foo.bar"], True) + self.client.enhance_config, ["foo.bar"], config) self.assertEqual(mock_get_utility().add_message.call_count, 1) installer.rollback_checkpoints.assert_called_once_with() self.assertEqual(installer.restart.call_count, 1)