From 9637142c4c5a19dad1722aa0274100a150a9d8a3 Mon Sep 17 00:00:00 2001 From: Brad Warren Date: Wed, 24 Jun 2015 15:27:34 -0700 Subject: [PATCH] Added auth_handler tests --- letsencrypt/auth_handler.py | 12 ++---- letsencrypt/tests/auth_handler_test.py | 51 +++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/letsencrypt/auth_handler.py b/letsencrypt/auth_handler.py index 4a685439f..019cb07dc 100644 --- a/letsencrypt/auth_handler.py +++ b/letsencrypt/auth_handler.py @@ -512,9 +512,7 @@ _ERROR_HELP = { 'to date TLS configuration that allows the server to communicate with ' 'the Let\'s Encrypt client.', 'unauthorized' : _ERROR_HELP_COMMON, - 'unknownHost' : - 'To fix these errors, please make sure that your domain name was ' - 'entered correctly.',} + 'unknownHost' : _ERROR_HELP_COMMON,} def _report_failed_challs(failed_achalls): @@ -552,16 +550,14 @@ def _generate_failed_chall_msg(failed_achalls): problems = dict() for achall in failed_achalls: - problems.setdefault(achall.error.description, []).append(achall.domain) + problems.setdefault(achall.error.description, set()).add(achall.domain) for problem in problems: - domains = problems[problem] - domains.sort() msg.append('\n\nDomains: ') - msg.append(', '.join(domains)) + msg.append(', '.join(sorted(problems[problem]))) msg.append('\nError: {0}'.format(problem)) if typ in _ERROR_HELP: msg.append('\n\n') msg.append(_ERROR_HELP[typ]) - return "".join(msg) + return ''.join(msg) diff --git a/letsencrypt/tests/auth_handler_test.py b/letsencrypt/tests/auth_handler_test.py index 24bceb5f8..15ce6a490 100644 --- a/letsencrypt/tests/auth_handler_test.py +++ b/letsencrypt/tests/auth_handler_test.py @@ -216,7 +216,8 @@ class PollChallengesTest(unittest.TestCase): self.assertEqual(authzr.body.status, messages.STATUS_PENDING) @mock.patch("letsencrypt.auth_handler.time") - def test_poll_challenges_failure(self, unused_mock_time): + @mock.patch("letsencrypt.auth_handler.zope.component.getUtility") + def test_poll_challenges_failure(self, unused_mock_time, unused_mock_zope): self.mock_net.poll.side_effect = self._mock_poll_solve_one_invalid self.assertRaises( errors.AuthorizationError, self.handler._poll_challenges, @@ -420,6 +421,54 @@ class IsPreferredTest(unittest.TestCase): self._call(acme_util.DVSNI_P, frozenset([acme_util.DVSNI_P]))) +class ReportFailedChallsTest(unittest.TestCase): + """Tests for letsencrypt.auth_handler._report_failed_challs.""" + # pylint: disable=protected-access + + def setUp(self): + from letsencrypt import achallenges + + kwargs = { + "chall" : acme_util.SIMPLE_HTTP, + "uri": "uri", + "status": messages.STATUS_INVALID, + "error": messages.Error(typ="tls", detail="detail"), + } + + self.simple_http = achallenges.SimpleHTTP( + challb=messages.ChallengeBody(**kwargs),# pylint: disable=star-args + domain="example.com", + key=acme_util.KEY) + + kwargs["chall"] = acme_util.DVSNI + self.dvsni_same = achallenges.DVSNI( + challb=messages.ChallengeBody(**kwargs),# pylint: disable=star-args + domain="example.com", + key=acme_util.KEY) + + kwargs["error"] = messages.Error(typ="dnssec", detail="detail") + self.dvsni_diff = achallenges.DVSNI( + challb=messages.ChallengeBody(**kwargs),# pylint: disable=star-args + domain="foo.bar", + key=acme_util.KEY) + + @mock.patch("letsencrypt.auth_handler.zope.component.getUtility") + def test_same_error_and_domain(self, mock_zope): + from letsencrypt import auth_handler + + auth_handler._report_failed_challs([self.simple_http, self.dvsni_same]) + call_list = mock_zope().add_message.call_args_list + self.assertTrue(len(call_list) == 1) + self.assertIn("Domains: example.com\n", call_list[0][0][0]) + + @mock.patch("letsencrypt.auth_handler.zope.component.getUtility") + def test_different_errors_and_domains(self, mock_zope): + from letsencrypt import auth_handler + + auth_handler._report_failed_challs([self.simple_http, self.dvsni_diff]) + self.assertTrue(mock_zope().add_message.call_count == 2) + + def gen_auth_resp(chall_list): """Generate a dummy authorization response.""" return ["%s%s" % (chall.__class__.__name__, chall.domain)