Test cases for various error cases (and associtated bugfixes)

This commit is contained in:
Peter Eckersley
2016-01-01 15:29:34 -08:00
parent b5828d92ad
commit 96f704f577
3 changed files with 47 additions and 12 deletions
+17 -10
View File
@@ -160,12 +160,14 @@ def _determine_account(args, config):
"must agree in order to register with the ACME " "must agree in order to register with the ACME "
"server at {1}".format( "server at {1}".format(
regr.terms_of_service, config.server)) regr.terms_of_service, config.server))
return zope.component.getUtility(interfaces.IDisplay).yesno( obj = zope.component.getUtility(interfaces.IDisplay)
msg, "Agree", "Cancel", cli_flag="--agree-tos") return obj.yesno(msg, "Agree", "Cancel", cli_flag="--agree-tos")
try: try:
acc, acme = client.register( acc, acme = client.register(
config, account_storage, tos_cb=_tos_cb) config, account_storage, tos_cb=_tos_cb)
except errors.MissingCommandlineFlag:
raise
except errors.Error as error: except errors.Error as error:
logger.debug(error, exc_info=True) logger.debug(error, exc_info=True)
raise errors.Error( raise errors.Error(
@@ -636,6 +638,8 @@ def obtain_cert(args, config, plugins):
def install(args, config, plugins): def install(args, config, plugins):
"""Install a previously obtained cert in a server.""" """Install a previously obtained cert in a server."""
# XXX: Update for renewer/RenewableCert # XXX: Update for renewer/RenewableCert
# FIXME: be consistent about whether errors are raised or returned from
# this function ...
try: try:
installer, _ = choose_configurator_plugins(args, config, installer, _ = choose_configurator_plugins(args, config,
@@ -643,14 +647,17 @@ def install(args, config, plugins):
except errors.PluginSelectionError, e: except errors.PluginSelectionError, e:
return e.message return e.message
domains = _find_domains(args, installer) try:
le_client = _init_le_client( domains = _find_domains(args, installer)
args, config, authenticator=None, installer=installer) le_client = _init_le_client(
assert args.cert_path is not None # required=True in the subparser args, config, authenticator=None, installer=installer)
le_client.deploy_certificate( assert args.cert_path is not None # required=True in the subparser
domains, args.key_path, args.cert_path, args.chain_path, le_client.deploy_certificate(
args.fullchain_path) domains, args.key_path, args.cert_path, args.chain_path,
le_client.enhance_config(domains, config) args.fullchain_path)
le_client.enhance_config(domains, config)
except errors.MissingCommandlineFlag, e:
return e.message
def revoke(args, config, unused_plugins): # TODO: coop with renewal config def revoke(args, config, unused_plugins): # TODO: coop with renewal config
+1 -1
View File
@@ -486,7 +486,7 @@ class NoninteractiveDisplay(object):
return OK, default return OK, default
def yesno(self, message, default=None, cli_flag=None, **kwargs): def yesno(self, message, yes_label=None, no_label=None, default=None, cli_flag=None):
# pylint: disable=unused-argument # pylint: disable=unused-argument
"""Decide Yes or No, without asking anybody """Decide Yes or No, without asking anybody
+29 -1
View File
@@ -81,7 +81,7 @@ class CLITest(unittest.TestCase): # pylint: disable=too-many-public-methods
self.assertEqual(1, mock_run.call_count) self.assertEqual(1, mock_run.call_count)
def _help_output(self, args): def _help_output(self, args):
"Run a help command, and return the help string for scrutiny" "Run a command, and return the ouput string for scrutiny"
output = StringIO.StringIO() output = StringIO.StringIO()
with mock.patch('letsencrypt.cli.sys.stdout', new=output): with mock.patch('letsencrypt.cli.sys.stdout', new=output):
self.assertRaises(SystemExit, self._call_stdout, args) self.assertRaises(SystemExit, self._call_stdout, args)
@@ -105,6 +105,7 @@ class CLITest(unittest.TestCase): # pylint: disable=too-many-public-methods
self.assertTrue("--checkpoints" not in out) self.assertTrue("--checkpoints" not in out)
out = self._help_output(['-h']) out = self._help_output(['-h'])
self.assertTrue("letsencrypt-auto" not in out) # test cli.cli_command
if "nginx" in plugins: if "nginx" in plugins:
self.assertTrue("Use the Nginx plugin" in out) self.assertTrue("Use the Nginx plugin" in out)
else: else:
@@ -130,6 +131,31 @@ class CLITest(unittest.TestCase): # pylint: disable=too-many-public-methods
out = self._help_output(['-h']) out = self._help_output(['-h'])
self.assertTrue(cli.usage_strings(plugins)[0] in out) self.assertTrue(cli.usage_strings(plugins)[0] in out)
def _cli_missing_flag(self, args, message):
"Ensure that a particular error raises a missing cli flag error containing message"
exc = None
try:
#self._call_no_clientmock(args)
with mock.patch('letsencrypt.cli.sys.stderr') as stderr:
out = cli.main(self.standard_args + args[:]) # NOTE: parser can alter its args!
#out = self._help_output(args)
print out
except errors.MissingCommandlineFlag, exc:
#print "checking for " + message + " in\n"+ str(exc)
self.assertTrue(message in str(exc))
self.assertTrue(exc is not None)
def test_noninteractive(self):
args = ['-n', 'certonly']
self._cli_missing_flag(args, "specify a plugin")
args.extend(['--apache', '-d', 'eg.is'])
self._cli_missing_flag(args, "register before running")
with mock.patch('letsencrypt.cli._auth_from_domains'):
with mock.patch('letsencrypt.cli.client.acme_from_config_key'):
args.extend(['--email', 'io@io.is'])
self._cli_missing_flag(args, "--agree-tos")
@mock.patch('letsencrypt.cli.client.acme_client.Client') @mock.patch('letsencrypt.cli.client.acme_client.Client')
@mock.patch('letsencrypt.cli._determine_account') @mock.patch('letsencrypt.cli._determine_account')
@mock.patch('letsencrypt.cli.client.Client.obtain_and_enroll_certificate') @mock.patch('letsencrypt.cli.client.Client.obtain_and_enroll_certificate')
@@ -209,6 +235,8 @@ class CLITest(unittest.TestCase): # pylint: disable=too-many-public-methods
ret, _, _, _ = self._call(args) ret, _, _, _ = self._call(args)
self.assertTrue("--webroot-path must be set" in ret) self.assertTrue("--webroot-path must be set" in ret)
self._cli_missing_flag(["--standalone"], "With the standalone plugin, you probably")
with mock.patch("letsencrypt.cli._init_le_client") as mock_init: with mock.patch("letsencrypt.cli._init_le_client") as mock_init:
with mock.patch("letsencrypt.cli._auth_from_domains"): with mock.patch("letsencrypt.cli._auth_from_domains"):
self._call(["certonly", "--manual", "-d", "foo.bar"]) self._call(["certonly", "--manual", "-d", "foo.bar"])