Skip to content

Commit

Permalink
test(auth_handler): tests for various error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sydneyli committed Mar 21, 2018
1 parent 82bc030 commit 1224b02
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
26 changes: 26 additions & 0 deletions certbot/tests/auth_handler_test.py
Expand Up @@ -289,6 +289,32 @@ def test_perform_error(self):
self.assertEqual(
self.mock_auth.cleanup.call_args[0][0][0].typ, "tls-sni-01")

@mock.patch("certbot.auth_handler.AuthHandler._respond")
def test_respond_error(self, mock_respond):
authzrs = [gen_dom_authzr(domain="0", challs=acme_util.CHALLENGES)]
mock_order = mock.MagicMock(authorizations=authzrs)
mock_respond.side_effect = errors.AuthorizationError

self.assertRaises(
errors.AuthorizationError, self.handler.handle_authorizations, mock_order)
self.assertEqual(self.mock_auth.cleanup.call_count, 1)
self.assertEqual(
self.mock_auth.cleanup.call_args[0][0][0].typ, "tls-sni-01")

@mock.patch("certbot.auth_handler.AuthHandler._poll_challenges")
@mock.patch("certbot.auth_handler.AuthHandler.verify_authzr_complete")
def test_incomplete_authzr_error(self, mock_verify, mock_poll):
authzrs = [gen_dom_authzr(domain="0", challs=acme_util.CHALLENGES)]
mock_order = mock.MagicMock(authorizations=authzrs)
mock_verify.side_effect = errors.AuthorizationError
mock_poll.side_effect = self._validate_all

self.assertRaises(
errors.AuthorizationError, self.handler.handle_authorizations, mock_order)
self.assertEqual(self.mock_auth.cleanup.call_count, 1)
self.assertEqual(
self.mock_auth.cleanup.call_args[0][0][0].typ, "tls-sni-01")

def _validate_all(self, aauthzrs, unused_1, unused_2):
for i, aauthzr in enumerate(aauthzrs):
azr = aauthzr.authzr
Expand Down
20 changes: 20 additions & 0 deletions certbot/tests/error_handler_test.py
Expand Up @@ -47,6 +47,10 @@ def setUp(self):
self.handler = error_handler.ErrorHandler(self.init_func,
*self.init_args,
**self.init_kwargs)
self.exit_handler = error_handler.ExitHandler(self.init_func,
*self.init_args,
**self.init_kwargs)

# pylint: disable=protected-access
self.signals = error_handler._SIGNALS

Expand Down Expand Up @@ -113,6 +117,22 @@ def test_sysexit_ignored(self):
pass
self.assertFalse(self.init_func.called)

def test_ignore_regular_exit(self):
func = mock.MagicMock()
self.handler.register(func)
with self.handler:
pass
self.init_func.assert_not_called()
func.assert_not_called()

def test_exit_handler_calls_cleanup(self):
func = mock.MagicMock()
self.exit_handler.register(func)
with self.exit_handler:
pass
self.init_func.assert_called_once_with(*self.init_args,
**self.init_kwargs)
func.assert_called_once_with()

if __name__ == "__main__":
unittest.main() # pragma: no cover

0 comments on commit 1224b02

Please sign in to comment.