From 8ebca1c0526962058c17744f3564ee4149ea92f6 Mon Sep 17 00:00:00 2001 From: Erica Portnoy Date: Tue, 20 Dec 2016 17:17:01 -0800 Subject: [PATCH] Return domains for _find_domains_or_certname (#3937) * Return domains for _find_domains_or_certname * Revamp find_domains_or_certname --- certbot/main.py | 15 ++++++++++++--- certbot/tests/main_test.py | 18 +++++++++++++++--- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/certbot/main.py b/certbot/main.py index c0e2f5271..838416822 100644 --- a/certbot/main.py +++ b/certbot/main.py @@ -284,17 +284,26 @@ def _find_domains_or_certname(config, installer): """Retrieve domains and certname from config or user input. """ domains = None + certname = config.certname + # first, try to get domains from the config if config.domains: domains = config.domains - elif not config.certname: + # if we can't do that but we have a certname, get the domains + # with that certname + elif certname: + domains = cert_manager.domains_for_certname(config, certname) + + # that certname might not have existed, or there was a problem. + # try to get domains from the user. + if not domains: domains = display_ops.choose_names(installer) - if not domains and not config.certname: + if not domains and not certname: raise errors.Error("Please specify --domains, or --installer that " "will help in domain names autodiscovery, or " "--cert-name for an existing certificate name.") - return domains, config.certname + return domains, certname def _report_new_cert(config, cert_path, fullchain_path): diff --git a/certbot/tests/main_test.py b/certbot/tests/main_test.py index 3665f09bb..673952b4e 100644 --- a/certbot/tests/main_test.py +++ b/certbot/tests/main_test.py @@ -159,10 +159,12 @@ class ObtainCertTest(unittest.TestCase): self.assertRaises(errors.ConfigurationError, self._call, ('certonly --webroot -d example.com -d test.com --cert-name example.com').split()) + @mock.patch('certbot.cert_manager.domains_for_certname') + @mock.patch('certbot.display.ops.choose_names') @mock.patch('certbot.cert_manager.lineage_for_certname') @mock.patch('certbot.main._report_new_cert') def test_find_lineage_for_domains_new_certname(self, mock_report_cert, - mock_lineage): + mock_lineage, mock_choose_names, mock_domains_for_certname): mock_lineage.return_value = None # no lineage with this name but we specified domains so create a new cert @@ -172,8 +174,10 @@ class ObtainCertTest(unittest.TestCase): self.assertTrue(mock_report_cert.call_count == 1) # no lineage with this name and we didn't give domains - self.assertRaises(errors.ConfigurationError, self._call, - ('certonly --webroot --cert-name example.com').split()) + mock_choose_names.return_value = ["somename"] + mock_domains_for_certname.return_value = None + self._call(('certonly --webroot --cert-name example.com').split()) + self.assertTrue(mock_choose_names.called) class FindDomainsOrCertnameTest(unittest.TestCase): """Tests for certbot.main._find_domains_or_certname.""" @@ -193,6 +197,14 @@ class FindDomainsOrCertnameTest(unittest.TestCase): # pylint: disable=protected-access self.assertRaises(errors.Error, main._find_domains_or_certname, mock_config, None) + @mock.patch('certbot.cert_manager.domains_for_certname') + def test_grab_domains(self, mock_domains): + mock_config = mock.Mock(domains=None, certname="one.com") + mock_domains.return_value = ["one.com", "two.com"] + # pylint: disable=protected-access + self.assertEqual(main._find_domains_or_certname(mock_config, None), + (["one.com", "two.com"], "one.com")) + class RevokeTest(unittest.TestCase): """Tests for certbot.main.revoke."""