From 8b9a271900e60b635e0fca4b018fd4a5ca9e0ccb Mon Sep 17 00:00:00 2001 From: Peter Eckersley Date: Tue, 10 Nov 2015 16:40:27 -0800 Subject: [PATCH] "Really" finalise User Agent tests - Add an extra test for the default UA string - Fixes: get_os_info() is returning a tuple, not a string - Workaround: Argument preprocessing is dangerous for test cases --- letsencrypt/configuration.py | 2 +- letsencrypt/tests/cli_test.py | 31 +++++++++++++++++++++---------- letsencrypt/tests/client_test.py | 4 ++-- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/letsencrypt/configuration.py b/letsencrypt/configuration.py index 345cf75b4..9ee702258 100644 --- a/letsencrypt/configuration.py +++ b/letsencrypt/configuration.py @@ -92,7 +92,7 @@ class NamespaceConfig(object): if self.namespace.user_agent is None: ua = "LetsEncryptPythonClient/{0} ({1}) Authenticator/{2} Installer/{3}" - ua = ua.format(letsencrypt.__version__, le_util.get_os_info(), + ua = ua.format(letsencrypt.__version__, " ".join(le_util.get_os_info()), authenticator.name if authenticator else "none", installer.name if installer else "none") self.namespace.user_agent = ua diff --git a/letsencrypt/tests/cli_test.py b/letsencrypt/tests/cli_test.py index b6c56558e..21a774a9c 100644 --- a/letsencrypt/tests/cli_test.py +++ b/letsencrypt/tests/cli_test.py @@ -13,6 +13,7 @@ from letsencrypt import account from letsencrypt import cli from letsencrypt import configuration from letsencrypt import errors +from letsencrypt import le_util from letsencrypt.plugins import disco @@ -43,7 +44,7 @@ class CLITest(unittest.TestCase): with mock.patch('letsencrypt.cli.sys.stdout') as stdout: with mock.patch('letsencrypt.cli.sys.stderr') as stderr: with mock.patch('letsencrypt.cli.client') as client: - ret = cli.main(args) + ret = cli.main(args[:]) # NOTE: parser can alter its args! return ret, stdout, stderr, client def _call_stdout(self, args): @@ -54,7 +55,7 @@ class CLITest(unittest.TestCase): args = self.standard_args + args with mock.patch('letsencrypt.cli.sys.stderr') as stderr: with mock.patch('letsencrypt.cli.client') as client: - ret = cli.main(args) + ret = cli.main(args[:]) # NOTE: parser can alter its args! return ret, None, stderr, client def test_no_flags(self): @@ -115,21 +116,31 @@ class CLITest(unittest.TestCase): @mock.patch('letsencrypt.cli.sys.stdout') @mock.patch('letsencrypt.cli.sys.stderr') - @mock.patch('letsencrypt.cli.client.acme_client.ClientNetwork') @mock.patch('letsencrypt.cli.client.acme_client.Client') @mock.patch('letsencrypt.cli._determine_account') @mock.patch('letsencrypt.cli.client.Client.obtain_and_enroll_certificate') @mock.patch('letsencrypt.cli._auth_from_domains') - def test_user_agent(self, _afd, _obt, det, _client, acme_net, _out, _err): - ua = "bandersnatch" + def test_user_agent(self, _afd, _obt, det, _client, _err, _out): # Normally the client is totally mocked out, but here we need more # arguments to automate it... - args = ["--user-agent", ua, "--standalone", "certonly", - "-m", "none@none.com", "-d", "example.com", '--agree-tos'] - args += self.standard_args + args = ["--standalone", "certonly", "-m", "none@none.com", + "-d", "example.com", '--agree-tos'] + self.standard_args det.return_value = mock.MagicMock(), None - cli.main(args) - acme_net.assert_called_once_with(mock.ANY, verify_ssl=True, user_agent=ua) + with mock.patch('letsencrypt.cli.client.acme_client.ClientNetwork') as acme_net: + cli.main(args[:]) # Protect args from alteration + os_ver = " ".join(le_util.get_os_info()) + ua = acme_net.call_args[1]["user_agent"] + self.assertTrue(os_ver in ua) + import platform + plat = platform.platform() + if "linux" in plat.lower(): + self.assertTrue(platform.linux_distribution()[0] in ua) + + with mock.patch('letsencrypt.cli.client.acme_client.ClientNetwork') as acme_net: + ua = "bandersnatch" + args += ["--user-agent", ua] + cli.main(args) + acme_net.assert_called_once_with(mock.ANY, verify_ssl=True, user_agent=ua) @mock.patch('letsencrypt.cli.display_ops') def test_installer_selection(self, mock_display_ops): diff --git a/letsencrypt/tests/client_test.py b/letsencrypt/tests/client_test.py index 4a61194f3..0007323fa 100644 --- a/letsencrypt/tests/client_test.py +++ b/letsencrypt/tests/client_test.py @@ -70,8 +70,8 @@ class ClientTest(unittest.TestCase): dv_auth=None, installer=None) def test_init_acme_verify_ssl(self): - self.acme_client.assert_called_once_with( - directory=mock.ANY, key=mock.ANY, verify_ssl=True) + net = self.acme_client.call_args[1]["net"] + self.assertTrue(net.verify_ssl) def _mock_obtain_certificate(self): self.client.auth_handler = mock.MagicMock()