mirror of
https://github.com/certbot/certbot.git
synced 2026-08-03 00:22:04 +02:00
Chase all the mock threads all over the place
This commit is contained in:
@@ -38,7 +38,7 @@ def acme_from_config_key(config, key):
|
|||||||
# TODO: Allow for other alg types besides RS256
|
# TODO: Allow for other alg types besides RS256
|
||||||
net = acme_client.ClientNetwork(key, verify_ssl=(not config.no_verify_ssl),
|
net = acme_client.ClientNetwork(key, verify_ssl=(not config.no_verify_ssl),
|
||||||
user_agent=_determine_user_agent(config))
|
user_agent=_determine_user_agent(config))
|
||||||
return acme_client.Client(directory=config.server, key=key, net=net)
|
return acme_client.Client(config.server, key=key, net=net)
|
||||||
|
|
||||||
|
|
||||||
def _determine_user_agent(config):
|
def _determine_user_agent(config):
|
||||||
|
|||||||
@@ -46,12 +46,18 @@ class CLITest(unittest.TestCase): # pylint: disable=too-many-public-methods
|
|||||||
shutil.rmtree(self.tmp_dir)
|
shutil.rmtree(self.tmp_dir)
|
||||||
|
|
||||||
def _call(self, args):
|
def _call(self, args):
|
||||||
|
"Run the cli with output streams and actual client mocked out"
|
||||||
|
with mock.patch('letsencrypt.cli.client') as client:
|
||||||
|
ret, stdout, stderr = self._call_no_clientmock(args)
|
||||||
|
return ret, stdout, stderr, client
|
||||||
|
|
||||||
|
def _call_no_clientmock(self, args):
|
||||||
|
"Run the client with output streams mocked out"
|
||||||
args = self.standard_args + args
|
args = self.standard_args + args
|
||||||
with mock.patch('letsencrypt.cli.sys.stdout') as stdout:
|
with mock.patch('letsencrypt.cli.sys.stdout') as stdout:
|
||||||
with mock.patch('letsencrypt.cli.sys.stderr') as stderr:
|
with mock.patch('letsencrypt.cli.sys.stderr') as stderr:
|
||||||
with mock.patch('letsencrypt.cli.client') as client:
|
|
||||||
ret = cli.main(args[:]) # NOTE: parser can alter its args!
|
ret = cli.main(args[:]) # NOTE: parser can alter its args!
|
||||||
return ret, stdout, stderr, client
|
return ret, stdout, stderr
|
||||||
|
|
||||||
def _call_stdout(self, args):
|
def _call_stdout(self, args):
|
||||||
"""
|
"""
|
||||||
@@ -119,20 +125,18 @@ 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)
|
||||||
|
|
||||||
@mock.patch('letsencrypt.cli.sys.stdout')
|
|
||||||
@mock.patch('letsencrypt.cli.sys.stderr')
|
|
||||||
@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')
|
||||||
@mock.patch('letsencrypt.cli._auth_from_domains')
|
@mock.patch('letsencrypt.cli._auth_from_domains')
|
||||||
def test_user_agent(self, _afd, _obt, det, _client, _err, _out):
|
def test_user_agent(self, _afd, _obt, det, _client):
|
||||||
# Normally the client is totally mocked out, but here we need more
|
# Normally the client is totally mocked out, but here we need more
|
||||||
# arguments to automate it...
|
# arguments to automate it...
|
||||||
args = ["--standalone", "certonly", "-m", "none@none.com",
|
args = ["--standalone", "certonly", "-m", "none@none.com",
|
||||||
"-d", "example.com", '--agree-tos'] + self.standard_args
|
"-d", "example.com", '--agree-tos'] + self.standard_args
|
||||||
det.return_value = mock.MagicMock(), None
|
det.return_value = mock.MagicMock(), None
|
||||||
with mock.patch('letsencrypt.cli.client.acme_client.ClientNetwork') as acme_net:
|
with mock.patch('letsencrypt.cli.client.acme_client.ClientNetwork') as acme_net:
|
||||||
cli.main(args[:]) # Protect args from alteration
|
self._call_no_clientmock(args)
|
||||||
os_ver = " ".join(le_util.get_os_info())
|
os_ver = " ".join(le_util.get_os_info())
|
||||||
ua = acme_net.call_args[1]["user_agent"]
|
ua = acme_net.call_args[1]["user_agent"]
|
||||||
self.assertTrue(os_ver in ua)
|
self.assertTrue(os_ver in ua)
|
||||||
@@ -144,7 +148,7 @@ class CLITest(unittest.TestCase): # pylint: disable=too-many-public-methods
|
|||||||
with mock.patch('letsencrypt.cli.client.acme_client.ClientNetwork') as acme_net:
|
with mock.patch('letsencrypt.cli.client.acme_client.ClientNetwork') as acme_net:
|
||||||
ua = "bandersnatch"
|
ua = "bandersnatch"
|
||||||
args += ["--user-agent", ua]
|
args += ["--user-agent", ua]
|
||||||
cli.main(args)
|
self._call_no_clientmock(args)
|
||||||
acme_net.assert_called_once_with(mock.ANY, verify_ssl=True, user_agent=ua)
|
acme_net.assert_called_once_with(mock.ANY, verify_ssl=True, user_agent=ua)
|
||||||
|
|
||||||
def test_install_abspath(self):
|
def test_install_abspath(self):
|
||||||
@@ -305,7 +309,6 @@ class CLITest(unittest.TestCase): # pylint: disable=too-many-public-methods
|
|||||||
['-d', '*.wildcard.tld'])
|
['-d', '*.wildcard.tld'])
|
||||||
|
|
||||||
def test_parse_domains(self):
|
def test_parse_domains(self):
|
||||||
from letsencrypt import cli
|
|
||||||
plugins = disco.PluginsRegistry.find_all()
|
plugins = disco.PluginsRegistry.find_all()
|
||||||
|
|
||||||
short_args = ['-d', 'example.com']
|
short_args = ['-d', 'example.com']
|
||||||
@@ -411,28 +414,27 @@ class CLITest(unittest.TestCase): # pylint: disable=too-many-public-methods
|
|||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
date in mock_get_utility().add_message.call_args[0][0])
|
date in mock_get_utility().add_message.call_args[0][0])
|
||||||
|
|
||||||
@mock.patch('letsencrypt.cli.client.acme_from_config_key')
|
@mock.patch('letsencrypt.cli.client.acme_client')
|
||||||
def test_revoke_with_key(self, mock_acme_client):
|
def test_revoke_with_key(self, mock_acme_client):
|
||||||
server = 'foo.bar'
|
server = 'foo.bar'
|
||||||
self._call(['--cert-path', CERT, '--key-path', KEY,
|
self._call_no_clientmock(['--cert-path', CERT, '--key-path', KEY,
|
||||||
'--server', server, 'revoke'])
|
'--server', server, 'revoke'])
|
||||||
with open(KEY) as f:
|
with open(KEY) as f:
|
||||||
mock_acme_client.assert_called_once_with(
|
mock_acme_client.Client.assert_called_once_with(
|
||||||
server, key=jose.JWK.load(f.read()))
|
server, key=jose.JWK.load(f.read()), net=mock.ANY)
|
||||||
with open(CERT) as f:
|
with open(CERT) as f:
|
||||||
mock_acme_client.Client().revoke.assert_called_once_with(
|
cert = crypto_util.pyopenssl_load_certificate(f.read())[0]
|
||||||
jose.ComparableX509(crypto_util.pyopenssl_load_certificate(
|
mock_revoke = mock_acme_client.Client().revoke
|
||||||
f.read())[0]))
|
mock_revoke.assert_called_once_with(jose.ComparableX509(cert))
|
||||||
|
|
||||||
@mock.patch('letsencrypt.cli._determine_account')
|
@mock.patch('letsencrypt.cli._determine_account')
|
||||||
def test_revoke_without_key(self, mock_determine_account):
|
def test_revoke_without_key(self, mock_determine_account):
|
||||||
mock_determine_account.return_value = (mock.MagicMock(), None)
|
mock_determine_account.return_value = (mock.MagicMock(), None)
|
||||||
_, _, _, client = self._call(['--cert-path', CERT, 'revoke'])
|
_, _, _, client = self._call(['--cert-path', CERT, 'revoke'])
|
||||||
with open(CERT) as f:
|
with open(CERT) as f:
|
||||||
# pylint: disable=protected-access
|
cert = crypto_util.pyopenssl_load_certificate(f.read())[0]
|
||||||
client._acme_from_config_key().revoke.assert_called_once_with(
|
mock_revoke = client.acme_from_config_key().revoke
|
||||||
jose.ComparableX509(crypto_util.pyopenssl_load_certificate(
|
mock_revoke.assert_called_once_with(jose.ComparableX509(cert))
|
||||||
f.read())[0]))
|
|
||||||
|
|
||||||
@mock.patch('letsencrypt.cli.sys')
|
@mock.patch('letsencrypt.cli.sys')
|
||||||
def test_handle_exception(self, mock_sys):
|
def test_handle_exception(self, mock_sys):
|
||||||
@@ -469,7 +471,6 @@ class CLITest(unittest.TestCase): # pylint: disable=too-many-public-methods
|
|||||||
traceback.format_exception_only(KeyboardInterrupt, interrupt)))
|
traceback.format_exception_only(KeyboardInterrupt, interrupt)))
|
||||||
|
|
||||||
def test_read_file(self):
|
def test_read_file(self):
|
||||||
from letsencrypt import cli
|
|
||||||
rel_test_path = os.path.relpath(os.path.join(self.tmp_dir, 'foo'))
|
rel_test_path = os.path.relpath(os.path.join(self.tmp_dir, 'foo'))
|
||||||
self.assertRaises(
|
self.assertRaises(
|
||||||
argparse.ArgumentTypeError, cli.read_file, rel_test_path)
|
argparse.ArgumentTypeError, cli.read_file, rel_test_path)
|
||||||
|
|||||||
Reference in New Issue
Block a user