From 0d7f32fa984e2e82918d644dfe6913bfe765055f Mon Sep 17 00:00:00 2001 From: Garrett Robinson Date: Thu, 9 Apr 2015 17:37:24 -0700 Subject: [PATCH] Unit tests for setting authenticator via cmd line --- letsencrypt/client/client.py | 7 ++-- letsencrypt/client/tests/client_test.py | 50 ++++++++++++++++++------- letsencrypt/scripts/main.py | 5 +-- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/letsencrypt/client/client.py b/letsencrypt/client/client.py index 19b982502..91b271784 100644 --- a/letsencrypt/client/client.py +++ b/letsencrypt/client/client.py @@ -397,11 +397,10 @@ def determine_authenticator(all_auths, config): try: auth = avail_auths[config.authenticator] except KeyError: - logging.error( - "The specified authenticator '%s' could not be found", - config.authenticator) logging.info(list_available_authenticators(avail_auths)) - return + raise errors.LetsEncryptClientError( + "The specified authenticator '%s' could not be found" % + config.authenticator) elif len(avail_auths) > 1: auth = display_ops.choose_authenticator(avail_auths.values(), errs) elif len(avail_auths.keys()) == 1: diff --git a/letsencrypt/client/tests/client_test.py b/letsencrypt/client/tests/client_test.py index 2310dbe87..63170b517 100644 --- a/letsencrypt/client/tests/client_test.py +++ b/letsencrypt/client/tests/client_test.py @@ -1,9 +1,9 @@ """letsencrypt.client.client.py tests.""" -from collections import namedtuple import unittest import mock +from letsencrypt.client import configuration from letsencrypt.client import errors @@ -19,7 +19,8 @@ class DetermineAuthenticatorTest(unittest.TestCase): self.mock_apache = mock.MagicMock( spec=ApacheConfigurator, description="Standalone Authenticator") - self.mock_config = mock.Mock() + self.mock_config = mock.MagicMock( + spec=configuration.NamespaceConfig, authenticator=None) self.all_auths = { 'apache': self.mock_apache, @@ -27,29 +28,30 @@ class DetermineAuthenticatorTest(unittest.TestCase): } @classmethod - def _call(cls, all_auths): + def _call(cls, all_auths, config): from letsencrypt.client.client import determine_authenticator - # TODO: add tests for setting the authenticator via the command line - mock_config = namedtuple("Config", ['authenticator']) - return determine_authenticator(all_auths, - mock_config(authenticator=None)) + return determine_authenticator(all_auths, config) @mock.patch("letsencrypt.client.client.display_ops.choose_authenticator") def test_accept_two(self, mock_choose): mock_choose.return_value = self.mock_stand() - self.assertEqual(self._call(self.all_auths), self.mock_stand()) + self.assertEqual(self._call(self.all_auths, self.mock_config), + self.mock_stand()) def test_accept_one(self): self.mock_apache.prepare.return_value = self.mock_apache - self.assertEqual( - self._call(dict(apache=self.all_auths['apache'])), - self.mock_apache) + one_avail_auth = { + 'apache': self.mock_apache + } + self.assertEqual(self._call(one_avail_auth, self.mock_config), + self.mock_apache) def test_no_installation_one(self): self.mock_apache.prepare.side_effect = ( errors.LetsEncryptNoInstallationError) - self.assertEqual(self._call(self.all_auths), self.mock_stand) + self.assertEqual(self._call(self.all_auths, self.mock_config), + self.mock_stand) def test_no_installations(self): self.mock_apache.prepare.side_effect = ( @@ -59,7 +61,8 @@ class DetermineAuthenticatorTest(unittest.TestCase): self.assertRaises(errors.LetsEncryptClientError, self._call, - self.all_auths) + self.all_auths, + self.mock_config) @mock.patch("letsencrypt.client.client.logging") @mock.patch("letsencrypt.client.client.display_ops.choose_authenticator") @@ -68,7 +71,26 @@ class DetermineAuthenticatorTest(unittest.TestCase): errors.LetsEncryptMisconfigurationError) mock_choose.return_value = self.mock_apache - self.assertTrue(self._call(self.all_auths) is None) + self.assertTrue(self._call(self.all_auths, self.mock_config) is None) + + def test_choose_valid_auth_from_cmd_line(self): + standalone_config = mock.MagicMock(spec=configuration.NamespaceConfig, + authenticator='standalone') + self.assertEqual(self._call(self.all_auths, standalone_config), + self.mock_stand) + + apache_config = mock.MagicMock(spec=configuration.NamespaceConfig, + authenticator='apache') + self.assertEqual(self._call(self.all_auths, apache_config), + self.mock_apache) + + def test_choose_invalid_auth_from_cmd_line(self): + invalid_config = mock.MagicMock(spec=configuration.NamespaceConfig, + authenticator='foobar') + self.assertRaises(errors.LetsEncryptClientError, + self._call, + self.all_auths, + invalid_config) class RollbackTest(unittest.TestCase): diff --git a/letsencrypt/scripts/main.py b/letsencrypt/scripts/main.py index 269f66744..20813f11e 100644 --- a/letsencrypt/scripts/main.py +++ b/letsencrypt/scripts/main.py @@ -171,9 +171,8 @@ def main(): # pylint: disable=too-many-branches, too-many-statements try: auth = client.determine_authenticator(all_auths, config) logging.debug("Selected authenticator: %s", auth) - except errors.LetsEncryptClientError: - logging.critical("No authentication mechanisms were found on your " - "system.") + except errors.LetsEncryptClientError as err: + logging.critical(str(err)) sys.exit(1) if auth is None: