Unit tests for setting authenticator via cmd line

This commit is contained in:
Garrett Robinson
2015-04-09 18:04:59 -07:00
parent 79f5ebe734
commit 0d7f32fa98
3 changed files with 41 additions and 21 deletions
+3 -4
View File
@@ -397,11 +397,10 @@ def determine_authenticator(all_auths, config):
try: try:
auth = avail_auths[config.authenticator] auth = avail_auths[config.authenticator]
except KeyError: except KeyError:
logging.error(
"The specified authenticator '%s' could not be found",
config.authenticator)
logging.info(list_available_authenticators(avail_auths)) 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: elif len(avail_auths) > 1:
auth = display_ops.choose_authenticator(avail_auths.values(), errs) auth = display_ops.choose_authenticator(avail_auths.values(), errs)
elif len(avail_auths.keys()) == 1: elif len(avail_auths.keys()) == 1:
+36 -14
View File
@@ -1,9 +1,9 @@
"""letsencrypt.client.client.py tests.""" """letsencrypt.client.client.py tests."""
from collections import namedtuple
import unittest import unittest
import mock import mock
from letsencrypt.client import configuration
from letsencrypt.client import errors from letsencrypt.client import errors
@@ -19,7 +19,8 @@ class DetermineAuthenticatorTest(unittest.TestCase):
self.mock_apache = mock.MagicMock( self.mock_apache = mock.MagicMock(
spec=ApacheConfigurator, description="Standalone Authenticator") spec=ApacheConfigurator, description="Standalone Authenticator")
self.mock_config = mock.Mock() self.mock_config = mock.MagicMock(
spec=configuration.NamespaceConfig, authenticator=None)
self.all_auths = { self.all_auths = {
'apache': self.mock_apache, 'apache': self.mock_apache,
@@ -27,29 +28,30 @@ class DetermineAuthenticatorTest(unittest.TestCase):
} }
@classmethod @classmethod
def _call(cls, all_auths): def _call(cls, all_auths, config):
from letsencrypt.client.client import determine_authenticator from letsencrypt.client.client import determine_authenticator
# TODO: add tests for setting the authenticator via the command line return determine_authenticator(all_auths, config)
mock_config = namedtuple("Config", ['authenticator'])
return determine_authenticator(all_auths,
mock_config(authenticator=None))
@mock.patch("letsencrypt.client.client.display_ops.choose_authenticator") @mock.patch("letsencrypt.client.client.display_ops.choose_authenticator")
def test_accept_two(self, mock_choose): def test_accept_two(self, mock_choose):
mock_choose.return_value = self.mock_stand() 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): def test_accept_one(self):
self.mock_apache.prepare.return_value = self.mock_apache self.mock_apache.prepare.return_value = self.mock_apache
self.assertEqual( one_avail_auth = {
self._call(dict(apache=self.all_auths['apache'])), 'apache': self.mock_apache
self.mock_apache) }
self.assertEqual(self._call(one_avail_auth, self.mock_config),
self.mock_apache)
def test_no_installation_one(self): def test_no_installation_one(self):
self.mock_apache.prepare.side_effect = ( self.mock_apache.prepare.side_effect = (
errors.LetsEncryptNoInstallationError) 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): def test_no_installations(self):
self.mock_apache.prepare.side_effect = ( self.mock_apache.prepare.side_effect = (
@@ -59,7 +61,8 @@ class DetermineAuthenticatorTest(unittest.TestCase):
self.assertRaises(errors.LetsEncryptClientError, self.assertRaises(errors.LetsEncryptClientError,
self._call, self._call,
self.all_auths) self.all_auths,
self.mock_config)
@mock.patch("letsencrypt.client.client.logging") @mock.patch("letsencrypt.client.client.logging")
@mock.patch("letsencrypt.client.client.display_ops.choose_authenticator") @mock.patch("letsencrypt.client.client.display_ops.choose_authenticator")
@@ -68,7 +71,26 @@ class DetermineAuthenticatorTest(unittest.TestCase):
errors.LetsEncryptMisconfigurationError) errors.LetsEncryptMisconfigurationError)
mock_choose.return_value = self.mock_apache 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): class RollbackTest(unittest.TestCase):
+2 -3
View File
@@ -171,9 +171,8 @@ def main(): # pylint: disable=too-many-branches, too-many-statements
try: try:
auth = client.determine_authenticator(all_auths, config) auth = client.determine_authenticator(all_auths, config)
logging.debug("Selected authenticator: %s", auth) logging.debug("Selected authenticator: %s", auth)
except errors.LetsEncryptClientError: except errors.LetsEncryptClientError as err:
logging.critical("No authentication mechanisms were found on your " logging.critical(str(err))
"system.")
sys.exit(1) sys.exit(1)
if auth is None: if auth is None: