1
0
mirror of https://github.com/certbot/certbot.git synced 2026-01-21 19:01:07 +03:00

Unit tests for setting authenticator via cmd line

This commit is contained in:
Garrett Robinson
2015-04-09 17:37:24 -07:00
parent 79f5ebe734
commit 0d7f32fa98
3 changed files with 41 additions and 21 deletions

View File

@@ -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:

View File

@@ -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):

View File

@@ -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: